<<

Applied Lecture Note - 10

Basic and Monte-Carlo Method -2

고려대학교 화공생명공학과 강정원 Table of Contents

1. General 2. Reduction Techniques 3. Metropolis Monte Carlo 1.1 Introduction

 Monte Carlo Method  Any method that uses random numbers  Random the population  Application • Science and engineering • Management and finance  For given subject, various techniques and error analysis will be presented  Subject : evaluation of definite

b I = ρ(x)dx a 1.1 Introduction

 Monte Carlo method can be used to compute integral of any d (d-fold )  Error comparison of d-fold integrals  Simpson’s rule,… E ∝ N −1/ d −  Monte Carlo method E ∝ N 1/ 2 purely statistical, not rely on the dimension !

 Monte Carlo method WINS, when d >> 3 1.2 Hit-or-Miss Method

 Evaluation of a definite integral

b I = ρ(x)dx a h X X X ≥ ρ X h (x) for any x X  that a random point reside inside X O the area O O I N' O O r = ≈ O O (b − a)h N a b

 N : Total number of points  N’ : points that reside inside the region N' I ≈ (b − a)h N 1.2 Hit-or-Miss Method

Start

Set N : large integer

N’ = 0 h X X X X X Choose a point x in [a,b] = − + X Loop x (b a)u1 a O N times O O y = hu O O Choose a point y in [0,h] 2 O O

a b if [x,y] reside inside then N’ = N’+1

I = (b-a) h (N’/N)

End 1.2 Hit-or-Miss Method

 Error Analysis of the Hit-or-Miss Method  It is important to know how accurate the result of are  The rule of 3σ’s  Identifying Random N = 1 X  X n N n=1  From central theorem , X is normal variable in the limit of large N

each X n X mean value : μ mean value : μ variance : σ 2 variance : σ 2 / N 1.2 Hit-or-Miss Method

 Sample Mean : estimation of actual mean value (μ) N μ = 1 '  xn N n=1 1 N N' μ = = = x01 '  xn r N = N n 1 p(x) r 1-r N =  xn N' n=1

 Accuracy of simulation  the most probable error 0.6745σ σ = V (X ) N 1.2 Hit-or-Miss Method

 Estimation of error

0.6745σ σ = V (X ) N

 We do not know exact value of s , m  We canσ also estimate the variance and the mean value from samples … 1 N '2 = (x − μ')2 −  n N 1 n=1 1.2 Hit-or-Miss Method

 For present problem (evaluation of integral) exact answer (I) is known  estimation of error is,

V (X ) = E(X 2 ) − []E(X ) 2

E(X ) =  xp(x) =1× r + 0×(1− r) = r = μ E(X 2 ) =  x2 p(x) =1× r + 0×(1− r) = r

V (X ) = r − r 2 = (1− r)r = 2 σ = (1− r)r σ I = r(a − b)h = μ(a − b)h

0.6745σ r(1− r) I((b − a)h − I) I HM = = 0.6745×(b − a)h = 0.6745 error N N N 1.3 Sample Mean Method

ρ ρ  ρ(x) is a continuous function in x and has a mean value ; b (x)dx 1 b < >= a = ρ(x)dx b ρ − a dx b a a b ∴I = (x)dx = (b − a) < ρ > a ρ N < >= 1 ρ  (xn ) N n=1 = − + = xn (b a)un a un uniform in [0,1] 1.3 Sample Mean Method

 Error Analysis of Sample Mean Method  Identifying random variable μ N =ρ 1 Y Yn N n=1 1 N 1 N =< >≈ y = ρ(x ) Y  n  n N n=1 N n=1 ρ ρ  Variance N ρ < 2 >= 1 ρ 2 (xn ) 2 2  V ( ) =< > −[]< ρ > N n=1

V (x) I SM = 0.6745σ = (b − a)×0.6745 error N 1.3 Sample Mean Method

 If we know the exact answer,

b L ρ(x)2 dx − I 2 I SM = 0.6745 a error N 1.3 Sample Mean Method

Start

Set N : large integer

s1 = 0, s2 = 0

Loop xn = (b-a) un + a N times ρ yn = (xn)

2 s1 = s1 + yn , s2 = s2 + yn

Estimate mean μ’=s /N SM V ' 1 I = (b − a)×0.6745 Estimate variance V’ = s /N – μ’2 error 2 N

End QUIZ

π  Compare the error for the integral / 2 π I = sin xdx = − cos x / 2 =1 0 0

using HM and SM method

0.6745σ r(1− r) I((b − a)h − I) I HM = = 0.6745×(b − a)h = 0.6745 error N N N

b L ρ(x)2 dx − I 2 I SM = 0.6745 a error N Example : Comparison of HM and SM

π  Evaluate the integral π / 2 π I = sin xdx = − cos x / 2 =1 0 0 I((b − a)h − I) 1(( / 2 − 0)×1−1) π / 2 −1 I HM = 0.6745 = 0.6745 = 0.6745 error N N N ρ< ρ >= I /(b − a) = 2 /π 1 π / 2 < 2 >= sin 2 xdx =1/ 2 − 0 ρ b a π 1  2  V ( ) = −   2 π 2  π V ' ( 2 / 4)(1/ 2 − 4 / 2 ) π 2 /8 −1 I SM = 0.6745(b − a) = 0.6745 = 0.6745 error N N N Example : Comparison of HM and SM

 Comparison of error

π 2 SM −1 I error = 8 ≈ 0.64 SM method has 36 % less error than HM I HM π error −1 2

 No of evaluation having the same error

π 2 −1 SM = 8 HM ≈ HM SM method is more than twice faster than HM N π N 0.41N −1 2 2.1 Variance Reduction Technique - Introduction

 Monte Carlo Method and μ  Monteμ Carlo Method : Take values from random sample  From , σ 2 = = σ 2 / N μ  3s rule σ μ P( − 3 ≤ X ≤ − 3σ ) ≈ 0.9973

 Most probable error 0.6745σ Error ≈ ± N  Important characteristics Error ∝1/ N Error ∝ σ 2.1 Variance Reduction Technique - Introduction

 Reducing error  *100 samples reduces the error order of 10  Reducing variance  Variance Reduction Technique

 The value of variance is closely related to how samples are taken  Unbiased sampling  Biased sampling • More points are taken in important parts of the population 2.2 Motivation of Variance Reduction Technique

 If we are using sample-mean Monte Carlo Method  Variance depends very much on the behavior of ρ(x) ρ(x) varies little  variance is small ρ(x) = const  variance=0  Evaluation of a integralμ b − a N I'= (b − a) = ρ(x ) Y  n N n=1  Near minimum points  contribute less to the summation  Near maximum points  contribute more to the summation

 More points are sampled near the peak ” strategy” 2.3 Variance Reduction using Rejection Technique

 Variance Reduction for Hit-or-Miss method  In the domain [a,b] choose a comparison function

w(x) ≥ ρ(x) w(x) x ρ(x) X = X W (x) w(x)dx X −∞ X b X = O A w(x)dx O a O O O − Au = w(x) x = W 1(Au) O O a b  Points are generated on the area under w(x) function  Random variable that follows distribution w(x) 2.3 Variance Reduction using Rejection Technique

 Points lying above ρ(x) is rejected N' I ≈ A N w(x) ρ(x) = X y w(x )u X n n n X X ≤ρ X 1 if yn (xn ) O q =  O n  > ρ O 0 if yn (xn ) O O O O

a b q10 = P(q) r 1-r r I / A 2.3 Variance Reduction using Rejection Technique

 Error Analysis E(Q) = r, V (Q) = r(1− r) I = Ar = AE(Q)

− RJ ≈ I(A I) Hit or Miss method Ierror 0.67 N A = (b − a)h

Error reduction A → I then Error → 0 2.3 Variance Reduction using Rejection Technique

Start

Set N : large integer w(x) ρ(x) X X X N’ = 0 X X O 1 O Loop Generate u1, x= W- (Au1) O N times O O O O

Generate u2, y=u2 w(x) a b If y<= f(x) accept value N’ = N’+1 Else : reject value − RJ I'(A I') I = (b-a) h (N’/N) I ≈ 0.67 error N

End 2.4 Importance Sampling Method

 Basic idea  Put more points near maximum  Put less points near minimum

 F(x) : transformation function (or weight function_

x F(x) = f (x)dx −∞ y = F(x) x = F −1(y) 2.4 Importance Sampling Method

dy / dxρ= f (x) → dx = dy / f (x)

b (x) b  ρ(x) I = dy = f (x)dx a a   f (x)  f (x) ρ(x) γ (x) = f (x) η < > = bη f (x) f (x)dx a

if we choose f(x) = cρ(x) ,then variance will be small The magnitude ofγ error depends on the choice of f(x)

= b =< γ > I (x) f (x)dx f a 2.4 Importance Sampling Method

 Estimate of error γ N =< > ≈ 1 γ I f  (xn ) N n=1

V (γ ) = f Ierror 0.67 γ N γ =< 2 > − < γ > 2 V f ( ) f ( f )

< γ 2 > −I 2 I IS = 0.67 f error N 2.4 Importance Sampling Method

Start

Set N : large integer

s1 = 0 , s2 = 0

Loop Generate xn according to f(x) N times γ ρ n = (xn) / f ( xn)

γ Add n to s1 γ Add n to s2 V ' 2 IS I ‘ =s1/N , V’=s2/N-I’ I = 0.67 error N

End 3. Metropolis Monte Carlo Method and Importance Sampling

of a property in Canonical Ensemble

A(r N )exp(−U (r N ) / kT)dr N < > =  A NVT  exp(−U (r N ) / kT)dr N exp(−U (r N ) / kT) = A(r N )dr N  Z(r N ) = Ν (r N )A(r N )dr N

Probability 3. Metropolis Monte Carlo Method and Importance Sampling

N  Create ni random points in a volume ri such that = N ni N (ri )L < > = 1 N A NVT ni A(r ) L trials

 Problem : How we can generate ni random points N according to N (ri )?

We cannot use inversion method Use with Metropolis 3. Metropolis Monte Carlo Method and Importance Sampling

 Markov chain :Sequence of trials satisfies few some conditions  that has no memory  Selection of the next state only depends on current state, and not on prior state π  Process is fully defined by a set of transition ij

π ij = probability of selecting state j next, given that presently in state i. Π π Transition-probability matrix collects all ij Markov Chain

 Notation  Outcome ρ  Transition matrix π

 Example  Reliability of a computer • if it is running 60 % of running correctly on the next day • if it is down it has 80 % of down on the next day

0.6 0.4 =   π   0.3 0.7 Markov Chain

ρ(1) = (0.5 0.5) ρ(2) = ρ(1)π = (0.45 0.55)

ρ(3) = ρ(2)π = ρ(1)π2 = (0.435 0.565)

τ (1) τ ρ = limρ π = (0.4286 0.5714) limiting behavior always converges to a certain value →∞ independent of initial condition ρ π ρπ = ρ = ρ  m mn n m

π = Stochastic matrix : sum of the probability should be 1  mn 1 n

Features - Every state can be eventually reached from another state - The resulting behavior follows a certain probability