33
Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor

Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Embed Size (px)

Citation preview

Page 1: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Monte Carlo Methods and Area Estimates

CS3220 - Summer 2008Jonathan Kaldor

Page 2: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Monte Carlo Methods

• In this course so far, we have assumed (either explicitly or implicitly) that we have some clear mathematical problem to solve

• Model to describe some physical process (linear or nonlinear, maybe with some simplifying assumptions)

Page 3: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Monte Carlo Methods

• Suppose we don’t have a good model for the overall process, though (or we wish to validate our model against the process). How can we go about this?

• Suppose we can imitate an experiment (trial, etc). Can we use this to draw conclusions about the overall process?

Page 4: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Monte Carlo Methods

• If we can simulate the experiment on a computer, we can change the data inputs and observe the effects on the results

• In particular, if the experiment involves some random chance, we can run it a bunch of times and accumulate statistics on the outputs

Page 5: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Monte Carlo Methods

• When we simulate a process on a computer that involves random chance, that is known as a Monte Carlo simulation

• One simulation run: particular choices for each of the random choices.

Page 6: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Uses

• Whenever the underlying model is unknown / difficult to compute

• Whenever the inputs are unknown (or are known with a large amount of uncertainty)

Page 7: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Applications

• Particle Physics

• Physical Chemistry

• Finance

• Computer Graphics

Page 8: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Physically Based Rendering

• Rendering an image requires computing the light arriving at each point in the camera

• Light can arrive at the camera after bouncing any number of times - we can look at one set of bounces as a path of light

• Model can be expressed as a complex differential-integral equation that can be very difficult to evaluate

Page 9: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Physically Based Rendering

• We can simulate it using a Monte Carlo approach:

• Simulate a particular path for light

• Do this a bunch (a bunch!) of times

• In the limit, we will end up with the average distribution of light in the scene

Page 10: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

!""#$%& '( !"# $%&'(&)*+,'( ,' -%&./+)( )*+, -.%/0##1'(2& /3 45667!.8 )99:;

<'2=%# >+ ?@/(1 A$'% '@@=B'($C#1 DE $ &"/C@'2ACF %#&=@C'(2 '( $ 1'33=&#1 2@/G /3 B=@C'"@E &0$CC#%#1 @'2ACF $(1 H'#G#1 3%/B I*! $D/H#J K=%B#CA/1 "%/1=0#& CA# &$B# &"%#$1 /3 '@@=B'($C'/( G'CA @/G#% (/'&# '( )J) A/=%& CA$( "$CA C%$0'(2 1/#& '( L9 A/=%&J MA# "A/C/( B$" &@'2AC@ED@=%& CA# '(1'%#0C @'2ACF D=C CA# C#NC=%# /3 CA# A$'% %#B$'(& &A$%"J MA# "A/C/2%$"A &A/G& $ &'B'@$% 2@/GF CA/=2A (/C '1#(C'0$@ D#0$=&# /31'33#%#(0#& '( CA# "%/"#%C'#& /3 CA# A$'%J MA#&# 'B$2#& $%# @/2$%'CAB'0$@@E &0$@#1 C/ 0/B"%#&& 1E($B'0 %$(2#J

<'2=%# L+ 7#(1#%'(2& /3 $ "/(EC$'@ @'C 3%/B D#A'(1J O'CA 1'%#0C '@@=B'($C'/( /(@EF CA# A$'% $""#$%& C// /"$P=#F G'CA $ A$%1Q#12#1 A'2A@'2ACJ!00/=(C'(2 3/% B=@C'"@# &0$CC#%'(2 $11& &/3C(#&& $(1 $ C%$(&@=0#(C P=$@'CEF $& &##( '( CA# %#&=@C& /3 $ I99 A/=% "$CA C%$0'(2 $(1 /=% B#CA/1 '()JR A/=%&J MA# 0= 9 'B$2#F GA'0A '2(/%#& 1'%#0C'/($@'CE '( CA# %$1'$(0# !#@1F "%/1=0#& CA# G%/(2 '(C#(&'CE (#$% CA# "@$(# /3 CA# @'2AC &/=%0#J

<'2=%# I9+ 7#(1#%'(2& /3 $ "/(EC$'@ @'C 3%/B CA# 3%/(CJ S'%#0C '@@=B'($C'/( 0$"C=%#& CA# &=%3$0# A'2A@'2AC D=C A$& #(C'%#@E CA# G%/(2 0/@/%JK=% (#G B#CA/1 0/%%#0C@E &'B=@$C#& CA# 0/@/% $(1 &/3C(#&& 0$=&#1 DE B=@C'"@# &0$CC#%'(2 '( )J* A/=%&F $2%##'(2 G#@@ G'CA CA# :9 A/=% "$CAC%$0#1 %#&=@CJ MA# 0= 9 %#&=@CF GA'0A $&&=B#& B=@C'"@E &0$CC#%#1 @'2AC '& '&/C%/"'0F 1#B/(&C%$C#& CA# 'B"/%C$(0# /3 B$'(C$'('(2 1'%#0C'/( '(CA# &0$CC#%#1 %$1'$(0#J

R

[Moon and Marschner, 2006]

Page 11: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

A Simple Example

• Suppose we want to verify some basic rules about probability with rolling a die

• In particular, what are the odds of rolling two 1s in a row?

• Basic probability: 1/36 (1/6 chance of rolling the first 1, another 1/6 chance for the second 1), or p = 0.0278 chance

Page 12: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

A Simple Example

• Monte Carlo experiment: roll a die twice, count how many times we roll 1 twice in a row. Divide the number of occurrences by the total number of trials to give us the probability of occurrence

• For 100 trials: 0.0200 For 1000 trials: 0.0310For 10000 trials: 0.0293For 100000 trials: 0.0278

Page 13: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

A Simple Example

• We can already see some general properties of Monte Carlo simulations

• Simulating each trial is relatively quick

• ... but we typically need a lot of trials to converge to the correct answer (with only 4 digits of precision to boot!)

Page 14: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• Monte Carlo simulations require a good source of randomness

• What is randomness to begin with?

• Intuitively: no pattern in the numbers

• May also have some constraints on distribution (either uniform or normally distributed)

Page 15: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• On (almost all) computers, the best we can do is a pseudorandom sequence

• Called so because the process for determining the next number is entirely deterministic, although the resulting sequence “looks” random

• We typically can provide a seed which controls the initial starting point

Page 16: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• That’s not to say that the pseudorandom sequences we get are not random

• They can pass several tests of randomness

• Much more common problem: misuse of random numbers by programmers that makes them less random

Page 17: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• An example: suppose we want to generate random numbers uniformly distributed in a circle with unit diameter

• Uniformly distributed: probability of landing in a particular region depends only on the area of the region (equal area regions will have approximately equal numbers of points inside)

Page 18: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• Here are two algorithms

• 1.) Generate two random numbers r1, r2 uniformly in the unit square. If sqrt(r12 + r22) ≤ 1, return the numbers; otherwise, repeat.

• 2.) Generate two random numbers r1 and r2 uniformly. Return r1/2 cos(2πr2) and r2/2 sin(2πr2)

Page 19: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• These algorithms look like they will both generate points randomly in the circle... but their characteristics are quite different

Page 20: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation
Page 21: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation
Page 22: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

50% of points

50% of points

Not 50% of area!

Page 23: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Computing Random Numbers

• When using randomness in our algorithms, we need to be careful that we are using it correctly

• In particular, that we dont destroy randomness or distribution properties of our sequence when manipulating them

Page 24: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Areas Via Monte Carlo

• The previous discussion leads to a method for estimating the area / volume of an arbitrarily shaped object

• We only need to be able to test whether or not a point is inside the object

Page 25: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Areas Via Monte Carlo

• Procedure: generate a uniformly distributed random point in some enclosing rectangle of area A and test whether it is inside or outside the object

• After n trials, we have p of our points inside our object. The estimated area is then p*A/n

Page 26: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Areas Via Monte Carlo

Page 27: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Areas Via Monte Carlo

• Why do we need uniformly distributed points in order to get a good estimate of the area / volume?

Page 28: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Area Via Monte Carlo

• We can use this to compute an estimate for the value of π in a similar way as well

• 10,000 trials: 3.1144100,000 trials: 3.1385

Page 29: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Integrals Via Monte Carlo

• We can also use Monte Carlo simulation to estimate the value of integrals

• ∫01 f(x) dx ≈ 1/N ∑ f(xi)where we have N uniformly distributed random points in [0, 1]

Page 30: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Integrals Via Monte Carlo

• Note that this is only over integral bounds from 0 to 1. In general, we have an integral over a to b

• 1/(b-a) ∫ab f(x) dx ≈ 1/N ∑ f(xi)

• Moving the weight to the other side, we get:∫ab f(x) dx ≈ (b-a)/N ∑ f(xi)

Page 31: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Integrals Via Monte Carlo

• This can be directly extended to multiple integrals:

∫cd ∫ab f(x,y) dx dy ≈ (b-a)(c-d)/N ∑ f(xi, yi)

Page 32: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Integrals Via Monte Carlo

• In general, this is an extremely slow way of getting “good” estimates (at least in terms of error)

• For integrals, the error is typically decreased with the square root of N (that is, the error is around 1/√N), which is usually nowhere near good quadrature algorithms)

Page 33: Monte Carlo Methods and Area Estimates - Cornell … · Monte Carlo Methods and Area Estimates CS3220 - Summer 2008 Jonathan Kaldor. Monte Carlo Methods ... as a Monte Carlo simulation

Estimating Integrals Via Monte Carlo

• The benefit of Monte Carlo is with higher dimension multiple integrals, and with extremely complex integrals (like those in rendering)

• It also provides an easy (but slow!) ground truth to compare against approximations

• Rendering!