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

## Метаданные

- **Канал:** Two Minute Papers
- **YouTube:** https://www.youtube.com/watch?v=HZWwaLVATA8
- **Дата:** 29.04.2015
- **Длительность:** 11:47
- **Просмотры:** 6,050

## Описание

It is now time to implement a simple Monte Carlo integration scheme, the sample mean. It is indeed quite simple and seems to work quite well on some cases, but it apparently breaks down in others. This is a very important lesson: intuition is tremendously useful to get a good visual understanding of complicated theories, but there are times when we hit the wall. If this happens, we have to proceed using formal mathematics. Let's figure out what went wrong and fix it together soon! (There will be a few segments with Thomas Auzinger, then the solution is presented in segment #23)

About the course:
This course aims to give an overview of basic and state-of-the-art methods of rendering. Offline methods such as ray and path tracing, photon mapping and many other algorithms are introduced and various refinement are explained. 

The basics of the involved physics, such as geometric optics, surface and media interaction with light and camera models are outlined. 

The apparatus of Monte Carlo methods is introduced which is heavily used in several algorithms and its refinement in the form of stratified sampling and the Metropolis-Hastings method is explained. 

At the end of the course students should be familiar with common techniques in rendering and find their way around the current state-of-the-art of the field. Furthermore the exercises should deepen the attendees' understanding of the basic principles of light transport and enable them to write a simple rendering program themselves.

These videos are the recordings of the lectures of 2015 at the Teschnische Universität Wien by Károly Zsolnai and Thomas Auzinger

Course website and slides → http://www.cg.tuwien.ac.at/courses/Rendering/
Subscribe → http://www.youtube.com/subscription_center?add_user=keeroyz
Web → https://cg.tuwien.ac.at/~zsolnai/
Twitter → https://twitter.com/karoly_zsolnai

## Содержание

### [0:00](https://www.youtube.com/watch?v=HZWwaLVATA8) <Untitled Chapter 1>

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 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 at 0 at 0. 15 at 2 and values like that how do i compute the actual integral from this function from this these samples well we will take a look through an extremely difficult example which is integrating x okay so let's solve this with multiple different methods method one

### [0:54](https://www.youtube.com/watch?v=HZWwaLVATA8&t=54s) Method 1

what does the mathematician do find a primitive function what is the primitive function of x squared over two all we have to do is substitute one at zero and therefore we get one half excellent so i know that i'm looking for one half what does the engineer do the engineer knows that this is a linear function therefore this is going to be the area of the triangle what are the lengths of the triangle the base is one because i'm integrating from zero to one the height is also one because if i go one to the right then i'm going to go one 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 a 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 4. 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 values is exactly one half 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 see how small this is the actual function this 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 one half up to quite a few digits so this works really well but there's 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 samples to get damn it okay so the question is 0. 61 more than 1. 5 to a good approximation is this 1. 6 more than what exactly so this is an overestimation excellent what about 100 samples lisa will help me out hopefully it's louder perfect that's an underestimation okay what about 1 000 samples who will you help me out maybe may i ask her no estimation it's an underestimation yeah ask your name okay marco knows that this is an underestimation and this is a weird behavior right because i have overestimations and underestimations of this integral 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 uh 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 sine 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

### [5:12](https://www.youtube.com/watch?v=HZWwaLVATA8&t=312s) Stochastic Convergence

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 2 times sine square of x 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 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 of 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

### [6:35](https://www.youtube.com/watch?v=HZWwaLVATA8&t=395s) Primitive Functions

for primitive functions excellent what is the primitive function for the sine square 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 pie again what does the engineer do well these are not triangles anymore so you better look it up on go from alpha 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 war from alpha doesn't know mathematics doesn't know anything but this he has his 20 whatever line c plus 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 2 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 okay excellent they run this program and it starts out maybe pretty well 3. 6 mm okay and as i add more samples i will get one not pi i get one okay so 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 give 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 those and i mean the timing is a bit sub-optimal 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 us then i come back we complete this lecture we will know

### [10:58](https://www.youtube.com/watch?v=HZWwaLVATA8&t=658s) Write a Global Illumination Program

how to write a global illumination program so this is exactly what we're going to do i have implemented the whole global illumination thing it is beautiful it can do everything it can be do beautiful indirect illumination caustics i think it's in 250 lines it's readable it's understandable and many people have learned how to do global illumination from this program so after three lectures from thomas then i finished this that's one lecture the next lecture is going to be a cold walkthrough so we are going to take into look through the code what i have written how this works how is frana's law inserted here where do i use smells log i do all these things you will see everything in code itself it's going to be very practical

---
*Источник: https://ekstraktznaniy.ru/video/14995*