Professional Documents
Culture Documents
Part III: Monte Carlo Methods
Part III: Monte Carlo Methods
Part III: Monte Carlo Methods
Physics 75.502
n=20
Rev. 1.3
Topics:
Introduction
Random Number generators
Special distributions
General Techniques
Multidimensional simulation
References:
The Art of Computer Programming, D.E. Knuth,
Addison-Wesley, vol 2, 1969.
Monte Carlo Theory and Practice, F. James, Rep.
Prog. Phys., Vol. 43, 1980, 1145.
Portable Random Number Generators, W.H. Press,
S.A. Teukolsky, Computers in Physics, Vol. 6, No. 5,
1992, 522.
n=30
SOR_w=1
SOR_w=1.8 SOR_w=1.5
&
Physics 75.502
139
Jacobi
n=0
$'
138
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
140
Physics 75.502
Random Numbers
What is a random number? Is 3?
&
Rev. 1.3
141
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
142
Pseudo-Random Numbers
Physics 75.502
143
61002=37210000
21002= 4410000
41002=16810000
81002=65610000
x n = In / M
An early example :
Middle Square (John Von Neumann, 1946)
eg. 57721566492=33317792380594909291
so the next number is given by
&
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
144
Physics 75.502
I n+1 = (a I n + c) mod m
m > I0, a, c
Choice of multiplier, a
&
145
Choice of modulus, m
a, c 0
Rev. 1.3
%&
1998/99
i) c is relatively prime to m;
ii) a-1 is a multiple of p, for every prime
p dividing m;
iii) a-1 is a multiple of 4, if m is a
multiple of 4
Rev. 1.3
1998/99
'
Physics 75.502
$'
146
Physics 75.502
147
500
400
300
RANDU generator
200
100
31
&
%&
0.1
0.2
0.3
0.4
0.5
0.6
0.7
Random number
Looks okay
Rev. 1.3
1998/99
Rev. 1.3
0.8
0.9
1998/99
'
Physics 75.502
$'
148
Physics 75.502
149
0.9
1
0.8
0.7
0.8
0.6
0.8
0.4
0.6
0.2
0.4
0.6
1
1
1
0.75
0.5
0.25
0.2
0.75
0.75
0.5
0.5
0.5
0.25
0.25
0.75
0.5
0.25
0.4
1
0.3
0.2
0.8
0.6
0.8
0.4
0.6
0.2
0.4
0.2
0.1
1
0.75
0.5
0.25
&
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.9
%&
0.75
0.5
0.25
0.75
0.5
0.25
0.25
0.5
0 1
0.8
Rev. 1.3
1998/99
Rev. 1.3
0.75
1998/99
'
Physics 75.502
$'
150
Physics 75.502
151
Warning
2953
566
120
41
&
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
152
Physics 75.502
153
x=RAND(IDUM)
RANMAR generator
This generator (available in the CERN library,
KERNLIB, requires 103 initial seeds. These
seeds can be set by a single integer from 1 to
900,000,000.
Each choice will generate an independat series
each of period, 1043.
&
Rev. 1.3
For example
x=RAND(IDUM)+RAND(IDUM)
%&
1998/99
may be changed to
x=2.*RAND(IDUM)
Rev. 1.3
1998/99
'
Physics 75.502
$'
154
Rev. 1.3
155
Problem:
DO 1 I=1,100
IDUM=IDUM+1
x=RAND(IDUM)
...
1 CONTINUE
&
Solution:
Physics 75.502
Algorithm:
t
%&
PLOT or record
vs.
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
156
Physics 75.502
157
Solution to exercise 6:
Exercise 6
N0 = 100
= 0:01 s;1 t = 1 s
N0 = 5000
= 0:03 s;1 t = 1 s :
100
5000
90
4500
80
4000
70
3500
60
3000
50
2500
40
2000
30
1500
20
1000
10
500
100
200
300
100
200
300
N0=5000, =0.03
10 2
10 3
10 2
10
N = N0 e; t
10
%&
1
Rev. 1.3
N0=100, =0.01
dN = ;N dt
&
1998/99
100
200
N0=100, =0.01
300
100
200
N0=5000, =0.03
Rev. 1.3
300
1998/99
'
Physics 75.502
$'
Poisson Distribution
158
Poisson Distribution
Physics 75.502
Poisson Distribution
P = pn (1 ; p)m;n (m ;mn! )! n!
n
m;n
T
m!
T
1; m
= m
(m ; n)! n!
m
T
1; m
;n
T
1; m
m!
(m ; n)!
p = t
where = N as t must be small enough so that
t 1. The probability of observing n decays in time T
is therefore:
m
n
m
;
n
P = p (1 ; p)
n :
&
Rev. 1.3
159
e;T
mn
P = n e; =n!
where = T . This is known as the Poisson distribution.
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
Poisson Distribution
160
Physics 75.502
Poisson Distribution
161
Solution to Exercise 7:
320
Exercise 7
280
240
N0 = 500
N0 = 500
200
160
120
80
40
140
120
100
&
Rev. 1.3
80
60
40
%&
1998/99
20
10
12
14
16
18
20
22
24
26
Rev. 1.3
28
1998/99
'
Physics 75.502
Mean value:
&
( = NT )
=
=
n=0
1
X
n=0
(n ; )2
n e;
n!
n
(n2 ; 2n + 2 )
n!
Rev. 1.3
e;
163
1
n;1
n e; = X
;
n (n ; 1)! e
n!
n=1
n=0
1
n
X
=
(n + 1) n! e;
n=0
= ( + 1)
1
X
n e; = ;22
;2n
n!
n=0
1
n
X
2 n! e; = 2
n=0
2
2
So, = + ; 22 + 2 = .
1
X
n
;
hni =
n n! e
n=0
1 n;1
X
;
= e
n=1 (n ; 1)!
1 m
X
;
=
= e
m=0 m!
1
X
Physics 75.502
1
X
Variance:
2
$'
162
n2
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
164
&
Rev. 1.3
Physics 75.502
165
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
166
Physics 75.502
Binomial Distribution
167
Binomial Distribution
The binomial distribution describes the results of repeated
experiments which has only two possible outcomes.
Suppose a radioactive source is monitored for a time
interval T . There is a probability p that one or more
disintegrations would be detected in that time interval. If a
total of m intervals were recorded, the probability that n of
them had at least one decay is
= pn (1 ; p)m;n
m :
n
&
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
168
&
Rev. 1.3
Physics 75.502
Rejection Technique
169
Rejection Technique
Problem: Generate a series of random numbers, xi , which
follow a distribution function f (x).
In the rejection technique, a trial value, xtrial is chosen at
random. It is accepted with a probability proportional to
f (xtrial ).
Algorithm:
Choose trial x, given a uniform random number 1:
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
Rejection Technique
170
Physics 75.502
Rejection Technique
171
p
Naccept = p(1 ; p)Ntrial
p = NNaccept
trial
f(x)
I
I
x min
x max
I=
&
xmin
=
=
Z xmax
2
%&
Naccept 2
Naccept
1
Naccept 1 ; Naccept N
trial
2
Ntrial
Ntrial
Naccept
1 ; 1
Naccept Ntrial
1 1;p
Ntrial p
;2
So the relative accuracy only improves with Ntrial
Rev. 1.3
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
Rejection Technique
172
f big
f(x)
x max
&
Inversion Technique
173
Inversion Technique
x min
Physics 75.502
%&
Zx
f (x) dx
x
min
Z xmax
f (x) dx
xmin
Rev. 1.3
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
Inversion Technique
174
Physics 75.502
Inversion Technique
175
Exercise 8
R x ; 12
0R x dx dx =
4 ;1
0x 2
1 1
2x2 =
) generate x according to x = 4 2
R x ;x
R 01 e ;xdx =
0 e dx
1 ; e;x =
) generate x according to x = ; ln(1 ; )
Note that the simple rejection technique would not work for
either of these examples.
&
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
Inversion Technique
176
Solution to Exercise 8:
Physics 75.502
Inversion Technique
177
160
Importance Sampling
140
120
100
80
60
40
20
theta
1800
1600
1400
f a(x)
1200
1000
800
600
&
400
200
theta
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
Inversion Technique
$'
178
179
Z xmax
I=
f (x) dx = nnaccept wmax Ia
trial
xmin
R xmax a
where Ia = xmin f (x) dx.
But the integral can be found more eciently (ie. more
accuratly for the same amount of CPU), by using the
weights of all trial values:
Z xmax
Z xmax f (x)
fa (x) dx
I=
f (x) dx =
xmin
xmin f a (x)
Z xmax
=
w(x)f a(x) dx
xmin
Rx a
But, xmin f (x) dx=Ia = , so f a(x)dx = Ia d
Z1
X
w = Iahwi
I = w( )Ia d = Ia n 1
trial i
0
-1/2
x = 21
Decide to accept:
Inversion Technique
Example:
&
Physics 75.502
Rev. 1.3
%&
1998/99
ntrial
h w i2
Rev. 1.3
1998/99
'
Physics 75.502
Inversion Technique
$'
180
Physics 75.502
Inversion Technique
2) 2 D gaussian
181
2
2=
(x-1/2) dx = 1/12
&
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
Multidimensional Simulation
Multidimensional Simulation
$'
182
Physics 75.502
Multidimensional Simulation
183
k
k
k
k0 = 1 + (k=m)(1
; cos )
d = 2 k0 2 k0 + k ; sin2
d 2m2 k
k k0
O. Klein, Y. Nishina, Z. Physik, 52, 853 (1929)
The angular distribution of the photon is:
f(x,y) dy
&
Rev. 1.3
%&
(
) d d
= 2m2
!
0 3
k + k0 ; k0 2 sin2 sin d d
k
k
k
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
$'
Multidimensional Simulation
184
Physics 75.502
a(
) d d
=
=
2
2m2
2
2m2
"
k0 sin d d
k
;1
k
1+ m u
du d
2 #
k
1 + 2m ; 1
&
Rev. 1.3
185
Exercise 9
where u = (1 ; cos ).
u is generated according to:
u = mk
Multidimensional Simulation
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
Multidimensional Simulation
Solution to Exercise 9:
$'
186
Physics 75.502
Multidimensional Simulation
187
140
120
100
80
60
40
20
Algorithm:
0
0.4
0.8
1.2
1.6
2.4
2.8
1200
1000
800
600
400
&
200
0.4
0.8
1.2
1.6
2.4
2.8
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99
'
Physics 75.502
Multidimensional Simulation
$'
188
Physics 75.502
Multidimensional Simulation
189
For example:
EGS (SLAC)
GEANT (CERN)
Once is known,
select events that are
missed with a
probabilty of :
Resolution, offset
Detector response
Eres
Eoffset
Emeas = Etrue + Eres G + Eoffset
Efficiency
Emeas - Etrue
Background, noise
&
Rev. 1.3
%&
1998/99
Rev. 1.3
1998/99