Chapter 2 The Mote Carlo Method The Mote Carlo Method stads for a broad class of computatioal algorithms that rely o radom sampligs. It is ofte used i physical ad mathematical problems ad is most useful whe it is difficult or impossible to use other algorithms. There are maily three distict classes of applicatios: umerical itegratio, optimizatio, ad geeratig draws from a probability distributio. ( https://cse.sc.edu/~terejau/files/tutorialmc.pdf ) Sectio 2. Computig Itegrals Simple Mote Carlo for fidig mi ad max of y = f (x) radomly geerate poits x, x 2,... Mi = Max = f (x ), if f (x 2 ) < f (x ), the replace Mi = f (x 2 ), Max = f (x ), cotiues Cosider the area below the curve y = f (x) iside the rectagle of area R. We radomly geerate a poit (x, y ) iside the rectagle. If y f (x ), the this poit is below the curve. If i N trials, M poits are below the curve, the: area of uder the curve M N R
2
I theory, as N, the limit is exactly the area. Mote Carlo Method for itegrals: Recall that I = b a f (x) dx is the siged area betwee the curve y = f (x) ad x axis. If f (x) 0 it is the area. We may use the above method to approximate the itegral as follows: Set c = mi {f (x) : a x b}, d = max {f (x) : a x b} update: c = mi {c, 0}, d = max {d, 0} N is total trials, iitialize N = M = I = 0 Each trial, radomly geerate a x b, c y d If 0 y f (x ), the M = M + If 0 y f (x ), the M = M The above two step ca be combied as: If (f (x ) y ) y 0, the M = M + sig (y ) I = (M/N) (b a) (d c) For double itegrals, we use volumes istead. Read Example i page 22. Exercise : Use Mote Carlo to fid the followig itegral withi a error of 0 5.What is N? 2 0 e x2 cos xdx 3
4 Experimet: try usig ormal distributio isteat of uiform distributio, compare results. Mote Carlo is ot very efficiet. It is used oly for very complicated situatios. Sectio 2.2 Mea Time betwee Failures Cosider a system has several compoets. Each has a failure time that is ormally distributed with mea µ i ad variace σ 2 i. The the system failure time is T = mi {T, T 2,..., T } Aalytic solutio for E [T ] : Let F i be the CDF of T i. Note that T (ω) T i (ω),ad T (ω) > t T i (ω) > t for i =, 2,..., {ω : T (ω) > t} = {ω : T i (ω) > t}
5 The CDF F of T is (assumig idepedece) ad F (t) = P (T t) = P (T > t) = P ( {ω : T i (ω) > t}) = P (ω : T i (ω) > t) = ( F i (t)) ( ) df (t) = d ( F i (t)) = ( F i (t)) df i k= i= k E [T ] = This itegral is so difficult. Mote Carlo for mea ad variace: t k= i= k ( F i (t)) df i (t)
6 µ = E [X] = lim σ 2 = E N X (ω ) + X (ω 2 ) +... + X (ω N ) N ( X (ω ) = lim N N + X (ω 2) N +... + X (ω ) N) N [ (X µ) 2] = E [ X 2 2µX µ 2] = E [ X 2] 2µE [X] + µ 2 = E [ X 2] µ 2 Exercise #2: MC for E [T ] (See Routie i page 23) Sectio 2.3 Servicig Requests Mote Carlo for radom variables, meas/variaces Lemma Suppose that CDF F X (x) of a radom variable X is cotiuous ad strictly icreasig. The F X (X) = Y U (0, ), i.e., Y is uiform distributio i [0, ]. Proof: Usig graph of y = F X (x). For ay 0 < y 0 <, there is a uique x 0 such that y 0 = F X (x 0 ).Sice F X is strictly icreasig, {x : F X (x) y 0 } = (, x 0 ] By defiitio, y 0 = P (X x 0 ).From the above, we see that if ω {ω : F X (X (ω)) y 0 }
7 the X (ω) {x : F X (x) y 0 } = (, x 0 ] X (ω) x 0 So ω {ω : X (ω) x 0 }. Cosequetly {ω : F X (X (ω)) y 0 } {ω : X (ω) x 0 }. Sice F X is icreasig, we actually have {ω : F X (X (ω)) y 0 } = {ω : X (ω) x 0 }. So F Y (y 0 ) = P (F X (X) y 0 ) = P (X x 0 ) = y 0 Now, for ay give F (x), we first radomly geerate a sample y U (0, ), the calculate x = F (y) as a sample for F. Estimate E [g (X)] for give CDF F X (x) or PDF ρ X : For ay, geerate a sequece of radom variables x i as above with CDF F X
compute I particular, g (x i ) E [g (X)] = x 2 i ( g (x) df X = x i E [X] g (x) ρ X (x) dx ) 2 x i E [ X 2] E [X] 2 = σ 2 Cetral Limit Theory: The average of a large umber idepedet radom variable coverges to a ormal distributio. More precisely, let X i be a radom variable with mea µ ad variace σ 2, the (X i µ) σ N (0, ), as 8
9 Error is O (/ ) g (X i ) E [g (X)] = = (g (X i) E [g (X)]) (g (X i) E [g (X)]) σ σ N (0, ) σ Cosider checkout time i a library or grocery story. services has the expoetial distributio Assume the time T betwee requests for ρ (t) = e t/a a, for t > 0 Assume there are m checkout lies. If the first oe is busy, the the requested is haded out to the secod, ad so o. If all lies are busy, the request is rejected. Assume that each lie processes each request i time S that is ormally distributed with mea µ ad variace σ 2.
0 The CDF for T is The iverse fuctio F is y = F (t) = t 0 e x/a a dx = e t/a t = a l ( y) So there exists a uiform distributio i [0, ], Y U [0, ] See routie i page 25 T = a l ( Y ) Homework: Exercise : Usig MC to fid the itegral (see Example i page 22), withi a error of 0 5.What is the smallest N? Exercise 2: Ru MC Routie i page 23. The write your ow routie for the followig cases: (a) There are five devices T,..., T 5, which have ormal distributios with mea,2,3,4,5, ad stadard deviatio, 2, 3, 4, 5, respectively. (b) Repeat (a) with uiform distributio istead (with the same eas ad stadard deviatios). (Hit: U [ µ σ 3, µ + σ 3 ] has mea µ ad variace σ 2 )
Exercise 3: Write a MC routie to compute E [X] ad σ 2 [X] with give CDF of X. The test your MC routie usig expoetial distributio with λ = 2 ad λ = 5. Exercise 4: Write a Matlab routie to geerate N (0, ) (e.g., fid mea ad variace) usig usig "rad" commad oly (you caot use "rad" commad) ad Lemmas of page 25. Test your results i 2(a). Optioal Develop a Matlab routie for MC itegratio of geeral itegral f (x, y) dxdy for ay fuctio f (x, y) i ad domai D. Note that D is ot ecessarily a rectaglar domai. For istace, D could be the uit disk. The, (accurate up to 0 3 ) (i) use your program to compute 2 0 dy 2 e (x2 y) si x cos ydx (ii) Let B be the uit disk, i.e., B : x 2 + y 2, use MC to fid e (x2y) si x cos ydxdy D B