Rendering: Monte Carlo Integration I Bernhard Kerbl Research - - PowerPoint PPT Presentation

β–Ά
rendering monte carlo integration i
SMART_READER_LITE
LIVE PREVIEW

Rendering: Monte Carlo Integration I Bernhard Kerbl Research - - PowerPoint PPT Presentation

Rendering: Monte Carlo Integration I 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


slide-1
SLIDE 1

Rendering: Monte Carlo Integration I

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

ΰΆ±

slide-2
SLIDE 2

Integrating the cosine-weighted radiance 𝑀𝑗(𝑦, πœ•) at a point 𝑦 Integral of the light function

  • ver the hemisphere, w.r.t.

direction/solid angle πœ• This is easier said than done!

How do we integrate over the hemisphere? 𝑀𝑗(𝑦, πœ•) depends on lights, geometry… how can we integrate that?

Today’s Goal

Rendering – Monte Carlo Integration I 2

slide-3
SLIDE 3

The solution involves methods from statistics, probability and calculus that are combined to achieve Monte Carlo Integration This is a lot to take in, some of the concepts are complex We choose to explore them in an illustrative way because grasping the underlying ideas makes their application much easier We will try to present the bare necessities you need to write a rendering routine two versions: a formal and an intuitive one

Today’s Goal

Rendering – Monte Carlo Integration I 3

slide-4
SLIDE 4

Fundamentals Recap

Calculus

Derivatives Integrals

Probability and Statistics

Discrete/Continuous Random Variables Uniform/Non-Uniform Distributions Probability Density Function Expected Value and Variance

Rendering – Monte Carlo Integration I 4

slide-5
SLIDE 5

Derivatives

Derivative 𝑔′(𝑦) of 𝑔(𝑦) gives the rate of change of 𝑔(𝑦) at point 𝑦 Answers the question: how does 𝑧 = 𝑔(𝑦) change within an infinitesimally small range 𝑒𝑦 around 𝑦

𝑔 𝑦+𝑒𝑦 βˆ’π‘”(𝑦) 𝑦+π‘’π‘¦βˆ’π‘¦

= 𝑒𝑧

𝑒𝑦

Closed-form solutions don’t always exist (discontinuous functions) Functions of multiple variables can be derived w.r.t. any of them, yielding a partial derivative (indicated by e.g. πœ–π‘¦ instead of 𝑒𝑦)

Rendering – Monte Carlo Integration I 5

slide-6
SLIDE 6

Basic notation: 𝐺 𝑦 = Χ¬ 𝑔 𝑦 𝑒𝑦 𝐺 𝑦 is any function that fulfills 𝐺 𝑦 β€² = 𝑔 𝑦 , thus it is generally called the β€œanti-derivative” of 𝑔 𝑦 By this definition, solutions can include arbitrary constants 𝑑, e.g.:

Χ¬ 𝑦 𝑒𝑦 =

2 3 𝑦2 3

+ 𝑑 Χ¬ 𝑦 𝑒𝑦 =

𝑦2 2 + 𝑑

Χ¬ cos 𝑦 𝑒𝑦 = sin 𝑦 + 𝑑

Indefinite Integral

Rendering – Monte Carlo Integration I 6

slide-7
SLIDE 7

Definite Integral: An interpretation

Rendering – Monte Carlo Integration I 7

Basic notation: Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦, with

the variable of integration 𝑦 the integration interval [𝑏, 𝑐] for 𝑦 the function 𝑔 𝑦 to integrate (integrand) the differential 𝑒𝑦 for 𝑦

Informally: β€œThe area under the curve”[1] The differential is an β€œinfinitesimal range”, making 𝑔 𝑦 β‹… 𝑒𝑦 an infinitesimal area. The integral is the sum of these areas in [𝑏, 𝑐]

slide-8
SLIDE 8

Basic notation: Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦, with

the variable of integration 𝑦 the integration interval [𝑏, 𝑐] for 𝑦 the function 𝑔 𝑦 to integrate (integrand) the differential 𝑒𝑦 for 𝑦

Informally: β€œThe area under the curve”[1] The differential is an β€œinfinitesimal range”, making 𝑔 𝑦 β‹… 𝑒𝑦 an infinitesimal area. The integral is the sum of these areas in [𝑏, 𝑐]

Definite Integral: An interpretation

Rendering – Monte Carlo Integration I 8

Δ𝑦 𝑔(𝑦)

slide-9
SLIDE 9

Basic notation: Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦, with

the variable of integration 𝑦 the integration interval [𝑏, 𝑐] for 𝑦 the function 𝑔 𝑦 to integrate (integrand) the differential 𝑒𝑦 for 𝑦

Informally: β€œThe area under the curve”[1] The differential is an β€œinfinitesimal range”, making 𝑔 𝑦 β‹… 𝑒𝑦 an infinitesimal area. The integral is the sum of these areas in [𝑏, 𝑐]

Definite Integral: An interpretation

Rendering – Monte Carlo Integration I 9

Δ𝑦 𝑔(𝑦)

slide-10
SLIDE 10

Basic notation: Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦, with

the variable of integration 𝑦 the integration interval [𝑏, 𝑐] for 𝑦 the function 𝑔 𝑦 to integrate (integrand) the differential 𝑒𝑦 for 𝑦

Informally: β€œThe area under the curve”[1] The differential is an β€œinfinitesimal range”, making 𝑔 𝑦 β‹… 𝑒𝑦 an infinitesimal area. The integral is the sum of these areas in [𝑏, 𝑐]

Definite Integral: An interpretation

Rendering – Monte Carlo Integration I 10

𝑔(𝑦) Δ𝑦

slide-11
SLIDE 11

Basic notation: Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦, with

the variable of integration 𝑦 the integration interval [𝑏, 𝑐] for 𝑦 the function 𝑔 𝑦 to integrate (integrand) the differential 𝑒𝑦 for 𝑦

Informally: β€œThe area under the curve”[1] The differential is an β€œinfinitesimal range”, making 𝑔 𝑦 β‹… 𝑒𝑦 an infinitesimal area. The integral is the sum of these areas in [𝑏, 𝑐]

Definite Integral: An interpretation

Rendering – Monte Carlo Integration I 11

𝑔(𝑦) Δ𝑦 = 𝑒𝑦

slide-12
SLIDE 12

With a solution for the indefinite integral 𝐺 𝑦 = Χ¬ 𝑔 𝑦 𝑒𝑦, we can solve Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦 = 𝐺 𝑐 βˆ’ 𝐺(𝑏)

Example:

Unit circle: 𝑦2 + 𝑧2 = 1, area is 𝜌 𝑔 𝑦 = 𝑧 = 1 βˆ’ 𝑦2 Χ¬ 𝑔 𝑦 𝑒𝑦 = 1

2 ( 1 βˆ’ 𝑦2 β‹… 𝑦 + sinβˆ’1 𝑦)

Χ¬

1 𝑔 𝑦 𝑒𝑦 = 𝐺 1 βˆ’ 𝐺 0 = 𝜌 4

Solving Definite Integrals

Rendering – Monte Carlo Integration I 12

𝑧 𝑦 1 1

slide-13
SLIDE 13

With a solution for the indefinite integral 𝐺 𝑦 = Χ¬ 𝑔 𝑦 𝑒𝑦, we can solve Χ¬

𝑏 𝑐 𝑔 𝑦 𝑒𝑦 = 𝐺 𝑐 βˆ’ 𝐺(𝑏)

Example:

Unit circle: 𝑦2 + 𝑧2 = 1, area is 𝜌 𝑔 𝑦 = 𝑧 = 1 βˆ’ 𝑦2 Χ¬ 𝑔 𝑦 𝑒𝑦 = 1

2 ( 1 βˆ’ 𝑦2 β‹… 𝑦 + sinβˆ’1 𝑦)

Χ¬

1 𝑔 𝑦 𝑒𝑦 = 𝐺 1 βˆ’ 𝐺 0 = 𝜌 4

Solving Definite Integrals

Rendering – Monte Carlo Integration I 13

𝑧 𝑦 1 1

𝝆 πŸ“

slide-14
SLIDE 14

Remarks on Definite Integrals

To generalize to π‘œ-D, we will talk about β€œvolume” rather than area We use subscript-only symbol Χ¬

𝐸 for integral over entire domain 𝐸

Integrating 1 over range [𝑏, 𝑐] gives the length/volume of the range Integrating 1 over an π‘œ-D domain gives the volume of the domain A domain 𝐸 with X ∈ [0, 2], 𝑍 ∈ [2, 5] in and Z ∈ [1, 1.5], we have: π‘Šπ‘π‘š 𝐸 = Χ¬

𝐸 1 𝑒𝐸 = Χ¬ 2 Χ¬ 2 5 Χ¬ 1 1.5 1 𝑒𝑦 𝑒𝑧 𝑒𝑨 = 2 Γ— 3 Γ— 0.5 = 3

Rendering – Monte Carlo Integration I 14

slide-15
SLIDE 15

Random Variables

We indicate random variables with capital letters π‘Œ, 𝑍, … and some Greek symbols for special random variables Random variables are drawn from some domain of possible results We define an outcome, or β€œevent” for draws from random variables. π‘Œπ‘— marks an observed outcome of a given random variable π‘Œ Random variables can be discrete or continuous. Functions of random variables can themselves be seen as random variables

Rendering – Monte Carlo Integration I 15

slide-16
SLIDE 16

Uniform and Non-Uniform Distributions

Rendering – Monte Carlo Integration I 16

The occurrence of values drawn from a random variable usually follows a given probability distribution If a random variable has a uniform distribution, all possible

  • utcomes are equally likely to occur (e.g., a fair die or fair coin)

For non-uniform distributions, the probability of certain values is significantly higher than others (e.g., population body height)

slide-17
SLIDE 17

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 17

slide-18
SLIDE 18

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 – Monte Carlo Integration I 18

slide-19
SLIDE 19

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 – Monte Carlo Integration I 19

1

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

?

slide-20
SLIDE 20

𝑄

π‘Œ 𝑐 βˆ’ 𝑄 π‘Œ 𝑏

= 𝑄𝑠 𝑏 ≀ π‘Œπ‘— ≀ 𝑐 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 – Monte Carlo Integration I 20

𝑧 𝑦 1 1 𝑄(𝑦)

slide-21
SLIDE 21

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 – Monte Carlo Integration I 21

𝑧 𝑦 1 𝑄(𝑦)

slide-22
SLIDE 22

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 – Monte Carlo Integration I 22

𝑦 1 π‘ž0 π‘ž0 π‘ž0 π‘ž0 π‘ž1 π‘ž1 π‘ž1 π‘ž3 π‘ž2 π‘ž2 𝑦 1 π‘ž0 π‘ž1 π‘ž3 π‘ž2

Outcome Probabilities Cumulated Probabilities (CDF)

slide-23
SLIDE 23

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 – Monte Carlo Integration I 23

slide-24
SLIDE 24

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 – Monte Carlo Integration I 24

slide-25
SLIDE 25

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 – Monte Carlo Integration I 25

slide-26
SLIDE 26

Basic Sampling of Random Variables

We want to generate samples for a custom random variable from a distribution 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 – Monte Carlo Integration I 26

slide-27
SLIDE 27

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 – Monte Carlo Integration I 27

slide-28
SLIDE 28

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 – Monte Carlo Integration I 28

𝜊

1 𝑄(𝑦)

π‘Œπ‘— 𝜊

1

π‘Œπ‘—

π‘ž0 π‘ž0 π‘ž0 π‘ž0 π‘ž1 π‘ž1 π‘ž1 π‘ž3 π‘ž2 π‘ž2

slide-29
SLIDE 29

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 – Monte Carlo Integration I 29

slide-30
SLIDE 30

Warping Uniform To Exponential Distribution

Rendering – Monte Carlo Integration I 30

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) = x auto x_ = uniform_samples[i] = uniform_dist(rand_eng_uniform); auto X_i = -std::log(1.0 - x_) / LAMBDA; warped_samples[i] = X_i; } }

slide-31
SLIDE 31

Histograms of generated samples

Warping Uniform To Exponential Distribution

Rendering – Monte Carlo Integration I 31

π‘Œπ‘— = 𝑄

π‘Œ βˆ’1 πœŠπ‘—

πœŠπ‘— 𝑆𝑗 π‘Œπ‘—

slide-32
SLIDE 32

Warping Uniform To Exponential Distribution

Rendering – Monte Carlo Integration I 32

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) = x auto 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

slide-33
SLIDE 33

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 – Monte Carlo Integration I 33

slide-34
SLIDE 34

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 – Monte Carlo Integration I 34

slide-35
SLIDE 35

2D with 𝑍 = 𝜊. For π‘Œ, use π‘Œ ∈ [0,

𝜌 2) and π‘ž 𝑦 = cos 𝑦

𝑄

π‘Œ 𝑦 = Χ¬ π‘ž 𝑦 𝑒𝑦 = Χ¬cos 𝑦 𝑒𝑦 = sin 𝑦

𝑄

π‘Œ βˆ’1 𝜊 = sinβˆ’1(𝜊)

Inversion Method Examples in 2D

Rendering – Monte Carlo Integration I 35 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 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 = x_; auto Y_i = asin(y_); samples2D[i] = std::make_pair(X_i, Y_i); } }

slide-36
SLIDE 36

π‘Œ and 𝑍 in range 0,1 For both variables, π‘ž 𝑀 = 2𝑀, 𝑄 𝑀 = 𝑀2, π‘„βˆ’1 𝜊 = 𝜊

Inversion Method Examples in 2D

Rendering – Monte Carlo Integration I 36 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 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); } }

slide-37
SLIDE 37

Let’s pick a slow-growing portion of the distribution function Compared to 0,1 , densities only double inside range 2,4

Choosing a Different Range

Rendering – Monte Carlo Integration I 37

slide-38
SLIDE 38

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 – Monte Carlo Integration I 38 0,5 1 1,5 2 2,5 3 3,5 4 0,5 1 1,5 2 2,5 3 3,5 4

Y X

slide-39
SLIDE 39

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 𝑄 𝑦 = 𝐺(𝑦)

𝑑

while ensuring π‘ž 𝑦 ∝ 𝑔(𝑦)

Restricting the PDF / CDF

Rendering – Monte Carlo Integration I 39

slide-40
SLIDE 40

For range [𝑏, 𝑐] where 𝑏 β‰  0, we add a constant offset 𝑙 = βˆ’π‘„(𝑏) Try π‘Œ, 𝑍 ∈ 2,4 and 𝑔 𝑀 = 2𝑀 again We compute 𝑑𝑍 = π‘‘π‘Œ = Χ¬

2 4 2𝑀 𝑒𝑀 = 12 and add 𝑙 = βˆ’ 4 12 to get:

𝑄 𝑀 = 𝑀2βˆ’4

12 , π‘„βˆ’1 𝜊 = 2 3 β‹… 𝜊 + 1

Restricting the PDF / CDF

Rendering – Monte Carlo Integration I 40 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

slide-41
SLIDE 41

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 – Monte Carlo Integration I 41

slide-42
SLIDE 42

Another Look at the PDF

We saw samples being β€œwarped”: we can interpret the inversion method as a spatial transformation of uniform samples Let’s treat regular intervals in the input domain as infinitesimal hypercubes: a segment in 1D, a square in 2D and a cube in 3D If we warp a space where each variable is 𝜊 to one with joint PDF π‘žπΈ, then

1 π‘žπΈ is the change in volume of the hypercubes after warping

Rendering – Monte Carlo Integration I 42

slide-43
SLIDE 43

Visualizing the PDF in 1D

Let’s look at an example with a custom 1D random variable If the target defines the variable π‘Œ, π‘žπ‘Œ 𝑦 = 2𝑦 means the volume

  • f transformed hypercubes at x = 1 is half of those at x = 0.5

We check for tiny 1D hypercubes (0.01-long segments)

π‘žπ‘Œ 𝑦 = 2𝑦, 𝑄

π‘Œ 𝑦 = 𝑦2, 𝑦 = 𝑄 π‘Œ βˆ’1 𝜊 =

𝜊 οƒŸ x = 0.5 at 𝜊 = 0.25 1.00 βˆ’ 0.99 β‰ˆ 0,005: 0.25 βˆ’ 0.24 β‰ˆ 0,010:

Rendering – Monte Carlo Integration I 43

slide-44
SLIDE 44

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 – Monte Carlo Integration I 44

𝑍 = ΞΎ2 and π‘Œ ∈ 0,1 , π‘žπ‘Œ 𝑦 = 2𝑦 ΞΎ1, ΞΎ2

slide-45
SLIDE 45

ΞΎ1, ΞΎ2

The areas of all 2D hypercubes (squares) are scaled by

1 π‘žπ‘Œ 𝑦

On the right, rectangles at (1, 𝑧) are half the width of the original

Visualizing the PDF in 2D

Rendering – Monte Carlo Integration I 45

𝑍 = ΞΎ2 and π‘Œ ∈ 0,1 , π‘žπ‘Œ 𝑦 = 2𝑦

slide-46
SLIDE 46

We just saw samples of π‘Œ, 𝑍 ∈ 0,1 with π‘žπ‘Œ 𝑦 = 2𝑦, π‘žπ‘(𝑧) = 2𝑧

Visualizing the PDF in 2D

Rendering – Monte Carlo Integration I 46 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1

2D Variables with Linear PDFs

slide-47
SLIDE 47

In this 2D setup, we have joint PDF π‘ž 𝑦, 𝑧 = π‘žπ‘Œ 𝑦 π‘žπ‘ 𝑧 = 4𝑦𝑧 The areas near point (1,1) are squished to

1 4 of the original squares

Visualizing the PDF in 2D

Rendering – Monte Carlo Integration I 47

ΞΎ1, ΞΎ2 π‘Œ, 𝑍 ∈ 0,1 , π‘žπ‘Œ 𝑦 = 2𝑦, π‘žπ‘ 𝑧 = 2𝑧

slide-48
SLIDE 48

This PDF condenses areas at higher values of x, 𝑧, expands at lower If the area changes, the points in it distribute accordingly!

Visualizing the PDF in 2D

Rendering – Monte Carlo Integration I 48 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1

2D Variables with Linear PDFs

0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1

Canonic Uniform Variables

slide-49
SLIDE 49

Expected Value

Expected value of a continuous variable π‘Œ, its domain 𝐸 and distribution defined by PDF π‘žπ‘Œ 𝑦 , is defined as: 𝐹 π‘Œ π‘žπ‘Œ = ΰΆ±

𝐸

𝑦 β‹… π‘žπ‘Œ 𝑦 𝑒𝑦 Computes a weighted average over domain, basic average if π‘Œ = 𝜊 Answers the question: β€œWhat is the average value that we can expect to draw from π‘Œ?”

Rendering – Monte Carlo Integration I 49

slide-50
SLIDE 50

Variance

Average (expected), squared deviation from the mean 𝜈 = 𝐹 π‘Œ π‘žπ‘Œ πœπ‘Œ

2 = π‘Šπ‘π‘  π‘Œ = 𝐹

π‘Œ βˆ’ 𝜈 2 π‘žπ‘Œ Taking its root πœπ‘Œ

2 yields the standard deviation πœπ‘Œ

Answers the question: β€œHow strongly do values drawn from π‘Œ fluctuate about its expected value?” Note that, as for expected value, PDF π‘žπ‘Œ is included in the definition

Rendering – Monte Carlo Integration I 50

slide-51
SLIDE 51

Monte Carlo Integration

With refreshed knowledge of calculus, random variables, CDFs and PDFs, we have all the tools to approach Monte Carlo integration Simply put, integration approximates the area under a curve with increasing accuracy by splitting it into ever smaller, basic shapes Let us consider this approach to find a way for computing the integral of given functions by sampling

Rendering – Monte Carlo Integration I 51

slide-52
SLIDE 52

Why Monte Carlo Integration?

We cannot always find a closed-form solution for the integral The light function in rendering is one such case We might have decent idea what the function of incoming light looks like, but its exact shape is not known

Computing the total incoming light at a point means evaluating entire scene geometry for every point we hit Hard shadows make the light function discontinuous The rendering equation is an infinite-dimensional (!) integral

Rendering – Monte Carlo Integration I 52

slide-53
SLIDE 53

Approximating the Integral

We can sample an integrand 𝑔(𝑦) evenly at regular intervals β„Ž Find areas of trapezoids under the curve and compute their sum Can simplify to rectangles instead of trapezoids Needs more samples for same precision, but simpler

Rendering – Monte Carlo Integration I 53

𝑔(𝑦) 𝑦

slide-54
SLIDE 54

Multidimensional Problems

Regular sampling causes noticeable patterns and aliasing Need π‘‚π‘œ samples to evaluate an π‘œ-D function at

1 𝑂 intervals

If we want to sample the grid in 2D, we must change the total number of samples in increments of 2𝑂 + 1, e.g.: 1, 4, 9, 16, etc. This only gets worse with more dimensions (curse of dimensionality)

Rendering – Monte Carlo Integration I 54

𝑔(𝑦) 𝑦 The integral computed from these samples will vastly underestimate the true value!

slide-55
SLIDE 55

The Curse of Dimensionality

Rendering – Monte Carlo Integration I 55

slide-56
SLIDE 56

The Curse of Dimensionality

Rendering – Monte Carlo Integration I 56

slide-57
SLIDE 57

The Curse of Dimensionality

Rendering – Monte Carlo Integration I 57

slide-58
SLIDE 58

Monte Carlo Integration

Two observations for the integration of a function via sampling

The order of the samples doesn’t matter, only their sum We can switch the fixed interval 1

𝑂 with something expected to be 1 𝑂

Replace fixed-order regular samples with uniform random variable

Doesn’t matter that generated values are not in any defined order With 𝑂 uniform samples, the expected interval between them is 1

𝑂

Randomness also reduces aliasing problems!

Rendering – Monte Carlo Integration I 58

slide-59
SLIDE 59

Monte Carlo Integration for Uniform Variables

We take 𝑂 uniform, random samples and treat the results as if we

  • btained them by subdividing the domain into 𝑂 regular intervals

Sum samples of 𝑔 𝑦 , multiply with domain volume and average If this seems coarse, remember: we want an approximation of the total area under the curve that improves with increasing 𝑂

Rendering – Monte Carlo Integration I 59

ΰΆ±

𝐸

𝑔 𝑦 𝑒𝑦 β‰ˆ π‘Šπ‘π‘š(𝐸) 𝑂 ෍

𝑗=1 𝑂

𝑔(π‘Œπ‘—)

𝑔(𝑦) 𝑏 𝑐

slide-60
SLIDE 60

Monte Carlo Integration for Non-Uniform Variables

We can generalize the Monte Carlo integration to work with variables that have arbitrary PDFs. The final MC formula: π‘ž(π‘Œπ‘—) tells us how likely it is that samples land in that portion of the domain: values that are sampled frequently receive a smaller weight We can see

1 π‘ž π‘Œπ‘— as the volume of a hypercube π‘Š π‘Œπ‘— at sample

location π‘Œπ‘— and see that

π‘Šπ‘Œπ‘— 𝑂 𝑔(π‘Œπ‘—) is quite close to π‘Šπ‘π‘š 𝐸 𝑂

𝑔(π‘Œπ‘—)

Rendering – Monte Carlo Integration I 60

ΰΆ±

𝐸

𝑔(𝑦) 𝑒𝑦 β‰ˆ 𝐺𝑂 = 1 𝑂 ෍

𝑗=1 𝑂 𝑔(π‘Œπ‘—)

π‘ž(π‘Œπ‘—)

slide-61
SLIDE 61

The Rationale Behind 1/π‘ž(𝑦)

Using a non-uniform π‘ž(𝑦) to sample a constant function 𝑔(𝑦) Sample arrows indicate the value of

1 π‘ž(𝑦): blue = low, red = high

Red samples are rare, they represent a larger area under the curve

Rendering – Monte Carlo Integration I 61

𝑦 𝑔(𝑦) 𝑦 π‘ž(𝑦)

slide-62
SLIDE 62

The Rationale Behind 1/π‘ž(𝑦)

Using a non-uniform π‘ž(𝑦) to sample a non-uniform function 𝑔(𝑦) Same weight for each sample: overestimates area under the curve Using 1

𝑂 σ𝑗=1 𝑂 𝑔(π‘Œπ‘—) π‘ž(π‘Œπ‘—) instead of π‘Šπ‘π‘š(𝐸) 𝑂

σ𝑗=1

𝑂

𝑔(π‘Œπ‘—) is the right choice

Rendering – Monte Carlo Integration I 62

𝑦 𝑔(𝑦) 𝑦 π‘ž(𝑦)

1 𝑂 ෍

𝑗=1 𝑂 𝑔(π‘Œπ‘—)

π‘ž(π‘Œπ‘—) π‘Šπ‘π‘š(𝐸) 𝑂 ෍

𝑗=1 𝑂

𝑔(π‘Œπ‘—)

slide-63
SLIDE 63

The Rationale Behind 1/π‘ž(𝑦)

Rendering – Monte Carlo Integration I 63

𝑦 𝑔(𝑦) 𝑦 π‘ž(𝑦)

1 𝑂 ෍

𝑗=1 𝑂 𝑔(π‘Œπ‘—)

π‘ž(π‘Œπ‘—) π‘Šπ‘π‘š(𝐸) 𝑂 ෍

𝑗=1 𝑂

𝑔(π‘Œπ‘—)

Final word: During Monte Carlo integration, we use

1 π‘ž 𝑦 𝑂 from the start as the Δ𝑦, so that

Δ𝑦 β‹… 𝑔(𝑦) gives us an area under the curve. The more samples 𝑂 we take, the closer the distance between the two closest samples near a point 𝑦 gets to

1 π‘ž 𝑦 𝑂 and the better the

approximation of the true integral, i.e., the sum of infinitesimal areas under the curve.

slide-64
SLIDE 64

Verifying the Monte Carlo Integral

Formal verification that expected value of 𝐺𝑂 is the integral of 𝑔 𝑦 Constants and sums can be moved

  • ut of the expected value operator

Expected value for any event π‘Œπ‘— drawn from π‘Œ is equal to 𝐹[π‘Œ] Probability of

𝑔 𝑦 π‘ž 𝑦 depends only on 𝑦

Rendering – Monte Carlo Integration I 64

𝐹[𝐺𝑂] = E 1 𝑂 ෍

𝑗=1 𝑂 𝑔 π‘Œπ‘—

π‘ž π‘Œπ‘— with π‘Œ ∈ 𝐸 = 1 𝑂 ෍

𝑗=1 𝑂

𝐹 𝑔 π‘Œπ‘— π‘ž π‘Œπ‘— = 1 𝑂 ෍

𝑗=1 𝑂

ΰΆ±

𝐸

𝑔 𝑦 π‘ž 𝑦 π‘ž 𝑦 𝑒𝑦 = 1 𝑂 ෍

𝑗1 𝑂

ΰΆ±

𝐸

𝑔 𝑦 𝑒𝑦 = ΰΆ±

𝐸

𝑔 𝑦 𝑒𝑦

slide-65
SLIDE 65

Importance Sampling

Importance sampling = picking a good PDF that adapts to 𝑔 𝑦 Intuitive justification: Sample more in places where we have larger contributions to the integral to capture high-frequency details there

Rendering – Monte Carlo Integration I 65

slide-66
SLIDE 66

Choosing the Right PDF

𝐺𝑂 is itself a random variable, variance shows up as random noise π‘Šπ‘π‘  𝐺𝑂 =

1 𝑂 π‘Šπ‘π‘  𝑔 𝑦 π‘ž 𝑦

= 1

𝑂 𝐹 𝑔 𝑦 π‘ž 𝑦 βˆ’ E 𝑔 𝑦 π‘ž 𝑦 2 π‘ž

No noise if

𝑔 𝑦 π‘ž 𝑦 is a constant β†’ what is a good PDF to choose?

Rendering – Monte Carlo Integration I 66

ΰΆ±

𝐸

𝑔 𝑦 𝑒𝑦 β‰ˆ 𝐺𝑂 = 1 𝑂 ෍

𝑗=1 𝑂 𝑔 π‘Œπ‘—

π‘ž π‘Œπ‘—

slide-67
SLIDE 67

Choosing the Right PDF

Choose a PDF that mimics the shape of 𝑔(𝑦), but is easy to sample

Note: Χ¬

𝐸 π‘ž(𝑦) 𝑒𝑦 must integrate to 1, so can’t just take π‘ž 𝑦 = 𝑔(𝑦)

To normalize Χ¬

𝐸 𝑔(𝑦) 𝑒𝑦, we would have to know the integral ∴

Rendering – Monte Carlo Integration I 67

slide-68
SLIDE 68

The Importance of Importance Sampling

Rendering – Monte Carlo Integration I 68

slide-69
SLIDE 69

The Importance of Importance Sampling

Rendering – Monte Carlo Integration I 69

slide-70
SLIDE 70

The Importance of Importance Sampling

Rendering – Monte Carlo Integration I 70

slide-71
SLIDE 71

Monte Carlo Integration Pseudo Code

A minimal sampling and integration procedure could look like this:

Given: function f(x), PDF p(x) and CDF P(x) value = 0 for i in [0, N) do u = uniform_random_sample() x = P_inverse(u) value += f(x)/p(x) end for value /= N

Rendering – Monte Carlo Integration I 71

slide-72
SLIDE 72

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/

Rendering – Monte Carlo Integration I 72