48
Computer Graphics CMU 15-462/15-662, Fall 2016 Monte Carlo Integration

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

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

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