 Excellent. So this was the heat or mis. Why heat or mis? 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 don't know the integral of the functions too complicated. But I can evaluate it. I can evaluate it at 0 at 0.15 at 2 and very little like that. How do I compute the actual integral from this function 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. 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 at 0 and therefore we get 1. So I know that I am looking for 1.5. 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 am integrating from 0 to 1. The height is also 1 because if I go 1 to the right that I am going to go 1, it's upwards as well because this is x. So the area of the triangle is the base times the height over 2. So this is 1.5 again. Now we have the mathematician and the engineer. What does Monte Carlo guy do? A Monte Carlo guy didn't stand in that matrix 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. Simple as 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 are losses exactly 1.5. 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 you can we can see how small this is. This is the actual function is double f that I'm interested in and f of x 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.5 up to quite a few digits. So this works really really well. But there's something really interesting about this. So if I draw one sample from this integral. Then I have an over estimation of the result. Why? Because I'm looking for 0.5 and I have 0.87. What about 10 samples? Is this an over estimation or under estimation? 10 samples. I wasn't paying attention to the samples before because I was thinking about 1 million samples to get. Damn it. Okay. So the question is is 0.61 more than 0.5? To a good approximation. Is this 1.6 more than what? Opened the idea is more than what? Opened 5. Yeah. Exactly. So this is an over estimation. Excellent. What about 100 samples? We saw we'll have the output fully. It's an under estimation. It's 0.3. An under estimation. Perfect. That's an under estimation. Okay. What about 1000 samples? We'll have maybe an under estimation. It's an under estimation. Yeah. It's the name of it. Okay. Marker knows that this is an under estimation. And this is a weird behavior, right? Because I have over estimations and under estimations of this interval. But in the end, it seems that they are going to the deviations are going to be less and less. Okay. So this almost looks like a sign. So it's like, if you like, algebra, the convergence is something like sign of x times x. Is it? No, because it's going to get large sign of x over x. So this is like a sign that starts out with large deviations and large amplitude. Then 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 we add more samples, it's guaranteed to be closer. Let's have another example. Let's integrate this function two times sign square of x. Yeah. You can see the function is very constant. We're going to be, as results were, function. Yes, I guess that's the figure that you always decadent where the frequency is right. Then you get to compute. There's a probability to have such favor and what you could say that yes, this can happen one. But this has very low probability because why would you have the same region over and over again? And you can also do smart things like putting a grid on a function and sampling 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 with copying this? That was a pretty remarkable thing. Exactly how it goes. OK, what does the mathematician guy do? Look for primitive functions. Excellent. What is the primitive function for the sine square of x? What is one half of x minus the sine times the cosine? Let's do the actual substitution. We have our well-learned pie together. What does the engineer do? Well, these are not trying those anymore. So you better look it up on more from alpha. And you will get something like this. And the result is again pie. So wonderful. Engineering works. OK, what does Monte Carlo guy do? Monte Carlo guy doesn't know what from alpha. Doesn't know mathematics. Doesn't know anything. But he has his 21-ever 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 pie. OK, so let's substitute this function, where this double f is now the sine square of x. And I have also this multiplier of 2 in line 35. So on the right side, you see that this is what I was 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 more. OK, excellent. And the run this program and it starts out maybe pretty well, 3.6. OK. And as I add more samples, I will get one. Not pie. I get one. OK. 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? You'll have to 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 scaring 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 the 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, but you cannot go through intuition, then it 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's going to travel to Japan for a half a year long, half a 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 have 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 to complicated program, 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. I think it is beautiful. It can work with me. It can be to beautiful, interactive 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 program. 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 read, how this works, how is friend as long inserted here? Where do I use friend as long? How do I do all these things? You will see everything in code. It's going to be very practical.