<<

Random Processes Random or Stochastic processes You cannot predict from the observation of one event, how the next will come out

Examples: Coin: the only prediction about outcome – 50% the coin will land on its tail : In large number of throws – probability 1/6 Monte Carlo Simulation 1 2

Question: What is the most probable number for the sum of two dice? Applications for MC simulation

1 2 3 4 5 6 Stochastic processes 1 2 3 4 5 6 7 Complex systems (science) 2 3 4 5 6 7 8 Numerical integration Risk management 3 4 5 6 7 8 9 36 possibilities Financial planning 4 5 6 7 8 9 10 6 times – for 7 … 5 6 7 8 9 10 11

6 7 8 9 10 11 123 4

5 6

1 7 8

How do we do that?

You let the computer to throw “the coin” and record the outcome You need a program that generates randomly a Part 1 variable … with relevant probability distribution Random number generators

9

Sources of Random Numbers Tables

Tables Most significant Hardware (external sources of random numbers – A Million Random Digits with 100,000 Normal Deviates generates random numbers from a physics process. by RAND

Software (source of pseudorandom numbers) 00000 10097 32533 76520 13586 34673 54876 80959 09117 39292 74945 00001 37542 04805 64894 74296 24805 24037 20636 10402 00822 91665 00002 08422 68953 19645 09303 23209 02560 15953 34764 35080 33606 00003 99019 02529 09376 70715 38311 31165 88676 74397 04436 27659 00004 12807 99970 80157 36147 64032 36653 98951 16877 12171 76833 00005 66065 74717 34072 76850 36697 36170 65813 39885 11199 29170 00006 31060 10805 45571 82406 35303 42614 86799 07439 23403 09732 00007 85269 77602 02051 65692 68665 74818 73053 85247 18623 88579 00008 63573 32135 05325 47048 90553 57548 28468 28709 83491 25624 00009 73796 45753 03529 64778 35808 34282 60935 20344 35273 88435 00010 98520 17767 14905 68607 22109 40558 60970 93433 50500 73998 00011 11805 05431 39808 27732 50725 68248 29405 24201 52775 67851 00012 83452 99634 06288 98083 13746 70078 18475 40610 68711 77817 00013 88685 40200 86507 58401 36766 67951 90364 76493 29609 11062 00014 99594 67348 87517 64969 91826 08928 93785 61368 23478 34113 11 ..... 12

2 http://www.toshiba.co.jp/about/press/2008_02/pr0702.htm 13 14

Software - Random Number Generators Good Random Number Generators

There are no true random number generators but Two important issues: pseudo RNG! 1. 2. knowledge of the distribution. Reason: computers have only a limited number of bits to represent a number Other (still important) issues It means: the sequence of random numbers will repeat 1. independent of the previous number itself (period of the generator) 2. long period 3. produce the same sequence if started with same initial conditions 4. fast

15 16

Two techniques for RNG Linear Congruent Method for RNG

The standard methods of generating pseudorandom Generates a random sequence of numbers

numbers use modular reduction in congruential {x1, x2, …xk} of length M over the interval [0,M-1] relationships. ⎛ axi−1 + c ⎞ Two basic techniques for generating uniform random xi = mod(axi−1 + c, M ) = remainder⎜ ⎟ 0 ≤ x < M ⎝ M ⎠ i−1 numbers: starting value x is called “” 1. congruential methods 0 coefficients a and c should be chosen very 2. feedback shift register methods. carefully For each basic technique there are many variations.

note: mod(b, M ) = b − int(b / M )* M

17 the method was suggested by D. H. Lehmer in 1948 18

3 xi = mod(axi−1 + c, M ) Example: mod(b, M ) = b − int(b / M ) * M Random Numbers on interval [A,B]

a=4, c=1, M=9, x1=3 Scale results from xi on [0,M-1] to yi on [0,1] x2 = 4 x = 8 3 yi = xi /(M −1) x4 = 6 x5-10 = 7, 2, 0, 1, 5, 3 Scale results from xi on [0,1] to yi on [A,B]

interval: 0-8, i.e. [0,M-1] yi = A + (B − A)xi period: 9 i.e. M numbers (then repeat)

19 20

Magic numbers for Linear Congruent Method Other Linear Congruential Generators

9 Multiple Recursive Generators M (length of the sequence) is quite large many versions including “Lagged Fibonacci” However there is no overflow 9 Matrix Congruential Generators (for 32 bit machines M=231 ≈ 2*109) 9 Add-with-Carry, Subtract-with-Borrow, and Multiply - Good “magic” number for linear congruent method: with-Carry Generators

xi = mod(axi−1 + c, M )

a = 16,807, c = 0, M = 2,147,483,647 for c = 0 “multiplicative congruential generator”:

21 22

Other Generators How can we be check the RNG?

9 Nonlinear Congruential Generators Plots: 9 Feedback Shift Register Generators ƒ 2D figure, where xi and yi are from two random 9 Generators Based on Cellular Automata sequences (parking lot test) 9 Generators Based on Chaotic Systems ƒ 3D figure (xi, yi, zi) 9 … ƒ 2D figure for correlation (xi, xi+k)

James E. Gentle – “Random Number Generation and Monte Carlo Methods Second edition - 2004

23 24

4 How can we check the RNG? Test Suites (most known) for RNG Example of other assessments the NIST Test Suite (NIST, 2000) includes sixteen tests Uniformity. A random number sequence should contain http://csrc.nist.gov/groups/ST/toolkit/rng/index.html numbers distributed in the unit interval with equal probability. Use bins. 1 N 1 “DIEHARD Battery of Tests of Randomness (eighteen tests) k-th momentum xk = xk ≈ ∑ i http://www.stat.fsu.edu/pub/diehard/ N i=1 k +1 N near-neighbor correlation 1 1 ∑ xi xi+k ≈ N i=1 4 TestU01: includes the tests from DIEHARD and NIST and several other tests that uncover problems in some generators that pass DIEHARD and NIST

25 http://www.iro.umontreal.ca/~simardr/testu01/tu01.html 26

Software for RNG “Industrial” methods in C/C++ and Fortran

C/C++ and Fortran (90,95) provide built-in uniform random rand 1. call SEED number generators, random Changes the starting point of the but … except for small studies, these built-in generators should be avoided. drand48 pseudorandom number generator. A number of Fortran and C/C++ programs are available in rn 2. call RANDOM StatLib: http://lib.stat.cmu.edu/ drand Returns a pseudorandom number NetLib: http://www.netlib.org/liblist.html srand greater than or equal to zero and GAMS: http://gams.nist.gov/ less than one from the uniform … GNU Scientific Library (GSL) http://www.gnu.org/software/gsl/ distribution. IMSL (International Mathematics and Statistics Library) libraries contain a large number of RNGs 27 28

3 Example: srand and rand in C++ 4 Standard RNG in C++ // generate integer random numbers between i1 and i2 6 #include 1 #include library #include 6 #include 2 srand(seed) is used to initialize the RNG #include 6 using namespace std; 3 rand() returns a pseudo random integer in int main () 5 the range 0 to RAND_MAX. { 3 RAND_MAX = 32767 int nmax=10; /* generate 10 random numbers*/ int i1=1, i2=6, irandom; Generating integer random numbers in a range i1 – i2: srand (123); /* initial seed */ //srand(time(NULL)); // better to "randomize" seed values random_i = i1 + (rand()%(i2-i1+1)); for (int i=0; i < nmax; i=i+1) a better method to do the same { irandom = i1+rand()%(i2-i1+1);number between i1 & i2*/ random_i = i1 + int(1.0*(i2-i1+1)*rand()/(RAND_MAX+1.0)); cout << " " << irandom << endl; } Generating real random numbers between 0.0 and 1.0 system("pause"); return 0; drandom = 1.0*rand()/(RAND_MAX+1); 29 } 30

5 Example: cont. for float Example /* generate random numbers between 0.0 and 1.0 */ 140 #include uniformity of random numbers from rand #include for 1000 random numbers #include 120 #include #include 100 using namespace std; int main () { 80 int nmax = 10; /*generate 10 random number*/ double drandom; cout.precision(4); 60 0.4563 cout.setf(::fixed | ios::showpoint); 0.2816 srand(4567); /* initial seed value */ 0.4452 40 for (int i=0; i < nmax; i=i+1) 0.8693 { 0.8514 20 drandom = 1.0*rand()/(RAND_MAX+1); 0.6432 cout << "d = " << drandom << endl; bin a in numbers of random number } 0.0493 0.9999 0 system("pause"); 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 return 0; 0.601731 32 } 0.0548 bins

Example two random sequences (parking lot test) 1400 Example: 1.0 uniformity of random numbers from rand 1200 for 10000 random numbers 2D distribution for two 0.8 random sequences xi 1000 and yi 0.6 800 k-th moment of the y(i) 0.4 random number 600 distribution 0.2 400 0.0 200 0.0 0.2 0.4 0.6 0.8 1.0 x(i) number of random numbers in a bin a in numbers of random number 5000 points, 0 4 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 k-th momentum =0.1991 33 near-neighbor correlation = 0.2507 34 bins

correlation test Example: 1.0 Comment to rand in C++

2D distribution for 0.8 “The version of rand() that comes with your C++ compiler

correlation (xi, xi+5) will in all probability be a pretty simple generator and 0.6 wouldn't be appropriate for scientific use. … It may well be random enough for use in simple programs and

x(i+5) 0.4 .”

0.2 Jacobs, B. C++ Random Numbers. A tutorial for beginners, introducing the functions srand() and rand() 0.0 0.0 0.2 0.4 0.6 0.8 1.0 x(i) see also http://www.netlib.org/random/ 5000 points, k-th momentum =0.1991 Source codes for various random number generators in C near-neighbor correlation = 0.2507 35 and Fortran, including the RANLIB library 36

6 Standard RNG in Fortran Practice 1 (homework)

srand(iseed) seeds the random number generator 1. Write a program to generate random numbers using rand() Return real random numbers in the the linear congruent method range 0.0 through 1.0. 2. Plot 2D distribution for two random sequences xi and yi Generating a real random number between 0.0 and 1.0 3. Plot 2D distribution for correlation (xi, xi+4) call srand(iseed) 4. Evaluate 5-th moment of the random number x = rand() distribution

5. Use some built-in RNG for problems 2-4. There are very many good uniform and non-uniform random number generators written in Fortran.

37 38

Monte Carlo Integration

There are very many methods for numerical integration Part 2 Can MC approach compete with sophisticated methods? Can we gain anything from integration by Monte Carlo Integration “gambling”?

40

Integration by rejection Problem: High-Dimensional Integration hit and miss method

Example: Integration for a system with 12 electrons. Example: area of a circle R 3*12=36 dimensional integral Radius: R Area of the : 4R2 If 64 points for each integration then =6436 points to evaluate 1. loop over N 53 For 1 Tera Flop computer = 10 seconds 2. generate a pair of random numbers That is … 3 times more then the age of the x and y on [-1,1] universe! 3. if (x*x+y*y) < 1 then m=m+1

4. since Acircle/Asquare = m/N 2 5. Acircle = m/N*Asquare = (m/N)*4R 41 42

7 One more Integration by mean value

example b I = ∫ f (x)dx = (b − a) f a b 1 N I f (x)dx (b a) f (x ) S = ∫ ≈ − ∑ i ± Δ a N i=1 2 f 2 − f ΔS = (b − a) N the error evaluation is based on the Compute N pairs of random numbers xi and yi with N N 1 2 1 2 0.0 ≤x ≤2.0 and -1.5 ≤ y ≤1.5. f = ∑ f (xi ) f = ∑ f (xi ) N i=1 N i=1 ⎛ n − n ⎞ F = A⎜ + − ⎟ Traditional methods (Simpson, …) – N points are chosen n with equal spacing. ⎝ N ⎠ 43 44 – random

Example: 1D integration (C++) π double int_mc1d(double(*f)(double), double a, double b, int n) sin(x)dx = 2.0 /* 1D intergration using Monte-Carlo method for f(x) on [a,b] Example ∫ input: f - Function to integrate (supplied by a user) 0 a - Lower limit of integration n Trapez. Simpson Monte Carlo b - Upper limit of integration n - number random points 2 1.570796 2.094395 2.483686 output:r - Result of integration 4 1.896119 2.004560 2.570860 Comments: be sure that following headers are included 8 1.974232 2.000269 2.140117 #include #include 16 1.993570 2.000017 1.994455 */ 32 1.998393 2.000001 2.005999 { 64 1.999598 2.000000 2.089970 double r, x, u; 128 1.999900 2.000000 2.000751 srand(time(NULL)); /* initial seed value (use system time) */ 256 1.999975 2.000000 2.065036 r = 0.0; 512 1.999994 2.000000 2.037365 1024 1.999998 2.000000 1.988752 for (int i = 1; i <= n; i=i+1) { 2048 2.000000 2.000000 1.989458 u = 1.0*rand()/(RAND_MAX+1); // random between 0.0 and 1.0 4096 2.000000 2.000000 1.991806 x = a + (b-a)*u; // random x between a and b 8192 2.000000 2.000000 2.000583 r = r + f(x); } 16384 2.000000 2.000000 1.987582 r = r*(b-a)/n; 32768 2.000000 2.000000 1.991398 return r; 45 65536 2.000000 2.000000 1.997360 46 }

π x cos(10x2 )dx = 0.0003156 Example ∫ 2 many methods to increase accuracy 0 x +1 n Trapez. Simpson Monte Carlo Example: antithetic variates – using “mirror points” 64 0.004360 -0.013151 0.081207 128 0.001183 -0.001110 0.155946 b 1 N / 2 256 0.000526 -0.000311 0.071404 I = f (x)dx ≈ (b − a) ()f (x ) + f (a + (b − x ) 512 0.000368 0.000006 0.002110 ∫ ∑ i i N i=1 1024 0.000329 0.000161 -0.004525 a 2048 0.000319 0.000238 -0.010671 Antithetic variates have negative covariances, thus 4096 0.000316 0.000277 0.000671 8192 0.000316 0.000296 -0.009300 reducing the variance of the sum 16384 0.000316 0.000306 -0.009500 32768 0.000316 0.000311 -0.005308 65536 0.000316 0.000313 -0.000414 more methods can be found in 131072 0.000316 0.000314 0.001100 262144 0.000316 0.000315 0.001933 James E. Gentle – “Random Number Generation and 524288 0.000316 0.000315 0.000606 Monte Carlo Methods 1048576 0.000316 0.000315 -0.000369 2097152 0.000316 0.000316 0.000866 Second edition - 2004 4194304 0.000316 0.000316 0.000330 47 48

8 Example: nD integration (C++) double int_mckd(double(*fn)(double[],int),double a[], Multidimensional Monte Carlo double b[], int n, int m) /* input is similar to 1D integration*/ { double r, x[n], p; int i, j; b d N srand(time(NULL));/* initial seed value (use system time) */ 1 r = 0.0; dx dyf (x, y) ≅ (b − a)(d − c) f (x , y ) p = 1.0; ∫∫ N ∑ i i a c i=1 // step 1: calculate the common factor p for (j = 0; j < n; j = j+1) p = p*(b[j]-a[j]);

// step 2: integration for (i = 1; i <= m; i=i+1) { // calculate random x[] points for (j = 0; j < n; j = j+1) { x[j] = a[j] + (b[j]-a[j])*rand()/(RAND_MAX+1); } r = r + fn(x,n); } r = r*p/m; 49 return r; 50 }

1 1 1 1 1 1 1 Error in Monte Carlo integration dx dx dx dx dx dx (x x x )2 dx 12.83333333 ∫ 1 ∫∫2 3 ∫ 4 ∫ 5 ∫ 6 ∫1 + 2 +K+ 7 7 = 0 0 0 0 0 0 0 1 Error in Monte - Carlo 1D case 7D Integral N 8 11.478669 1 16 12.632578 Error in Monte - Carlo nD case 32 13.520213 N Example 64 13.542921 1 128 13.263171 Error in Simson 1D case 4 256 13.178140 N 512 12.850561 1/ n ⎛ 1 ⎞ 1024 12.747383 Error in Simson nD case ⎜ 4 ⎟ 2048 12.745207 ⎝ N ⎠ 4096 12.836080 8192 12.819113 16384 12.790508 32768 12.765735 65536 12.812653 at n 7 or 8 the error in Monte Carlo integration is 131072 12.809303 similar to that of conventional scheme 262144 12.831216 51 524288 12.832844 52 total elapsed time = 1 seconds

Practice: Integration

Use Monte Carlo integration (both rejection and mean value methods) to evaluate 3 5 Part 3 ∫ exp(−x)dx and ∫ sin(2x2 )dx 0 0 Evaluate 7-D integral Non-uniform distributions 1 1 1 1 1 1 1 dx dx dx dx dx dx (x x x )2 dx ∫ 1 ∫∫2 3 ∫ 4 ∫ 5 ∫ 6 ∫1 + 2 +K+ 7 7 0 0 0 0 0 0 0

53

9 Non-uniform distributions Method 1: von Neumann rejection Generating non-uniform distribution with a probability Most situation in physics – random numbers with non- distribution w(x) uniform distribution ƒ • generate two random numbers ƒ experiments with different types of distributions xi on [xmin, xmax] ƒ … yi on [ymin, ymax] Principal idea: Generating non-uniform random number •if yi < w(xi), accept distributions with a uniform random number generators •if yi > w(xi), reject • The xi so accepted will have the weighting w(x)

55 56

Example for w(x)=exp(-x2) Example double w(double); 3000 int main () { int nmax = 50000; non-uniform distribution double xmin=0.0, xmax=2.0, ymin, ymax; 2500 w(x) = exp(-x2) double x, y;

ymax = w(xmin); ymin = w(xmax); 2000 srand(time(NULL)); for (double i=1; i <= nmax; i=i+1) { 1500 x = xmin + (xmax-xmin)*rand()/(RAND_MAX+1); y = ymin + (ymax-ymin)*rand()/(RAND_MAX+1); if (y > w(x)) continue; file_3 << " " << x << endl; /* output to a file */ 1000 } system("pause"); return 0; 500 }

/* Probability distribution w(x) */ number of random numbers in a bin double w(double x) 0 { 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 return exp(0.0-1.0*x*x); 57 58 } bins

Example Method 2: Inversion method 10000 energy = 100.0 Works if the function you are trying to use for a distribution has sigma = 20.0 8000 an inverse. Let p ( y ) = f ( y ) is some arbitrary distribution

6000 From the fundamental transformation law of probabilities p( y)dy = p(x)dx or p( y) = p(x) dx / dy

4000 Let p(x) is a uniform probability distribution We need to solve the differential equation

normal energy distribution normal energy 2000 dx = f ( y) ⇒ x = f ( y)dy = F( y) dy ∫ −1 0 then y = F ( x ) provides the non-uniform distribution 40 60 80 100 120 140 160 59 60 energy

10 Example: exponential distribution Practice: non-uniform distribution p( y) = exp(− y) Use the von Neumann rejection technique to generate a dx = exp(− y) normal distribution of standard deviation 1.0 dy x = ∫ exp(− y)dy → exp(− y) (take positive distribution) y = −ln(x)

y is a positive exponential distribution generated from a uniform distribution x Quite often analytic solutions are not feasible. However, very many program libraries have 61 62 most common non-uniform distributions

more on integration – importance of sampling The Metropolis

or more attention to regions corresponding to large In 1953 Metropolis introduced “the idea of importance values of the integrand sampling” that can considerably improve speed and quality b b f (x) I = f (x)dx = p(x)dx of calculations. ∫ ∫ p(x) a a b 1 N I w(x) f (x)dx (b a) w(x ) f (x ) where p(x) is a probability density over x = ∫ ≈ − ∑ i i a N i=1 The density p(x) is called the importance function In the simplest version, x i + 1 = x i + h ( 2 u i − 1 ) where h is a Then with xi from the distribution with density p step and ui is from a uniform random distribution 1 N f (x ) The step is accepted if I ≈ (b − a) ∑ i N i=1 p(xi ) w(xi+1) ≥ αi w(xi ) where α is a random number from a uniform distribution 63 i 64

Random Walk

A simple random walk is a sequence of unit steps where each step is taken in the direction of one of the coordinate axis, and Part 4 each possible direction has equal probability of being chosen.

Random walk on a lattice: Random Walk ƒ In two dimensions, a single step starting at the point with integer coordinates (x,y) would be equally likely to move to any of one of the four neighbors (x+1,y), (x-1,y), (x,y+1) and (x,y-1). ƒ In one dimension walk there are two possible neighbors ƒ In three dimensions there are six possible neighbors. 66

11 Random Walk simulates:

Brownian motion (answer the question - how many collisions, on average, a particle must take to travel a distance R). Electron transport in metals, … …

67 68

Example: random walk (Fortran) Practice 2 (random walk) integer*4 iu, it, is, itests, isteps, iway, x, y real*4 rand, d, dav

1. Write a program that simulate a random 2D walk with the read (*,*) itests, isteps same step size . Four directions are possible (N, E, S, W). … dav=0.0 Your program will involve two large integers, M = the number of do it=1,itests random walks to be taken and N = the maximum number of steps in a x=0 y=0 single walk. do is=1,isteps 2. iway= int(0.0+4.0*rand()) Find the average distance to be from the origin point after if(iway.eq.0) x = x+1 N steps if(iway.eq.1) x = x-1 if(iway.eq.2) y = y+1 3. Is there any finite bound on the expected number of steps if(iway.eq.3) y = y-1 c write(7,101) x,y before the first return to the origin? end do d = sqrt((float(x))**2+(float(y))**2) dav = dav + d end do dav = dav/float(itests) 69 write(*,100) itests, isteps, dav 70 …

20 20

2D random walk 2D random walk 15 15

10 10

y y 5 5 example example

0 0

-5 -5 -5 0 5 10 15 20 -5 0 5 10 15 20

x 71 x 72

12 Various models of random walk A persistent random walk

Persistent random walk A persistent random walk in 2 dimensions in a city Restricted random walk with n*n blocks Self-avoiding random walk Condition: the walker can not step back … Goal: find average number of steps to get out the city Examples of applications: ƒ Spread of inflectional diseases and effects of immunization ƒ Spreading of fire

73 74

24 24

22 persistent 2D random walk 22 persistent 2D random walk from the center from the center 20 of 24*24 city 20 of 24*24 city 18 18 16 16

14 14

12 12

y y 10 10

8 8

6 6 4 4

2 2

0 0 024681012141618202224 0 2 4 6 8 1012141618202224

x 75 x 76 persistent random walk in a city persistent random walk in a city

24 Example: (Fortran)

22 persistent 2D random walk from the center 20 do while (out.eq.0) of 24*24 city skip=0 18 iway = int(1.0+4.0*rand()) c check can we use this iway or not 16 if(abs(iway – iold).eq.2) skip=1 14 c if step is allowed the walker goes if(skip.eq.0) then 12 nsteps = nsteps+1

y if(iway.eq.1) x = x+1 10 if(iway.eq.3) x = x-1 8 if(iway.eq.2) y = y+1 if(iway.eq.4) y = y-1 6 c check conditions to be out of n*n city 4 if(x.le.0.or.x.ge.ncity) out=1 if(y.le.0.or.y.ge.ncity) out=1 2 Average number of blocks to go to iold=iway end if 0 leave the city with 24*24 blocks end do 024681012141618202224from the center: 92 blocks x 77 78 persistent random walk in a city from a random point: 47 blocks

13 10

Polymer simulation 8 69 monomers

Random walk in 2 dimensions 6 Condition: self-avoiding 2D random walk 4

2

0 y -2

-4

-6

-8

-10 -2024681012141618 x 79 80

2 24 22 24 monomers 179 monomers 20 0 18 16 14 12 -2 10 8 y

y 6 -4 4 2 0 -2 -6 -4 -6 -8 -8 -10 -4 -2 0 2 4 6 -30 -25 -20 -15 -10 -5 0 5 x x 81 82

Example: (from Fortran) Polymer simulation … 0.30 do while (out.eq.0) xold=x average yold=y 0.25 c = rand() polymer size = 72 if(c.le.0.25) x = x+1 end-to-end size = 12 if(c.gt.0.25.and.c.le.0.50) x=x-1 0.20 if(c.gt.0.50.and.c.le.0.75) y=y+1 if(c.gt.0.75) y=y-1 c is the (x,y) site vacant them accept the last step 0.15 if(a(x,y).eq.0) then

a(x,y)=1 probability n = n+1 0.10 else x=xold 0.05 y=yold end if c*** exit conditions 0.00 c no more available steps around new x,y where to go 0 50 100 150 200 250 300 350 400 if(a(x+1,y)*a(x,y+1)*a(x-1,y)*a(x,y-1).eq.1) out=1 end do 83 polymer size 84 …

14 Example The Metropolis algorithm (cont.) a group of atoms interact by Lennard-Jones potential

The metropolis sampling is most efficient for multidirectional 12 6 ⎡⎛ σ ⎞ ⎛ σ ⎞ ⎤ problems. V (r) = 4ε ⎢⎜ ⎟ − ⎜ ⎟ ⎥ ⎣⎢⎝ r ⎠ ⎝ r ⎠ ⎦⎥ In a traditional random walk all visiting points are equal. Find positions of n atoms that gives the min value of the total potential. Method: Monte-Carlo variations What is we want the random walker to spend more time in a examples: n=19 n=7 specific region, e.g. where for a 2D walk g(x,y) is larger. 2 5

x'= x + h(2ui −1) then consider 4

1 y'= y + h(2u −1) g(x', y') 3 i+1 = and generate

2 y

g(x, y) y

0 some random number α 1 if q ≥ α the step is accepted 0

-1 -1 -1 0 1 2 -1012345 if q < α the step is rejected x 85 x 86

Buffon problem for D=1 enter numbers of tests Example 10000 The French naturalist and mathematician Comte de Buffon showed that enter numbers of intervals in the grid 10 the probability that a needle of length L thrown randomly onto a grid of enter the needle size L<1 parallel lines with distance D≥L apart intersects a line is 2L/(D*π). 0.5 hit = 3.157E-01 c*** loop over trials hit = 0 buffon = 3.183E-01 do it=1,itests x0 = float(N)*D*rand() k = int(x0/D) Buffon problem for D=1 x1 = x0 - D*float(k) enter numbers of tests x2 = D - x1 100000 x = min(x1,x2) enter numbers of intervals in the grid dx = 0.5*abs(L*cos(1.0**rand())) 50 if(dx.ge.x) hit = hit + 1 enter the needle size L<1 end do c*** average number of hits 0.9 ahit = float(hit)/float(itests) hit = 5.717E-01 buffon = (2*L)/(pi*D) buffon = 5.730E-01 87 88

Lets make a Example c*** loop over trials enter numbers of tests win1 = 0 10000 investigate a simple problem that generated much attention several years win2 = 0 win1 = 3.359E-01 do it=1,itests ago and for which many mathematicians obtained an incorrect solution. The win2 = 6.641E-01 problem was the analysis of the optimal strategy in a television show a(1) = rand() a(2) = rand() popular at the time. The show was Let’s Make a Deal with host Monty Hall. a(3) = rand() At some point in the show, a contestant was given a choice of selecting one choice = 1 + int(3.0*rand()) of three possible items, each concealed behind one of three closed doors. b(1) = a(choice) The items varied considerably in value. After the contestant made a choice if(choice.eq.1) b(2) = max(a(2),a(3)) but before the chosen door was opened, the host, who knew where the if(choice.eq.2) b(2) = max(a(1),a(3)) most valuable item was, would open one of the doors not selected and if(choice.eq.3) b(2) = max(a(1),a(2)) if(b(1).ge.b(2)) then reveal a worthless item. The host would then offer to let the contestant win1 = win1 + 1 select a different door from what was originally selected. The question, of else course, is should the contestant switch? A popular magazine writer Marilyn win2 = win2 + 1 vos Savant concluded that the optimal strategy is to switch. This strategy is end if counterintuitive to many mathematicians, who would say that there is end do c*** average number of games and wins nothing to be gained by switching; that is, that the probability of improving awin1 = float(win1)/float(itests) the selection is 0.5. Study this problem by Monte Carlo methods. What is awin2 = float(win2)/float(itests) the probability of improving the selection by switching? Be careful to write (*,101) awin1, awin2 understand all of the assumptions, and then work the problem analytically 89 90 also. (A Monte Carlo study is no substitute for analytic study.)

15 write (*,*)'enter numbers of tests, money and goal' Example read (*,*) itests, money1, money2 The gambler's ruin problem. Suppose that a person decides to try to c*** loop over trials total = 0 increase the amount of money in his/her pocket by participating in some wins = 0 gambling. Initially, the gambler begin with $m in capital. The gambler do it=1,itests x=money1 decides that he/she will gamble until a certain goal, $n (n>m), is achieved games=0 or there is no money left (credit is not allowed). On each throw of a coin do while(x.gt.0.and.x.lt.money2) games = games + 1 (roll of the dice, etc.) the gambler either win $1 or lose $1. If the gambler luck = 1 achieves the goal he/she will stop playing. If the gambler ends up with no if(rand().le.0.5) luck=-1 x = x+luck money he/she is ruined. end do What are chances for the gambler to achieve the goal as a function of k, total = total+games if(x.gt.0) wins = wins+1 where k=n/m? end do How long on average will it take to play to achieve the goal or to be c*** average number of games and wins agames = float(total)/float(itests) ruined? awins = float(wins)/float(itests) aloose = 1.0-awins 91 write (*,100) itests, money1, money2 92 write (*,101) awins, aloose, agames

The gambler`s ruin problem. The gambler`s ruin problem. Chances to reach certain goal Chances to reach certain goal enter numbers of tests, money and goal enter numbers of tests, money and goal 10000 100000 10 10 100 100

tests: 10000 tests: 100000 initial: 10 initial: 10 goal: 100 goal: 100 win = 1.026E-01 win = 9.44000E-03 loose = 8.974E-01 loose = 9.90560E-01 games = 9.019E+02 games = 4.51806E+02

chance to win in each bet 50/50 chance to win in each bet 49/51 93 94

Example What is the distribution of cooking times of the hamburgers that he removes? An industrious physics major finds a job at a local fast food restaurant to What is a chance for a customer to get a well cooked hamburger if it help him pay his way through college. His task is to cook 20 hamburgers takes 5 minutes to cook a hamburger. on a grill at any one time. When a hamburger is cooked, he is supposed to replace it with uncooked hamburger. However, our physics major does Does the answers to the first two questions change if he cooks 40 not pay attention to whether the hamburger is cooked or not. His method hamburgers at any one time? is to choose a hamburger at random and replace it by an uncooked one. He does not check if the hamburger that he removes from the grill is Comment: For simplicity, assume that he replaces a hamburger at a ready. regular interval of 30 seconds and there is an indefinite supply of uncooked hamburgers.

95 96

16 nburger = 20 do i = 1, tsteps tsteps = 100000 x = 1.0 + rand()*real(nburger) burger = int(x) bmin = 10 do j = 1, nburger bmax = 20 if(j.eq.burger) then tcook(b(j)+1) = tcook(b(j)+1) + 1 c initialization (burgers) if(b(j)+1.gt.tmax) tmax = b(j)+1 do i = 1, nburger if(b(j).lt.bmin) cook1 = cook1 + 1 b(i) = 0 if(b(j).ge.bmin.and.b(j).le.bmax) end do cook2 = cook2 + 1 c initialization (time distribution) if(b(j).gt.bmax) cook3 = cook3 + 1 do i = 1, tsteps b(j) = 0 tcook(i) = 0 else end do b(j) = b(j) + 1 c other variables end if cook1 = 0 end do cook2 = 0 end do cook3 = 0 write(*,102) tmax tmax = 0 c calculate and write chances for undercooked, good c c initial seed number calculated from current time cooked, overcooked burgers call gettim(ihour,imin,isec,imsec) rcook1 = real(cook1)/real(cook1+cook2+cook3) init = imsec*isec*imin+ihour rcook2 = real(cook2)/real(cook1+cook2+cook3) call srand(init) 97 rcook3 = real(cook3)/real(cook1+cook2+cook3) 98 write(*,101) rcook1, rcook2, rcook3

for 100,000 burgers 60 20 burgers on the grill grill for 20 burgers max cooking time = 237 50 burgers cooked = 1,000 undercooked = 0.39941001 well cooked = 0.25903001 over cooked = 0.34156001 40

40 burgers on the grill max cooking time = 463 30 undercooked = 0.22596000

well cooked = 0.18769000 burgers over cooked = 0.58635002 20

10

0 0 102030405060708090100 99 cooking time (min) 100

6000 Applications of Monte-Carlo simulations

grill for 20 burgers 5000 9 integration burgers cooked = 100,000 9 statistical physics 9 aerodynamic 4000 9 quantum chromodynamics 9 molecular dynamic simulation 3000 9 experimental particle physics 9 cellular automata

burgers 9 percolation 2000 9 radiation field and energy transport 9 … 1000 9 Finance and business 9 …

0 0 102030405060708090100 cooking time (min) 101 102

17 Cellular automation

Cellular automata – dynamic computational models that are discrete in space, state and time.

Applications – physics, biology, economics, …

Random walk is an example of cellular automata.

see also “The Game of Life” is a cellular automaton devised by John What is a chance to Horton Conway in 1970. Life is an example of emergence and self- encounter a bear? organization - complex patterns can emerge from the What data do you need implementation of very simple rules. for your simulation?

103 104

18