Monte Carlo I
Previous lecture
Analytical illumination formula
This lecture
Numerical evaluation of illumination
Review random variables and probability
Monte Carlo integration
Sampling from distributions
Sampling from shapes
Variance and efficiency
Lighting and Soft Shadows
E( x)
L ( x, )cos d
i
H2
Challenges
Visibility and
blockers
Varying light
distribution
Complex source
geometry
Source: Agrawala. Ramamoorthi, Heirich, Moll, 2000
Penumbras and Umbras
Monte Carlo Lighting
Fixed
1 eye ray per pixel
1 shadow ray per eye ray
Random
Monte Carlo Algorithms
Advantages
Easy to implement
Easy to think about (but be careful of statistical bias)
Robust when used with complex integrands and
domains (shapes, lights, …)
Efficient for high dimensional integrals
Efficient solution method for a few selected points
Disadvantages
Noisy
Slow (many samples needed for convergence)
Random Variables
X is chosen by some random process
X ~ p( x ) probability distribution (density)
function
Discrete Probability Distributions
Discrete events Xi with probability pi
n
pi 0
pi 1
pi
i 1
1
Cumulative PDF (distribution)
j
Pj pi
i 1
Construction of samples
To randomly select an event,
Select Xi if Pi 1 U Pi
Uniform random variable
U
Pi
X3
0
Continuous Probability Distributions
PDF (density) p( x )
p( x ) 0
P( x )
CDF (distribution)
x
1
P( x ) p( x )dx
0
P( x ) Pr( X x )
P(1) 1
Pr( X ) p( x ) dx
P( ) P( )
0
0
Uniform
1
Sampling Continuous Distributions
Cumulative probability distribution function
P( x ) Pr( X x )
Construction of samples
Solve for X=P-1(U)
1
U
Must know:
1.
The integral of p(x)
2.
The inverse function P-1(x)
0
X
Example: Power Function
Assume
1
p( x ) (n 1) x
n 1 1
x
1
0 x dx n 1 n 1
0
n
n
P( x ) x n1
X ~ p( x) X P1(U ) n1 U
Trick
Y max(U1,U2 ,
n 1
,Un ,Un1 )
Pr(Y x ) Pr(U x ) x n 1
i 1
Sampling a Circle
2 1
2
1
r 2
A r dr d r dr d 0
2 0
0 0
0
0
1
r
p(r , ) dr d r dr d p(r , )
p(r , ) p(r ) p( )
1
1
2
1
P( )
2
2
r d
p( )
p(r ) 2r
P(r ) r
2
2 U1
r U2
dr
Sampling a Circle
WRONG Equi-Areal
RIGHT Equi-Areal
2 U1
2 U1
r U2
r U2
Rejection Methods
1
I f ( x ) dx
0
y f (x)
dx dy
y f ( x )
Algorithm
Pick U1 and U2
Accept U1 if U2 <
f(U1)
Wasteful? Efficiency = Area / Area of rectangle
Sampling a Circle: Rejection
do {
X=1-2*U1
Y=1-2*U2
}
while(X2 + Y2 > 1)
May be used to pick random 2D directions
Circle techniques may also be applied to the sphere
Monte Carlo Integration
1
Definite integral
I ( f ) f ( x) dx
1
Expectation of f
0
E[ f ] f ( x) p( x) dx
0
Random variables
Xi ~ p( x ) Yi f ( Xi )
Estimator
1 N
FN Yi
N i 1
Unbiased Estimator
E[FN ] I ( f )
1
E[ FN ] E[
N
Properties
1
N
E[ Yi ] E[Yi ]
1
N
E[aY ] aE[Y ]
1
N
i
i
1
Assume uniform probability
distribution for now
N
Y ]
i 1
N
1
E[Yi ]
N
i 1
N 1
N
E[ f ( X )]
i 1
f ( x ) p( x ) dx
i 1 0
N 1
f ( x )dx
i 1 0
f ( x ) dx
0
i
i
Over Arbitrary Domains
1
p( x) U a
ba
b
b
I f ( x)dx
a
b
b
I (b a) f ( x) p( x)dx
b
1
P( x) p( x)
1
ba a
a
a
I (b a) E ( f )
I (b a) f
1
I (b a )
N
1
ba
N
i 1
f ( X i)
a
b
Non-Uniform Distributions
b
I f ( x)dx
a
b
I
a
b
f ( x)
p( x)dx
p( x)
I g ( x) p( x)dx
a
I E (g )
1
I
N
N
g( X )
i
i 1
f ( x)
g ( x)
p( x)
Direct Lighting – Directional Sampling
L( x, ) cos d
E ( x)
H2
A
x
*
Ray intersection
Sample
x * ( x, )
uniformly by
Yi L( x* ( x, i ), i ) cos i 2
x
Direct Lighting – Area Sampling
cos cos
E ( x) L( x, ) cos d Lo ( x, )V ( x, x)
dA
2
x x
A
H2
A
x
x
Ray direction
Sample
x uniformly by A
Yi Lo ( xi, i) V ( x, xi)
0 visible
V ( x, x)
1 visible
x x
cos i cos i
x xi
2
A
Examples
Fixed
4 eye rays per pixel
1 shadow ray per eye ray
Random
Examples
Uniform grid
Stratified random
4 eye rays per pixel
16 shadow rays per eye ray
Examples
Uniform grid
Stratified random
4 eye rays per pixel
64 shadow rays per eye ray
Examples
Uniform grid
Stratified random
4 eye rays per pixel
100 shadow rays per eye ray
Examples
4 eye rays per pixel
16 shadow rays per eye ray
64 eye rays per pixel
1 shadow ray per eye ray
Variance
Definition
V [Y ] E[(Y E[Y ])2 ]
E[Y 2 2YE[Y ] E[Y ]2 ]
E[Y 2 ] E[Y ]2
Properties
V [Yi ] V [Yi ]
i
i
V[aY ] a2V[Y ]
Variance decreases with sample size
1 N
1
V [ Yi ] 2
N i 1
N
N
1
V [Yi ] V [Y ]
N
i 1
Direct Lighting – Directional
Sampling
E ( x)
L( x, ) cos d
H2
Ray intersection
A
x*
x * ( x, )
uniformly by
Yi L( x* ( x, i ), i ) cos i 2
Sample
uniformly by
*
Yi L( x ( x, i ), i )
Sample
x
Sampling Projected Solid Angle
Generate cosine weighted distribution
Examples
Projected solid angle
Area
4 eye rays per pixel
100 shadow rays
4 eye rays per pixel
100 shadow rays
Variance Reduction
Efficiency measure
1
Efficiency
Variance Cost
Techniques
Importance sampling
Sampling patterns: stratified, …
Sampling a Triangle
u0
v0
(u,1 u)
v
u v 1
u
A
1
0
1u
0
p(u, v) 2
u v 1
(1 u)
dv du (1 u) du
0
2
1
2 1
0
1
2
Sampling a Triangle
Here u and v are not independent! p(u, v) 2
Conditional probability
p(u, v)
p(u | v)
p (u ) p (u , v)dv
p (v )
1u
p(u) 2 dv 2(1 u)
0u
0
P(u0 ) 2(1 u) du (1 u0 )
0
1
p(v | u)
(1 u)
vo
vo
0
0
P(v0 | u0 ) p(v | u0 ) dv
2
u0 1 U1
v0 U1U2
v0
1
dv
(1 u0 )
(1 u0 )
© Copyright 2026 Paperzz