TU Wien Rendering #17 - Monte Carlo Integration: Sample Mean & An Important Lesson

Lecture excerpt (TU Wien Rendering #17) introducing Monte Carlo integration using the sample mean. The speaker demonstrates integrating simple functions (f(x)=x and 2·sin^2(x)) by sampling, compares mathematician/engineer/Monte Carlo approaches, shows stochastic convergence (over- and under-estimates shrinking with more samples), and emphasizes the limits of intuition and the need for mathematical analysis for harder problems. The lecture previews upcoming lectures by Thomas (three lectures while he is in Japan) and a later code walkthrough and global-illumination implementation.
Chapters
Excellent. So this was the hit or miss. Why hit or miss? Because the ball that I throw is either below or above the function. Now what we will actually use is the sample mean. The sample mean is different. I would like to integrate this function and I can take samples of it. Samples here mean that I have F of x and 2x. I can substitute a number and I can evaluate the function there. So I. I don't know the integral of the function. It's too complicated. But I can evaluate it. I can evaluate it at 0, at 0.15, at 2, and values like that. How do I compute the actual integral from these samples? Well, we will take a look through an extremely difficult example, which is integrating x. So let's solve this with multiple different methods. Method 1. What does the mathematician do? Find a primitive function. What is the primitive function of x? X squared over 2. All we have to do is substitute 1 and 0 and therefore we get 1 half. Excellent. So I know that I'm looking for 1 half. What does the engineer do? The engineer knows that this is a linear function. Therefore this is going to be the area of a triangle. What are the lengths of the triangle? The base is 1 because I'm integrating from 0 to 1. The height is also 1 because if I go 1 to the right, then I'm going to go 1 upwards as well, because this is X. So the area of the triangle is the base times the height over two. So this is one half again. Now we have the mathematician and the engineer. What does Monte Carlo Guy do? Monte Carlo Guy didn't study mathematics at all, so he cannot do any of these. So what Monte Carlo Guy is going to do is he's going to take samples of this function. So I evaluate F of x at 0.2. How much is it at 0.2? Well, obviously 0.2. Simplest possible example. What about 0.4? Well, at 0.4, this is 0.4 and so on. So I have taken four randomly chosen samples from this function. And this is called sample mean. This means averaging. So let's take the average of all of these. So the average of all of these values is exactly 1/2. So this gives me the actual perfect result for an integral that I could otherwise not solve. Now, we can code this very easily in just a few lines of code. And there's already excess lines of code because of printing and whatnot. But we can see how small this is. This is the actual function, this double F that I'm interested in. And F equals X. So it's not really that difficult. What is the output of this program? After many samples, I approach very close to 1/2, up to quite a few digits. So this works really, really well. But there is something really interesting about this. So if I draw one sample from this integral, then I have an overestimation of the result. Why? Because I'm looking for 0.5 and I have 0.87. What about 10 samples? Is this an overestimation or underestimation? 10 samples.
I wasn't paying attention to the sentence before because I was thinking about 1 million 10 samples to get.
Damn it. Okay, so the question is, is 0.61 more than 0.5? To a good approximation it is.
Is 1.6 more than what? It's more than 1.
Exactly. So this is an overestimation. Excellent. What about 100 samples? MISA will help me out. Hopefully it's an underestimation. Perfect. That's an underestimation. Okay, what about 1,000 samples? Who will help me out? Maybe, May I ask her? Underestimation. It's an underestimation. May I ask your name? Marco. Okay, Marco knows that this is an underestimation. And this is a weird behavior, right? Because I have overestimations and underestimations of this inner problem. But in the end it seems that they are going to. The deviations are going to be less and less. So this almost looks like a sign. So it's like, if you like algebra, the convergence is something like sine of x times X. Is it? No, because it's going to get large sine of x over x. So this is like a sign that starts out with large deviations and large amplitude and it gets smaller and smaller. This is how the convergence of Monte Carlo estimators go. And this we call, by the way, stochastic convergence. So it means that it can be over and under the integral. But as you add more samples, it's guaranteed to be closer. Let's have another example. Let's integrate this function two times sine squared of x. Just a quick question.
If you have frequency, the function is higher than the same Radio 3 that results were not mistaken. You're always the examples where the frequency is high. Then you get the complete one.
There's a probability to having such bad luck. And what you could say that yes, this can happen one, but this has very low probability, because why would you hit the same region over and over again? And you can also do smart things like putting a grid on the function and something that. So that's one thing, but what you will see later that we will have unbiased estimators. And this means that you can expect the error to shrink in time. But this will be a couple lectures down the line. Is everything fine? Regarding this. That was a good remark. That's exactly how it goes. Okay, what does the mathematician Guy do? Look for primitive functions. Excellent. What is the primitive function for the sine squared of x? Well, it's one half of x minus the sine times the cosine. Let's do the actual substitution. We have our well earned PI again. What does the engineer do? Well, these are not triangles anymore. So you better look it up on WolframAlpha and you will get something like this. And the result is again PI. So wonderful. Engineering works. Okay, what does Monte Carlo Guy do? Monte Carlo Guy doesn't know work from Alpha, doesn't know mathematics, doesn't know anything. But he has his 20 whatever line C program. Let's take samples of this. What are we looking for? What was the number? What was the end result? It was PI. Okay, so let's substitute this function where this double F is now the sine square of x. And I have also this multiplier of two in line 35. So this on the right side. You see that? This is what I am looking for. This is what we have changed. Now just one more time, what am I looking for? What would be the perfect result? One. Okay, excellent. They run this program and it starts out maybe dwell 3.6. Okay. And as I add more samples, I will get one, not PI. I get one. Okay, So I have been lying to you. I have been lying to you all along. This doesn't work at all. And we don't have the slightest idea why this doesn't work. That's one of the most important lessons during this course. Not because of this thing. Who cares? We'll study this thing and sort it out. But you start out. If you have a difficult problem, you start out trying to understand it with your intuition. You don't start throwing multi dimensional integrals everywhere. You start out thinking of what is going on. There is a diffuse interaction. There is scattering in the atmosphere. How does it look like? You use your intuition. And your intuition can get you very far. So in the integration of this F of X, the intuition of this sample mean could get us the perfect solution. But there may be more complicated cases where your intuition fails. And this doesn't mean that intuition is not useful, but it means that it can only take you so far. So if you have barriers like this that you cannot go through. Intuition then is the point when you start using mathematics and you start to evaluate what is going on, you start to look at the details. So use the intuition to get an idea of what's going on. And then if you run into obstacles, use mathematics to sort out the details. That's one of the most important lessons out there for you when you will go out there and try to study really complicated theories. So this doesn't work. I have been lying to you all along. How can we sort it out? Well, after the commercials we will know a bit more. The commercial will come in the form of Thomas because he is going to travel to Japan for a half a year long, half year long research project. So he has a few lectures left, three of them in particular, and he has to hold them now because he's going to take the plane afterwards. So the next three lectures are going to be held by Thomas. And I mean the timing is a bit suboptimal because I had to cut this lecture in half. But at least you know how Monte Carlo integration works. And he is going to tell you more about this and then we will complete this unit and at the end of this unit, so before I get too complicated, three lectures from Thomas, then I come back, we complete this lecture, we will know how to write a global illumination program. So this is exactly what we're going to do. I have implemented the whole global illumination think it is beautiful. It's, it can do everything. It can do beautiful indirect illumination caustics. I think it's in 250 lines. It's readable, it's understandable and many, many people have learned how to do global illumination from this problem. So after three lectures from Thomas, then I finish this. That's one lecture. The next lecture is going to be a code walkthrough. So we are going to take into, we are going to look through the code, what I have written, how this works, how is Fresnel's Law inserted here? Where do I use Snell's law? How do I do all these things? You will see everything in code. It's going to be very practical.










