0% found this document useful (0 votes)
188 views18 pages

Cp01 Random

Here is a Fortran program that implements the linear congruent method to generate random numbers and performs the requested analyses: number generators available in libraries like IMSL, NAG, program lcg implicit none and GNU Scientific Library (GSL). integer, parameter :: n = 10000 integer :: i, a, c, m, seed, x real :: u For serious work, use one of these libraries rather than the built-in rand(). a = 16807 c = 0 m = 2147483647 37 seed = 12345 38 do i=1,n x = mod(a*x + c,m)

Uploaded by

juan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
188 views18 pages

Cp01 Random

Here is a Fortran program that implements the linear congruent method to generate random numbers and performs the requested analyses: number generators available in libraries like IMSL, NAG, program lcg implicit none and GNU Scientific Library (GSL). integer, parameter :: n = 10000 integer :: i, a, c, m, seed, x real :: u For serious work, use one of these libraries rather than the built-in rand(). a = 16807 c = 0 m = 2147483647 37 seed = 12345 38 do i=1,n x = mod(a*x + c,m)

Uploaded by

juan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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
Dice: In large number of throws
probability 1/6

Monte Carlo Simulation


1

Question: What is the most probable number for


the sum of two dice?

36 possibilities
6 times for 7

Applications for MC simulation

1 2 3
1 2 3 4

4
5

5
6

6
7

2 3 4 5

3 4 5 6
4 5 6 7

7
8

8
9

9
10

5 6 7 8

10 11

Stochastic processes
Complex systems (science)
Numerical integration
Risk management

6 7 8 9 10 11 12

Financial planning
Cryptography

How do we do that?
You let the computer to throw the coin and record

Part 1

the outcome
You need a program that generates randomly a
variable

Random number generators

with relevant probability distribution

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)

11

00000
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
.....

10097
37542
08422
99019
12807
66065
31060
85269
63573
73796
98520
11805
83452
88685
99594

32533
04805
68953
02529
99970
74717
10805
77602
32135
45753
17767
05431
99634
40200
67348

76520
64894
19645
09376
80157
34072
45571
02051
05325
03529
14905
39808
06288
86507
87517

13586
74296
09303
70715
36147
76850
82406
65692
47048
64778
68607
27732
98083
58401
64969

34673
24805
23209
38311
64032
36697
35303
68665
90553
35808
22109
50725
13746
36766
91826

54876
24037
02560
31165
36653
36170
42614
74818
57548
34282
40558
68248
70078
67951
08928

80959
20636
15953
88676
98951
65813
86799
73053
28468
60935
60970
29405
18475
90364
93785

09117
10402
34764
74397
16877
39885
07439
85247
28709
20344
93433
24201
40610
76493
61368

39292
00822
35080
04436
12171
11199
23403
18623
83491
35273
50500
52775
68711
29609
23478

74945
91665
33606
27659
76833
29170
09732
88579
25624
88435
73998
67851
77817
11062
34113

12

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

13

Software - Random Number Generators

14

Good Random Number Generators

There are no true random number generators but

Two important issues:

pseudo RNG!

1. randomness
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 basic techniques for RNG

Linear Congruent Method for RNG

The standard methods of generating pseudorandom


numbers use modular reduction in congruential
relationships.

Generates a random sequence of numbers


{x1, x2, xk} of length M over the interval [0,M-1]

ax + c
xi = mod(axi 1 + c, M ) = remainder i 1
0 xi 1 < M
M

Two basic techniques for generating uniform random


numbers:
1.

congruential methods

2.

feedback shift register methods.

starting value x0 is called seed


coefficients a and c should be chosen very
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

Example:

xi = mod(axi 1 + c, M )

Random Numbers on interval [A,B]

mod(b, M ) = b int(b / M ) * M

a=4, c=1, M=9, x1=3


x2 = 4
x3 = 8
x4 = 6
x5-10 = 7, 2, 0, 1, 5, 3

Scale results from xi on [0,M-1] to yi on [0,1]

yi = xi /( M 1)
Scale results from xi on [0,1] to yi on [A,B]

yi = A + ( B A) xi

interval: 0-8, i.e. [0,M-1]


period: 9
i.e. M numbers (then repeat)

19

Magic numbers for Linear Congruent Method

20

Other Linear Congruential Generators

M (length of the sequence) is quite large

9 Multiple Recursive Generators


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 with-Carry Generators

Good magic number for linear congruent method:

xi = mod(axi 1 + c, M )
a = 16,807, c = 0, M = 2,147,483,647
for c = 0 multiplicative congruential generator:
21

22

How can we be check the RNG?

Other Generators
9 Nonlinear Congruential Generators

Plots:

9 Feedback Shift Register Generators


9 Generators Based on Cellular Automata

2D figure, where xi and yi are from two random

3D figure (xi, yi, zi)

2D figure for correlation (xi, xi+k)

sequences (parking lot test)

9 Generators Based on Chaotic Systems


9

James E. Gentle Random Number Generation and


Monte Carlo Methods
Second edition - 2004
23

24

Test Suites (most known) for RNG

How can we check the RNG?


Example of other assessments

the NIST Test Suite (NIST, 2000) includes sixteen tests

Uniformity. A random number sequence should contain


numbers distributed in the unit interval with equal
probability. Use bins.
k-th momentum

xk =

1
N

near-neighbor correlation

k
i

i =1

1
N

DIEHARD Battery of Tests of Randomness (eighteen tests)

1
k +1

x x
i =1

http://csrc.nist.gov/groups/ST/toolkit/rng/index.html

i i+k

http://www.stat.fsu.edu/pub/diehard/

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
http://www.iro.umontreal.ca/~simardr/testu01/tu01.html 26

25

Industrial methods in C/C++ and Fortran

Software for RNG


C/C++ and Fortran (90,95) provide built-in uniform random
number generators,

rand

but except for small studies, these built-in generators


should be avoided.

drand48

random

A number of Fortran and C/C++ programs are available in


StatLib: http://lib.stat.cmu.edu/

rn

NetLib: http://www.netlib.org/liblist.html

srand

drand

GAMS: http://gams.nist.gov/

GNU Scientific Library (GSL) http://www.gnu.org/software/gsl/

1. call SEED
Changes the starting point of the
pseudorandom number generator.
2. call RANDOM
Returns a pseudorandom number
greater than or equal to zero and
less than one from the uniform
distribution.

IMSL (International Mathematics and Statistics Library)


libraries contain a large number of RNGs
27

28

Standard RNG in C++

// generate integer random numbers between i1 and i2


#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

#include <cstdlib> library


srand(seed)

is used to initialize the RNG

rand()

returns a pseudo random integer in


the range 0 to RAND_MAX.
RAND_MAX = 32767

Generating integer random numbers in a range i1 i2:

int main ()
{
int nmax=10;
/* generate 10 random numbers*/
int i1=1, i2=6, irandom;
srand (123);
/* initial seed */
//srand(time(NULL)); // better to "randomize" seed values

random_i = i1 + (rand()%(i2-i1+1));
a better method to do the same
random_i = i1 + int(1.0*(i2-i1+1)*rand()/(RAND_MAX+1.0));
Generating real random numbers between 0.0 and 1.0
drandom = 1.0*rand()/(RAND_MAX+1);

3
4
6
1
6
2
6
3
5
3

Example: srand and rand in C++

29

for (int i=0; i < nmax; i=i+1)


{
irandom = i1+rand()%(i2-i1+1);number between i1 & i2*/
cout << " " << irandom << endl;
}
system("pause");
return 0;
30

Example: cont. for float

Example

uniformity of random numbers from rand


for 1000 random numbers

120
100
80
60
40
20
0
0.0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

32

bins

two random sequences (parking lot test)

Example:

uniformity of random numbers from rand


for 10000 random numbers

1200

1.0

2D distribution for two

0.8

random sequences xi

1000

and yi
random number

600

distribution

0.6

y(i)

k-th moment of the

800

0.4

0.2

400
0.0
0.0

200

0.2

0.4

0.6

0.8

1.0

x(i)
5000 points,
4
k-th momentum <x >=0.1991
near-neighbor correlation = 0.2507 34

0
0.0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

33

bins

correlation test

Example:

Comment to rand in C++

1.0

2D distribution for

The version of rand() that comes with your C++ compiler

0.8

correlation (xi, xi+5)

will in all probability be a pretty simple generator and


wouldn't be appropriate for scientific use. It may well

0.6

x(i+5)

number of random numbers in a bin

1400

Example

140

number of random numbers in a bin

/* generate random numbers between 0.0 and 1.0 */


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int main ()
{
int nmax = 10;
/*generate 10 random number*/
double drandom;
cout.precision(4);
0.4563
cout.setf(ios::fixed | ios::showpoint);
0.2816
0.4452
srand(4567); /* initial seed value */
for (int i=0; i < nmax; i=i+1)
0.8693
{
0.8514
drandom = 1.0*rand()/(RAND_MAX+1);
0.6432
cout << "d = " << drandom << endl;
0.0493
}
0.9999
system("pause");
0.6017
return 0;
31
}
0.0548

be random enough for use in simple programs and


0.4

games.
Jacobs, B. C++ Random Numbers. A tutorial for

0.2

beginners, introducing the functions srand() and rand()


0.0
0.0

0.2

0.4

0.6

0.8

1.0

x(i)
5000 points,
4
k-th momentum <x >=0.1991
near-neighbor correlation = 0.2507

see also http://www.netlib.org/random/


Source codes for various random number generators in C
35

and Fortran, including the RANLIB library

36

Standard RNG in Fortran

Practice 1 (homework)

srand(iseed)

seeds the random number generator

1.

rand()

Return real random numbers in the


range 0.0 through 1.0.

2.

Plot 2D distribution for two random sequences xi and yi

3.

Plot 2D distribution for correlation (xi, xi+4)

4.

Evaluate 5-th moment of the random number

Write a program to generate random numbers using


the linear congruent method

Generating a real random number between 0.0 and 1.0


call srand(iseed)
x = rand()

distribution
5.

There are very many good uniform and non-uniform random


number generators written in Fortran.

Use some built-in RNG for problems 2-4.

37

38

Monte Carlo Integration


There are very many methods for numerical

Part 2

integration
Can MC approach compete with sophisticated
methods?

Monte Carlo Integration

Can we gain anything from integration by


gambling?

40

Integration by rejection
hit and miss method

Problem: HighHigh-Dimensional Integration

Example: area of a circle


Radius: R
Area of the square: 4R2

Example: Integration for a system with 12 electrons.


3*12=36 dimensional integral
If 64 points for each integration then =6436 points
to evaluate

1.

loop over N

For 1 Tera Flop computer = 1053 seconds

2.

That is 3 times more then the age of the


universe!

generate a pair of random numbers


x and y on [-1,1]

3.

if (x*x+y*y) < 1 then m=m+1

4.

since Acircle/Asquare = m/N

5.

Acircle = m/N*Asquare = (m/N)*4R2

41

42

One more
example

Integration by mean value


b

I = f ( x)dx = (b a ) f
a

I = f ( x )dx (b a )
a

f2 f

S = ( b a )
Compute N pairs of random numbers xi and yi with
0.0 x 2.0 and -1.5 y 1.5.

n n
Fn = A +

1
f =
N
43

Example: 1D integration (C++)

r = 0.0;
for (int i = 1; i <= n; i=i+1)
{
u = 1.0*rand()/(RAND_MAX+1); // random between 0.0 and 1.0
x = a + (b-a)*u;
// random x between a and b
r = r + f(x);
}
r = r*(b-a)/n;
45
return r;

Example
n
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304

x
0

x
cos(10 x 2 )dx = 0.0003156
+1

Trapez.
Simpson
Monte Carlo
0.004360 -0.013151 0.081207
0.001183 -0.001110 0.155946
0.000526 -0.000311 0.071404
0.000368 0.000006 0.002110
0.000329 0.000161 -0.004525
0.000319 0.000238 -0.010671
0.000316 0.000277 0.000671
0.000316 0.000296 -0.009300
0.000316 0.000306 -0.009500
0.000316 0.000311 -0.005308
0.000316 0.000313 -0.000414
0.000316 0.000314 0.001100
0.000316 0.000315 0.001933
0.000316 0.000315 0.000606
0.000316 0.000315 -0.000369
0.000316 0.000316 0.000866
0.000316 0.000316 0.000330

f ( x ) S
i

i =1

f (x )

f2

i =1

1
=
N

f
i =1

( xi )

the error evaluation


is based on the
normal distribution

Traditional methods (Simpson, ) N points are chosen


with equal spacing.
Monte Carlo method random sampling

44

double int_mc1d(double(*f)(double), double a, double b, int n)


/* 1D intergration using Monte-Carlo method for f(x) on [a,b]
input: f - Function to integrate (supplied by a user)
a - Lower limit of integration
b - Upper limit of integration
n - number random points
output:r - Result of integration
Comments: be sure that following headers are included
#include <cstdlib>
#include <ctime>
*/
{
double r, x, u;
srand(time(NULL)); /* initial seed value (use system time) */

1
N

Example

sin( x ) dx = 2.0
0

n
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536

Trapez.
1.570796
1.896119
1.974232
1.993570
1.998393
1.999598
1.999900
1.999975
1.999994
1.999998
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000

Simpson
2.094395
2.004560
2.000269
2.000017
2.000001
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000
2.000000

Monte Carlo
2.483686
2.570860
2.140117
1.994455
2.005999
2.089970
2.000751
2.065036
2.037365
1.988752
1.989458
1.991806
2.000583
1.987582
1.991398
1.997360

46

many methods to increase accuracy


Example: antithetic variates using mirror points
b

I = f ( x )dx (b a )
a

1
N

N /2

( f ( x ) + f ( a + (b x ) )
i =1

Antithetic variates have negative covariances, thus


reducing the variance of the sum

more methods can be found in


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

48

Example: nD integration (C++)

Multidimensional Monte Carlo

double int_mckd(double(*fn)(double[],int),double a[],


double b[], int n, int m)
/* input is similar to 1D integration*/
{
double r, x[n], p;
int i, j;
srand(time(NULL));/* initial seed value (use system time) */
r = 0.0;
p = 1.0;

dx dyf ( x, y) (b a)(d c) N f ( x , y )
i

i =1

// step 1: calculate the common factor p


for (j = 0; j < n; j = j+1) p = p*(b[j]-a[j]);

49

Error in Monte Carlo integration

Error in Monte - Carlo

nD case

Error in Simson

1D case

Error in Simson

nD case

Example
1/ n

1
4
N

at n 7 or 8 the error in Monte Carlo integration is


similar to that of conventional scheme
51

50

dx dx dx dx dx dx ( x

1
N
1
N
1
N4

Error in Monte - Carlo 1D case

// 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;
return r;
}

+x2 + K + x7 ) 2 dx7 = 12.83333333

7D Integral
8
11.478669
16
12.632578
32
13.520213
64
13.542921
128
13.263171
256
13.178140
512
12.850561
1024
12.747383
2048
12.745207
4096
12.836080
8192
12.819113
16384
12.790508
32768
12.765735
65536
12.812653
131072
12.809303
262144
12.831216
524288
12.832844
total elapsed time = 1 seconds

52

Practice: Integration
Use Monte Carlo integration (both rejection and mean
value methods) to evaluate
3

exp( x )dx

sin(2 x

and

)dx

Non-uniform distributions

Evaluate 7-D integral


1

dx dx dx dx dx dx ( x
1

Part 3

+x2 + K + x7 ) 2 dx7

53

Method 1: von Neumann rejection

Non-uniform distributions

Generating non-uniform distribution with a probability


distribution w(x)

Most situation in physics random numbers with nonuniform distribution

radioactive decay

experiments with different types of distributions

generate two random


numbers
xi on [xmin, xmax]
yi on [ymin, ymax]
if yi < w(xi), accept
if yi > w(xi), reject
The xi so accepted will
have the weighting w(x)

Principal idea: Generating non-uniform random number


distributions with a uniform random number generators

55

Example for w(x)=exp(-x2)

56

Example

double w(double);
int main ()
{
int nmax = 50000;
double xmin=0.0, xmax=2.0, ymin, ymax;
double x, y;

number of random numbers in a bin

3000

ymax = w(xmin);
ymin = w(xmax);
srand(time(NULL));
for (double i=1; i <= nmax; i=i+1)
{
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 */
}
system("pause");
return 0;
}
/* Probability distribution w(x) */
double w(double x)
{
return exp(0.0-1.0*x*x);
}

non-uniform distribution
2
w(x) = exp(-x )

2500

2000

1500

1000

500

0
0.0

57

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

2.0

58

bins

Example

Method 2: Inversion method

10000

normal energy distribution

energy = 100.0
sigma = 20.0

Works if the function you are trying to use for a distribution has
an inverse.

8000

Let

p( y ) = f ( y ) is some arbitrary distribution

6000

From the fundamental transformation law of probabilities

4000

Let p(x) is a uniform probability distribution

p( y )dy = p( x )dx

or

p( y ) = p( x ) dx / dy

We need to solve the differential equation

dx
= f ( y ) x = f ( y )dy = F ( y )
dy
then
y = F 1 ( x ) provides the non-uniform distribution

2000

0
40

60

80

100

120

140

160

59

60

energy

10

Example: exponential distribution

Practice: nonnon-uniform distribution

p( y ) = exp( y )
dx
= exp( y )
dy

Use the von Neumann rejection technique to generate a


normal distribution of standard deviation 1.0

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
most common non-uniform distributions

61

62

more on integration importance of sampling

The Metropolis algorithm

or more attention to regions corresponding to large


values of the integrand

In 1953 Metropolis introduced the idea of importance

I = f ( x )dx =

sampling that can considerably improve speed and quality

f ( x)
p( x )dx
p( x)

of calculations.
b

I = w( x ) f ( x )dx (b a )

where p( x ) is a probability density over x

The density p( x ) is called the importance function

1
N

f ( xi )

p( x )
i =1

w( x ) f ( x )
i =1

In the simplest version, xi +1 = xi + h (2ui 1) where h is a


step and ui is from a uniform random distribution
The step is accepted if

Then with xi from the distribution with density p

I (b a )

1
N

63

w( xi +1 )
i
w( xi )
where i is a random number from a uniform distribution
64

Random Walk

Part 4
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
each possible direction has equal probability of being chosen.

Random walk on a lattice:


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)


1.

integer*4 iu, it, is, itests, isteps, iway, x, y


real*4 rand, d, dav

Write a program that simulate a random 2D walk with the


same step size . Four directions are possible (N, E, S, W).
Your program will involve two large integers, M = the number of
random walks to be taken and N = the maximum number of steps in a
single walk.

2.

Find the average distance to be from the origin point after


N steps

3.

Is there any finite bound on the expected number of steps


before the first return to the origin?

69

read

(*,*) itests, isteps

dav=0.0
do it=1,itests
x=0
y=0
do is=1,isteps
iway= int(0.0+4.0*rand())
if(iway.eq.0) x = x+1
if(iway.eq.1) x = x-1
if(iway.eq.2) y = y+1
if(iway.eq.3) y = y-1
c
write(7,101) x,y
end do
d = sqrt((float(x))**2+(float(y))**2)
dav = dav + d
end do
dav = dav/float(itests)
write(*,100) itests, isteps, dav

20

20

2D random walk

2D random walk
15

10

10

example

15

example

70

-5

-5
-5

10

15

20

-5
71

10

15

20
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

20
18
16
14

12
10
8
6
4
2
0
0

10 12 14 16 18 20 22 24

75

24

persistent 2D random walk


from the center
of 24*24 city

22
20
18
16
14
12

persistent 2D random walk


from the center
of 24*24 city

22

74

persistent random walk in a city

24

10
8
6
4
2
0
0

10 12 14 16 18 20 22 24

76

Example: (Fortran)

24

persistent 2D random walk


from the center
of 24*24 city

22
20
18
16
14
12

persistent random walk in a city

persistent random walk in a city

73

10
8
6
4
2
0
0

Average number of blocks to go to


leave the city with 24*24 blocks
4 6 8 10 12 14 16 18 20 22 24
from the center:
92 blocks
x
77
from a random point: 47 blocks

do while (out.eq.0)
skip=0
iway = int(1.0+4.0*rand())
c check can we use this iway or not
if(abs(iway iold).eq.2) skip=1
c if step is allowed the walker goes
if(skip.eq.0) then
nsteps = nsteps+1
if(iway.eq.1) x = x+1
if(iway.eq.3) x = x-1
if(iway.eq.2) y = y+1
if(iway.eq.4) y = y-1
c check conditions to be out of n*n city
if(x.le.0.or.x.ge.ncity) out=1
if(y.le.0.or.y.ge.ncity) out=1
iold=iway
end if
end do
78

13

10

Polymer simulation

69 monomers

Random walk in 2 dimensions

Condition: self-avoiding 2D random walk

4
2

0
-2
-4
-6
-8
-10
-2

24 monomers
0

-2

-4

-6

-8
-2

12

14

16

18
80

179 monomers

-30

-25

-20

-15

-10

-5

x
81

Example: (from Fortran)

24
22
20
18
16
14
12
10
8
6
4
2
0
-2
-4
-6
-8
-10

do while (out.eq.0)
xold=x
yold=y
c = rand()
if(c.le.0.25) x = x+1
if(c.gt.0.25.and.c.le.0.50) x=x-1
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
if(a(x,y).eq.0) then
a(x,y)=1
n = n+1
else
x=xold
y=yold
end if
c*** exit conditions
c no more available steps around new x,y where to go
if(a(x+1,y)*a(x,y+1)*a(x-1,y)*a(x,y-1).eq.1) out=1
83
end do

82

Polymer simulation
0.30

average
polymer size = 72
end-to-end size = 12

0.25

0.20

probability

-4

10

79

0.15
0.10

0.05
0.00
0

50

100 150 200 250 300 350 400

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


problems.
In a traditional random walk all visiting points are equal.
What is we want the random walker to spend more time in a

12 6
V ( r ) = 4
r
r
Find positions of n atoms that gives the min value of the total potential.
Method: Monte-Carlo variations
examples:
n=19
n=7

specific region, e.g. where for a 2D walk g(x,y) is larger.

then consider

y ' = y + h( 2ui +1 1)

g ( x' , y ' )
and generate
g ( x, y )
some random number

q=

x ' = x + h( 2ui 1)

if q the step is accepted


if q < the step is rejected 85

Example
The French naturalist and mathematician Comte de Buffon showed that
the probability that a needle of length L thrown randomly onto a grid of
parallel lines with distance DL apart intersects a line is 2L/(D*).
c*** loop over trials
hit = 0
do it=1,itests
x0 = float(N)*D*rand()
k = int(x0/D)
x1 = x0 - D*float(k)
x2 = D - x1
x = min(x1,x2)
dx = 0.5*abs(L*cos(1.0*pi*rand()))
if(dx.ge.x) hit = hit + 1
end do
c*** average number of hits
ahit = float(hit)/float(itests)
buffon = (2*L)/(pi*D)

-1

-1
-1

-1

86

Buffon problem for D=1


enter numbers of tests
10000
enter numbers of intervals in the grid
10
enter the needle size L<1
0.5
hit
=
3.157E-01
buffon =
3.183E-01
Buffon problem for D=1
enter numbers of tests
100000
enter numbers of intervals in the grid
50
enter the needle size L<1
0.9
hit
=
5.717E-01
buffon =
5.730E-01

87

Example
investigate a simple problem that generated much attention several years
ago and for which many mathematicians obtained an incorrect solution. The
problem was the analysis of the optimal strategy in a television game show
popular at the time. The show was Lets Make a Deal with host Monty Hall.
At some point in the show, a contestant was given a choice of selecting one
of three possible items, each concealed behind one of three closed doors.
The items varied considerably in value. After the contestant made a choice
but before the chosen door was opened, the host, who knew where the
most valuable item was, would open one of the doors not selected and
reveal a worthless item. The host would then offer to let the contestant
select a different door from what was originally selected. The question, of
course, is should the contestant switch? A popular magazine writer Marilyn
vos Savant concluded that the optimal strategy is to switch. This strategy is
counterintuitive to many mathematicians, who would say that there is
nothing to be gained by switching; that is, that the probability of improving
the selection is 0.5. Study this problem by Monte Carlo methods. What is
the probability of improving the selection by switching? Be careful to
understand all of the assumptions, and then work the problem analytically
89
also. (A Monte Carlo study is no substitute for analytic study.)

88

Lets make a deal


c*** loop over trials
enter numbers of tests
win1 = 0
10000
win2 = 0
win1 =
3.359E-01
do it=1,itests
win2 =
6.641E-01
a(1) = rand()
a(2) = rand()
a(3) = rand()
choice = 1 + int(3.0*rand())
b(1) = a(choice)
if(choice.eq.1) b(2) = max(a(2),a(3))
if(choice.eq.2) b(2) = max(a(1),a(3))
if(choice.eq.3) b(2) = max(a(1),a(2))
if(b(1).ge.b(2)) then
win1 = win1 + 1
else
win2 = win2 + 1
end if
end do
c*** average number of games and wins
awin1 = float(win1)/float(itests)
awin2 = float(win2)/float(itests)
write (*,101) awin1, awin2
90

15

write (*,*)'enter numbers of tests, money and


goal'
read (*,*) itests, money1, money2

Example
The gambler's ruin problem. Suppose that a person decides to try to
increase the amount of money in his/her pocket by participating in some
gambling. Initially, the gambler begin with $m in capital. The gambler
decides that he/she will gamble until a certain goal, $n (n>m), is achieved
or there is no money left (credit is not allowed). On each throw of a coin
(roll of the dice, etc.) the gambler either win $1 or lose $1. If the gambler
achieves the goal he/she will stop playing. If the gambler ends up with no
money he/she is ruined.
What are chances for the gambler to achieve the goal as a function of k,
where k=n/m?
How long on average will it take to play to achieve the goal or to be
ruined?
91

The gambler`s ruin problem.


Chances to reach certain goal
enter numbers of tests, money and goal
10000
10
100
tests:
initial:
goal:
win
=
loose =
games =

c*** loop over trials


total = 0
wins = 0
do it=1,itests
x=money1
games=0
do while(x.gt.0.and.x.lt.money2)
games = games + 1
luck = 1
if(rand().le.0.5) luck=-1
x = x+luck
end do
total = total+games
if(x.gt.0) wins = wins+1
end do
c*** average number of games and wins
agames = float(total)/float(itests)
awins = float(wins)/float(itests)
aloose = 1.0-awins
write (*,100) itests, money1, money2
write (*,101) awins, aloose, agames

92

The gambler`s ruin problem.


Chances to reach certain goal
enter numbers of tests, money and goal
100000
10
100

10000
10
100
1.026E-01
8.974E-01
9.019E+02

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

chance to win in each bet 50/50

chance to win in each bet 49/51


93

Example
An industrious physics major finds a job at a local fast food restaurant to
help him pay his way through college. His task is to cook 20 hamburgers
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
not pay attention to whether the hamburger is cooked or not. His method
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
ready.

95

94

What is the distribution of cooking times of the hamburgers that he


removes?
What is a chance for a customer to get a well cooked hamburger if it
takes 5 minutes to cook a hamburger.
Does the answers to the first two questions change if he cooks 40
hamburgers at any one time?

Comment: For simplicity, assume that he replaces a hamburger at a


regular interval of 30 seconds and there is an indefinite supply of
uncooked hamburgers.

96

16

nburger = 20
tsteps = 100000
bmin
bmax

= 10
= 20

c initialization (burgers)
do i = 1, nburger
b(i) = 0
end do
c initialization (time distribution)
do i = 1, tsteps
tcook(i) = 0
end do
c other variables
cook1 = 0
cook2 = 0
cook3 = 0
tmax = 0
c initial seed number calculated from current time
call gettim(ihour,imin,isec,imsec)
init = imsec*isec*imin+ihour
call srand(init)

97

do i = 1, tsteps
x = 1.0 + rand()*real(nburger)
burger = int(x)
do j = 1, nburger
if(j.eq.burger) then
tcook(b(j)+1) = tcook(b(j)+1) + 1
if(b(j)+1.gt.tmax) tmax = b(j)+1
if(b(j).lt.bmin) cook1 = cook1 + 1
if(b(j).ge.bmin.and.b(j).le.bmax)
cook2 = cook2 + 1
if(b(j).gt.bmax) cook3 = cook3 + 1
b(j) = 0
else
b(j) = b(j) + 1
end if
end do
end do
write(*,102) tmax
c calculate and write chances for undercooked, good c
cooked, overcooked burgers
rcook1 = real(cook1)/real(cook1+cook2+cook3)
rcook2 = real(cook2)/real(cook1+cook2+cook3)
rcook3 = real(cook3)/real(cook1+cook2+cook3) 98
write(*,101) rcook1, rcook2, rcook3

for 100,000 burgers

60

20 burgers on the grill


max cooking time =
237
undercooked = 0.39941001
well cooked = 0.25903001
over cooked = 0.34156001

grill for 20 burgers


burgers cooked = 1,000

50

40

burgers

40 burgers on the grill


max cooking time =
463
undercooked = 0.22596000
well cooked = 0.18769000
over cooked = 0.58635002

30

20

10

0
0

10

20

99

30

40

50

60

70

80

90

100

cooking time (min)

100

Applications of Monte-Carlo simulations

6000

grill for 20 burgers


burgers cooked = 100,000

5000

9 integration
9 statistical physics
9 aerodynamic

4000

9 quantum chromodynamics

burgers

9 molecular dynamic simulation


9 experimental particle physics

3000

9 cellular automata
9 percolation

2000

9 radiation field and energy transport


9
9 Finance and business

1000

0
0

10

20

30

40

50

60

70

cooking time (min)

80

90

100
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


Horton Conway in 1970. Life is an example of emergence and selforganization - complex patterns can emerge from the
implementation of very simple rules.
103

What is a chance to
encounter a bear?
What data do you need
for your simulation?
104

18

You might also like