Rndom Numers nd Monte Crlo Methods Rndom Numer Methods The integrtion methods discussed so fr ll re sed upon mking polynomil pproximtions to the integrnd. Another clss of numericl methods relies upon using rndom numers. These methods hve come to e known under the generl ruric Monte Crlo methods, fter the fmous gmling csino. Before discussing Monte Crlo integrtion, we must digress to lern out rndom numers. Rndom Numer Genertors Any stndrd rndom numer genertor produces set of uniformly distriuted (equl proility) numers on some intervl. For the intervl [,] we define P(r)dr = proility of generting rndom numer in the intervl dr ner r For this proility ide to mke sense we must hve P(r)dr = 1= totl proility of generting rndom numer In the cse of uniform distriution, P(r) = constnt nd hence P(r) = 1 All such numericl rndom numer genertors give sequence of numers with lrge repet cycle on some intervl. A good genertor will hve very lrge repet cycle(10 8 10 9 nonrepeting numers). In MATLAB, the rnd(n,m) function genertes uniformly distriuted pseudo-rndom numers. We illustrte the uniform distriution of 100,000 rndom numers elow: >> hist(rnd(1,100000),10) Pge 1
which oviously shows the uniform distriution on the intervl [0,1]. MATLAB lso generte normlly (Gussin) distriuted rndom numers using the rndn function. >> hist(rndn(1,100000),20)) which is oviously norml or gussin distriution. Pge 2
These two methods re exmples of "simple smpling" techniques. In mny prolems we need to use proility distriutions tht reflect the pproprite physics of the system under study. In this cse we use different technique clled "importnce smpling". The lgorithm for generting rndom numers with specified distriution(not uniform) goes this wy. Suppose tht we hve set of uniformly distriuted numers (s ove) in the intervl [0,1]. Then the rule r j = x j 0 dx'w(x') will generte set of numers x i rule w(x). For exmple, suppose w(x) = e x otin r j = 1 e x j x j = log(1 r j ) { } distriuted ccording to the 0 x 4. Then we which is the desired result. This is illustrted in the plot elow: >> hist((-log(1-rnd(1,100000))),20) Integrtion using Importnce Smpling (Monte Crlo Method) Now consider function to e integrted, s shown elow: Pge 3
y f(x) x The integrl is just the re under the curve. The width of the intervl (-) times the verge vlue of the function is lso the vlue of the integrl, tht is, f (x)dx = ( ) f verge = ( ) f So if we hd some indepent wy of clculting the verge vlue of the integrnd, then we could evlute the integrl. Tht is where we cn use rndom numers. Imgine tht we hve list of rndom numers, x j, uniformly distriuted etween nd. To clculte the function verge, we simply evlute f(x) t ech of the rndomly selected points, nd divide y the numer of points: f N = 1 N N i=1 f (x i ) As the numer of points used in clculting the verge increses, f N pproches the true verge vlue, f. Therefore, s numericl pproximtion we cn write f (x)dx = N f (x i ) N i=1 Alterntively, we cn look t this so-clled Monte Crlo integrtion method in the following wy: To integrte the function f(x) over the intervl [,] we Pge 4
[1] find some vlue M such tht f(x) < M over the intervl [,] [2] select rndom numer x from uniform distriution over the intervl [,] [3] select rndom numer y from uniform distriution over the intervl [0,M] [4] determine if y > f(x) or y f(x) [5] repet this process N times, keeping trck of the numer of times y f(x) or under the curve (= successes); cll the totl numer of successes S. The estimted proility of success is then S N = Are under curve Totl re inside rectngle = f (x)dx M ( ) where the rectngle used is shown in the figure elow: y y = M f(x) x After numer of trils, the vlue of the integrl cn e clculted from the ove formul f (x)dx = M ( ) S N Think out throwing drts nd counting the numer of drts tht lnd in the re representing the integrl. Your progrm ove only works if the integrnd is greter thn or equl to zero everywhere over the rnge of integrtion. Suppose, in fct, tht the function f(x) ws not lwys greter thn zero in the intervl [,] s shown elow. Pge 5
y y = M f(x) x y = -R We cn modify the Monte Crlo integrtion method to hndle such cses, i.e., fix the prolem with f(x) possily eing less thn zero s follows. To integrte the function f(x) over the intervl [,] we [1] find some vlue M such tht f(x) < M over the intervl [,] [2] find some R such tht f(x) > - R over the intervl [,] [2] select rndom numer x from uniform distriution over the intervl [,] [3] select rndom numer y from uniform distriution over the intervl [-R,M] [4] determine if y > f(x) or y f(x) [5] repet this process N times, keeping trck of the numer of times y f(x) or under the curve (= successes); cll the totl numer of successes S. The estimted proility of success is then S N = Are under curve Totl re inside rectngle = f (x)dx = (M + R)( ) S N f (x)dx (M + R)( ) This must now e corrected for the fct tht the line y = -R hs een used s the seline for the integrl insted of the line y = 0. This is ccomplished y sutrcting the rectngulr re R(-). The finl integrl is then f (x)dx = (M + R)( ) S R( ) N Pge 6
The Metropolis Algorithm Suppose tht we wnt to generte set of points in some, possily multidimensionl, spce of vriles X distriuted with proility density w(x) (not necessrily uniform). The Metropolis lgorithm genertes set of points X 0, X 1, X 2,... s those visited successively y rndom wlker moving through the X spce. As the wlk ecomes longer nd longer, the points it connects will pproximte closely the desired distriution. The rules for the rndom wlk re s follows: [1] Suppose tht the wlker is t point X n in the sequence. To generte X n+1 it mkes tril step to new point X t. This new point cn e chosen in ny convenient mnner, for exmple, uniformly t rndom within multidimensionl cue of smll side δ out X n. [2] This tril step is then "ccepted" or "rejected" ccording to the rtio r = w(x t ) w(x n ) tht is, if r is lrger thn one, then the step is lwys ccepted (i.e., we put X n+1 = X t, while if r is less thn one, the step is ccepted with proility r. This ltter step is conveniently ccomplished y compring r with rndom numer η uniformly distriuted in the intervl nd ccepting the step if η < r. If the tril step is not ccepted, then it is rejected, nd we put X n+1 = X n. This genertes X n+1, nd we my proceed to generte X n+2 y the sme process. Any ritrry point X n cn e used to strt this rndom wlk. A MATLAB code to use the Metropolis lgorithm for the cse w(x) = e 0.2 X 2 looks like: x=[]; X0=0; delt=4; nccept=0; Pge 7
n=0; while (nccept < 5000) n=n+1; XT = X0 + delt*(2*rnd(1,1)-1); rtio=exp(0.5*(x0^2-xt^2)); if (rtio > rnd) x=[x,xt]; X0=XT; nccept=nccept+1; hist(x,40) which clerly reflects the proposed Gussin distriution. A good rule is to choose δ so tht out 1/3 of the trils is ccepted nd to choose X 0 such tht w(x) is ner mximum. Becuse successive points in this distriution re not sttisticlly indepent of ech other, some cre must e tken when choosing set of points to use from lrger set tht hs een generted erlier. Generlly, the ccepted method is to choose points seprted y some intervl, sy every k th point, where k is such tht ny correltions re wshed out. Pge 8
A simple Metropolis method function is : function z=metropolis(input) % must hve nother funct.m file defined rnd('seed',sum(100*clock)); x=input(1); delt=input(2); xtril=x+delt*(2*rnd(1)-1); w=funct(xtril)/funct(x); if (rnd < w) z=xtril; else z= x; The code elow tests this function for w(x) = e 0.2 X 2 : zz=zeros(100000) z=metropolis([0.0,4.0]); for j=1:100000 z=metropolis([z,4.0]); zz(j)=z hist(zz,40); Agin this clerly reflects the correct distriution. Simple Simultion Exmple Using Rndom Numers Pge 9
Another exmple of using rndom numers to simulte prticles in ox with two distinct sides. Suppose prticle cn e t only two positions XR nd XL nd tht w(x) = n R N δ X, XL + n L N δ X, XR tht is, the proility of eing on given side of the ox is given y the rtio of the numer of prticles on tht side of the ox to the totl numer of prticles. Consider the progrm elow. We hve N = 1000 prticles in ox. We strt with rndom frction of the N prticles on the LHS of the ox. If ll prticles on the LHS, then we s one to the RHS. In ll other cses, we then use the Metropolis lgorithm to decide whether we decrese the numer of prticles on the LHS y 1 (increse the numer on the RHS y 1) or vice vers. The rtio r in this cse is the proility of eing on the LHS. rtio = r = w(xl) = n L N Consider the progrm elow: % Metropoplis simultion rnd('seed',sum(100*clock)); N=1000; tmx=10000; nl=round(rnd*n); t=0; p=plot(t,nl,'.','ersemode','none'); xis([0,tmx,0,n]); while (t <= tmx) t=t+1; rtio=nl/n; if (rtio >= 1) nl=nl-1; else if (rnd <= rtio) nl=nl-1; else nl=nl+1; Pge 10
set(p,'xdt',t,'ydt',nl) drwnow The result is: When we run this simultion, we lwys up with pproximtely 1/2 of the prticles on ech side(the equilirium configurtion) nd the simultion ccurtely represents the fluctutions present t equilirium. This exmple illustrtes how chnce or rndom motion cn generte deterministic ehvior. Pge 11