rendering: importance sampling - tu wien

100
Rendering: Importance Sampling Bernhard Kerbl Research Division of Computer Graphics Institute of Visual Computing & Human-Centered Technology TU Wien, Austria With slides based on material by Jaakko Lehtinen, used with permission

Upload: others

Post on 04-Oct-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rendering: Importance Sampling - TU Wien

Rendering: Importance Sampling

Bernhard Kerbl

Research Division of Computer Graphics

Institute of Visual Computing & Human-Centered Technology

TU Wien, AustriaWith slides based on material by Jaakko Lehtinen, used with permission

Page 2: Rendering: Importance Sampling - TU Wien

Todayโ€™s Goal

Improve the efficiency of Monte Carlo with importance sampling

Understand how we can produce custom distributions in simple 1D, 2D and 3D domains by warping simple, uniform random variables

Learn how we can transform samples between cartesian and non-cartesian domains (e.g., from polar (๐œƒ, ๐œ™) to XYZ vectors)

Understand how we can incorporate these steps into path tracing

Rendering โ€“ Importance Sampling 2

Page 3: Rendering: Importance Sampling - TU Wien

Importance Sampling

All these things sound tediousโ€ฆ why do we need to create samples from arbitrary distributions? In different domains even?

When we sample, e.g., the hemisphere, we can use any PDF we like

We know the selection of the proper ๐‘ ๐‘ฅ as importance samplingRendering โ€“ Importance Sampling 3

๐‘“(๐‘ฅ)

๐‘(๐‘ฅ)

๐‘“(๐‘ฅ)

๐‘(๐‘ฅ)

Bad sampling (high variance) Importance sampling (low variance)

Page 4: Rendering: Importance Sampling - TU Wien

Importance Sampling

Remember: if possible, you want a PDF ๐‘ ๐‘ฅ that mimics ๐‘“ ๐‘ฅ !

Rendering โ€“ Importance Sampling 4

Page 5: Rendering: Importance Sampling - TU Wien

Letโ€™s look at an application for importance sampling in practice

Consider a target function ๐‘“(๐‘ฅ)

You want to compute its integral, but have no closed-form solution or donโ€™t know what ๐‘“(๐‘ฅ) is?

Clearly, a case for Monte Carlo

Monte Carlo Integration with Importance Sampling

Rendering โ€“ Importance Sampling 5

Page 6: Rendering: Importance Sampling - TU Wien

If we take another look, the shape of this function seems familiarโ€ฆ

It appears to be quite close to ๐‘ฅ2!

We already know that uniform sampling of ๐‘“(๐‘ฅ) is only one way to do Monte Carlo integrationโ€ฆ

Letโ€™s try instead with ๐‘ ๐‘ฅ โˆ ๐‘ฅ2

Monte Carlo Integration with Importance Sampling

Rendering โ€“ Importance Sampling 6

Page 7: Rendering: Importance Sampling - TU Wien

Both methods convergetowards the same result

But the importance-sampledmethod converges quicker!

Letโ€™s see what the codebehind it looks like..

Uniform vs Importance Sampling (Python)

integrate_mc(0, 100, N, f, p_uniform, gen_uniform) vs integrate_mc(0, 100, N, f, p_x2, gen_x2)

Rendering โ€“ Importance Sampling 7

Page 8: Rendering: Importance Sampling - TU Wien

Uniform vs Importance Sampling (Python)

Rendering โ€“ Importance Sampling 8

integrate_mc(0, 100, N, f, p_uniform, gen_uniform) vs integrate_mc(0, 100, N, f, p_x2, gen_x2)

def integrate_mc(a: float, b: float, N: int, f, p, gen):

X = gen(a, b, N)

estimates = f(X)/p(X, a, b)

result = estimates.sum() / N

return result

def p_uniform(x, a: float, b: float):

return x/(b-a)

def p_x2(x, a: float, b: float):

b3 = ((b**3)/3)

a3 = ((a**3)/3)

return x**2/(b3-a3)

def gen_uniform(a: float, b: float, N: int):

xi = np.random.rand(N)

return xi * (b - a) + a

def gen_x2(a: float, b: float, N: int):

xi = np.random.rand(N)

b3 = (b**3)

a3 = (a**3)

return (a3+xi*(b3-a3))**(1.0/3.0)

By the end of the day, thisshould make sense to you!

Page 9: Rendering: Importance Sampling - TU Wien

Last time, we did uniform hemisphere sampling, and it worked

But perhaps we can also useimportance sampling here?

Can we perhaps importance-sample the rendering equation?

The hemisphere is a peculiar domain. Sampling it with arbitrary distributions is a little bit more complexโ€ฆ

Importance Sampling on the Hemisphere

Rendering โ€“ Importance Sampling 9

Page 10: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 10

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 11: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 11

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Sampling the Unit Disk 2:

Crossing Domains

Importance

Sampling Unlocked!

Page 12: Rendering: Importance Sampling - TU Wien

Discrete Random Variables

In daily life, we are mostly confronted with discrete random results

A coin flip

Toss of a die

Cards in a deck

Each possible outcome of a random variable is associated with a specific probability ๐‘. Probabilities must sum up to 1 (100%)

E.g., a fair die: ๐‘‹ โˆˆ 1,2,3,4,5,6 and ๐‘1 = ๐‘2 = โ‹ฏ = ๐‘6 =1

6

Rendering โ€“ Monte Carlo Integration I 12

Page 13: Rendering: Importance Sampling - TU Wien

Continuous Random Variables

A continuous random variable ๐‘‹ with a given range [๐‘Ž, ๐‘) can assume any value ๐‘‹๐‘– that fulfills ๐‘Ž โ‰ค ๐‘‹๐‘– < ๐‘

Working with continuous variables generalizes the methodology for many complex evaluations that depend on probability theory

There are infinitely many possible outcomes and, consequently, the observation of any specific event has with vanishing probability

How can we find the probabilities for continuous variables?[2]

Rendering โ€“ Importance Sampling 13

Page 14: Rendering: Importance Sampling - TU Wien

Cumulative Distribution Function (CDF)

For continuous variables, we cannot assign probabilities to values

The cumulative distribution function (CDF) lets us compute the probability of a variable taking on a value in a specified range [2]

We use notation ๐‘ƒ๐‘‹ ๐‘ฅ for the CDF of ๐‘‹โ€™s distribution, which yields the probability of ๐‘‹ taking on any value โ‰ค ๐‘ฅ

Rendering โ€“ Importance Sampling 14

0 1

If ๐‘‹ can take on any value with equal probability, what is the probability of ๐‘‹ = 0.5?

?

Page 15: Rendering: Importance Sampling - TU Wien

๐‘ƒ๐‘‹ ๐‘ โˆ’ ๐‘ƒ๐‘‹ ๐‘Ž = ๐‘ƒ๐‘Ÿ ๐‘Ž โ‰ค ๐‘‹๐‘– โ‰ค ๐‘

Read as: ๐‘กโ„Ž๐‘’ ๐‘๐‘Ÿ๐‘œ๐‘๐‘Ž๐‘๐‘–๐‘™๐‘–๐‘ก๐‘ฆ ๐‘œ๐‘“ ๐‘‹ ๐‘ก๐‘Ž๐‘˜๐‘–๐‘›๐‘” ๐‘œ๐‘› ๐‘Ž๐‘›๐‘ฆ ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ ๐‘“๐‘Ÿ๐‘œ๐‘š 0 ๐‘ก๐‘œ ๐‘,๐‘š๐‘–๐‘›๐‘ข๐‘  ๐‘กโ„Ž๐‘’ ๐‘๐‘Ÿ๐‘œ๐‘๐‘Ž๐‘๐‘–๐‘™๐‘–๐‘ก๐‘ฆ ๐‘œ๐‘“ ๐‘‹ ๐‘ก๐‘Ž๐‘˜๐‘–๐‘›๐‘” ๐‘œ๐‘› ๐‘Ž๐‘›๐‘ฆ ๐‘ฃ๐‘Ž๐‘™๐‘ข๐‘’ ๐‘“๐‘Ÿ๐‘œ๐‘š 0 ๐‘ก๐‘œ ๐‘Ž

Example: uniform variable ฮพgenerates values in range [0, 1):

๐‘ƒ๐œ‰ ๐‘ฅ = ๐‘ฅ

๐‘ƒ๐œ‰ 0.75 โˆ’ P๐œ‰ 0. 5 = 0.25

Probability for a Range with CDF

Rendering โ€“ Importance Sampling 15

๐‘ฆ

๐‘ฅ0 1

1

๐‘ƒ(๐‘ฅ)

Page 16: Rendering: Importance Sampling - TU Wien

Properties of the CDF

CDF is bounded by [0, 1] and monotonic increasing

Probability of no outcome is 0, the probability of some outcome is 1

Die: Rolling a number between 1 and 6 cannot be less probable than rolling a number between 1 and 5

CDFs can be applied for discrete and continuous random variables

How do we compute the CDF?

Rendering โ€“ Importance Sampling 16

๐‘ฆ

๐‘ฅ0

1๐‘ƒ(๐‘ฅ)

Page 17: Rendering: Importance Sampling - TU Wien

Determine the limits [๐‘Ž, ๐‘] of your variable ๐‘‹

For each outcome, find its probability ๐‘๐‘Ž, โ€ฆ , ๐‘๐‘The CDF of that variable is then a function ๐‘ƒ๐‘‹ ๐‘ฅ = ฯƒ๐‘–=๐‘Ž

๐‘ฅ ๐‘๐‘–

Computing the CDF for Discrete Random Variables

Rendering โ€“ Importance Sampling 17

๐‘ฅ0

1

๐‘0 ๐‘0 ๐‘0 ๐‘0

๐‘1 ๐‘1 ๐‘1

๐‘3

๐‘2๐‘2

๐‘ฅ0

1

๐‘0 ๐‘1

๐‘3๐‘2

Outcome Probabilities Cumulated Probabilities (CDF)

Page 18: Rendering: Importance Sampling - TU Wien

Probability Density Function (PDF)

The PDF ๐‘(๐‘ฅ) is the derivative of the CDF ๐‘ƒ(๐‘ฅ): ๐‘ ๐‘ฅ =๐‘‘๐‘ƒ(๐‘ฅ)

๐‘‘๐‘ฅ

For a PDF ๐‘(๐‘ฅ), ๐‘ƒ ๐‘ฅ = โˆซ ๐‘ ๐‘ฅ ๐‘‘๐‘ฅ and โˆซ๐‘Ž๐‘๐‘ ๐‘ฅ ๐‘‘๐‘ฅ = ๐‘ƒ ๐‘ โˆ’ ๐‘ƒ(๐‘Ž)

๐‘(๐‘ฅ) must be positive everywhere: a negative value would mean we

can find [๐‘Ž, ๐‘] such that โˆซ๐‘Ž๐‘๐‘ ๐‘ฅ ๐‘‘๐‘ฅ has a negative probability

๐‘๐‘‹(๐‘ฅ) can be understood as the relative probability of ๐‘‹๐‘– = ๐‘ฅ.I.e., if ๐‘๐‘‹ ๐‘Ž = 2๐‘๐‘‹(๐‘), then ๐‘‹๐‘– = ๐‘Ž is twice as likely as ๐‘‹๐‘– = ๐‘

Rendering โ€“ Importance Sampling 18

Page 19: Rendering: Importance Sampling - TU Wien

Notes about the PDF

Notation may look like probability, but PDF values can be >1!

For both discrete and continuous variables, we can referenceโ€œ๐‘(๐‘ฅ)โ€ to describe their distribution

Summary: for a continuous variable ๐‘‹ with a known, integrable PDF, we can find the CDF and probabilities of ๐‘‹ landing in a given range

โ€ฆis this actually helpful?

Rendering โ€“ Importance Sampling 19

Page 20: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 20

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 21: Rendering: Importance Sampling - TU Wien

Creating Variables with Custom Distributions

By discovering the CDF, we have found a powerful new tool

The function is often invertible: this means, we can generate random variables with a desired distribution!

Rationale: Since the CDF is monotonic increasing, there is a unique value of ๐‘ƒ๐‘‹ ๐‘ฅ for every ๐‘ฅ with ๐‘๐‘‹ ๐‘ฅ > 0

More informally, if we plot a 1D CDF, any ๐‘ฅ value that ๐‘‹ can take on has a unique, corresponding coordinate on the ๐‘ฆ-axis

Rendering โ€“ Importance Sampling 21

Page 22: Rendering: Importance Sampling - TU Wien

Basic Sampling with Canonical Random Variables

We want to generate samples for a custom distribution from a random input that we can easily obtain in a computer environment

Our assumed input is the canonical random variable ๐œ‰:

continuous (i.e., a real data type)

with uniform distribution

in the range [0, 1)

Goal: warp samples of ๐œ‰ to ones distributed according to some ๐‘(๐‘ฅ)

Rendering โ€“ Importance Sampling 22

Page 23: Rendering: Importance Sampling - TU Wien

The Canonical Random Variable

Our assumed default input variable

PDF for ๐œ‰ is 1 in range [0,1) and 0 everywhere else

CDF for ๐œ‰ is linear

Important property: we have equality ๐‘ƒ ๐œ‰๐‘– = ๐œ‰๐‘–

Rendering โ€“ Importance Sampling 23

Page 24: Rendering: Importance Sampling - TU Wien

The Inversion Method

For discrete variables: we draw ๐œ‰ and iterate event probabilities

When their sum first surpasses ๐œ‰, we have found ๐‘‹๐‘–For continuous variables: exploit ๐‘ƒ๐‘‹โ€™s bijectivity and use its inverse!

We can use canonic ๐œ‰ to compute ๐‘‹๐‘– = ๐‘ƒ๐‘‹โˆ’1(๐œ‰) according to ๐‘๐‘‹(๐‘ฅ)

Rendering โ€“ Importance Sampling 24

๐œ‰

0

1๐‘ƒ(๐‘ฅ)

๐‘‹๐‘–

๐œ‰

0

1

๐‘‹๐‘–

๐‘0 ๐‘0 ๐‘0 ๐‘0

๐‘1 ๐‘1 ๐‘1

๐‘3

๐‘2๐‘2

Page 25: Rendering: Importance Sampling - TU Wien

Example: Exponential Distribution

Used mainly for estimation of time intervals between two events

The probability of a value decreases exponentially

Needs additional parameter ๐œ†, often called rate parameter

We can find its PDF and CDF in most probability text books

๐‘ ๐‘ฅ, ๐œ† = ๐œ†๐‘’โˆ’๐œ†๐‘ฅ

๐‘ƒ ๐‘ฅ, ๐œ† = 1 โˆ’ eโˆ’๐œ†๐‘ฅ, ๐‘ƒโˆ’1 ๐‘ฅโ€ฒ, ๐œ† = โˆ’log(1โˆ’๐‘ฅ)

๐œ†

Rendering โ€“ Importance Sampling 25

Page 26: Rendering: Importance Sampling - TU Wien

Warping Uniform To Exponential Distribution

Rendering โ€“ Importance Sampling 26

const size_t NUM_SAMPLES = 10'000;

std::array<double, NUM_SAMPLES> exponential_samples{};std::array<double, NUM_SAMPLES> uniform_samples{};std::array<double, NUM_SAMPLES> warped_samples{};

void inversionDemo(){

const double LAMBDA = 5.0;

std::default_random_engine rand_eng_uniform(0xdecaf);std::default_random_engine rand_eng_exponential(0xcaffe);

std::uniform_real_distribution<double> uniform_dist(0.0, 1.0);std::exponential_distribution<double> exponential_dist(LAMBDA);

for (int i = 0; i < NUM_SAMPLES; i++){

auto R_i = exponential_dist(rand_eng_exponential);exponential_samples[i] = R_i;

// uniform distribution: CDF(x) = xauto x_ = uniform_samples[i] = uniform_dist(rand_eng_uniform);

auto X_i = -std::log(1.0 - x_) / LAMBDA;warped_samples[i] = X_i;

}}

Page 27: Rendering: Importance Sampling - TU Wien

Histograms of generated samples

Warping Uniform To Exponential Distribution

Rendering โ€“ Importance Sampling 27

๐‘‹๐‘– = ๐‘ƒ๐‘‹โˆ’1 ๐œ‰๐‘–

๐œ‰๐‘–

๐‘…๐‘–

๐‘‹๐‘–

Page 28: Rendering: Importance Sampling - TU Wien

Warping Uniform To Exponential Distribution

Rendering โ€“ Importance Sampling 28

const size_t NUM_SAMPLES = 10'000;

std::array<double, NUM_SAMPLES> exponential_samples{};std::array<double, NUM_SAMPLES> uniform_samples{};std::array<double, NUM_SAMPLES> warped_samples{};

void inversionDemo(){

const double LAMBDA = 5.0;

std::default_random_engine rand_eng_uniform(0xdecaf);std::default_random_engine rand_eng_exponential(0xcaffe);

std::uniform_real_distribution<double> uniform_dist(0.0, 1.0);std::exponential_distribution<double> exponential_dist(LAMBDA);

for (int i = 0; i < NUM_SAMPLES; i++){

auto R_i = exponential_dist(rand_eng_exponential);exponential_samples[i] = R_i;

// uniform distribution: CDF(x) = xauto x_ = uniform_samples[i] = uniform_dist(rand_eng_uniform);

auto X_i = -std::log(1.0 - x_) / LAMBDA;warped_samples[i] = X_i;

}}

This is actually the implementation

in many standard libraries anyway

Page 29: Rendering: Importance Sampling - TU Wien

Mix Multiple Random Variables

Letโ€™s add another variable and combine them for 2D output

In doing so, we are extending our sampling domain

The sampling domain is defined by

The number of variables, and

Their respective ranges

Think of the domain as a space with the axes representing variables

Rendering โ€“ Importance Sampling 29

Page 30: Rendering: Importance Sampling - TU Wien

Joint PDF

If multiple variables are in a domain, the joint PDF probability density of a given point in that domain depends on all of them

In the simplest case, with independent variables ๐‘‹ and ๐‘Œ, the joint PDF of their shared domain PDF is simply ๐‘ ๐‘ฅ, ๐‘ฆ = ๐‘๐‘‹ ๐‘ฅ ๐‘๐‘Œ(๐‘ฆ)

We can sample independent variables in a domain by computing and sampling the inverse of their respective CDFs, separately

Rendering โ€“ Importance Sampling 30

Page 31: Rendering: Importance Sampling - TU Wien

2D with ๐‘Œ = ๐œ‰. For ๐‘‹, use ๐‘‹ โˆˆ [0,๐œ‹

2) and ๐‘ ๐‘ฅ = cos ๐‘ฅ

๐‘ƒ๐‘‹ ๐‘ฅ = โˆซ ๐‘ ๐‘ฅ ๐‘‘๐‘ฅ = โˆซcos ๐‘ฅ ๐‘‘๐‘ฅ = sin ๐‘ฅ

๐‘ƒ๐‘‹โˆ’1 ๐œ‰ = sinโˆ’1(๐œ‰)

Inversion Method Examples in 2D

Rendering โ€“ Importance Sampling 31

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8

Y

X

void inversionDemo2D(){

std::default_random_engine x_rand_eng(0xdecaf);std::default_random_engine y_rand_eng(0xcaffe);

std::uniform_real_distribution<double> uniform_dist;

for (int i = 0; i < NUM_SAMPLES; i++){

auto x_ = uniform_dist(x_rand_eng);auto y_ = uniform_dist(y_rand_eng);

auto X_i = asin(x_);auto Y_i = y_;samples2D[i] = std::make_pair(X_i, Y_i);

}}

Page 32: Rendering: Importance Sampling - TU Wien

๐‘‹ and ๐‘Œ in range 0,1

For both variables, ๐‘ ๐‘ฃ = 2๐‘ฃ, ๐‘ƒ ๐‘ฃ = ๐‘ฃ2, ๐‘ƒโˆ’1 ๐œ‰ = ๐œ‰

Inversion Method Examples in 2D

Rendering โ€“ Importance Sampling 32

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Y

X

std::array<std::pair<double, double>, NUM_SAMPLES> samples2D{};

void inversionDemo2D(){

std::default_random_engine x_rand_eng(0xdecaf);std::default_random_engine y_rand_eng(0xcaffe);

std::uniform_real_distribution<double> uniform_dist;

for (int i = 0; i < NUM_SAMPLES; i++){

// uniform distribution: CDF(x) = x auto x_ = uniform_dist(x_rand_eng); auto y_ = uniform_dist(y_rand_eng);

auto X_i = sqrt(x_);auto Y_i = sqrt(y_);

samples2D[i] = std::make_pair(X_i, Y_i);}

}

Page 33: Rendering: Importance Sampling - TU Wien

Letโ€™s pick a slow-growing portion of the distribution function

Compared to 0,1 , cumulative density only doubles in 2,4

Choosing a Different Range

Rendering โ€“ Importance Sampling 33

๐‘ ๐‘ฅ = 2๐‘ฅ ๐‘ƒ ๐‘ฅ = ๐‘ฅ2 (๐‘™๐‘œ๐‘”๐‘๐‘™๐‘œ๐‘ก)

Page 34: Rendering: Importance Sampling - TU Wien

Try ๐‘‹ and ๐‘Œ in range 2,4

For both variables, ๐‘ ๐‘ฃ = 2๐‘ฃ, ๐‘ƒ ๐‘ฃ = ๐‘ฃ2, ๐‘ƒโˆ’1 ๐œ‰ = ๐œ‰

Nothing happens.

How can we adapt variable ranges?

Something is missing!

Inversion Method Examples in 2D

Rendering โ€“ Importance Sampling 34

0

0.5

1

1.5

2

2.5

3

3.5

4

0 0.5 1 1.5 2 2.5 3 3.5 4

Y

X

Page 35: Rendering: Importance Sampling - TU Wien

Consider a given range from ๐‘Ž to ๐‘ for a variable and a candidate PDF ๐‘“(๐‘ฅ) with the desired distribution shape

If โˆซ๐‘Ž๐‘๐‘“ ๐‘ฅ ๐‘‘๐‘ฅ โ‰  1, ๐‘“ ๐‘ฅ is not a valid PDF for this variable

The probability that the result is one of all possible results โ‰  100%

To fix this, we compute the proportionality constant ๐‘ = โˆซ๐‘Ž๐‘๐‘“ ๐‘ฅ ๐‘‘๐‘ฅ

and compute a valid ๐‘ƒ ๐‘ฅ =๐น(๐‘ฅ)

๐‘and ๐‘ ๐‘ฅ =

๐‘“(๐‘ฅ)

๐‘โˆ ๐‘“(๐‘ฅ)

Restricting the PDF / CDF

Rendering โ€“ Importance Sampling 35

Page 36: Rendering: Importance Sampling - TU Wien

For range [๐‘Ž, ๐‘] where ๐‘Ž โ‰  0, we adda constant offset ๐‘˜ = โˆ’๐‘ƒ(๐‘Ž)

Try ๐‘‹, ๐‘Œ โˆˆ 2,4 and ๐‘“ ๐‘ฃ = 2๐‘ฃ again

We compute ๐‘๐‘Œ = ๐‘๐‘‹ = โˆซ242๐‘ฃ ๐‘‘๐‘ฃ = 12 and add ๐‘˜ = โˆ’

4

12to get:

๐‘ƒ ๐‘ฃ =๐‘ฃ2โˆ’4

12, ๐‘ƒโˆ’1 ๐œ‰ = 2 3 โ‹… ๐œ‰ + 1, ๐‘ ๐‘ฃ =

2๐‘ฃ

12

Restricting the PDF / CDF

Rendering โ€“ Importance Sampling 36

2

2.2

2.4

2.6

2.8

3

3.2

3.4

3.6

3.8

4

2 2.5 3 3.5 4

Y

X

Page 37: Rendering: Importance Sampling - TU Wien

The Inversion Method, Completed

Find a candidate function ๐‘“(๐‘ฅ) with the desired distribution shape

Choose the range [๐‘Ž, ๐‘] in ๐‘“(๐‘ฅ) you want your variable to imitate

Determine the indefinite integral ๐น ๐‘ฅ = โˆซ ๐‘“ ๐‘ฅ ๐‘‘๐‘ฅ

Compute the proportionality constant ๐‘ = ๐น ๐‘ โˆ’ ๐น(๐‘Ž)

The CDF for the new variable ๐‘‹ is ๐‘ƒ๐‘‹ ๐‘ฅ =๐น ๐‘ฅ โˆ’๐น(๐‘Ž)

๐‘

Compute the inverse of the CDF ๐‘ƒ๐‘‹โˆ’1 ๐œ‰

Use ๐‘ƒ๐‘‹โˆ’1(๐œ‰) to warp the samples of a canonic random variable

so that they are distributed with ๐‘ ๐‘ฅ =๐‘“(๐‘ฅ)

๐‘in the range [๐‘Ž, ๐‘)

Rendering โ€“ Importance Sampling 37

Page 38: Rendering: Importance Sampling - TU Wien

Deriving the ๐‘ ๐‘ฅ โˆ ๐‘ฅ2 Sample Generation Functions

integrate_mc(0, 100, N, f, p_uniform, gen_uniform) vs integrate_mc(0, 100, N, f, p_x2, gen_x2)

def integrate_mc(a: float, b: float, N: int, f, p, gen):

X = gen(a, b, N)

estimates = f(X)/p(X, a, b)

result = estimates.sum() / N

return result

def p_uniform(x, a: float, b: float):

return x/(b-a)

def p_x2(x, a: float, b: float):

b3 = ((b**3)/3)

a3 = ((a**3)/3)

return x**2/(b3-a3)

Rendering โ€“ Importance Sampling 38

Page 39: Rendering: Importance Sampling - TU Wien

integrate_mc(0, 100, N, f, p_uniform, gen_uniform) vs integrate_mc(0, 100, N, f, p_x2, gen_x2)

def integrate_mc(a: float, b: float, N: int, f, p, gen):

X = gen(a, b, N)

estimates = f(X)/p(X, a, b)

result = estimates.sum() / N

return result

def p_uniform(x, a: float, b: float):

return x/(b-a)

def p_x2(x, a: float, b: float):

b3 = ((b**3)/3)

a3 = ((a**3)/3)

return x**2/(b3-a3)

Deriving the ๐‘ ๐‘ฅ โˆ ๐‘ฅ2 Sample Generation Functions

Rendering โ€“ Importance Sampling 39

๐น ๐‘ฅ =๐‘ฅ3

3, ๐‘ =

๐‘3 โˆ’ ๐‘Ž3

3,

๐‘ ๐‘ฅ =๐‘ฅ2

๐‘,

๐‘ƒ๐‘‹โˆ’1 ๐œ‰ =

3๐‘Ž3 + ๐œ‰(๐‘3 โˆ’ ๐‘Ž3),

def gen_uniform(a: float, b: float, N: int):

xi = np.random.rand(N)

return xi * (b - a) + a

def gen_x2(a: float, b: float, N: int):

xi = np.random.rand(N)

b3 = (b**3)

a3 = (a**3)

return (a3+xi*(b3-a3))**(1.0/3.0)

Page 40: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 40

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 41: Rendering: Importance Sampling - TU Wien

Sampling a Unit Disk

Imagine we have a disk-shaped surface with radius ๐‘Ÿ = 1 that registers incoming light (color) from directional light sources

As an exercise, we want to approximate the total incoming light over the diskโ€™s surface area

We integrate over an area of size ๐œ‹

We will use the Monte Carlo integral for that

Rendering โ€“ Importance Sampling 41

2

2r = 1

Page 42: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Disk

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average ร— ๐œ‹

By drawing uniform samples in ๐‘ฅ and ๐‘ฆ, we cannot cover the area precisely

Inscribed square: information lost

Circumscribed square: unnecessary samples

Rendering โ€“ Importance Sampling 42

Page 43: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Disk

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average

By drawing uniform samples in ๐‘ฅ and ๐‘ฆ, we cannot cover the area precisely

Inscribed square: information lost

Circumscribed square: unnecessary samples

Rendering โ€“ Importance Sampling 43

Page 44: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Disk

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average

By drawing uniform samples in ๐‘ฅ and ๐‘ฆ, we cannot cover the area precisely

Inscribed square: information lost

Circumscribed square: unnecessary samples

Rendering โ€“ Importance Sampling 44

Page 45: Rendering: Importance Sampling - TU Wien

Back to the Unit Disk

We do not want to waste samples if we can avoid it

Instead, find a way to generate uniform samples on the disk

Create samples in a non-cartesian domain: 2D polar coordinates

Polar coordinates defined by radius ๐‘Ÿ โˆˆ [0,1) and angle ๐œƒ โˆˆ [0,2๐œ‹)

Transformation to cartesian coordinates: ๐‘ฅ = ๐‘Ÿ sin ๐œƒy = ๐‘Ÿ cos ๐œƒ

Rendering โ€“ Importance Sampling 45

Page 46: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Disk?

Convert two ๐œ‰ to ranges 0, 1 , [0,2๐œ‹) to get polar coordinates

Convert to cartesian coordinates

Rendering โ€“ Importance Sampling 46

void sampleUnitDisk(){

std::default_random_engine r_rand_eng(0xdecaf);std::default_random_engine theta_rand_eng(0xcaffe);

std::uniform_real_distribution<double> uniform_dist(0.0, 1.0);

for (int i = 0; i < NUM_SAMPLES; i++){

auto r = uniform_dist(r_rand_eng);auto theta = uniform_dist(theta_rand_eng) * 2 * M_PI;auto x = r * sin(theta);auto y = r * cos(theta);

samples2D[i] = std::make_pair(x, y);}

}

Page 47: Rendering: Importance Sampling - TU Wien

We successfully sampled the unit disk in the proper range

However, the distribution is notuniform with respect to the area

Samples clump together at center

Averaging those samples will give us a skewed result for the integral!

Clumping

-1

-0.5

0

0.5

1

-1 -0.5 0 0.5 1

Rendering โ€“ Importance Sampling 47

Page 48: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Disk: A Solution

The area of a disk is proportional to ๐‘Ÿ2, times a constant factor ๐œ‹

If we see the disk as concentric rings of width ฮ”๐‘Ÿ, the ๐‘— inner rings

up to radius ๐‘Ÿ๐‘— = ๐‘—ฮ”๐‘Ÿ should contain ๐‘Ÿ๐‘—

๐‘Ÿ

2๐‘ out of ๐‘ total samples

Conversely, the ๐‘–๐‘กโ„Ž sample should lie in the ring at radius ๐‘Ÿ๐‘– = ๐‘Ÿ๐‘–

๐‘

Since ๐œ‰ is uniform in [0, 1), we can switch ๐‘–

๐‘for ๐œ‰๐‘– to get ๐‘Ÿ๐‘– = ๐‘Ÿ ๐œ‰๐‘–

Rendering โ€“ Importance Sampling 48

Page 49: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Disk: A Solution

It works, and it is not even a bad way toarrive at the correct solution

However, for more complex scenarios, wemight struggle to find the solution so easily

With the tools we introduced earlier (and a few new tricks), we can formalize this process for arbitrary setups!

Rendering โ€“ Importance Sampling 49

-1

-0.5

0

0.5

1

-1 -0.5 0 0.5 1

-1

-0.5

0

0.5

1

-1 -0.5 0 0.5 1

Page 50: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 50

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 51: Rendering: Importance Sampling - TU Wien

Another Look at the PDF

Rendering โ€“ Importance Sampling

We saw samples being โ€œwarpedโ€: we can interpret the inversion method as a spatial transformation of uniform samples

Letโ€™s treat the space in the input domain like a grid of infinitesimal hypercubes: segments in 1D, squares in 2D and cubes in 3D[5]

If we warp a domain where each variable is ๐œ‰ to one with joint PDF

๐‘๐ท, then 1

๐‘๐ทis the change in volume of the hypercubes after warping

51

Page 52: Rendering: Importance Sampling - TU Wien

Visualizing the PDF in 2D

The left represents our inputs and the right our target distribution

This time, we warp grid coordinates with the inversion method

Rendering โ€“ Importance Sampling 52

ฮพ1, ฮพ2 ๐‘Œ = ฮพ2 and ๐‘‹ โˆˆ 0,1 , ๐‘๐‘‹ ๐‘ฅ = 2๐‘ฅ

Page 53: Rendering: Importance Sampling - TU Wien

ฮพ1, ฮพ2

The areas of all 2D hypercubes (grid cells) are scaled by 1

๐‘๐‘‹ ๐‘ฅ ๐‘๐‘Œ(๐‘ฆ)

๐‘๐‘‹ ๐‘ฅ = 2๐‘ฅ, cells on the right at (1, ๐‘ฆ) are half their original width

Visualizing the PDF in 2D

Rendering โ€“ Importance Sampling 53

๐‘Œ = ฮพ2 and ๐‘‹ โˆˆ 0,1 , ๐‘๐‘‹ ๐‘ฅ = 2๐‘ฅ

Page 54: Rendering: Importance Sampling - TU Wien

Earlier, we saw samples ๐‘‹, ๐‘Œ โˆˆ 0,1 with ๐‘๐‘‹ ๐‘ฅ = 2๐‘ฅ, ๐‘๐‘Œ(๐‘ฆ) = 2๐‘ฆ

Visualizing the PDF in 2D

Rendering โ€“ Importance Sampling 54

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

2D Variables with Linear PDFs

Page 55: Rendering: Importance Sampling - TU Wien

In this 2D setup, we have joint PDF ๐‘ ๐‘ฅ, ๐‘ฆ = ๐‘๐‘‹ ๐‘ฅ ๐‘๐‘Œ ๐‘ฆ = 4๐‘ฅ๐‘ฆ

Space near point (1,1) is compressed down to 1

4of its original size

Visualizing the PDF in 2D

Rendering โ€“ Importance Sampling 55

ฮพ1, ฮพ2 ๐‘‹, ๐‘Œ โˆˆ 0,1 , ๐‘๐‘‹ ๐‘ฅ = 2๐‘ฅ, ๐‘๐‘Œ ๐‘ฆ = 2๐‘ฆ

Page 56: Rendering: Importance Sampling - TU Wien

This PDF compresses space at higher values of x, ๐‘ฆ, dilates at lower

If space shrinks or grows, samples in it become denser or sparser

Visualizing the PDF in 2D

Rendering โ€“ Importance Sampling 56

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

ฮพ1, ฮพ2 ๐‘‹, ๐‘Œ โˆˆ 0,1 , ๐‘๐‘‹ ๐‘ฅ = 2๐‘ฅ, ๐‘๐‘Œ ๐‘ฆ = 2๐‘ฆ

Page 57: Rendering: Importance Sampling - TU Wien

Letโ€™s transform a regular grid from polar to cartesian coordinates

Polar To Cartesian Coordinates

Rendering โ€“ Importance Sampling 57

๐‘‹ = ๐‘Ÿ cos(๐œƒ)๐‘Œ = ๐‘Ÿ sin(๐œƒ)

๐‘Ÿ

๐œƒ

Page 58: Rendering: Importance Sampling - TU Wien

Take 100k samples, transform and see in which square they end up

First Attempt to Learn the PDF

Rendering โ€“ Importance Sampling 58

๐‘‹ = ๐‘Ÿ cos(๐œƒ)๐‘Œ = ๐‘Ÿ sin(๐œƒ)

Page 59: Rendering: Importance Sampling - TU Wien

Take 100k samples, transform and see in which square they end up

First Attempt to Learn the PDF

Rendering โ€“ Importance Sampling 59

ฮพ1, ฮพ2 ๐‘‹ = ๐œ‰1 cos(2๐œ‹๐œ‰2), ๐‘Œ = ๐œ‰1 sin(2๐œ‹๐œ‰2)

Page 60: Rendering: Importance Sampling - TU Wien

Knowing the PDF

If we know the effect of a transformation ๐‘‡ on the PDF, we can

Use it in the Monte Carlo integral to weight our samples, or

Compensate to get a uniform sampling method after transformation

Rendering โ€“ Importance Sampling 60

๐ผ๐‘›๐‘๐‘ข๐‘ก (๐œ‰1, ๐œ‰2) ๐ถ๐‘Ž๐‘Ÿ๐‘ก๐‘’๐‘ ๐‘–๐‘Ž๐‘› (๐‘ฅ, ๐‘ฆ)

๐‘ƒ๐‘œ๐‘™๐‘Ž๐‘Ÿ (r, ๐œƒ)

๐‘‡(๐‘Ÿ, ๐œƒ)Scale +

compensate

Page 61: Rendering: Importance Sampling - TU Wien

Knowing the PDF

If we know the effect of a transformation ๐‘‡ on the PDF, we can

Use it in the Monte Carlo integral to weight our samples, or

Compensate to get a uniform sampling method after transformation

Rendering โ€“ Importance Sampling 61

๐ผ๐‘›๐‘๐‘ข๐‘ก (๐œ‰1, ๐œ‰2) ๐ถ๐‘Ž๐‘Ÿ๐‘ก๐‘’๐‘ ๐‘–๐‘Ž๐‘› (๐‘ฅ, ๐‘ฆ)

๐‘ƒ๐‘œ๐‘™๐‘Ž๐‘Ÿ (r, ๐œƒ)

๐‘‡(๐‘Ÿ, ๐œƒ)

Equal area!

Scale +

compensate

Page 62: Rendering: Importance Sampling - TU Wien

Computing the PDF after a Transformation

Assume a random variable ๐ด and a bijective transformation ๐‘‡ that yields another variable ๐ต = ๐‘‡ ๐ด

Bijectivity implies that ๐‘ = ๐‘‡(๐‘Ž) must be either monotonically increasing or decreasing with ๐‘Ž

This implies that there is a unique ๐ตi for every ๐ดi, and vice versa

In this case, the CDFs for the two variables fulfill ๐‘ƒ๐ต ๐‘‡(๐‘Ž) = ๐‘ƒ๐ด(๐‘Ž)

Rendering โ€“ Importance Sampling 62

Page 63: Rendering: Importance Sampling - TU Wien

Computing the PDF after a Transformation

If ๐‘ = ๐‘‡(๐‘Ž) and ๐‘ increases with ๐‘Ž, we have: ๐‘‘๐‘ƒ๐ต(๐‘)

๐‘‘๐‘Ž=

๐‘‘๐‘ƒ๐ด(๐‘Ž)

๐‘‘๐‘Ž

If ๐‘ decreases with ๐‘Ž (e.g., ๐‘ = โˆ’๐‘Ž), we have: โˆ’๐‘‘๐‘ƒ๐ต(๐‘)

๐‘‘๐‘Ž=

๐‘‘๐‘ƒ๐ด(๐‘Ž)

๐‘‘๐‘Ž

Since ๐‘๐ต is the non-negative derivative of ๐‘ƒ๐ต, we can rewrite as:

๐‘๐ต ๐‘๐‘‘๐‘

๐‘‘๐‘Ž= ๐‘๐ด ๐‘Ž ,

๐‘๐ต ๐‘ =๐‘‘๐‘

๐‘‘๐‘Ž

โˆ’1

๐‘๐ด ๐‘Ž

Rendering โ€“ Importance Sampling 63

๐‘ˆ๐‘ ๐‘–๐‘›๐‘”:๐‘‘๐‘ƒ๐‘‹ ๐‘ฅ

๐‘‘๐‘ฆ=๐‘๐‘‹ ๐‘ฅ ๐‘‘๐‘ฅ

๐‘‘๐‘ฆ

Page 64: Rendering: Importance Sampling - TU Wien

Computing the PDF after a Transformation

Letโ€™s interpret ๐‘๐ต ๐‘ =๐‘‘๐‘

๐‘‘๐‘Ž

โˆ’1๐‘๐ด ๐‘Ž

It is the probability density of ๐ด at ๐‘Ž, multiplied by ๐‘‘๐‘

๐‘‘๐‘Ž

โˆ’1

๐‘‘๐‘

๐‘‘๐‘Ž

โˆ’1has two intuitive interpretations:

the change in sample density at point ๐‘Ž if we transform ๐‘Ž by ๐‘‡or, the reciprocal change in volume (space) for a volume element (hypercube) at point ๐‘Ž if we transform ๐ด by transformation ๐‘‡

Rendering โ€“ Importance Sampling 64

Page 65: Rendering: Importance Sampling - TU Wien

Multidimensional Transformations

If we try to apply the above to the unit disk, we fail at ๐‘ฅ = ๐‘Ÿ sin ๐œƒ

We canโ€™t evaluate ๐‘‘๐‘ฅ

๐‘‘๐‘Ÿ

โˆ’1: the transformation that produces one

target variable is dependent on both input variables and vice-versa

We cannot compute the change in the PDF between individual variables, we must take them all into account simultaneously

Itโ€™s matrix time!Rendering โ€“ Importance Sampling 65

Page 66: Rendering: Importance Sampling - TU Wien

Multidimensional Transformations

We write the set of ๐‘ values from a multidimensional variable ิฆ๐ด

as a vector ิฆ๐‘Ž and the ๐‘ outputs of transformation ๐‘‡ as a vector ๐‘:

ิฆ๐‘Ž =

๐‘Ž1โ‹ฎ๐‘Ž๐‘

, ๐‘ =๐‘1โ‹ฎ๐‘๐‘

=๐‘‡1( ิฆ๐‘Ž)โ‹ฎ

๐‘‡๐‘( ิฆ๐‘Ž)= ๐‘‡( ิฆ๐‘Ž)

Instead of quantifying the change in volume incurred by ๐‘‡ ๐‘Ž ,๐‘‘๐‘‡ ๐‘Ž

๐‘‘๐‘Ž, our goal is now to quantify the change incurred by ๐‘‡ ิฆ๐‘Ž

Rendering โ€“ Importance Sampling 66

Page 67: Rendering: Importance Sampling - TU Wien

The Jacobian Matrix

For a transformation ๐‘ = ๐‘‡( ิฆ๐‘Ž), we can define the Jacobian matrix that contains all ๐‘๐‘— , ๐‘Ž๐‘– combinations of partial differentials

๐ฝ๐‘‡( ิฆ๐‘Ž) =

๐œ•๐‘1

๐œ•๐‘Ž1โ‹ฏ

๐œ•๐‘1

๐œ•๐‘Ž๐‘

โ‹ฎ โ‹ฑ โ‹ฎ๐œ•๐‘๐‘€

๐œ•๐‘Ž1โ‹ฏ

๐œ•๐‘๐‘€

๐œ•๐‘Ž๐‘

If we consider ิฆ๐ดโ€™s domain as a space with ๐‘ axes, ๐ฝ๐‘‡( ิฆ๐‘Ž) gives the

change of the edges of a volume element from ิฆ๐‘Ž to ๐‘ = ๐‘‡( ิฆ๐‘Ž)Rendering โ€“ Importance Sampling 67

Page 68: Rendering: Importance Sampling - TU Wien

The Jacobian Matrix, Visualized

Change in edges of a volume element (infinitesimal hypercube) at ิฆ๐‘Ž

๐ฝ๐‘‡( ิฆ๐‘Ž) =

๐œ•๐‘1๐œ•๐‘Ž1

โ‹ฏ๐œ•๐‘1๐œ•๐‘Ž๐‘

โ‹ฎ โ‹ฑ โ‹ฎ๐œ•๐‘๐‘๐œ•๐‘Ž1

โ‹ฏ๐œ•๐‘๐‘๐œ•๐‘Ž๐‘

Rendering โ€“ Importance Sampling 68

๐‘ิฆ๐‘Ž10

๐œ•๐‘1๐œ•๐‘Ž1๐œ•๐‘2๐œ•๐‘Ž1

01

๐œ•๐‘1๐œ•๐‘Ž2๐œ•๐‘2๐œ•๐‘Ž2

Page 69: Rendering: Importance Sampling - TU Wien

The Jacobian

The columns of a square matrix can be interpreted as the natural

base vectors of a space

10โ‹ฎ0

,

01โ‹ฎ0

if they were transformed by it

The determinant . of a matrix yields the volume of a parallelepiped spanned by these vectors[3]

๐ฝ๐‘‡ , the Jacobian of ๐‘‡, gives the change in volume at ิฆ๐‘Ž due to ๐‘‡

Rendering โ€“ Importance Sampling 69

๐‘

๐‘ฑ๐‘ป ๐’‚

Page 70: Rendering: Importance Sampling - TU Wien

Computing the PDF of a Transformation

Letโ€™s try polar coordinates again: ๐‘ฅ๐‘ฆ = ๐‘‡

๐‘Ÿ๐œƒ

=๐‘Ÿ sin ๐œƒ๐‘Ÿ cos ๐œƒ

๐œ•๐‘‡๐‘Ÿ๐œƒ

๐œ•๐‘Ÿ๐œƒ

= ๐ฝ๐‘‡ =

๐œ•๐‘ฅ

๐œ•๐‘Ÿ

๐œ•๐‘ฅ

๐œ•๐œƒ๐œ•๐‘ฆ

๐œ•๐‘Ÿ

๐œ•๐‘ฆ

๐œ•๐œƒ

=cos ๐œƒ โˆ’๐‘Ÿ sin ๐œƒsin ๐œƒ ๐‘Ÿ cos ๐œƒ

= r

๐‘ ๐‘ฅ, ๐‘ฆ =๐‘(๐‘Ÿ,๐œƒ)

๐‘Ÿ, or ๐‘ ๐‘Ÿ, ๐œƒ = ๐‘Ÿ ๐‘ ๐‘ฅ, ๐‘ฆ , which tells us: the change in

probability density from (๐‘Ÿ, ๐œƒ) to ๐‘ฅ, ๐‘ฆ is inverse proportional to ๐’“

Rendering โ€“ Importance Sampling 70

Page 71: Rendering: Importance Sampling - TU Wien

Sampling Joint PDFs Correctly

For independent variables, the joint PDF ๐‘(๐‘ฅ, ๐‘ฆ, โ€ฆ ) is ๐‘๐‘‹ ๐‘ฅ ๐‘๐‘Œ ๐‘ฆ โ€ฆ

In general, this is an assumption that we should not rely on

Furthermore, after a transformation, only the joint PDF is known

The proper way to sample multiple variables ๐‘‹, ๐‘Œ is to compute

the marginal density function ๐‘๐‘‹ ๐‘ฅ of one

the conditional density function ๐‘๐‘Œ ๐‘ฆ|๐‘ฅ of the other

Rendering โ€“ Importance Sampling 71

Page 72: Rendering: Importance Sampling - TU Wien

Marginal and Conditional Density Function

Assume we have obtained the joint PDF ๐‘ ๐‘ฅ, ๐‘ฆ of variables ๐‘‹, ๐‘Œwith ranges [๐‘Ž๐‘‹, ๐‘๐‘‹) and [๐‘Ž๐‘Œ, ๐‘๐‘Œ)

In a 2D domain with ๐‘‹, ๐‘Œ we can think of ๐‘๐‘‹ ๐‘ฅ as the average density of ๐‘ ๐‘ฅ, ๐‘ฆ at a given ๐‘ฅ over all possible values ๐‘ฆ

We can obtain the marginal density function for one of them by

integrating out all the others, e.g.: ๐‘๐‘‹ ๐‘ฅ = โˆซ๐‘Ž๐‘Œ๐‘๐‘Œ ๐‘ ๐‘ฅ, ๐‘ฆ ๐‘‘๐‘ฆ

We can then find ๐‘ ๐‘ฆ|๐‘ฅ =๐‘(๐‘ฅ,๐‘ฆ)

๐‘๐‘‹(๐‘ฅ)

Rendering โ€“ Importance Sampling 72

Page 73: Rendering: Importance Sampling - TU Wien

Adding More Variables

What to do for multiple variables, e.g. ๐‘‹, ๐‘Œ and ๐‘?

Find first marginal density ๐‘๐‘‹ ๐‘ฅ = โˆซ๐‘Ž๐‘๐‘๐‘โˆซ๐‘Ž๐‘Œ๐‘๐‘Œ๐‘ ๐‘ฅ, ๐‘ฆ, ๐‘ง ๐‘‘๐‘ฆ ๐‘‘๐‘ง

Find first conditional density ๐‘๐‘‹ ๐‘ฆ, ๐‘ง|๐‘ฅ =๐‘ ๐‘ฅ,๐‘ฆ,๐‘ง

๐‘๐‘‹ ๐‘ฅ

Find second marginal density ๐‘๐‘Œ ๐‘ฆ|๐‘ฅ = โˆซ๐‘Ž๐‘๐‘๐‘๐‘ ๐‘ฅ, ๐‘ฆ, ๐‘ง ๐‘‘๐‘ง

Find second conditional density ๐‘๐‘‹ ๐‘ง|๐‘ฅ, ๐‘ฆ =๐‘ ๐‘ฆ,๐‘ง|๐‘ฅ

๐‘๐‘Œ ๐‘ฆ|๐‘ฅ

Integrate + invert first marginal, first and second conditional densities

Sample each of them

Extend ad libitum to even more variables

Rendering โ€“ Importance Sampling 73

Page 74: Rendering: Importance Sampling - TU Wien

Sampling the Unit Disk: The Formal Solution

We know the proportionality constant is ๐œ‹ (area of sampled disk)

Since we want uniform sampling and sample probabilities should

integrate to 1, the target PDF in cartesian coordinates is ๐‘ ๐‘ฅ, ๐‘ฆ =1

๐œ‹

๐ฝ๐‘‡ told us that ๐‘ ๐‘Ÿ, ๐œƒ = ๐‘Ÿ ๐‘ ๐‘ฅ, ๐‘ฆ , so we want ๐‘ ๐‘Ÿ, ๐œƒ =๐‘Ÿ

๐œ‹

๐‘๐‘… ๐‘Ÿ = โˆซ02๐œ‹๐‘ ๐‘Ÿ, ๐œƒ ๐‘‘๐œƒ = 2๐‘Ÿ and ๐‘ ๐œƒ|๐‘Ÿ =

๐‘(๐‘Ÿ,๐œƒ)

๐‘๐‘…(๐‘Ÿ)=

1

2๐œ‹

Rendering โ€“ Importance Sampling 74

Page 75: Rendering: Importance Sampling - TU Wien

Sampling the Unit Disk: The Formal Solution

If we create samples in polar coordinates for these PDFs, we will get the uniform distribution in (๐‘ฅ, ๐‘ฆ) after applying transformation ๐‘‡

Rendering โ€“ Importance Sampling 75

๐‘Ÿ

๐œƒ

Page 76: Rendering: Importance Sampling - TU Wien

Sampling the Unit Disk: The Formal Solution

Integrate marginal and conditional PDFs andinvertโ€”we get the same solution as before:

๐‘Ÿ = P๐‘…โˆ’1 ๐œ‰1 = ๐œ‰1

๐œƒ = ๐‘ƒฮ˜โˆ’1 ๐œ‰2 = 2๐œ‹๐œ‰2

๐‘ ๐œƒ|๐‘Ÿ is constant: no matter what radius we are looking at, all positions on a ring of that radius (angle) should be equally likely

Final integral: ๐‘…๐บ๐ต๐‘ก๐‘œ๐‘ก๐‘Ž๐‘™ =๐œ‹

๐‘ฯƒ๐‘–=1๐‘ ๐‘…๐บ๐ต(๐‘…๐‘– ๐‘ ๐‘–๐‘› ฮ˜๐‘– , ๐‘…๐‘– ๐‘๐‘œ๐‘  ฮ˜๐‘–)

Rendering โ€“ Importance Sampling 76

Page 77: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 77

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 78: Rendering: Importance Sampling - TU Wien

Moving on to the Hemisphere

This took as a while, but we have seen all the formal procedures

We only need to switch from integrating planar area to points ๐œ”on hemisphere surface (i.e., vectors ๐‘ฅ, ๐‘ฆ, ๐‘ง with length 1)

Use spherical coordinates and bijective ๐‘‡ from (๐‘Ÿ, ๐œƒ, ๐œ™) to ๐‘ฅ, ๐‘ฆ, ๐‘ง :๐‘ฅ = ๐‘Ÿ sin ๐œƒ cos๐œ™๐‘ฆ = ๐‘Ÿ sin ๐œƒ sin๐œ™๐‘ง = ๐‘Ÿ cos ๐œƒ

Rendering โ€“ Importance Sampling 78

Page 79: Rendering: Importance Sampling - TU Wien

Deriving Integration Over Hemisphere

Each direction ๐œ” represents an infinitesimal surface area portion ๐‘‘๐œ”

How do we integrate a function ๐‘“(๐œ”) with differential ๐‘‘๐œ”?

Integration over points on hemisphere surface ๐œ”, w.r.t. (๐œƒ, ๐œ™)

Rendering โ€“ Importance Sampling 79

๐œƒ

๐œ™

๐œ”๐‘‘๐œ”

๐‘›

Page 80: Rendering: Importance Sampling - TU Wien

Deriving Integration Over Hemisphere

We assume a planar surface with an upright facing normal ๐‘›

We use the integral intervals ๐œƒ โˆˆ 0,๐œ‹

2, ๐œ™ โˆˆ [0, 2๐œ‹)

I.e., a curve from perpendicular to parallel for ๐œƒ, a ring for ๐œ™

Rendering โ€“ Importance Sampling 80

2๐œ‹

๐œ‹

2

๐‘›

Page 81: Rendering: Importance Sampling - TU Wien

Deriving Integration Over Hemisphere

We can split the surface along ๐œƒ into ribbons of width ฮ”๐œƒ โ†’ ๐‘‘๐œƒ

The upper edge of the ribbon is slightly shorter than the lower

If we keep adding more and more ribbons, this difference vanishes

Rendering โ€“ Importance Sampling 81

ฮ”๐œƒ

Page 82: Rendering: Importance Sampling - TU Wien

Deriving Integration Over Hemisphere

As a ribbonโ€™s width goes to ๐‘‘๐œƒ, its area becomes its length times ๐‘‘๐œƒ

We can find this length by projecting the ribbon to the ground

Using trigonometry, we find the length of a ribbon is 2๐œ‹sin ๐œƒ

Rendering โ€“ Importance Sampling 82

Page 83: Rendering: Importance Sampling - TU Wien

Deriving Integration Over Hemisphere

As a ribbonโ€™s width goes to ๐‘‘๐œƒ, its area becomes its length times ๐‘‘๐œƒ

We can find this length by projecting the ribbon to the ground

Using trigonometry, we find the length of a ribbon is 2๐œ‹sin ๐œƒ

Rendering โ€“ Importance Sampling 83

sin ๐œƒ๐œƒ

๐‘›

Page 84: Rendering: Importance Sampling - TU Wien

The length of a ribbon spans the entire interval ๐œ™ โˆˆ [0, 2๐œ‹)

Convert the length to an integral over ๐‘‘๐œ™: 2๐œ‹sin ๐œƒ = โˆซ02๐œ‹sin ๐œƒ ๐‘‘๐œ™

The final integral: โˆซฮฉ๐‘“ ๐œ” ๐‘‘๐œ” = โˆซ0

๐œ‹

2 โˆซ02๐œ‹๐‘“(๐œ”) sin ๐œƒ ๐‘‘๐œ™ ๐‘‘๐œƒ

Deriving Integration Over Hemisphere

Rendering โ€“ Importance Sampling 84

๐œƒ

๐œ™

๐œ”๐‘‘๐œ”

๐‘›

Page 85: Rendering: Importance Sampling - TU Wien

Deriving PDF for Hemisphere Sampling

Integral of ๐‘“(๐œ”) over area ฮ”๐œ” = โˆซฮ”๐œ” ๐‘“(๐œ”) ๐‘‘๐œ”

Integral of ๐‘“(๐œ”) w.r.t. (๐‘‘๐œƒ, ๐‘‘๐œ™) = โˆซฮ”๐œƒ โˆซฮ”๐œ™ ๐‘“ ๐œ” sin ๐œƒ ๐‘‘๐œ™ ๐‘‘๐œƒ

Integration domain and ๐‘“ ๐œ” are identical, thus: ๐‘‘๐œ” = sin ๐œƒ ๐‘‘๐œ™ ๐‘‘๐œƒ

๐œ” โ†” ๐œƒ, ๐œ™ is bijective, we have ๐‘ ๐œƒ, ๐œ™ d๐œƒ ๐‘‘๐œ™ = ๐‘ ๐œ” ๐‘‘๐œ” and:

๐‘ ๐œƒ, ๐œ™ = sin ๐œƒ ๐‘ ๐œ”

Rendering โ€“ Importance Sampling 85

ฮ”๐œ”ฮ”๐œƒ

Page 86: Rendering: Importance Sampling - TU Wien

Deriving PDF for Hemisphere Sampling, the Formal Way

Target distribution in ๐œ”, which is (๐‘ฅ, ๐‘ฆ, ๐‘ง) with x2 + ๐‘ฆ2 + ๐‘ง2 = 1

The transformation ๐‘‡ from (๐‘Ÿ, ๐œƒ, ๐œ™) to ๐‘ฅ, ๐‘ฆ, ๐‘ง :๐‘ฅ = ๐‘Ÿ sin ๐œƒ cos๐œ™๐‘ฆ = ๐‘Ÿ sin ๐œƒ sin๐œ™๐‘ง = ๐‘Ÿ cos ๐œƒ

The Jacobian of the transformation ๐‘‡ gives ๐ฝ๐‘‡ = ๐‘Ÿ2 sin ๐œƒ

๐‘Ÿ = 1, so we have ๐‘ 1, ๐œƒ, ๐œ™ = sin ๐œƒ ๐‘(๐‘ฅ, ๐‘ฆ, ๐‘ง) = sin ๐œƒ ๐‘(๐œ”)

Rendering โ€“ Importance Sampling 86

Page 87: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Hemisphere

The domain, i.e., the unit hemisphere surface area, is 2๐œ‹.

Uniformly sampling the domain over ๐œ” implies ๐‘ ๐œ” =1

2๐œ‹

Hence, since ๐‘ 1, ๐œƒ, ๐œ™ = sin ๐œƒ ๐‘ ๐œ” , we want ๐‘ ๐œƒ, ๐œ™ =sin ๐œƒ

2๐œ‹

Marginal density ๐‘ฮ˜(๐œƒ): โˆซ02๐œ‹๐‘ ๐œƒ, ๐œ™ ๐‘‘๐œ™ = sin ๐œƒ

Conditional density ๐‘ ๐œ™|๐œƒ :๐‘ ๐œƒ,๐œ™

๐‘ฮ˜(๐œƒ)=

1

2๐œ‹Rendering โ€“ Importance Sampling 87

Page 88: Rendering: Importance Sampling - TU Wien

Uniformly Sampling the Unit Hemisphere โ€“ Complete

Antiderivative of ๐‘ฮ˜(๐œƒ): โˆซ sin ๐œƒ ๐‘‘๐œƒ = 1 โˆ’ cos ๐œƒ (added constant 1)

Antiderivative of ๐‘ ๐œ™|๐œƒ : โˆซ1

2๐œ‹๐‘‘๐œ™ =

๐œ™

2๐œ‹

Invert them to get ๐œƒ = cosโˆ’1 ๐œ‰1 (cos is symmetric), ๐œ™ = 2๐œ‹๐œ‰2

Apply transformation ๐‘‡ on (๐œƒ, ๐œ™) to obtain uniformly distributed ๐œ”

Finally done!Rendering โ€“ Importance Sampling 88

Page 89: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 89

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 90: Rendering: Importance Sampling - TU Wien

Importance Sampling the Diffuse BRDF

Letโ€˜s look once more at the reflected light in the rendering equation

When we bounce at a point ๐‘ฅ, we already know quite a bit:

If we use a diffuse BRDF, then ๐‘“ ๐‘ฅ, ๐œ” โ†’ ๐‘ฃ is a constant factor๐œŒ

๐œ‹

We can predict the cosine termโ€”it depends on our choice of ๐œ”

The tricky part, the big unknown, is the ๐ฟ ๐‘ฅ โ† ๐œ”

Which directions will indirect light come from?

Rendering โ€“ Importance Sampling 90

เถฑฮฉ

๐‘“๐‘Ÿ ๐‘ฅ, ๐œ” โ†’ ๐‘ฃ ๐ฟ ๐‘ฅ โ† ๐œ” cos ๐œƒ๐œ” ๐‘‘๐œ”

๐‘“(๐‘ฅ)

Page 91: Rendering: Importance Sampling - TU Wien

If we donโ€˜t know anything about ๐ฟ, letโ€˜s just assume a constant k

๐œŒ

๐œ‹and ๐‘˜ are constant, so clearly,

๐œŒ

๐œ‹๐‘˜ cos ๐œƒ๐œ” โˆ cos ๐œƒ๐œ”

With these assumptions, theintegrand function is governedentirely by the term ๐’„๐’๐’” ๐œฝ !

Importance Sampling the Diffuse BRDF

Rendering โ€“ Importance Sampling 91

เถฑฮฉ

๐œŒ

๐œ‹๐‘˜ cos ๐œƒ๐œ” ๐‘‘๐œ”

๐œƒ

Page 92: Rendering: Importance Sampling - TU Wien

Importance Sampling the Diffuse BRDF

We know that the ideal distribution ๐‘(๐‘ฅ) for importance sampling a function ๐‘“ ๐‘ฅ is the one that minimizes variance, i.e., โˆ ๐‘“ ๐‘ฅ itself

With the assumption of constant light from all directions, ourintegrand ๐‘“ ๐‘ฅ was simplified to something proportional to cos ๐œƒ

Idea: Importance-sample hemispheresaround hit points with diffuse materialswith distribution ๐‘ ๐œ” โˆ cos ๐œƒ๐œ”

Rendering โ€“ Importance Sampling 92

๐‘› ๐‘›

Uniform

hemisphere sampling

Cosine-weighted

hemisphere sampling

Page 93: Rendering: Importance Sampling - TU Wien

Cosine-Weighted Hemisphere Sampling?

In the first half, we saw how you can apply the inversion method for sampling arbitrary distributions

In the second half, we were all about making sure that we can reach our target distribution when we move from one domain to another

Cosine-weighted hemisphere sampling is a combination of the two

We have gone through all the necessary steps. Try to solve this formally with the inversion method as an exercise!

Rendering โ€“ Importance Sampling 93

Page 94: Rendering: Importance Sampling - TU Wien

Cosine-Weighted Hemisphere Sampling!

Malleyโ€™s method: uniformly pick ๐‘ฅ, ๐‘ฆ samples on the unit disk

Project them to the hemisphere surface ๐‘ง = 1 โˆ’ ๐‘ฅ2 โˆ’ ๐‘ฆ2

๐‘ ๐œ” =cos ๐œƒ

๐œ‹

Done! Your samples are nowdistributed with ๐‘ ๐œ” โˆ cos ๐œƒ!(Why? And why does this work? Try to find your own proof!)

Rendering โ€“ Importance Sampling 94

Page 95: Rendering: Importance Sampling - TU Wien

Importance Sampling the Diffuse BRDF

Rendering โ€“ Importance Sampling 95

AO, 64 samples, uniform hemisphere sampling AO, 64 samples, diffuse BRDF importance sampling

Page 96: Rendering: Importance Sampling - TU Wien

More Importance Sampling

The impact will be much greater when we add non-diffuse materials

BRDF functions canbe rather complexโ€ฆ

โ€ฆbut can often benicely approximated

You will want to sample with distributions more complex than cos ๐œƒ

Rendering โ€“ Importance Sampling 96

Page 97: Rendering: Importance Sampling - TU Wien

Importance Sampling the Full Rendering Equation

As you can imagine, this is a much more complex task

In fact, an enormous amount of research in rendering is actively pursuing better and better ways to make this happen

Other sophisticated methods, like multiple importance sampling(MIS), can be of great help here!

We will hear more about MIS next timeโ€ฆ

Rendering โ€“ Importance Sampling 97

Page 98: Rendering: Importance Sampling - TU Wien

Importance Sampling Summary

If we do Monte Carlo integration of ๐‘“(๐‘ฅ) , itโ€™s best to use a sample distribution ๐‘(๐‘ฅ) that closely mimics ๐‘“(๐‘ฅ)

For a desired ๐‘ ๐‘ฅ โˆ ๐‘“(๐‘ฅ), we can use the inversion method to getthe methods for generating samples and probability densities

If you cannot turn ๐‘“(๐‘ฅ) into a valid PDF, try to find a close match

When we transform samples between domains, we have to makesure they have the desired distribution in the target domain!

Rendering โ€“ Importance Sampling 98

Page 99: Rendering: Importance Sampling - TU Wien

Todayโ€™s Roadmap

Rendering โ€“ Importance Sampling 99

What exactly are CDFs and PDFs?

What is the inversion method?

How to sample arbitrary functions?

Whatโ€™s the fastest way to cosine-

weighted hemisphere sampling?

The Inversion

Method

Sampling the

Unit Disk

Required

Background

(CDF, PDF)

Importance Sampling

for the Diffuse BRDF

Sampling the

Hemisphere

Importance

Sampling Unlocked!

Sampling the Unit Disk 2:

Crossing Domains

Page 100: Rendering: Importance Sampling - TU Wien

References and Further Reading

Slide set based mostly on chapter 13 of Physically Based Rendering: From Theory to Implementation

[1] Steven Strogatz, Infinite Powers: How Calculus Reveals the Secrets of the Universe

[2] Video, Why โ€œprobability of 0โ€ does not mean โ€œimpossibleโ€ | Probabilities of probabilities, part 2: https://www.youtube.com/watch?v=ZA4JkHKZM50

[3] Video, The determinant | Essence of linear algebra, chapter 6: https://www.youtube.com/watch?v=Ip3X9LOh2dk

[4] SIGGRAPH 2012 Course: Advanced (Quasi-) Monte Carlo Methods for Image Synthesis, https://sites.google.com/site/qmcrendering/

[5] Wikipedia, Volume Element, https://en.wikipedia.org/wiki/Volume_element

Rendering โ€“ Importance Sampling 100