Lecture 15: Monte Carlo Integration Computer Graphics and Imaging UC Berkeley
Reminder: Quadrature-Based Numerical Integration f(x) Z b a f(x)dx x 0 = a x 1 x 2 x 3 x 4 = b E.g. trapezoidal rule - estimate integral assuming function is piecewise linear
Multi-Dimensional Integrals (Rendering Examples)
2D Integral: Recall Antialiasing By Area Sampling Point sampling Area sampling Integrate over 2D area of pixel
2D Integral: Irradiance from the Environment Computing flux per unit area on surface, due to incoming light from all directions. Z E(p) = H 2 L i (p,!) cos d! Contribution to irradiance from light arriving from direction! Hemisphere:H 2 d! Light meter da
3D Integral: Motion Blur Integrate over area of pixel and over exposure time. Cook et al. 1984
5D Integral: Real Camera Pixel Exposure Credit: lycheng99, http://flic.kr/p/x4dezh Integrate over 2D lens pupil, 2D pixel, and over exposure time
5D Integral: Real Camera Pixel Exposure p 0 r 2 Lens aperture d p Sensor plane Pixel area Q pixel = 1 Z t 1 d 2 t 0 Z A lens Z A pixel L(p 0! p,t) cos 4 dp dp 0 dt
The Curse of Dimensionality
High-Dimensional Integration Complete set of samples: Curse of dimensionality N = n n n {z } y d = n d Numerical integration error: Error 1 n = 1 N 1/d Random sampling error: Error = Variance 1/2 1 p N In high dimensions, Monte Carlo integration requires fewer samples than quadrature-based numerical integration Global illumination = infinite-dimensional integrals
Example: Monte Carlo Lighting Sample center Light Center of light Sample random Random point on light 1 sample per pixel
Example: Monte Carlo Lighting Light Center Sample center of light Random Sample random point on light True answer
Overview: Monte Carlo Integration Idea: estimate integral based on random sampling of function Advantages: General and relatively simple method Requires only function evaluation at any point Works for very general functions, including discontinuities Efficient for high-dimensional integrals avoids curse of dimensionality Disadvantages: Noise. Integral estimate is random, only correct on average Can be slow to converge need a lot of samples
Probability Review
Random Variables X random variable. Represents a distribution of potential values X p(x) probability density function (PDF). Describes relative probability of a random process choosing value (x) Example: uniform PDF: all values over a domain are equally likely e.g. A six-sided die X takes on values 1, 2, 3, 4, 5, 6 p(1) = p(2) = p(3) = p(4) = p(5) = p(6)
Probability Distribution Function (PDF) n discrete values x i With probability p i p i Requirements of a probability distribution: x i p i 0 nx p i =1 i=1 Six-sided die example: p i = 1 6 Think: p i is the probability that a random measurement of X will yield the value x i X takes on the value x i with probability p i
Expected Value of a Random Variable The average value that one obtains if repeatedly drawing samples from the random distribution. X drawn from distribution with n discrete values with probabilities x i p i p i nx x i Expected value of X: E[X] = x i p i i=1 Die example: E[X] = nx i=1 i 6 = (1 + 2 + 3 + 4 + 5 + 6)/6 =3.5
Continuous Probability Distribution Function X p(x) A random variable X that can take any of a continuous set of values, where the relative probability of a particular value is given by a continuous probability density function p(x). Z Conditions on p(x): p(x) 0 and Z p(x) dx =1 Expected value of X: E[X] = xp(x) dx
Function of a Random Variable A function Y of a random variable X is also a random variable: X p(x) Y = f(x) Z Expected value of a function of a random variable: Z E[Y ]=E[f(X)] = f(x) p(x) dx
Monte Carlo Integration
Monte Carlo Integration Simple idea: estimate the integral of a function by averaging random samples of the function s value. f(x) Z b a f(x)dx a x i b
Monte Carlo Integration Let us define the Monte Carlo estimator for the definite integral of given function f(x) Z b Definite integral f(x)dx Random variable a X i p(x) Note: p(x) must be nonzero for all x where f(x) is nonzero Monte Carlo estimator F N = 1 N NX i=1 f(x i ) p(x i )
Example: Basic Monte Carlo Estimator Let us define the Monte Carlo estimator for the definite integral of given function f(x) Definite integral Z b a f(x)dx Uniform random variable X i p(x) = 1 b a Basic Monte Carlo estimator F N = b N a NX i=1 f(x i )
<latexit sha1_base64="z70w0ninmevictzhk+bvv8aqe4s=">aaacuxicbvflswmxgpy6vlsfvy9egswiogvxbpugcl3otchaqloxbjrvyjjdkqxylv5gqbz4q7x4unna8neobiaz+cixszqkbqzvvxa8qemz2bn5hwjpcwl5pby6dm2stfpwoilidcsihgmuwmnyk1gr1yzislbmdf8f+m0hpg1p1jxtpawjya3imafeoiks82or5agbllg6/bidtlad42ivc2vdchmnrbzbfxrg4azmpdukgyr+ivvjfrxufbxrqvogn0d7pb+wk37nhwknk2bekjdcrvh+xt2ezpipswuxph34qe3krftobesxcwzysug9uwvtrxwrzhtyysv9towuloot7y6yakj+nsijnkyni5euxn6z/95anos1mxsfdxku0swyrb8vijobbiig/aiu14xa0xoeum3drojeededdb9qdcue/588thr7teoaf3lqot0fttepg7aj2xdaizzcgvxaayg8wrt8wgfhpfduged9r73cagyd/sarfqejbq5z</latexit> <latexit sha1_base64="5ymzt5vt2pp+mnpiyc0v17ar8fm=">aaab9hicbvbns8naej3ur1q/oh69lbbbiyurqb0vvxisygyhjwwz3brln5uwu1fkyp/w4khfqz/gm//gtzudtj4yelw3w8y8iofmacf5tiplyyura9x12sbm1vaovbt3r+jueuqrmmeye2bforpu00xz2kkkxvhaatsyxxd++5fkxwjxpycj9sm8fcxkbgsjpfrciunm5llwgvna3647dwcktejcktshrktvf/ugmukjkjthwkmu6ytaz7dujhca13qpogkmyzykxumfjqjys+nvotoyygcfstqlnjqqvycyhck1iqltgwe9uvneif7ndvmdxvgze0mqqsczrwhkky5reqeamemj5hndmjhm3iricjsgtamqcmgdf3mrekeny4zze1zvxpvpvoeadueyxdihjtxaczwgioezxuhnerjerhfry9zascqzffgd6/mhtrmr7a==</latexit> Example: Basic Monte Carlo Estimator The basic Monte Carlo estimator is a simple special case where we sample with a uniform random variable Uniform random variable f(x) Z b a f(x)dx X i p(x) =C Z b (constant) p(x) dx =1 =) ) a Z b Cdx=1 p(x) a x i b a =) C = 1 b 1 a b a a x i b
<latexit sha1_base64="/s5fnbivq6ju+vsap67xvpguiqw=">aaacexicjvfds8mwfe3r15xfvr9fcbvokthbfdshwuaqfrktnbuss6rzuowlhyspmep/hh/nn3+jlz6ybuv088eliydz7uem57oro0ka5rumlywula/kvvnr6xubw8b2zpmiy45je4cs5g0xccjoqjqsskbaesfidxlpucobsd56ivzqmhiuo4h0fdqpqecxkopyjndbpw5lvwh7hohespn6cm0r+05cq1b6xm8er9x26fgarnpbtvpk9e+pdvp2t9de9d1iefo2qceximbfnbscb1ygiicrhmo82b0qxz4jjgziii5lrrkbic4pzitn27egecjd1ccdbqpke9fnjtgl8eaxpeifxj1awgn705egx4ir76poh8mbmnxg5f9aj5bevtehqrrleudpic9muizwvafyo5xgyuykimypeivea6sykwpberwcnfvleda8q1xxzielyu0+symh9kablieflken3iegaaimprr9raqdap96qs/rx9nwxcs8u+bx6edfxsu8gg==</latexit> Example: Basic Monte Carlo Estimator The basic Monte Carlo estimator is a simple special case where we sample with a uniform random variable Basic Monte Carlo estimator (derivation) F N = 1 N XNX f(x i ) (MC Estimator) i=1 p(x i ) = 1 N XNX f(x i ) i=1 1/(b a) = b a N XNX i=1 f(x i )
Unbiased Estimator Definition: A randomized integral estimator is unbiased if its expected value is the desired integral. Fact: the general and basic Monte Carlo estimators are unbiased (proof on next slide) Why do we want unbiased estimators?
Proof That Monte Carlo Estimator Is Unbiased E[F N ]=E = 1 N = 1 N " 1 N XNX i=1 XNX i=1 E XNX Z i=1 XNX Z Z b a Z b f(x i ) p(x i ) apple f(xi ) p(x i ) # f(x) p(x) p(x)dx E Properties of expected values: " X i # X Y i = E[Y i ] i E[aY ]=ae[y ] = 1 N = i=1 Z b a a f(x)dx f(x)dx The expected value of the Monte Carlo estimator is the desired integral.
Variance of a Random Variable Definition V [Y ]=E[(Y E[Y ]) 2 ] = E[Y 2 ] E[Y ] 2 Variance decreases linearly with number of samples V " 1 N NX i=1 Y i # = 1 N 2 NX i=1 V [Y i ]= 1 N 2 NV[Y ]= 1 N V [Y ] Properties of variance " N X NX V Y i # = V [Y i ] V [ay ]=a 2 V [Y ] i=1 i=1
More Random Samples Reduces Variance Light Center Random 1 shadow ray 16 shadow rays
Definite Integral Can Be N-Dimensional Example in 3D: Z x1 Z y1 Z z1 f(x, y, z) dx dy dz x 0 y 0 z 0 Uniform 3D random variable* X i p(x, y, z) = 1 1 1 x 1 x 0 y 1 y 0 z 1 z 0 Basic 3D MC estimator* F N = (x 1 x 0 )(y 1 y 0 )(z 1 z 0 ) N * Generalizes to arbitrary N-dimensional PDFs NX i=1 f(x i )
Example: Monte Carlo Estimate Of Direct Lighting Integral
Direct Lighting (Irradiance) Estimate Z E(p) = L(p,!) cos d! Idea: sample directions over Z L(p,!) hemisphere uniformly in solid angle Z d! Estimator: X i p(!) p(!) = 1 2 Y i = f(x i ) Y i = L(p,! i )cos i da F N = 2 N NX i=1 Y i
Direct Lighting (Irradiance) Estimate Sample directions over hemisphere uniformly in solid angle Z E(p) = L(p,!) cos d! Z Given surface point p A ray tracer evaluates radiance along a ray Z For each of N samples: Generate random direction:! i Compute incoming radiance arriving L i at p from direction: Compute incident irradiance due to ray:! i 2 Accumulate N de i into estimator de i = L i cos i
Direct Lighting - Solid Angle Sampling Light Blocker 100 shadow rays per pixel
Hemispherical Solid Angle Sampling, 100 shadow rays (random directions drawn uniformly from hemisphere)
The MC estimator uses different random directions at each pixel. Only some directions point towards the light. (Estimator is a random variable) Why does this image look noisy?
Observation: incoming radiance is zero for most directions in this scene Idea: integrate only over the area of the light (directions where incoming radiance could be non-zero)
Importance Sampling
Importance Sampling Simple idea: sample the integrand according to how much we expect it to contribute to the integral. ( ) ( ) Z b a f(x) dx complicated integrand Inefficient to sample heavily here! (small contribution to integral) b a Basic Monte Carlo: N NX i=1 f(x i ) (x i are sampled uniformly) Importance-Sampled Monte Carlo: our best guess for where the integrand is big Note: p(x) must be non-zero where f(x) is non-zero (x i are sampled proportional to p) If I sample x less frequently, each sample should count for more.
Effect of Sampling Distribution Fit f(x) p 2 (x) p 1 (x) What is the behavior of f(x)/p 1 (x)? f(x)/p 2 (x)? How does this impact the variance of the estimator?
Importance Sampling - Ideal Probability Distribution Idea: concentrate selection of random samples in parts of domain where function is large ( high value samples ) Fact: sampling according Recall definition of variance: f(x) to itself would be optimal (though in pratice V [ f] =E[ f 2 ] E 2 [ f] can only take an educated guess) p(x) =cf(x) f(x) = f(x) p(x) E[ f 2 ]= = Z apple f(x) Z apple =E[f] p(x) 2 f(x) f(x)/e[f] Z f(x)dx p(x)dx 2 f(x) E[f] dx If PDF is proportional to f then variance is 0! =E 2 [f]! V [ f] =0?!?
Direct Lighting Importance Sampling: Sample Lights Light Center Solid angle sampling Random Area sampling
Light Source Area Sampling 100 shadow rays If no occlusion is present, all directions chosen in computing estimate hit the light source. (Choice of direction only matters if portion of light is occluded from surface point p.)
0! 0 =p p 0 Direct Lighting: Area Integral (How to Normalize?) ZA0 L o (p 0,! 0 ) V (p, p 0 ) E(p) = cos cos 0 p p 0 2 da0 Change of variables to integral over area of light * dw = da0 cos 0 p 0 p 2 A 0 p 0 Binary visibility function: 1 if p is visible from p, 0 otherwise (accounts for light occlusion) Outgoing radiance from light point p, in direction w towards p! =p 0 p p
Direct Lighting: Area Integral (How to Normalize?) ZA0 L o (p 0,! 0 ) V (p, p 0 ) E(p) = cos cos 0 p p 0 2 da0 Sample shape uniformly by area A Z A 0 p 0 A 0 p(p 0 )da 0 =1 p(p 0 )= 1 A 0 0! 0 =p p 0 Estimator 0! =p 0 p F N = A0 N NX i=1 Y i p Y i = L o (p 0 i,! 0 i) V (p, p 0 i) cos i cos 0 i p p 0 i 2 0 X
Solid Angle Sampling vs Area Sampling Light Center Solid angle sampling Random Area sampling 100 random directions on hemisphere 100 random points on area of light source
How to Draw Samples From a Desired Probability Distribution?
Task: Draw A Random Value From a Given PDF Task: Given a PDF for a discrete random variable, p i p i x probability for each value i, Draw a random value X from this PDF. x i 1 Step 1: jx Calculate cumulative PDF: P j = p i i=1 Note: must have P j 0 apple P i apple 1 P n =1 0
<latexit sha1_base64="k0++xp6t+3q9wpv54l820+3dqnc=">aaab7xicbvbns8naej34wetx1aoxxsj4koki6keoeomxgrgfnptndtiu3wzc7kysot/ciwcvr/4fb/4bt20o2vpg4pheddpzwlrwbvz321lzxvvf2cxtlbd3dvf2kwehdzrjfeofjsjr7zbqffyib7gr2e4v0jgu2apht1o/9yhk80tem3gkquwhkkecuwolvpvckkce71wqbs2dgswtrybvkndsvb66/yrlmurdbnw647mpcxkqdgccj+vupjglbeqh2lfu0hh1km/onzbtq/rjlchb0pcz+nsip7hw4zi0nte1q73otcx/ve5moqsg5zlndeo2xxrlgpieth8nfa6qgtg2hdlf7a2edamiznieyjyeb/hlzekf165r7t1ftd4o0ijbmzzagxhwcxvoqbn8ydccz3ifnyd1xpx352peuuium0fwb87nd6lijq4=</latexit> <latexit sha1_base64="k0++xp6t+3q9wpv54l820+3dqnc=">aaab7xicbvbns8naej34wetx1aoxxsj4koki6keoeomxgrgfnptndtiu3wzc7kysot/ciwcvr/4fb/4bt20o2vpg4pheddpzwlrwbvz321lzxvvf2cxtlbd3dvf2kwehdzrjfeofjsjr7zbqffyib7gr2e4v0jgu2apht1o/9yhk80tem3gkquwhkkecuwolvpvckkce71wqbs2dgswtrybvkndsvb66/yrlmurdbnw647mpcxkqdgccj+vupjglbeqh2lfu0hh1km/onzbtq/rjlchb0pcz+nsip7hw4zi0nte1q73otcx/ve5moqsg5zlndeo2xxrlgpieth8nfa6qgtg2hdlf7a2edamiznieyjyeb/hlzekf165r7t1ftd4o0ijbmzzagxhwcxvoqbn8ydccz3ifnyd1xpx352peuuium0fwb87nd6lijq4=</latexit> <latexit sha1_base64="k0++xp6t+3q9wpv54l820+3dqnc=">aaab7xicbvbns8naej34wetx1aoxxsj4koki6keoeomxgrgfnptndtiu3wzc7kysot/ciwcvr/4fb/4bt20o2vpg4pheddpzwlrwbvz321lzxvvf2cxtlbd3dvf2kwehdzrjfeofjsjr7zbqffyib7gr2e4v0jgu2apht1o/9yhk80tem3gkquwhkkecuwolvpvckkce71wqbs2dgswtrybvkndsvb66/yrlmurdbnw647mpcxkqdgccj+vupjglbeqh2lfu0hh1km/onzbtq/rjlchb0pcz+nsip7hw4zi0nte1q73otcx/ve5moqsg5zlndeo2xxrlgpieth8nfa6qgtg2hdlf7a2edamiznieyjyeb/hlzekf165r7t1ftd4o0ijbmzzagxhwcxvoqbn8ydccz3ifnyd1xpx352peuuium0fwb87nd6lijq4=</latexit> Task: Draw A Random Value From a Given PDF Task: Given a PDF for a discrete random variable, p i p i x probability for each value i, Draw a random value X from this PDF. x i 1 Step 2: Given a uniform random variable 2 [0, 1) choose such that X = x i P i 1 < apple P i P j How to compute? Binary search. 0
Continuous Probability Distribution PDF p(x) Uniform distribution on unit interval p(x) 0 CDF P (x) Z x 0 1 P (x) = 0 p(x)dx 1 P (x) =Pr(X<x) P (1) = 1 Z b Pr(a apple X apple b) = a p(x)dx = P (b) P (a) 0 1
Sampling Continuous Probability Distributions Called the inversion method Cumulative probability distribution function P (x) =Pr(X<x) 1 Construction of samples: Solve for x = P 1 ( ) Must know the formula for: 1. The integral of p(x) 2. The inverse function P 1 (x) 0 x 1
Example: Sample Proportional to x 2 Given: f(x) =x 2 x 2 [0, 2] Want to sample according to this graph:
<latexit sha1_base64="hhwoo9ecpwf3r4umdh5dgi9efim=">aaacrxicbvhlbtqwfhxcq4txaes2finqeduokuiubvirg9i1etnwjkcjx7ljrdpozn+ggvn5ph6ahx+dm5okprispapz8om4b5r0mkz/ovjw7tt3723dtx48fpt4yejpsynxt1bavnsqtic5d6ckgslkvhdswoa6v3ccn3/p9eofyj2szxdcntdxvdkylijjobajxyyhshrplawmfbw7n3ujbbaxb+hr+okktlp2ei2wp7uumpywzfhmav9zubpjwcekwuua1d7hdopl8gf/2e30gu83kdo8dlyiikcx0nlh33v+r7sqnpujbz3scwebu1y6criyjdnjuh56e2qdgjnhdhaj36yoravbofdcuvmwnjj33kiucrqetq4als55bbmaddfg5n5dd0dfbaagzw3dmkjx7owe59q5lc6dsy/kxdd68n/armvyb+6lavoeizyhla2iwnp+72ghlqhuqwc4sdlclyozhrrb8mn9cdn1j98e093jx0l6+h68/21oy4u8ic/jnsnib7jpvpidmiuiehsdrj+iwzzgrzgltzfwoboyz8mviau/q+zkuq==</latexit> Example: Sample Proportional to x 2 Given: f(x) =x 2 x 2 [0, 2] Want to sample according to this graph: Step 0: compute PDF by normalizing p(x) =cf(x) =cx 2 Z Z 2 Z 2 Also 0 p(x) dx =1 0 cx 2 dx =1 =) ) c = 3 8 =) p(x) = 3x2 8
Z Z Example: Sample Proportional to x 2 Given: ) f(x) 8=x 2 x 2 [0, 2] Want to sample according to this ) p(x) = 3x2 8 Step 1: Compute CDF: Z x P (x) = 0 = x3 8 p(x)dx
Example: Sample Proportional to x 2 Given: f(x) =x 2 x 2 [0, 2] Applying the inversion method p(x) = 3 8 x2 Remember is uniform random number in [0,1) P (x) = x3 8 Step 2: Sample from p(x) = P (x) = x3 8 x = 3p 8 x
How to Uniformly Sample The Unit Circle?
Sampling Unit Circle: Simple but Wrong Method,= uniform random angle between 0 and 2 (r c = uniform random radius between 0 and 1 Return point: (r cos,rsin ) Source: [PBRT] Result Reference
Need to Sample Uniformly in Area Incorrect Not Equi-areal Correct Equi-areal =2 1 =2 1 r = 2 r = p 2 * See Shirley et al. p.331 for full explanation using inversion method
Rejection Sampling Circle s Area do { x = 1-2 * rand01(); y = 1-2 * rand01(); } while (x*x + y*y > 1.); Efficiency of technique: area of circle / area of square
Rejection Sampling 2D Directions do { x = 1-2 * rand01(); y = 1-2 * rand01(); } while (x*x + y*y > 1.); r = sqrt(x*x+y*y); x_dir = x/r; y_dir = y/r; Generates 2D directions with uniform probability
Approximate the Area of a Circle inside = 0 for (i = 0; i < N; ++i) { x = 1-2 * rand01(); y = 1-2 * rand01(); if (x*x + y*y < 1.) ++inside; } A = inside / N * 4;
Uniform Sampling of Hemisphere Generate random direction on hemisphere (all dirs. equally likely) p(!) = 1 2 Direction computed from uniformly distributed point on 2D square: q q ( 1, 2 )! ( 1 2 1 cos(2 2), 1 2 1 sin(2 2), 1 ) 1.0 0.8 0.6 0.4 0.2 0.2 0.4 0.6 0.8 1.0 Full derivation: see PBRT 3rd Ed. 13.6.1
Things to Remember Monte Carlo integration Unbiased estimators Good for high-dimensional integrals Estimates are visually noisy and need many samples Importance sampling can reduce variance (noise) if probability distribution fits underlying function Sampling random variables Inversion method, rejection sampling Sampling in 1D, 2D, disks, hemispheres
Acknowledgments Many thanks to Kayvon Fatahalian, Matt Pharr, and Pat Hanrahan, who created the majority of these slides. Thanks also to Keenan Crane.
Extra
Pseudo-Random Number Generation Example: cellular automata #30 http://mathworld.wolfram.com/rule30.html
Pseudo-Random Number Generation Example: cellular automata #30 http://mathworld.wolfram.com/rule30.html Center line values are a high-quality random bit sequence Once used for random number generator in Mathematica
Pseudo-Random Number Generation Credit: Richard Ling
Art Competition #1 Results
Art Competition #1 Winner Traced by hand in Adobe Illustrator to create a vectorized version of a lowresolution image of a dancing dog. Source video: https:// www.youtube.com/watch? v=by34jqxwfvs. UC Berkeley Caleb Wyllie Spring 2018
Art Competition #1 Winner Goal: create a crystal-like structure with only triangles, including an optical illusion to make it look like figure was reaching out of frame. Process: 3D modeled a person, decimated mesh down, rendered with a crystal material to create a reference, imported into Illustrator and created triangles over the image (5 hours). UC Berkeley Brian Aronowitz Spring 2018
Art Competition #1 Winner Derived from a JPEG photo of clouds, and processed by an online image triangulation tool to create a low-count polygon effect. The class SVG parser needed coding patch to read the RGB color format output by the online tool. UC Berkeley Hai Zhang Spring 2018