Monte Carlo Integration
Digital Image Synthesisg g y Yung-Yu Chuang
with slides by Pat Hanrahan and Torsten Moller
Introduction
• The integral equations generally don’t have
analytic solutions so we must turn to numerical analytic solutions, so we must turn to numerical methods.
St d d th d lik T id l i t ti
• Standard methods like Trapezoidal integration or Gaussian quadrature are not effective for
hi h di i l d di ti i t l
high-dimensional and discontinuous integrals.
) (
L
o( p, ω
o) L ( ) L L
e( p , ω
o)
i i
i
i
) ( p, ω ) cos θ ω ω
, ω p,
( L d
f
i
2f ( p, ω
o, ω
i) L
i( p, ω
i) cos θ
id ω
i
s
Numerical quadrature
• Suppose we want to calculate , but can’t solve it analytically The approximations I
ab f (x)dxcan t solve it analytically. The approximations through quadrature rules have the form
n
i
i
i f x
w I
1
) ˆ (
which is essentially the weighted sum of samples of the function at various pointsp p
Midpoint rule
convergence convergence
Trapezoid rule
convergence convergence
Simpson’s rule
• Similar to trapezoid but using a quadratic polynomial approximation
polynomial approximation
convergence convergence
assuming f has a continuous fourth derivative.
Curse of dimensionality and discontinuity
• For an sd f i f function f,
• If the 1d rule has a convergence rate of O(n-r), the sd rule would require a much larger number (ns) of samples to work as well as the 1d one.
Thus, the convergence rate is only O(n-r/s).
• If f is discontinuous, convergence is O(n, g ( -1/s) for ) sd.
Randomized algorithms
• Las Vegas v.s. Monte Carlo
L V l i h i h b
• Las Vegas: always gives the right answer by using randomness.
• Monte Carlo: gives the right answer on the average. Results depend on random numbers used, but statistically likely to be close to the right answer.
Monte Carlo integration
• Monte Carlo integration: uses sampling to estimate the values of integrals It only estimate the values of integrals. It only
requires to be able to evaluate the integrand at arbitrary points making it easy to implement
arbitrary points, making it easy to implement and applicable to many problems.
If l d it t th t
• If n samples are used, its converges at the rate of O(n-1/2). That is, to cut the error in half, it is
t l t f ti
necessary to evaluate four times as many samples.
• Images by Monte Carlo methods are often noisy.
Most current methods try to reduce noise.
Monte Carlo methods
• Advantages
E t i l t
– Easy to implement
– Easy to think about (but be careful of statistical bias)
R b t h d ith l i t d d
– Robust when used with complex integrands and domains (shapes, lights, …)
Efficient for high dimensional integrals – Efficient for high dimensional integrals
• Disadvantages
– Noisy
– Slow (many samples needed for convergence)
Basic concepts
• X is a random variable
A l i f i d i bl i
• Applying a function to a random variable gives another random variable, Y=f(X).
• CDF (cumulative distribution function) }
Pr{
)
(x X x
P
• PDF (probability density function): nonnegative, sum to 1 dP )(x
} {
) (
sum to 1
i l if d i bl ξ ( id d dx
x x dP
p ( )
)
(
• canonical uniform random variable ξ (provided by standard library and easy to transform to
th di t ib ti ) other distributions)
Discrete probability distributions
• Discrete events
X
i with probabilityp
i0
np
i
1
1
n
i i
p
p
i• Cumulative PDF (distribution)
p
ij
P
p1
• Construction of samples:
1
j i
i
P p
• Construction of samples: U
To randomly select an event, Select
X
i ifP
i1 U P
i0
P
i0
Uniform random variable
X
3Continuous probability distributions
• PDF p x
( )
Uniform( ) 0 p x
• CDF
x 1
(1) 1 P ( )
P x
0
( ) ( )
P x
p x dx( ) Pr( )
P x
X
xP (
X) ( )
d
0Pr(
X)
p x dx( )
0 1
( ) ( )
P
P
0 1Expected values
• Average value of a function f(x) over some distribution of values p(x) over its domain D distribution of values p(x) over its domain D
f x
f x p x dxE ( ) ( ) ( )
• Example: cos function over [0, π], p is uniform
Dp f x f x p x dx
E ( ) ( ) ( )
p [ , ], p
cos(x)
cos x 1 dx 0E p
cos(x)
0 cos x dx 0Variance
• Expected deviation from the expected value
F d l f if i h
• Fundamental concept of quantifying the error in Monte Carlo methods
( ) ( ) 2
)]
(
[ f x E f x E f x
V
Properties
af (x)
aE
f (x)
E
i
i E f X
X f
E
( )
( )
i i
af (x)
a2V
f (x)
V
af (x)
a V
f (x)
V
f (x)
E f (x)
2
E
f (x)
2
V
f (x)
E f (x)
E
f (x)
V
Monte Carlo estimator
• Assume that we want to evaluate the integral of evaluate the integral of f(x) over [a,b]
Gi if d
• Given a uniform random variable Xi over [a,b],
M t C l ti t Monte Carlo estimator
NX a f
F b ( )
says that the expected
i
i
N f X
F N
1
) (
says that the expected value E[FN] of the
estimator FN equals the estimator FN equals the integral
General Monte Carlo estimator
• Given a random variable X drawn from an arbitrary PDF p(x) then the estimator is arbitrary PDF p(x), then the estimator is
N f Xi
F 1 ( )
i i
N N p X
F
1 ( )
• Although the converge rate of MC estimator is O(N( 1/2),) slower than other integral methods, its converge rate is independent of the dimension, making it the only practical method for high
dimensional integral
Convergence of Monte Carlo
• Chebyshev’s inequality: let X be a random
variable with expected value μ and variance σ2 variable with expected value μ and variance σ2. For any real number k>0,
2
} 1
|
Pr{| X
k
k• For example, for k= , it shows that at least
half of the value lie in the interval 2 ( 2, 2)
• Let , the MC estimate FYi f (Xi)/ p(Xi) N becomes
1 N
) ,
(
i
i
N Y
F N
1
1
Convergence of Monte Carlo
• According to Chebyshev’s inequality,
1
[ ] 2
| ] [
|
Pr N N V FN
F E F
V
YY N N V
Y N V
N Y V F
V N 1 N i 1 N i 1 N i 1
]
[
2
2
Plugging into Chebyshev’s inequality
N N
N
N i i i i i i
N ] [
1 2 1
2
1
• Plugging into Chebyshev’s inequality,
2
] 1
[
| 1
|
Pr V Y
I FN
So, for a fixed threshold, the error decreases at
|
|
Pr FN I N
, ,
the rate N-1/2.
Properties of estimators
• An estimator FN is called unbiased if for all N That is, the expected value is independent of N.
Q F
E[ N ]
• Otherwise, the bias of the estimator is defined
as
• If the bias goes to zero as N increases the Q
F E
FN ] [ N ]
[• If the bias goes to zero as N increases, the estimator is called consistent
0 ]
[
li
[F ] 0lim
N
N
FQ F
E[ ] lim E FN Q
N
[ ]
lim
Example of a biased consistent estimator
• Suppose we are doing antialiasing on a 1d pixel, to determine the pixel value we need to
to determine the pixel value, we need to evaluate , where is the filter function withI
01w(x) f (x)dx w(x)
1 ( d) 1filter function with
• A common way to evaluate this is
0 w( dxx) 1
N
N
i N
i i i
N w X
X f
X F 1w
) (
) (
) (
• When N=1, we have
i1w(Xi ) Xf
X
( ) ( )
I dx
x f X
f X E
w
X f
X E w
F
E
1
011
1
1 1 [ ( )] ( )
) (
) (
) ] (
[
Example of a biased consistent estimator
• When N=2, we have
I dx
x dx w
x w
x f x w x
f x F w
E
01 1
0 1 2
2 1
2 2
1 2 1
) (
) (
) (
) (
) ( ) ] (
[
• However, when N is very large, the bias approaches to zeropp
N
i i i
N
X f X N w
F 1
1
) ( ) 1 (
Ni i
N w X
N1 1 ( ) X
f
N X
( ) ( )
1li 1
I dx x f x w dx
x w
dx x f x w X
N w
X f X N w
F
E N
i i
i i i
N
N N
1 1 0
0 1 0
1
1 ( ) ( )
) (
) ( ) ( )
1 ( lim
) ( ) ( lim
] [ lim
N i i
N
1 ( )
0Choosing samples
• FN N1
N f ((XXi))• Carefully choosing the PDF from which samples are drawn is an important technique to reduce
i p XiN 1 ( )
are drawn is an important technique to reduce variance. We want the f/p to have a low
variance Hence it is necessary to be able to variance. Hence, it is necessary to be able to draw samples from the chosen PDF.
• How to sample an arbitrary distribution from a
• How to sample an arbitrary distribution from a variable of uniform distribution?
– Inversionve s o – Rejection – Transform
Inversion method
• Cumulative probability distribution function
( ) Pr( ) P x X x
• Construction of samples
Solve for X=P-1(U)
1
Solve for X P (U)
• Must know:
U
• Must know:
1. The integral of p(x)
2. The inverse function P-11(x)
0
X
Proof for the inversion method
• Let U be an uniform random variable and its CDF is P (x) x We will show that Y P-1(U) has CDF is Pu(x)=x. We will show that Y=P-1(U) has the CDF P(x).
Proof for the inversion method
• Let U be an uniform random variable and its CDF is P (x) x We will show that Y P-1(U) has CDF is Pu(x)=x. We will show that Y=P-1(U) has the CDF P(x).
Pr
( )
Pr
( )
( ( )) ( )Pr Y x P1 U x U P x Pu P x P x
because P is monotonic,
2 1
2 1) ( ) if
(x P x x x
P
Thus, Y’s CDF is exactly P(x).
2 1
2
1) ( )
(
Inversion method
• Compute CDF P(x)
• Compute P-1(x)
• Obtain ξξ
• Compute Xi=P-1(ξ)
Example: power function
It is used in sampling Blinn’s microfacet model.
x
nnx
p ( )
Example: power function
A
It is used in sampling Blinn’s microfacet model.
• Assume
( ) ( 1) n p x n x
1 1 1
0 0
1
1 1
n
n x
x dx n n
0 0( ) n 1
P x x
1 1
~ ( ) ( ) n
X p x X P U U
Trick (It only works for sampling power distribution)
1 2 1
max( , , , n, n ) Y U U U U
1 1
n
11
Pr( ) Pr( ) n
i
Y x U x x
Example: exponential distribution
useful for rendering participating media.
ce
axx
p ( )
• Compute CDF P(x)
• Compute P-1(x)
• Compute P (x) Obt i ξ
• Obtain ξ
• Compute Xi=P-1(ξ)
Example: exponential distribution
useful for rendering participating media.
ce
axx
p ( )
0
1
ce
axdx c a
• Compute CDF P(x)
0x
ae
asds e
axx
P ( ) 1
• Compute P-1(x)
e ds
ae x
P ( ) 0 1
• Compute P (x)
1
Obt i ξ
) 1
1 ln(
)
1
( x
x a
P
• Obtain ξ
• Compute Xi=P-1(ξ)
1 ln
) 1
1 ln(
X ( )
a
a
Rejection method
• Sometimes, we can’t integrate into CDF or invert CDF
1
( )
I
f x dx invert CDF0
( )
fdx dy
y f x ( )
• Algorithm
( ) y f x
dx dy
• Algorithm
Pick U1 and U2
Accept
U
ifU < f(U )
Accept
U
1 ifU
2< f(U
1)
• Wasteful? Efficiency = Area / Area of rectangle
• Wasteful? Efficiency = Area / Area of rectangle
Rejection method
• Rejection method is a dart-throwing method without performing integration and inversion without performing integration and inversion.
1. Find q(x) so that p(x)<Mq(x) 2. Dart throwing
a. Choose a pair (X, ξ), where X is sampled from q(x) b. If (ξ<p(X)/Mq(X)) return X
• Equivalently, we pick a qu vale tly, we p c a point (X, ξMq(X)). If
it lies beneath (X) it lies beneath p(X) then we are fine.
Why it works
• For each iteration, we generate Xi from q. The sample is returned if ξ<p(X)/Mq(X) which
sample is returned if ξ<p(X)/Mq(X), which happens with probability p(X)/Mq(X).
S th b bilit t t i
• So, the probability to return x is x
p x
x p
q ( ) ( )
)
(
whose integral is 1/M
M x
x Mq
q( ) ( ) whose integral is 1/M
• Thus, when a sample is returned (with
probability 1/M) X is distributed according to probability 1/M), Xi is distributed according to p(x).
Example: sampling a unit disk
void RejectionSampleDisk(float *x, float *y) { float sx, sy;, y;
do {
sx = 1.f -2.f * RandomFloat();
sy = 1.f -2.f * RandomFloat();
} while (sx*sx + sy*sy > 1.f)
*x = sx; *y = sy;
*x = sx; *y = sy;
}
π/4~78.5% good samples, gets worse in higher dimensions, for example, for sphere, π/6~52.4%
dimensions, for example, for sphere, π/6 52.4%
Transformation of variables
• Given a random variable X from distribution px(x) to a random variable Y y(X) where y is one to to a random variable Y=y(X), where y is one-to- one, i.e. monotonic. We want to derive the
distribution of Y p (y) distribution of Y, py(y).
• Py
(
y(
x)) Pr{
Y
y(
x)} Pr{
X
x}
Px(
x)
P (x)
• PDF:
dx x dP dx
x y
dPy
( ( ))
x( )
xPx(x)
dx dx
) (x
dy py dy dP
y
p y
( )
) (
x Py(y)
) (x
px dxy dy
dx y y
py
( )
y) ( )
(
1
x dy p
y p
( )
y)
(
p xdx y y
py
x
Example
x x
px
( ) 2
X Y sin
2 1 1
1 sin 2
cos ) 2
( )
(cos )
(
yy x
x x p
x y
py x
cos
x1
yTransformation method
• A problem to apply the above method is that we usually have some PDF to sample from not we usually have some PDF to sample from, not a given transformation.
Gi d i bl X ith ( ) d
• Given a source random variable X with px(x) and a target distribution py(y), try transform X into t th d i bl Y th t Y h th to another random variable Y so that Y has the distribution py(y).
• We first have to find a transformation y(x) so that Px(x)=Pyy(y(x)). Thus,
)) (
( )
( x P
1P x
y
y xTransformation method
• Let’s prove that the above transform works.
W fi h h d i bl ( )
We first prove that the random variable Z= Px(x) has a uniform distribution. If so, then
P
y1( Z )
should have distribution Px(x) from the inversion method.
Thus Z is uniform and the transformation works
Z x
Pr
Px (X ) x
Pr
X Px (x)
Px(Px (x)) xPr 1 1
Thus, Z is uniform and the transformation works.
• It is an obvious generalization of the inversion method in which X is uniform and P (x)=x
method, in which X is uniform and Px(x)=x.
Example
x x
p
x( )
yp
y( y ) e
yExample
x x
p
x( )
yp
y( y ) e
y) 2
(
x
2x
P
x P
y( y ) e
y2 P
y1( y ) ln y
2 ln ln
2 2 )
ln(
)) (
( )
(
1
2
x x
x P
P x
y
y xThus, if X has the distribution , then the
2
x x
px( )
random variable has the distributionY 2ln X ln 2x
y
y y e
p ( )
Multiple dimensions
We often need the other way around,
Spherical coordinates
• The spherical coordinate representation of directions is i
directions is
sin sin
cos sin
r y
r x
cos r
z
sin
|
| J | r2 sin
| JT r
) , , ( sin
) , ,
(r r2 p x y z
p
Spherical coordinates
• Now, look at relation between spherical directions and a solid angles
directions and a solid angles
d d
d sin
• Hence, the density in terms of
,
d d p d p ( , ) d d p ( ) d p ( , ) ( )
) (
sin )
,
( p
p ( , ) p ( )
p
Multidimensional sampling
• Separable case: independently sample X from px and Y from p p(x y) p (x)p (y)
and Y from py. p(x,y)=px(x)py(y)
• Often, this is not possible. Compute the i l d it f ti ( ) fi t
marginal density function p(x) first.
p x y dy x
p ( ) ( )
• Then compute the conditional density function
p x y dy x
p ( ) ( , )
• Then, compute the conditional density function
) (
) ,
) (
|
( p x y
x y
p
• Use 1D sampling with p(x) and p(y|x).
) ) (
|
( y p x
p
Sampling a hemisphere
• Sample a hemisphere uniformly, i.e. p() c
2 ) 1
(
p ( )
1 p
2
1 c
• Sample first
2
) sin ,
(
p
sin
2 ) sin
, ( )
(
2 2
p d d
p
• Now sampling
0
2
0
2
1 )
(
) , ) (
|
(
p p p
) 2 (
p
Sampling a hemisphere
• Now, we use inversion technique in order to sample the PDF’s
sample the PDF s
)
sin ' ' 1 cos
( d
P ( ) sin 1 cos
0
d
P
| )
1 '
( d
P
• Inverting these:
' 2
) 2
| (
0
d
P
• Inverting these:
1
cos
1
2 1
2
Sampling a hemisphere
• Convert these to Cartesian coordinate
1
cos
1
2 1 1
) 1
2 cos(
cos
sin
x
2 1
2 cos
sin sin sin( 2
1) 1
12
y
Si il d i ti f f ll h
cos
1 z
• Similar derivation for a full sphere
Sampling a disk
RIGHT Equi-Areal WRONG Equi-Areal RIGHT Equi Areal WRONG Equi Areal
12
2 U r U
2 U
1r U
U
2r
U2Sampling a disk
RIGHT Equi-Areal WRONG Equi-Areal RIGHT Equi Areal WRONG Equi Areal
2 U
1 2 U
1r U
2r
U2Sampling a disk
• Uniform
) 1
,
(x y
p p(r,) rp(x, y) r
• Sample r first.
d 2
) (
) (
2
• Then sample
r d
r p r
p( ) ( , ) 2
0
• Then, sample .
2 1 )
(
) , ) (
|
(
r p
r r p
p
• Invert the CDF.
2 )(r p ) 2
(r r
P(r) r P(
| r)
P
) 2
|
( r P
r
1
2
r
2
2Shirley’s mapping
r U 1
2
4 1
U U
Sampling a triangle
0
u
( ,1u u)0
1
vu v
v
1
u v
u 1
2 1
1 1u 1
(1 u ) 1
1 u v
0 0 0
0
(1 ) 1
(1 )
2 2
A dv du u du u ( ) 2
p u v
( , ) 2
p u v
Sampling a triangle
• Here
u
andv
are not independent! p u v( , ) 2) , ) (
|
( p u v
v u
p
• Conditional probability
( ) d
)
( ( | ) ( )
v v p
u
p
1
( ) 2 2(1 )
u
d
p u v dv u
p ( ) ( , )
0 2
( ) u 2(1 ) (1 )
P u
u du u0
( ) 2 2(1 )
p u
dv u0 1 1
u U
( | ) 1 p v u
0 0 0
( ) 2(1 ) (1 )
P u
u du u0 1 2
v U U
0 0 0 0 0 0
( | ) ( | ) 1
(1 ) (1 )
o o
v v v
P v u p v u dv dv
u u
( | )
(1 ) p v u
u
0 0
0 0
(1 u ) (1 u )
Cosine weighted hemisphere
) cos
(
p
p ) ( d
1
2
0 0
2
cos sin
1
0 0c d d
2
2cos sin
1 c
0 d
1
c
d d
d sin
1 cos sin )
,
(
p
Cosine weighted hemisphere
1 cos sin )
,
(
p
, ) cos sin (
p
1 i 2 i i 2
)
( )
2 cos sin d 2 cos sin sin 2
(
0d
p
) 1 (
p
2
1 )
(
) , ) (
|
(
p p p
2
12 1 2 cos
) 1
(
P cos ( 1 2 )
2 1
1
1
2
2
)
2|
(
P
2
2
2 2
2)
|
(
2
2Cosine weighted hemisphere
• Malley’s method: uniformly generates points on the unit disk and then generates directions by the unit disk and then generates directions by projecting them up to the hemisphere above it.
Vector CosineSampleHemisphere(float u1 float u2){
Vector CosineSampleHemisphere(float u1,float u2){
Vector ret;
ConcentricSampleDisk(u1 u2 &ret x &ret y);
ConcentricSampleDisk(u1, u2, &ret.x, &ret.y);
ret.z = sqrtf(max(0.f,1.f - ret.x*ret.x - ret.y*ret.y));
ret.y ret.y));
return ret;
} }
r
Cosine weighted hemisphere
• Why does Malley’s method work?
U i di k li ( ) r
• Unit disk sampling
• Map to hemisphere p(r,)
) , (sin )
,
(r
i )
, ( r
Y T X ( , )
sin r
) ( )
( ))
(
( T x J x
1p x
p
y
T x
) ( )
( ))
(
( p
p
y T x
0 cos ) cos
(
x
J cos
1 ) 0
(
x
J
TCosine weighted hemisphere
) ,
( r
Y T X ( , )
sin r
) ,
( ( , )
) ( )
( ))
(
( T x J x
1p x p ( T ( x )) J ( x )
p ( x )
p
y
T x 0 cos
1 cos 0
0 ) cos
(
x
J
T
, ) ( , ) cos sin
( J p r
p
T
) ( , ) ,
( p
p
TSampling Phong lobe
np ( ) cos
c
np ( ) cos
2
/2c cos
n sin d d 1
0 0
0cos cos 1
2 c
n d 2 c 1
1 cos
1 cos
cos 2