 We have covered some problems. So we wanted to integrate this function two times sine squared of x from 0 to pi. And through engineering or through mathematics, we realized that this should be pi. And what we did is that we ramped the code that would integrate this through multi-hound iteration. And we got one instead. So there is some problem. There is some insufficient knowledge that we have that we have to remedy in some way. So why don't we take a look at another example which will reveal what we need for. So let's integrate this unbelievably difficult function from 1 to 0. Obviously this is x squared over 2. And the brackets show you that we have to substitute this for 1 to 5. What we get in mean n is 12. Now, let's do Monte Carlo integration. Let's pretend that we have integrate this function as we need like 2, analytically. So I take three completely random samples of this function. What does it mean? That I evaluate f of x at 1. Now, if I evaluate this x at 1, I have to use the get 1. I evaluate it at 3 and I also get that 3. So I have three samples now. And what I do is I simply average that. So this is 1 plus 3 plus 5 over 3. The end result is 3. But it shouldn't be that, right? Because the end result through analytic integration is exactly 4 times that. So something is definitely wrong with this Monte Carlo integration scheme. So what we know is that 3 is exactly 1, 4 of 12. So we see that there is a difference of the factor of 4. And if you take a closer look at the integration domain then you will see that 4 is exactly the size of the integration domain. You are integrating from 1 to 5. So just empirically, if we don't, this is 1 angle to look at the problem and to form it. You will see multiple angles. This is more like the engineering way of solving things. You don't know how to derive the full and correct solution. But you see that there is a factor of 4. 4 in the size of the integration domain. Well why don't we multiply with that and see what happens? And obviously it works. So if we multiply with the size of the integration domain, we get the result that we are looking for. So let's change the code. I multiply the previous function with the integration domain, which is from 0 to pi. And this is what I want to apply. And obviously I will get pi as an result, which is the correct solution for the previous integral. Now this is great and this looks very simple. And apparently this technique seems to work. But we still don't really know what is happening here. So we should use some black magic or mathematics, if you do, to see what is going on in the road. So imagine that we sample a function with the uniform distribution on 0 power. What does it mean? I have an interval from 0 to pi. And I generate random numbers on it. And every single number has the same probability. So this function would look like one of our pi, regardless of x in the parameter. Because it doesn't matter which part of the domain I choose, it will have the same probability with the future. Now, what we are essentially doing is integrating a function f of x multiplied by this something probability. Why? Because imagine that some regions of the function would have 0 probability to be sample. So imagine that I'm integrating from 0 to pi. But I will only take samples from 0 to 2. So there is a region in the function that I'm never going to visit. And I don't integrate this part. So tip that's one intuition. The other intuition is that if I draw samples not with uniform distribution, but with a different distribution, that in the average that I compute some regions of the function would be over exactly. Because I have a higher chance of sampling those. So what we are doing is, multiplying this f of x with a sampling probability of x. Now, this p of x is, in this case, to 1 over pi, the uniform distribution, which is obviously a constant. So get out of mind the role. And in the end, we have the integral of the function over pi. But this is not what I'm looking for. I just want to integrate the function itself. So I need to make this pi disappear. So I have this 1 over pi multiplier. What do I need to multiply with to get only this function? What should the question mark be? The flower. Excellent. Exactly. So I just killed this 1 over pi multiplier, which is this f of x sampling distribution. And if you take a look at it, yes, this is also the size of the integration for me. So this is a bit more rigorous. A bit more rigorous way to understand what is going on. This is through a derivation. Not just empirical stuff. What should I multiply with? We know a bit more about what is happening. I have a sampling distribution that I need to get it on. So if I have to 1 over pi multiplier, I got the 1 incorrectly. And if I use this scalar multiplier that I'm looking for, then I will get to the correct solution. Let's examine the whole thing. A bit 40, please. Different angles. I would like to show you how to solve the same problem in multiple different angles. So the super quick probability theory we can. We have an expected value. This is what we're looking for. What is an expected value? An expected value means that there is a value of something and there's a probability of getting these values. So let's take the expected value of the large score. How does it work? I can roll from 1 to 6. And they all have the same probability. All roles have the same probability. 1, 6. So the values are 1, 2, up to 6. And the probability is all the same, 1, 6. And if I have this up, then this says that the expected value of the large score is 3.5. Well, this means that if I need to guess what the next large score would be, then this would be the best value in order to minimize the error from the expected outcome. Now, if we would like to compute the expected value of something, then this means that I take the values that this something can take and I multiply it with the probability for this event. For instance, it is impossible to roll seven with the dice. So theoretically, you could put as the something a seven in there, but it would have zero probability. Therefore, you could not show up in the sum. And this is the discrete case. For the continuous case, we don't really need to do anything very serious. We just changed the summation to integration. So we are not using the discrete sum. But we are integrating continuous functions and we're using continuous subway distributions. Now, let's introduce this notation. What I'm looking for is the expected value of this function f of x after an n amount of samples. Because in multicore, you need to add more and more samples to get a more f whole representation of t. Now, what this means is f is the something and p is the something distribution. What we can do is that we can create a discrete sum that takes samples of this function and then multiplies with the size of the domain. And obviously, since we are taking the sum, we need to divide it by f. Because the more sample and the number of samples, the more samples we take from the function, the larger the number you get. So this is the averaging part. Now, you have to take a look at always the keep looking at the relevant quantities. So the expected value of this f of x does mean that in the integration, I won't apply it with this something probability. And on the right side in the Monte Carlo estimate, I will have the same quantity as on the left side. So if I'm looking for the expected value of x, then I will sample f of x. Now, if you take a look at that, you can see that this is just an approximation. This is not exactly the interval that we're looking for. But there is a multitude of theorems in computer science that show you that if you could use an infinite amount of samples, then you wouldn't approach the actual interval. And most courses on Monte Carlo integration show you different ways of proving this. But this is not what we are interested in. We would just believe that this is what is happening. It's actually very intuitive. Why this is happening? You remember seeing this sign wave that we sample with all these two and that ball. So you could see that if you have a lot of samples, you will get a good estimation of the error under the curve. Now, let's try to use different sample distributions. I mean, a few minutes, you will see why this would be a good idea in some cases. So I would like to integrate this f of x. I am now doing the transformation that is the identity acceleration. I didn't do anything to my f of x. I multiplied by p of x and then I divided by. So this is almost like a scalar multiplier and then I divided the same number. I get the very same thing. But if I would like to write that this is the expected value of something, then this will look a bit different because f over p is the something and p of x is the sample problem. So what we have now is the expected value of f over p. And the question is, what is the Monte Carlo estimator for this? And what we concluded in the previous slides that this should be the very same quantity as what I see in the expected value. So I will be something f over p. So I am not only something f. I am something f over the arbitrary chosen probability distribution. Now there are some good readings on how to do this well and why this is useful. So if you would like to know more about this, please read some of these documents. They are really well written and that's a rare thing nowadays because I have seen lots of not so well written guys on Monte Carlo integration. I need you to do a very long time to find something that has the quality that I should give out rather than to study. Now let's solve the actual example that we have previously with this formula. So f over p times p. So I am still integrating only f. And the sampling distribution was this two times sine square x. This was the function that we wanted to integrate and one over pi is the sampling distribution probability, sorry, uniform distribution over 1 to pi. So and yet in fact the integral of the original function. So I am looking for the expected value that's f over p. So I am going to sample in my code f over p. Let's put this in source code. If you look here, I now divide by the sampling distribution. So it's 1 over v minus a. So this means 1 over pi b and a. This a should have been 0 in this case. So I apologize for that differences in the code. I put the 2.5 in there because if you always a is always 0, then you may write code that works for integration from 0 to something but not 1 to something. So this is a cool thing to check if you have disappointed. So I apologize this a should be 0. But if you compute the actual result that you would be looking for, then you will get your pi. So this is the f. The first term in the sampling line 36. And after the division we have it. Wonderful. So this works. And from multiple angles we now understand how exactly this thing is working. Now if you write the good one to power integration routine and you solve the rendering equation with this. What you want to see is that as you add more samples, you will see first the really noisy image. And then as you add more and more samples this noise will slowly clean up. And if you think back in the previous lecture of mine, we have talked about over and under estimations of the integral. And this is exactly what shows up also in images. If we are trying to sample a function, I would like to be interested in the radiance. But as I add more and more samples, before I converge, I will get values that are larger than the actual intensities. And I will have values that are smaller. So this is what shows up visually as noise. So what you are looking for is always this samples per pixel metric. And when you have a noisy image, you would need to know how many samples I have used per pixel. And if it's still noisy, then you would need to add more samples. This is also some visualization on the evolution of the image after hundreds and then 100,000 samples. Depending on the algorithm, there are multiple ways of solving the rendering equation. You could have smarter algorithms that take longer to compute one sample because they are doing some smart magic. That this would mean that you would need less samples per pixel to get the first image. And the first algorithm that you use to study is actually the naive algorithm for hard tracing. And usually it is a tremendous amount of samples to compute an image. But since it is a simple algorithm, you can use your GPU or CPU to dish out a lot of samples per pixels in every second. Now, a bit of a beauty break, this is what we can get if we implement such a hard tracing. This was rather a bit luckscrutter. And some recent example. That's everyone who this is. Just raise your hand. Okay, how often people? Okay, excellent. So this is actually a margarine material from the Game of Thrones. And anyone has me and spoilers. I will be on that page. Okay. So please. And this is actuality because the Game of Thrones is running. Obviously, we all love the show. And there's also skin being rendered. So there's tons of stuff. So this is kind of. And you can solve this with a simple part. So that we will put together the theoretical part in the second half of this lecture. And then we will implement the next lecture. So when I see renders like this, what I feel is only comparable to religious spiritual wonder. It is absolutely amazing that we can compute something like this using only mathematics. These very simple things that I have shown you. And the other really cool thing is that we are writing these algorithms. We are creating products that use these algorithms. And these are given to world class artists who are just as good as an artist as we are engineers. And they are also giving it their best to create more and more free, cool models. And we can work together to create stuff like that. So this is absolutely amazing.