Download - Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Transcript
Page 1: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Computer Graphics

CMU 15-462/15-662, Fall 2016

Monte Carlo Integration

Page 2: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Talk Announcement

Jovan Popovic, Senior Principal Scientist at Adobe

Research will be giving a seminar on β€œCharacter Animator”

-- Monday October 24, from 3-4 in NSH 1507.

Page 3: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

From last class…

x

p

What we really want is to solve the reflection equation:

πœ”π‘œ

πœ”π‘–1

πœ”π‘–2N

Page 4: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Reflections

Page 5: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Review: fundamental theorem of calculus

𝐹(π‘Ž)

𝐹(𝑏)

?

If f is continuous over [a, b] and F is defined over [a, b] as:

then:

and

Page 6: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Definite integral: β€œarea under curve”

Page 7: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Simple case: constant function

Page 8: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Affine function:

Page 9: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Piecewise affine function

Sum of integrals of individual affine components

Page 10: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Piecewise affine function

If N-1 segments are of equal length:

Weighted

combination of

measurements.

Page 11: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Arbitrary function f(x)?

Quadrature rule: an approximation of the definite integral of a

function, usually stated as a weighted sum of function values at

specified points within the domain of integration.

Page 12: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Trapezoidal rule

Approximate integral of f(x) by assuming function is

piecewise linear.

For equal length segments:

Page 13: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Trapezoidal rule

Consider cost and accuracy of estimate as

(or )

Work: Error can be shown to be:

(for f(x) with continuous second derivative)

Page 14: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Integration in 2D

Consider integrating using the trapezoidal rule

(apply rule twice: when integrating in x and in y)

First application of rule

Second application

Must do much more work in 2D (n x n set of measurements) to get same

error bound on integral! Rendering requires computation of infinite

dimensional integrals – more soon!

Page 15: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Monte Carlo Integration

Page 16: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

A first example…

ࢡ𝑓 π‘₯, 𝑦 𝑑π‘₯𝑑𝑦

𝑓 π‘₯, 𝑦 = α‰Š1, π‘₯2 + 𝑦2 < 10, π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’

Consider the following integral:

where

What does this integral β€œmean”?

We’ll come back to this…

Page 17: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

A brief intro to probability theory…

A random variable 𝒙 is a quantity whose value depends on a

set of possible random events. A random variable can take

on different values, each with an associated probability.

𝒙 can take on values 1, 2, 3, 4,

5 or 6 with equal probability*

Random variables can be discrete or continuous

𝒙 is continuous and 2-D.

Probability of bulls-eye

depends on who is throwing.

Page 18: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Discrete random variables

𝒙 can take one of n discrete values π’™π’Š, each with

probability π’‘π’Š

It must always be the case that

What is the average (or expected) value 𝒙will take

after many, many experiments?

𝐸 𝒇(𝒙) =

π’Š=𝟏

𝒏

𝒇(π’™π’Š)π’‘π’ŠπΈ 𝒙 =

π’Š=𝟏

𝒏

π’™π’Šπ’‘π’Š

Q: What is the expected value of rolling a die?

Page 19: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Useful properties of Expected Value

𝐸 𝒙 + π’š = 𝐸 𝒙 + 𝐸(π’š)

𝐸 𝒙 βˆ— π’š = 𝐸 𝒙 βˆ— 𝐸(π’š)

β€œexpected value of sum is sum of expected values”

β€œexpected value of product is product of expected values”

Let 𝒙 and π’š be independent random variables or

functions of independent random variables

Q1: What does it mean that two random variables are independent?

Q2: What’s an example of two random variables that are correlated?

Page 20: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Variance of a discrete random variable

β€œHow far from the expected value can we expect the

outcome of each instance of the experiment to be”

𝑉 𝒙 =

π’Š=𝟏

𝒏

π’™π’Š βˆ’ 𝑬 π’™πŸπ’‘π’Š = 𝐸 π’™π’Š βˆ’ 𝐸 𝒙

𝟐

= 𝐸 π’™πŸ βˆ’ 𝐸 𝒙 𝟐 Q: What are some advantages

of this expression?

𝑉 𝒙 + π’š = 𝑉 𝒙 + 𝑉(π’š)β€œvariance of sum is sum of variances”

If 𝒙 and π’š are independent random variables or

functions of independent random variables:

Page 21: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Continuous random variables

π‘₯ can take infinitely many values according to a

probability density function 𝑝(π‘₯)

It must always be the case that

𝑝 π‘₯ β‰₯ 0 ΰΆ±

βˆ’βˆž

∞

𝑝 π‘₯ 𝑑π‘₯ = 1

π‘π‘Ÿ π‘₯ = π‘Ž = 0

π‘π‘Ÿ π‘Ž ≀ π‘₯ ≀ 𝑏 = ΰΆ±

π‘Ž

𝑏

𝑝 π‘₯ 𝑑π‘₯Probability of specific

result for an experiment

Page 22: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Continuous random variables

Cumulative Distribution Function

𝐢𝐷𝐹 𝑏 = ΰΆ±

βˆ’βˆž

𝑏

𝑝 π‘₯ 𝑑π‘₯ = 1

0

1

π‘π‘Ÿ π‘Ž ≀ π‘₯ ≀ 𝑏 = 𝐢𝐷𝐹 𝑏 βˆ’ 𝐢𝐷𝐹(π‘Ž)

Page 23: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Cumulative distribution function for

discrete random variables

CDF:

PDF:

Page 24: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Discrete vs Continuous RVs

Concepts from discrete case carry over to continuous

case. Just replace sums with integrals as needed!

𝐸 𝒇(𝒙) =

π’Š=𝟏

𝒏

𝒇(π’™π’Š)π’‘π’Š 𝐸 𝒙 = ΰΆ±

𝒂

𝒃

𝒇(𝒙)𝒑 𝒙 𝒅𝒙

Page 25: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

One more experiment

β–ͺ Assume π‘₯1, π‘₯2, … , π‘₯𝑁 are independent random variables

- e.g. repeating the same experiment over and over

β–ͺ Let 𝐺 𝑿 =1

𝑁σ𝒋=πŸπ‘΅ 𝑔 π‘₯𝑗

- e.g. average score after you throw 10 darts

β€œexpected value of sum is sum of expected values”

𝐸 𝐺 𝑿 = 𝑬1

𝑁

𝒋=𝟏

𝑡

𝑔 π‘₯𝑗 = 𝐸 𝑔 π‘₯ = 𝐺(𝑿)

Expected value of average of N trials is the same as the expected value of 1 trial,

is the same as average of N trials!

Page 26: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Monte Carlo Integration

β–ͺ Want to estimate the integral:

β–ͺ Make an estimate:

β–ͺ Assume for now a uniform probability distribution

- How do you randomly choose a point π’™πŸ in

interval [a, b]?

- What is the value of 𝒑(π’™πŸ)?

- What does estimate look like for a constant

function?

𝐼 = ࢱ

𝒂

𝒃

𝒇(𝒙) 𝒅𝒙

ሚ𝐼1 =𝒇 π’™πŸπ’‘ π’™πŸ

= π’ˆ(π’™πŸ)

Page 27: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Monte Carlo Integration

β–ͺ Want to estimate the integral:

β–ͺ Make an estimate:

β–ͺ What is the expected value of the estimate?

β–ͺ So we’re done…

𝐼 = ࢱ

𝒂

𝒃

𝒇(𝒙) 𝒅𝒙

ሚ𝐼1 =𝒇 π’™πŸπ’‘ π’™πŸ

= π’ˆ(π’™πŸ)

𝐸( ሚ𝐼1) = ࢱ

𝒂

𝒃𝒇 𝒙

𝒑 𝒙𝒑(𝒙)𝒅𝒙 = 𝐼 = 𝑔(π’™πŸ)

Page 28: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Monte Carlo Integration

β–ͺ Not so fast…

- This is like trying to decide based on one toss if

coin is fair or biased…

β–ͺ Why is it that you expect to get better estimates by

running more trials (𝐒. 𝐞. αˆšπΌπ‘)?

- expected value does not change…

β–ͺ Look at variance of estimate after N trials:

αˆšπΌπ‘ =

π’Š=𝟏

𝑡

π’ˆ(π’™π’Š)

Page 29: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Part 2 of last experiment

β–ͺ Assume π‘₯1, π‘₯2, … , π‘₯𝑁 are independent random variables

- e.g. repeating the same experiment over and over

β–ͺ Let 𝐺 𝑿 =1

𝑁σ𝒋=πŸπ‘΅ 𝑔 π‘₯𝑗

- e.g. average score after you throw 10 darts

β€œvariance of sum is sum of variances (assuming independent RVs)”

𝑉(𝐺 𝑿 ) = 𝑉1

𝑁

𝒋=𝟏

𝑡

𝑔 π‘₯𝑗 =1

𝑁𝑉(𝑔 π‘₯ )

Variance of N averaged trials decreases linearly with N as compared to variance

of each trial!

Page 30: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Monte Carlo Integration

β–ͺ In 3 easy steps:

- Define a probability distribution to draw samples from

- Evaluate integrand

- Estimate is weighted average of function samples

Q: how do we get the variance of the estimate to decrease?

A: Increase N, or decrease 𝑉(𝑔 π‘₯ )

ΰΆ±

π‘Ž

𝑏

𝑓(π‘₯)𝑑π‘₯ β‰ˆ1

𝑁

𝑖=1

𝑁𝑓 π‘₯𝑖𝑝 π‘₯𝑖

Page 31: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

A Variance Reduction Method

π’ˆ(𝒙) =𝒇 𝒙

𝒑 𝒙

π’‘πŸ 𝒙 =1

𝑏 βˆ’ π‘Ž

π’‘πŸ 𝒙 =…

Non-uniform (biased) sampling can reduce variance of π’ˆ(𝒙)

If PDF is proportional to f, g will have zero variance! Only one

sample needed! A free lunch?

Page 32: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

How do we generate samples according

to an arbitrary probability distribution?

Page 33: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

The discrete case

Select if falls in its

CDF bucket

Uniform random variable

Consider a loaded die…

Page 34: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

The continuous case

0

1

Exactly the same idea!

But must know the inverse of the

Cumulative Distribution Function!

And if we don’t?

π‘₯ = 𝐢𝐷𝐹1 πœ‰

Page 35: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Rejection Sampling

𝑝(π‘₯)

𝑀do forever {

sample u in [0, M]

sample t in [a, b]

if (u <= p(t))

return t;

}

𝑝(𝑑)

𝑑

𝑒

Page 36: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Uniformly sampling unit disk: a first try

β–ͺ = uniform random angle between 0 and

β–ͺ = uniform random radius between 0 and 1

β–ͺ Return point:

This algorithm does not produce uniform samples

on this 2d disk. Why?

Page 37: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Uniformly sampling unit disk

WRONG

Not Equi-areal

RIGHT

Equi-areal

Page 38: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Uniform sampling of unit disk via rejection sampling

do {

x = 1 - 2 * rand01();

y = 1 - 2 * rand01();

} while (x*x + y*y > 1.);

Generate random point within

unit circle

Page 39: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Sampling 2D directions

x = 1 - 2 * rand01();

y = 1 - 2 * rand01();

r = sqrt(x*x+y*y);

x_dir = x/r;

y_dir = y/r;

Goal: generate random

directions in 2D with

uniform probability

This algorithm is not correct. What is wrong?

Page 40: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Rejection sampling for 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;

Goal: generate random

directions in 2D with

uniform probability

Page 41: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Back to our problem: lighting estimate

x

p

What we really want is to solve the reflection equation:

πœ”π‘œ

πœ”π‘–1

πœ”π‘–2

N

Page 42: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Back to our problem: lighting estimate

MC Estimator:

Want to integrate reflection term over hemisphere

- using uniformly sampled directions

Page 43: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Light source

Occluder

(blocks light)

An example scene…

Page 44: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Hemispherical solid angle

sampling, 100 sample rays(random directions drawn uniformly

from hemisphere)

Why is the image β€œnoisy”?

Page 45: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Lighting estimator uses different

random directions for each pixel.

Some of those directions point

towards the light, others do not.

How can we make it better?

Page 46: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

β€œBiasing” sample selection for rendering applications

β–ͺ Note: β€œbiasing” selection of random samples is

different than creating a biased estimator!

β–ͺ Variance reduction method, but can think of it

also as importance sampling

Page 47: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Importance sampling example

Cosine-weighted hemisphere sampling in irradiance estimate:

Note: Samples along the hemisphere must also be

drawn according to this probability distribution!

Page 48: Monte Carlo Integration - 15-462/662 Spring 202015462.courses.cs.cmu.edu/fall2016content/lectures/...Β Β· One more experiment Assume T1, T2,…, T𝑁 are independent random variables-e.g.

Summary: Monte Carlo integration

β–ͺ Estimate value of integral using random sampling

- Why is it important that the samples are truly random?

- Algorithm gives the correct value of integral β€œon average”

β–ͺ Only requires function to be evaluated at random points on

its domain

- Applicable to functions with discontinuities, functions that are

impossible to integrate directly

β–ͺ Error of estimate independent of dimensionality of integrand

- Faster convergence in estimating high dimensional integrals than

non-randomized quadrature methods

- Suffers from noise due to variance in estimate

- more samples & variance reduction method can help


Top Related