Basic Statistics and Monte-Carlo Method -2
Total Page:16
File Type:pdf, Size:1020Kb
Applied Statistical Mechanics Lecture Note - 10 Basic Statistics and Monte-Carlo Method -2 고려대학교 화공생명공학과 강정원 Table of Contents 1. General Monte Carlo Method 2. Variance Reduction Techniques 3. Metropolis Monte Carlo Simulation 1.1 Introduction Monte Carlo Method Any method that uses random numbers Random sampling the population Application • Science and engineering • Management and finance For given subject, various techniques and error analysis will be presented Subject : evaluation of definite integral b I = ρ(x)dx a 1.1 Introduction Monte Carlo method can be used to compute integral of any dimension d (d-fold integrals) 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 Probability 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 simulations are The rule of 3σ’s Identifying Random Variable N = 1 X X n N n=1 From central mean 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 random variable 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 b − a 0 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 Sampling Distribution Monte Carlo Method : Take values from random sample From central limit theorem, 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 ”importance sampling 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 (γ ) I = 0.67 f error 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.