• 沒有找到結果。

Monte Carlo Integration

N/A
N/A
Protected

Academic year: 2022

Share "Monte Carlo Integration"

Copied!
15
0
0

加載中.... (立即查看全文)

全文

(1)

Monte Carlo Integration

Digital Image Synthesis Yung-Yu Chuang

12/6/2007

with slides by Pat Hanrahan and Torsten Moller

Introduction

• The integral equations generally don’t have analytic solutions, so we must turn to numerical methods.

• Standard methods like Trapezoidal integration or Gaussian quadrature are not effective for high-dimensional and discontinuous integrals.

= ) ω p, ( o

Lo Le(p,ωo)

i i i

i

o,ω ) (p,ω )cosθ ω ω

p,

2 f( Li d

s

+

Numerical quadrature

• Suppose we want to calculate , but can’t solve it analytically. The approximations through quadrature rules have the form

which is essentially the weighted sum of samples of the function at various points

= b

a f x dx

I ( )

=

= n

i

i if x w I

1

) ˆ (

Midpoint rule

convergence

(2)

Trapezoid rule

convergence

Simpson’s rule

• Similar to trapezoid but using a quadratic polynomial approximation

convergence

assuming f has a continuous fourth derivative.

Curse of dimensionality and discontinuity

• For an sd function f,

• If the 1d rule has a convergence rate of O(n-r), the sd rule would require a much larger number (ns) of samples to work as well as the 1d one.

Thus, the convergence rate is only O(n-r/s).

• If f is discontinuous, convergence is O(n-1/s) for sd.

Randomized algorithms

• Las Vegas v.s. Monte Carlo

• Las Vegas: always gives the right answer by using randomness.

• Monte Carlo: gives the right answer on the average. Results depend on random numbers used, but statistically likely to be close to the right answer.

(3)

Monte Carlo integration

• Monte Carlo integration: uses sampling to estimate the values of integrals. It only

requires to be able to evaluate the integrand at arbitrary points, making it easy to implement and applicable to many problems.

• If n samples are used, its converges at the rate of O(n-1/2). That is, to cut the error in half, it is necessary to evaluate four times as many samples.

• Images by Monte Carlo methods are often noisy.

Most current methods try to reduce noise.

Monte Carlo methods

• Advantages

– Easy to implement

– Easy to think about (but be careful of statistical bias) – Robust when used with complex integrands and

domains (shapes, lights, …)

– Efficient for high dimensional integrals

• Disadvantages

– Noisy

– Slow (many samples needed for convergence)

Basic concepts

• X is a random variable

• Applying a function to a random variable gives another random variable, Y=f(X).

• CDF (cumulative distribution function)

• PDF (probability density function): nonnegative, sum to 1

• canonical uniform random variable ξ (provided by standard library and easy to transform to other distributions)

dx x x dP

p ( )

) ( ≡

} Pr{

)

(x X x

P ≡ ≤

Discrete probability distributions

• Discrete events

X

i with probability

p

i

• Cumulative PDF (distribution)

• Construction of samples

• To randomly select an event,

• Select

X

i if

i 0 p

1

1

n i i

p

=

=

1

i i

P < ≤U P

pi

1 j

j i

i

P p

=

=

U 1

0

Uniform random variable X3 Pi

(4)

Continuous Probability Distributions

• PDF

• CDF

1

0 ( ) Pr( )

P x = X < x

Pr( ) ( )

( ) ( )

X p x dx

P P

β α

α β

β α

≤ ≤ =

= −

( ) p x

0 1

Uniform

( ) 0 p x

0

( ) ( )

x

P x =

p x dx

(1) 1

P =

( ) P x

Expected values

• Average value of a function f(x) over some distribution of values p(x) over its domain D

• Example: cos function over [0, π], p is uniform

[ ]

=

D

p f x f x p x dx

E ( ) ( ) ( )

[ ]

=

0π π1 =0

cos )

cos(x x dx

Ep

Variance

• Expected deviation from the expected value

• Fundamental concept of quantifying the error in Monte Carlo methods

[ ]

( )

[

( ) ( ) 2

]

)]

(

[f x E f x E f x

V = −

Properties

[

af(x)

]

aE

[

f(x)

]

E =

[ ]

⎥⎦=

⎢ ⎤

i

i i

i E f X

X f

E ( ) ( )

[

af(x)

]

a2V

[

f(x)

]

V =

[

f(x)

]

E

[ (

f(x)

)

2

]

E

[

f(x)

]

2

V = −

(5)

Monte Carlo estimator

• Assume that we want to evaluate the integral of f(x) over [a,b]

• Given a uniform random variable Xi over [a,b], Monte Carlo estimator

says that the expected value E[FN] of the estimator FNequals the integral

=

= − N

i

i

N f X

N a F b

1

) (

General Monte Carlo estimator

• Given a random variable X drawn from an arbitrary PDF p(x), then the estimator is

• Although the converge rate of MC estimator is O(N1/2), slower than other integral methods, its converge rate is independent of the dimension, making it the only practical method for high dimensional integral

=

= N

i i

i

N p X

X f F N

1 ( )

) 1 (

Convergence of Monte Carlo

• Chebyshev’s inequality: let X be a random variable with expected value μ and variance σ2. For any real number k>0,

• For example, for k= , it shows that at least half of the value lie in the interval

• Let , the MC estimate FYi = f(Xi)/p(Xi) N becomes

=

= N

i i

N Y

F N

1

1

2

} 1

|

Pr{|X −μ ≥kσ ≤ k

2

) 2 ,

2

(μ σ μ+ σ

Convergence of Monte Carlo

• According to Chebyshev’s inequality,

• Plugging into Chebyshev’s inequality,

So, for a fixed threshold, the error decreases at the rate N-1/2.

[ ]

V

[ ]

Y Y N

N V Y N V

N Y V F V

N

i i N

i i N

i i N

1 1

1 ] 1

[

1 2 1

2

1

∑ ∑

= = =

=

=

=

=

δ ⎪⎭δ

⎪⎬

⎪⎩

⎪⎨

⎧ ⎟

⎜ ⎞

≥⎛

2

] 1

| [ ] [

|

Pr N N V FN

F E F

δ ⎪⎭δ

⎪⎬

⎪⎩

⎪⎨

⎧ ⎟

⎜ ⎞

≥ ⎛

2

] 1

[

| 1

|

Pr V Y

I N FN

(6)

Properties of estimators

• An estimator FN is called unbiased if for all N

That is, the expected value is independent of N.

• Otherwise, the bias of the estimator is defined as

• If the bias goes to zero as N increases, the estimator is called consistent

Q F

E[ N]=

Q F E

FN]= [ N]− β[

0 ] [

lim =

N

N β F

Q F

E N

N =

[ ]

lim

Example of a biased consistent estimator

• Suppose we are doing antialiasing on a 1d pixel, to determine the pixel value, we need to

evaluate , where is the filter function with

• A common way to evaluate this is

• When N=1, we have

= 1

0w(x)f(x)dx

I w(x)

01w( dxx) =1

∑ ∑

=

= = N

i i

N

i i i

N w X

X f X F w

1 1

) (

) ( ) (

I dx x f X

f X E

w X f X E w F

E ⎥ = = ≠

⎢ ⎤

= ⎡ 1

01

1 1 1

1 [ ( )] ( )

) (

) ( ) ] (

[

Example of a biased consistent estimator

• When N=2, we have

• However, when N is very large, the bias approaches to zero

I dx x dx

w x w

x f x w x f x F w

E

+

=

∫ ∫

01 +

1

0 1 2

2 1

2 2 1

1

2 ( ) ( )

) ( ) ( ) ( ) ] (

[

=

= =

N

i i

N

i i i

N

X N w

X f X N w

F

1 1

) 1 (

) ( ) 1 (

I dx x f x w dx

x w

dx x f x w X

N w

X f X N w

F E

N

i i

N N

i i i

N

N N = = =

=

=

=

1 1 0

0 1 0

1

1 ( ) ( )

) (

) ( ) ( )

1 ( lim

) ( ) 1 (

lim ] [ lim

Choosing samples

• Carefully choosing the PDF from which samples are drawn is an important technique to reduce variance. We want the f/p to have a low

variance. Hence, it is necessary to be able to draw samples from the chosen PDF.

• How to sample an arbitrary distribution from a variable of uniform distribution?

– Inversion – Rejection – Transform

=

= N

i i

i

N p X

X f F N

1 ( )

) ( 1

(7)

Inversion method

• Cumulative probability distribution function

• Construction of samples

Solve for X=P-1(U)

• Must know:

1. The integral of p(x)

2. The inverse function P-1(x) U

X 1

0 ( ) Pr( )

P x = X < x

Proof for the inversion method

• Let U be an uniform random variable and its CDF is Pu(x)=x. We will show that Y=P-1(U) has the CDF P(x).

because P is monotonic,

Thus, Y’s CDF is exactly P(x).

{ }

Pr

{

( )

}

Pr

{

( )

}

( ( )) ( )

Pr Yx = P1 Ux = UP x =Pu P x =P x

2 1 2 1) ( )if

(x P x x x

P ≤ ≤

Inversion method

• Compute CDF P(x)

• Compute P-1(x)

• Obtain ξ

• Compute Xi=P-1(ξ)

Example: Power Function

• Assume

( ) ( 1) n p x = n+ x

1 1 1

0 0

1

1 1

n

n x

x dx n n

+

= =

+ +

( ) n 1

P x =x +

1 1

~ ( ) ( ) n

X p x ⇒ =X P U = + U

1 2 1

max( , , , n, n ) Y= U U L U U +

1 1

1

Pr( ) n Pr( ) n

i

Y x U x x

+ +

=

< =

< = Trick

It is used in sampling Blinn’s microfacet model.

(It only works for sampling power distribution)

Similarly, a trick to obtain a Gaussian distribution is to take average.

(8)

• Compute CDF P(x)

• Compute P-1(x)

• Obtain ξ

• Compute Xi=P-1(ξ)

Example: exponential distribution

useful for rendering participating media.

ce

ax

x

p ( ) =

0

= 1

ce

ax

dx c = a

x as ax

e ds

ae x

P ( ) = ∫

0

= 1

)

1 1 ln(

)

1

(

a x x

P

= − −

ξ ξ 1 ln

) 1 1 ln(

a X = − a − = −

Rejection method

• Algorithm

Pick U1and U2

Accept

U

1if

U

2

< f(U

1

)

• Wasteful?

1

0

( )

( )

y f x

I f x dx dx dy

<

=

=

∫∫

y= f x( )

Efficiency = Area / Area of rectangle

• Sometimes, we can’t integrate into CDF or invert CDF

Rejection method

• Rejection method is a dart-throwing method without performing integration and inversion.

1. Find q(x) so that p(x)<Mq(x) 2. Dart throwing

a. Choose a pair (X, ξ),where X is sampled from q(x) b. If (ξ<p(X)/Mq(X)) return X

• Equivalently, we pick a point (X, ξMq(X)). If it lies beneath p(X) then we are fine.

Why it works

• For each iteration, we generate Xi from q. The sample is returned if ξ<p(X)/Mq(X), which happens with probability p(X)/Mq(X).

• So, the probability to return x is

whose integral is 1/M

• Thus, when a sample is returned (with

probability 1/M), Xi is distributed according to p(x).

M x p x Mq

x x p

q ( )

) (

) ) (

( =

(9)

Example: sampling a unit disk

void RejectionSampleDisk(float *x, float *y) { float sx, sy;

do {

sx = 1.f -2.f * RandomFloat();

sy = 1.f -2.f * RandomFloat();

} while (sx*sx + sy*sy > 1.f)

*x = sx; *y = sy;

}

π/4~78.5% good samples, gets worse in higher dimensions, for example, for sphere, π/8~39.3%

Transformation of variables

• Given a random variable X from distribution px(x) to a random variable Y=y(X), where y is one-to- one, i.e. monotonic. We want to derive the distribution of Y, py(y).

• PDF:

) ( } Pr{

)}

( Pr{

)) (

(y x Y y x X x P x

Py = ≤ = ≤ = x

dx x dP dx

x y

dPy( ( )) x( )

=

) (x px dx

dy dy

y dP dx y dy

py y( )

)

( =

) ( )

(

1

x dx p

y dy

py x

⎟⎠

⎜ ⎞

=⎛

x

y Px(x)

Py(y)

Example

X Y

x x

px sin

2 ) (

=

=

2 1 1

1 sin 2 cos ) 2 ( ) (cos )

(

y y x

x x p x y

py x

= −

=

=

Transformation method

• A problem to apply the above method is that we usually have some PDF to sample from, not a given transformation.

• Given a source random variable X with px(x) and a target distribution py(y), try transform X into to another random variable Y so that Y has the distribution py(y).

• We first have to find a transformation y(x) so that Px(x)=Py(y). Thus,

)) ( ( )

(x P 1 P x y = y x

(10)

Transformation method

• Let’s prove that the above transform works.

We first prove that the random variable Z= Px(x) has a uniform distribution. If so, then

should have distribution Px(x) from the inversion method.

Thus, Z is uniform and the transformation works.

• It is an obvious generalization of the inversion method, in which X is uniform and Px(x)=x.

)

1( Z Py

{

Zx

}

=Pr

{

Px(X)≤x

}

=Pr

{

XPx (x)

}

=Px(Px (x))=x

Pr 1 1

Thus, if X has the distribution , then the random variable has the distribution

Example

x x

px( ) = py(y) =ey

) 2 (

x2

x

Px = Py(y) =ey

2 ln ln

2 2 ) ln(

)) ( ( )

(

2

1 = = −

= x x

x P P x

y y x

y y

Py1( )=ln

2 ln ln

2 −

= X

Y

x x px( )=

y

y y e

p ( )=

y

Multiple dimensions

We often need the other way around,

Spherical coordinates

• The spherical coordinate representation of directions is

θ φ θ

φ θ cos

sin sin

cos sin

r z

r y

r x

=

=

=

θ sin

|

|JT =r2

) , , ( sin )

, ,

(r r2 p x y z

p θ φ = θ

(11)

Spherical coordinates

• Now, look at relation between spherical directions and a solid angles

• Hence, the density in terms of

φ θ θ

ω d d

d = sin

φ θ ,

ω ω φ

θ φ

θ d d p d

p ( , ) = ( ) ) ( sin )

,

( θ φ θ p ω

p =

Multidimensional sampling

• Separable case: independently sample X from px and Y from py. p(x,y)=px(x)py(y)

• Often, this is not possible. Compute the marginal density function p(x) first.

• Then, compute the conditional density function

• Use 1D sampling with p(x) and p(y|x).

= p x y dy x

p ( ) ( , )

) (

) , ) (

|

( p x

y x x p

y

p =

Sampling a hemisphere

• Sample a hemisphere uniformly, i.e.

• Sample θ first

• Now sampling ψ

ω π 2 ) 1

( =

p

π φ θ

θ 2

) sin ,

( =

p

Ω

= ( )

1 p ω

π 2

= 1 c

θ π φ

φ θ φ θ

θ

π π sin

2 ) sin

, ( )

(

2

0 2

0

=

=

=

p d

d

p

π θ

φ θ θ

φ

2

1 )

( ) , ) (

|

( = =

p p p

c p(ω)=

Sampling a hemisphere

• Now, we use inversion technique in order to sample the PDF’s

• Inverting these:

θ α

α

θ

) αsin 1 cos (

0

=

=

d

P

π α φ θ π

φ

α

2 2

) 1

| (

0

=

=

d

P

2 1 1

2 cos

πξ φ

ξ θ

=

=

(12)

Sampling a hemisphere

• Convert these to Cartesian coordinate

• Similar derivation for a full sphere

2 1 1

2 cos

πξ φ

ξ θ

=

=

1

2 1 1

2 1 1

cos

1 ) 2 sin(

sin sin

1 ) 2 cos(

cos sin

ξ θ

ξ πξ

φ θ

ξ πξ

φ θ

=

=

=

=

=

=

z y x

Sampling a disk

1

2

2 U r U θ = π

=

1

2

2 U

r U

θ = π

=

RIGHT = Equi-Areal WRONG ≠ Equi-Areal

Sampling a disk

RIGHT = Equi-Areal WRONG ≠ Equi-Areal

1

2

2 U r U θ = π

=

1

2

2 U

r U

θ = π

=

Sampling a disk

• Uniform

• Sample r first.

• Then, sample .

• Invert the CDF.

π ) 1 , (x y =

p p(r,θ)=rp(x,y)=πr r d

r p r

p( ) ( , ) 2

2

0

=

=

π θ θ

θ

π θ θ

2 1 )

( ) , ) (

|

( = =

r p

r r p

p ) 2

(r r

P = π

θ θ ) 2

| ( r = P

ξ1

=

r θ =2πξ2

(13)

Shirley’s mapping

1 2

4 1

r U U U θ π

=

=

Sampling a triangle

2 1

1 1 1

0 0 0

0

(1 ) 1

(1 )

2 2

u u

A=

∫ ∫

dv du=

u du= − − = ( , ) 2

p u v = 0 0

1 u

v u v

≥ + ≤

u v

u+ =v 1 ( ,1u u)

) (

) , ) (

|

( p v

v u v p

u

p

Sampling a triangle

• Here

u

and

v

are not independent!

• Conditional probability

( , ) 2 p u v =

0

0 0 0 0 0

0 0

( | ) ( | ) 1

(1 ) (1 )

o o

v v v

P v u p v u dv dv

u u

= = =

∫ ∫

( | ) 1

(1 ) p v u

= u

0 2

0 0 0

( ) u 2(1 ) (1 )

P u =

u du = −u

1

0

( ) 2 2(1 )

u

p u dv u

=

=

0 1 1

u = − U

0 1 2

v = U U

p u v dv u

p( ) ( , )

Cosine weighted hemisphere θ

ω ) cos

( ∝

p

Ω

= p ( ω ) d ω 1

∫ ∫

=

2π π

θ θ θ φ

0 0

2

cos sin

1 c d d

φ θ θ

ω d d

d = sin

=

2

0

cos sin

2

1 c π

π

θ θ d θ

π1

= c

θ π θ

φ

θ 1 cos sin )

,

( =

p

(14)

Cosine weighted hemisphere θ

π θ φ

θ 1 cos sin )

,

( =

p

θ θ

θ φ

θ π θ

θ

π

1 cos sin 2 cos sin sin 2 )

(

2

0

= =

= ∫ d

p

π θ

φ θ θ

φ 2

1 )

( ) , ) (

|

( = =

p p p

2

1

2 1 2 cos ) 1

( θ = − θ + = ξ P

2

2

)

|

( ξ

π θ φ

φ = =

P

) 2 1 ( 2 cos 1

1

1

ξ

θ =

2 πξ

2

φ =

Cosine weighted hemisphere

• Malley’s method: uniformly generates points on the unit disk and then generates directions by projecting them up to the hemisphere above it.

Vector CosineSampleHemisphere(float u1,float u2){

Vector ret;

ConcentricSampleDisk(u1, u2, &ret.x, &ret.y);

ret.z = sqrtf(max(0.f,1.f - ret.x*ret.x - ret.y*ret.y));

return ret;

}

r φ

Cosine weighted hemisphere

• Why Malley’s method works?

• Unit disk sampling

• Map to hemisphere p(r,φ)=πr

) , (sin )

,

(r φ ⇒ θ φ

φ θ

φ φ

θ

=

= sin r

) , ( r φ

Y = T X = ( θ , φ )

) ( )

( ))

(

( T x J x

1

p x

p

y

=

T x

θ θ

1 cos 0

0 ) cos

( ⎟⎟ ⎠ =

⎜⎜ ⎞

= ⎛ x J

T

Cosine weighted hemisphere

φ φ

θ

=

= sin r

) , ( r φ

Y = T X = ( θ , φ )

) ( )

( ))

(

( T x J x

1

p x

p

y

=

T x

θ θ

1 cos 0

0 ) cos

( ⎟⎟ =

⎜⎜ ⎞

= ⎛ x J

T

π θ φ θ

φ

θ , ) ( , ) cos sin

( = J p r =

p

T

(15)

Sampling Phong lobe θ

ω

n

p( )∝cos

θ

ω

c n

p( )= cos

∫ ∫

= =

=

π φ

π θ

φ θ θ

2

θ

0 2 /

0

1 sin

cos d d

c n

=

=

0

1 cos

1 cos cos

2

θ

θ θ

π

c n d 1

1

2 =

+ n

π

c

π

2

+1

= n c

θ π θ

φ

θ

cos sin

2 ) 1 ,

( n n

p +

=

Sampling Phong lobe

' cos 1

1 )cos 1 ( cos cos

) 1 (

sin cos ) 1 ( ) ' (

sin cos ) 1 ( sin 2 cos

) 1 (

1

' cos

1 cos ' 1

0 '

0 2

0

θ

θ θ θ

θ θ θ θ

θ θ φ

θ π θ

θ

θ

θ θ

θ θ θ

π φ

+

= +

=

=

=

=

+ +

= +

=

+

=

+ + =

=

n

n n

n

n n

n n d

n

d n

P

n n d

p

( )

1 1

cos1 +

= n ξ

θ

θ π θ

φ

θ cos sin

2 ) 1 ,

( n n

p = +

Sampling Phong lobe

π φ φ θ π

φ

π θ θ

θ θ θ

φ θ θ

φ

φ φ

π

2 ' 2

) 1

|' (

2 1 sin cos ) 1 (

sin cos )

( ) , ) (

| (

'

0

21

=

+

=

=

+ =

=

=

d P

n p

p p n

n n

2πξ2

φ=

θ π θ

φ

θ cos sin

2 ) 1 ,

( n n

p = +

Sampling Phong lobe

) 2 , (cos ) , ( 1,

n= θ φ = 1 ξ1 πξ2

= cos1(1 2 1),2 2 2

) 1 ,

(θ φ ξ πξ

cosine-weighted hemisphere

2 2 1 2cos ) 1

(θ = θ+ θ P

θ

θ) 1 cos 1 1 cos2 ( = n+ = P

θ θ

θ

θ 2 sin2 1 cos2

2 ) 1 sin 2 1 2( 1 2 2 1 2cos

1 + = + = =

When n=1, it is actually equivalent to

參考文獻

相關文件

• If we repeatedly run both Monte Carlo algorithms, eventually one definite answer will come (unlike RP). – A positive answer from the one without

Keywords: multi-view representation of pedestrian, sequential Monte Carlo method, static parameters, dynamic parameters,

Spectrum Sample_L(Point &amp;p, float pEpsilon, LightSample &amp;ls float time Vector *wi LightSample &amp;ls, float time, Vector *wi, float *pdf, VisibilityTester *vis) = 0;.

Merton and Scholes received the 1997 Nobel Memorial Prize in Economic Sciences for their work. Zheng-Liang

• A language in ZPP has two Monte Carlo algorithms, one with no false positives and the other with no

• The randomized bipartite perfect matching algorithm is called a Monte Carlo algorithm in the sense that.. – If the algorithm finds that a matching exists, it is always correct

• Consider an algorithm that runs C for time kT (n) and rejects the input if C does not stop within the time bound.. • By Markov’s inequality, this new algorithm runs in time kT (n)

• Suppose, instead, we run the algorithm for the same running time mkT (n) once and rejects the input if it does not stop within the time bound.. • By Markov’s inequality, this