You are on page 1of 26

'

Physics 75.502 Boundary Value Problems

As an example, the potential within a square cavity where


$'
138 Physics 75.502 Part III: Monte Carlo Methods
$
139

one side is held at a constant potential, and the others held Part III: Monte Carlo Methods
at 0, is shown below. The starting point for each method is
potential=0 for all interior points:
n=0 n=10 n=20 n=30 Topics:
Jacobi

Introduction
Random Number generators
Special distributions
SOR_w=1

General Techniques
Multidimensional simulation
References:
SOR_w=1.8 SOR_w=1.5

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.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Part III: Monte Carlo Methods

Monte Carlo Techniques


$'
140 Physics 75.502 Part III: Monte Carlo Methods

In a uniform distribution of random numbers in


$
141

the range [0,1] , every number has the same


chance of turning up.

Monte Carlo refers to any procedure that Note that 0.00001 is just as likely
makes use of random numbers. as 0.50000

Monte Carlo methods are used in:


How to generate a sequence of random numbers.
Simulation of natural phenomena
Simulation of experimental appartus Use some chaotic system. (like balls in a
Numerical analysis barrel - Lotto 6-49).
Use a process that is inherently random:
Random Numbers radioactive decay
thermal noise
What is a random number? Is 3? cosmic ray arrival
Tables of a few million truely random
No such thing as a single random number. numbers do exist, but this isn’t enough for
most applications.
A sequence of random numbers is a set of

& %& %
numbers that have nothing to do with the other Hooking up a random machine to a
numbers in the sequence. computer is not a good idea. This would
lead to irreproducable results, making
debugging difficult.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation

Random Number Generation


$'
142 Physics 75.502 Random Number Generation
$
143

Pseudo-Random Numbers
This algorothm has problems in that the
These are sequences of numbers generated by sequence will have small numbers lumped
computer algorithms, usally in a uniform together, 0 repeats itself, and it can get itself
distribution in the range [0,1]. into short loops, for example:
To be precise, the alogrithms generate integers 61002=37210000
between 0 and M, and return a real value:
21002= 4410000
x n = In / M 41002=16810000
81002=65610000
An early example :
Middle Square (John Von Neumann, 1946) With more bits, long sequences are possible.
To generate a sequence of 10 digit integers, 38 bits 750,000 numbers
start with one, and square it amd then take
the middle 10 digits from the answer as the
next number in the sequence.

eg. 57721566492=33317792380594909291 A more complex algorithm does not


necessarily lead to a better random sequence.
so the next number is given by It is better to use an algorithm that is well
understood.

& %& %
The sequence is not random, since each
number is completely determined from the
previous. But it appears to be random.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation
$'
144 Physics 75.502 Random Number Generation
$
145

Linear Conguential Method (Lehmer, 1948) Choice of modulus, m


m should be as large as possible since the period
I n+1 = (a I n + c) mod m can never be longer than m.

Starting value (seed) = I0 One usually chooses m to be near the largest


integer that can be represented. On a 32 bit
a, c, and m are specially chosen machine, that is 231 ≈ 2×109.
a, c ≥ 0 and m > I0, a, c

A poor choice for the constants can lead to very Choice of multiplier, a
poor sequences.

example: a=c=Io=7, m=10


It was proven by M. Greenberger in 1961
results in the sequence: that the sequence will have period m, if and
7, 6, 9, 0, 7, 6, 9, 0,... only if:
i) c is relatively prime to m;
The choice c=0 leads to a somewhat faster ii) a-1 is a multiple of p, for every prime
algorithm, and can also result in long

& %& %
sequences. The method with c=0 is called:
p dividing m;
Multiplicative congruential. iii) a-1 is a multiple of 4, if m is a
multiple of 4

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation
$'
146 Physics 75.502 Random Number Generation
$
147

With c=0, one cannot get the full period, but in Results from Randu: 1D distribution
order to get the maximum possible, the
following should be satisfied:

i) I0 is relatively prime to m 500


ii) a is a primative element modulo m
It is possible to obtain a period of length m-1,
400
but usually the period is around m/4.

300
RANDU generator

200
A popular random number generator was
distributed by IBM in the 1960’s with the
algorithm:
100

In+1 = (65539 × In) mod 2 31

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
This generator was later found to have a

& %& %
Random number
serious problem...
Looks okay

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation
$'
148 Physics 75.502 Random Number Generation
$
149

Results from Randu: 2D distribution


Results from Randu: 3D distribution
1

0.9

1
0.8
0.8 1
0.6 0.8
0.7 0.4 0.6
0.2 0.4
1
0.6 0.2 0.75
1
0.75 0.5
1 0.5
0.75 0.25
0.5 0.25
0.5 0.25 0 1 0.75 0.5 0
0.25

0.4
1
0.8 1
0.3
0.6 0.8
0.4 0.6
0.2 0.2 0.4
0.2
0.1 1
0.75 1
0.5 0.75 0.25
0.25 0.5 0.5
0 0.25 0.25 0.75
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.75 0.5 0 1
1

& %& %
Problem seen when observed at the right angle!
Still looks okay

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation
$'
150 Physics 75.502 Random Number Generation
$
151

The Marsaglia effect


In 1968, Marsaglia published the paper,
Warning
Random numbers fall mainly in the planes

(Proc. Acad. Sci. 61, 25) which showed that this


behaviour is present for any multiplicative
congruential generator.
The authors of Numerical Recipies have
admitted that the random number
generators, RAN1 and RAN2 given in the
first edition, are “at best mediocre”.
For a 32 bit machine, the maximum number of
hyperplanes in the space of d-dimensions is: In their second edition, these are replaced by
ran0, ran1, and ran2, which have much
d= 3 2953 better properties.
d= 4 566
d= 6 120 The new routines can also be found in the
d=10 41
recent edition of Computers in Physics,
(Sept/Oct 1992 edition, page 522).

The RANDU generator had much less than the

& %& %
maximum.
The replacement of the multiplier from 65539 to
69069 improves the performance signifigantly.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation

One way to improve the behaviour of random


$'
152 Physics 75.502 Random Number Generation

Warning on the use of Random


$
153

number generators and to increase their period is Number generators.


to modify the algorithm:

In = (a×In-1 + b×In-2) mod m


In FORTRAN, random number generators
Which in this case has two initial seeds and can areusually used called as functions,
have a period greater than m.
x=RAND(IDUM)

Where the arguement, IDUM, is not used. In


RANMAR generator fortran, a function is supposed to be a function
of only the arguments, and so some compilers
will try to optimise the code by removing
multiple calls to random number generators.
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. For example

Each choice will generate an independat series x=RAND(IDUM)+RAND(IDUM)


each of period, ≈ 1043.

& %& %
may be changed to
This seems to be the ultimate in random
number generators! x=2.*RAND(IDUM)

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Random Number Generation

This can also be a problem when the


$'
154 Physics 75.502 Simulating Radioactive Decay

Simulating Radioactive Decay


$
155

random number generator is called


inside DO loops. This is a truly random process: The probability of decay is
constant (independent of the age of the nuclei).
The probability that a nucleus undergoes radioactive decay
Solution: in time t is p:
p = t (for t  1)
Fool the optimiser by always changing
the dummy argument:
Problem:
Consider a system initially having N0 unstable nuclei. How
DO 1 I=1,100 does the number of parent nuclei, N , change with time?
IDUM=IDUM+1 Algorithm:
x=RAND(IDUM) LOOP from t=0 to t, step t
... LOOP over each remaining parent nucleus
1 CONTINUE Decide if the nucleus decays:
IF(random # < t) then
reduce the number of parents by 1
But don’t try this if the random number ENDIF
generator uses the argument to save the END LOOP over nuclei

& %& %
seed for the next random number. PLOT or record N vs. t
(Numerical Recipies generators, for END LOOP over time
example)! END

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Simulating Radioactive Decay
$'
156 Physics 75.502 Simulating Radioactive Decay
$
157

Solution to exercise 6:
The 'experimental' results do not perfectly follow the
expected curve there are statistical uctuations.
Exercise 6
Write a program to implement the preceding algorithm. 100 5000

Graph the number of remaining nuclei as a function of time


90 4500
80 4000

for the following cases: 70


60
3500
3000

N0 = 100 = 0:01 s;1 t = 1 s 


50 2500
40 2000

= 0:03 s;1 t = 1 s :
30 1500
N0 = 5000 20 1000
10 500
0 0
0 100 200 300 0 100 200 300

Show the results on both linear and logarithmic scales for N0=100, α=0.01 N0=5000, α=0.03

times between 0 and 300 seconds. In addition, plot on the 10 2

same graphs the expected curve, given: 10 3

dN = ;N dt 10
10 2

ie : N = N0 e; t 10

& %& %
1 1
0 100 200 300 0 100 200 300

N0=100, α=0.01 N0=5000, α=0.03

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Poisson Distribution

Poisson Distribution
$'
158 Physics 75.502 Poisson Distribution
$
159

The probability of observing a total of n decays in a time P = pn (1 ; p)m;n (m ;mn! )! n!


interval T can be worked out as follows:
T n T m;n m!
= m 1; m (m ; n)! n!
Assume the number of decays in time T is much less than
the number of parent nuclei. (ie. assume constant
probability to observe a decay):
Break up T into m shorter intervals, duration t:
In the limit of t ! 0 (ie. m ! 1),
m
t
T
1; m ! e;T
T ;n
T 1; m ! 1
The probability to observe 1 decay in time t is: m! ! mn
(m ; n)!
p =  t
where  = N as t must be small enough so that The result is,
 t  1. The probability of observing n decays in time T P = n e; =n!

& %& %
is therefore: 
n m ; n m where  = T . This is known as the Poisson distribution.
P = p (1 ; p) n :

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Poisson Distribution
$'
160 Physics 75.502

Solution to Exercise 7:
Poisson Distribution
$
161

320

Exercise 7
280

240

Modify the program written for exercise 6 to simulate an 200

experiment that counts the number of decays observed in a 160

time interval, T . 120

Allow the experiment to be repeated and histogram the 80

distribution of the number of decays for the following two 40

cases: 0
0 1 2 3 4 5 6 7 8 9

N0 = 500 = 4  10;5 s;1 t = 10 s T = 100 s


N0=500, alpha=4e-5, T=100

140

N0 = 500 = 2  10;4 s;1 t = 10 s T = 100 s 120

100

In each case show the distribution using 1000 experiments.


Also, overlay the expected Poisson distribution.
80

Question: Are there limits on the value of t so that your


60

program will give reliable results? Explain. 40

& %& %
20

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

N0=500, alpha=2e-4, T=100

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Properties of the Poisson distribution

Properties of the Poisson distribution


$'
162 Physics 75.502 Properties of the Poisson distribution
$
163

n Do each term individually,


Pn = n! e; ( = NT ) 1
X n e; = X
n2
1  n;1
;
n (n ; 1)! e 

n=0 n! n=1
Mean value: X1 n 
1
X  n  = (n + 1) n! e; 
hni = n n! e ;  n=0
n=0 = ( + 1)
X1 n;1 
= e ;  1
X n e; = ;22
n=1 (n ; 1)! ;2n
n=0 n!
X1 m
= e ;  = X1 n 
m=0 m! 2 n! e; = 2
n=0
So,  =  +  ; 22 + 2 =  .
2 2

Variance:
1
X 
2 = (n ; )2 n e; Hence if n decays are observed, the 1 standard deviation
n=0 n! p
uncertainty is n. (This is also true for any other variable
1
X n 
(n2 ; 2n + 2 ) 

& %& %
= e; that follows the Poisson distribution.)
n=0 n!

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Properties of the Poisson distribution
$'
164 Physics 75.502 Gaussian (or Normal) Distribution
$
165

Many observables follow the Poisson distribution: Anything


whose probability of occurring is constant in time.
For example: Gaussian (or Normal) Distribution
number of observed events when eciency is constant
This is the most important distribution in statistical
number of entries in a histogram bin analysis.
G(xj  ) = p 1 e; 22
(x; )2
2
Some measurements lead to non-Poisson distributions: The mean of the distribution is  and the variance is  2.
For example:
number of radioactive decays observed in a xed time
interval, when there is a signicant reduction of parent For large , the Poisson distribution approaches the
nuclei Gaussian distribution (with 2 = ).
number of radioactive decays observed in a xed time The Gaussian distribution is a reasonable approximation of
interval, when there is signicant deadtime. (ie. the the Poisson distribution even for  as small as 5.
detector is not active for some period after an event is
recorded)

&
Dean Karlen/Carleton University Rev. 1.3
%&
1998/99 Dean Karlen/Carleton University Rev. 1.3
%
1998/99
'
Physics 75.502 Gaussian (or Normal) Distribution
$'
166 Physics 75.502 Binomial Distribution
$
167

Comparison of Poisson and Gaussian distributions:

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

P = pn (1 ; p)m;n m :
n

The mean of this distribution is: np


The variance of this distribution is: np(1 ; p)

&
Dean Karlen/Carleton University Rev. 1.3
%&
1998/99 Dean Karlen/Carleton University Rev. 1.3
%
1998/99
'
Physics 75.502 Simulating General Distributions

Simulating General Distributions


$'
168 Physics 75.502 Rejection Technique
$
169

Rejection Technique
The simple simulations considered so far, only required a
random number sequence that is uniformly distributed Problem: Generate a series of random numbers, xi , which
between 0 and 1. More complicated problems generally follow a distribution function f (x).
require random numbers generated according to specic
distributions.
For example, the radioactive decay of a large number of In the rejection technique, a trial value, xtrial is chosen at
nuclei (say 1023), each with a tiny decay probability, cannot random. It is accepted with a probability proportional to
be simulated using the methods developed so far. It would f (xtrial ).
be far too inecient and require very high numerical
precision.
Instead, a random number generated according to a Poisson Algorithm:
distribution could be used to specify the number of nuclei Choose trial x, given a uniform random number 1:
that disintigrate in some time T .
Random numbers following some special distributions, like xtrial = xmin + (xmax ; xmin) 1
the Poisson distribution, can be generated using special Decide whether to accept the trial value:
purpose algorithms, and ecient routines can be found in
various numerical libraries. if f (xtrial ) > 2 fbig then accept

& %& %
If a special purpose generator routine is not available, then where fbig  f (x) for all x, xmin  x  xmax. Repeat the
use a general purpose method for generating random algorithm until a trial value is accepted.
numbers according to an arbitrary distribution.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Rejection Technique
$'
170 Physics 75.502 Rejection Technique
$
171

The 1 standard deviation uncertainty can be derived using


the variance of the binomial distribution:
This algorithm can be visualized as throwing darts:

f big
p
Naccept = p(1 ; p)Ntrial p = NNaccept
trial

f(x)

2 
Naccept 2
I =
I Naccept

x min x max
= Naccept 1 ; Naccept N 1
Ntrial Ntrial trial 2
Naccept
This procedure also gives an estimate of the integral of f (x): = 1 ; 1
Z xmax Naccept Ntrial
I= f (x) dx  nnaccept fbig (xmax ; xmin) 1 1;p

xmin trial = Ntrial p

& %& %
;2
So the relative accuracy only improves with Ntrial
1

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Rejection Technique
$'
172 Physics 75.502 Inversion Technique

Inversion Technique
$
173

The rejection algorithm is not ecient if the distribution This method is only applicable for relatively simple
has one or more large peaks (or poles). distribution functions:
In this case trial events are seldomly accepted: First normalize the distribution function, so that it
becomes a probability distribution function (PDF).
f big Integrate the PDF analytically from the minimum x to
an aritrary x. This represents the probability of chosing
a value less than x.
Equate this to a uniform random number, and solve for
f(x) x. The resulting x will be distributed according to the
PDF.
In other words, solve the following equation for x, given a
uniform random number, :
x min x max Zx
In extreme cases, where there is a pole, fbig cannot be x
f (x) dx
specied. This algorithm doesn't work when the range of x
Z xmax
min =
f (x) dx

& %& %
is (;1 +1). A better algorithm is needed... xmin
This method is fully ecient, since each random number
gives an x value.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Inversion Technique
$'
174 Physics 75.502 Inversion Technique
$
175

Examples of the inversion technique

1) generate x between 0 and 4 according to f (x) = x; 21 : Exercise 8


R x ; 12
0R x dx dx = Write a program that generates the value according to the
4 ;1 distribution function:
0x 2
1 1 f ( ) = (sin2 + a cos2 );1
2x2 =

) generate x according to x = 4 2 in the range 0   2.


Compare the rejection technique and the inversion
2) generate x between 0 and 1 according to f (x) = e;x: technique:
R x ;x Generate 10000 values for each method using a = 0:5
R 01 e ;xdx = and also for a = 0:001.
0 e dx
1 ; e;x = Plot the results for each (4 plots) and overlay the
) generate x according to x = ; ln(1 ; )
distribution curve, f ( ), properly normalized.
Compare the CPU time required for the 4 runs.
Note that the simple rejection technique would not work for

& %& %
either of these examples.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502

Solution to Exercise 8:
Inversion Technique
$'
176 Physics 75.502 Inversion Technique

What if the rejection technique is


$
177

impractical and you can’t invert the


integral of the distribution function?
160

140 Importance Sampling


120

100 Replace the distribution function, f(x), by an


80
approximate form, f a(x), for which the inversion
60
technique can be applied.
40
Generate trial values for x with the inversion
20
technique according to f a(x), and accept the trial
0
0 1 2 3 4 5 6
value with the probability proportional to the
theta weight:
w = f(x) / f a(x)
1800

1600

1400

1200
f a(x)
1000

800

600

& %& %
400

200

The rejection technique is just the special case


where f a(x) is chosen to be a constant.
0
0 1 2 3 4 5 6

theta

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Inversion Technique
$'
178 Physics 75.502 Inversion Technique
$
179

Example: Note: the integral can be evaluated as before


Z xmax
Generate x according to f(x) = (1+x)x -1/2
I= f (x) dx = nnaccept wmax Ia
for the range 0 < x < 1. xmin trial
R xmax a
where Ia = xmin f (x) dx.
There is clearly a problem at x near 0.
But the integral can be found more eciently (ie. more
f a(x) needs to be chosen so the weights for the accuratly for the same amount of CPU), by using the
trial values of x are well behaved, weights of all trial values:
Z xmax Z xmax f (x)
w = f(x)/f a(x) I= f (x) dx = fa (x) dx
xmin xmin f a (x)
Z xmax
Try f a(x) = x -1/2, then w=1+x
= w(x)f a(x) dx
xmin
Rx a
Procedure: But, xmin f (x) dx=Ia = , so f a(x)dx = Ia d
x = λ21 Z1 X
I = w( )Ia d = Ia n 1
Generate trial x:
w = Iahwi
Decide to accept: if (1+x) > λ2 wmax accept 0 trial i
And the one standard deviation uncertainty is,
In this case, wmax=2, but in more complicated cases,
I 2 = 1 hw2i ; hwi2

& %& %
you may need to run the program to find the
maximum weight generared, and then pick a value a I ntrial h w i2
little larger, and rerun.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Inversion Technique

Generating random numbers according to the


$'
180 Physics 75.502

2) 2 D gaussian
Inversion Technique
$
181

Gaussian distribution.

There are 2 ways to handle this special case.

1) Central limit theorem


θ

“The sum of a large number of random numbers θ


will approach a Gaussian distribution”
θ θ θ

For a uniform istribution from 0 to 1,


the mean value is 1/2 θ θ θ

and the variance is ∞


θ π
σ 2= ⌠ 2
⌡ (x-1/2) dx = 1/12
λ
So just add 12 random numbers and subtract 6. θ πλ
The mean will be 0 and the variance will be 1. θ
θ

& %& %
This algorithm is coded in RG32 in the
CERN library.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Multidimensional Simulation

Multidimensional Simulation
$'
182 Physics 75.502 Multidimensional Simulation

Simulation of Compton Scattering


$
183

k’
k θ
Simulating a distribution in more than 1
dimension: -

If the distribution is separable, the variables are


uncorrelated, and hence each can be generated
The energy of the nal state photon is given by
as before: k
k0 = 1 + (k=m)(1 ; cos )
For example, if f(x,y) = g(x) h(y)

then generate x according to g(x) and y The dierential cross section is:
according to h(y).  
d = 2 k0 2 k0 + k ; sin2
d 2m2 k k k0
Otherwise, the distributions along each
dimension needs to be calculated:
O. Klein, Y. Nishina, Z. Physik, 52, 853 (1929)
The angular distribution of the photon is:
Dx(x) = ⌠
⌡ f(x,y) dy 2
(  ) d d = 2m2 
Typically, you will need to choose an  0 3 !
approximation of the distribution, f a(x,y) so the
integrals, ⌠⌡ f a(x,y)dx and ⌠⌡ f a(x,y)dy are
k + k0  ; k0 2 sin2 sin d d
k k k

& %& %
invertable. The weights for trial events are given
by, w = f(x,y) / f a(x,y) and the integral can be
evaluated as before, using the weights of all trial
events. (Event = x and y pair) The azimuthal angle, , can be generated independantly
from , by simply: = 2 1.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Multidimensional Simulation
$'
184 Physics 75.502 Multidimensional Simulation
$
185

To generate the polar angle, , an approximation is needed.


Note that for k
m, the cross section is sharply peaked at
small angles. Also, note that k0 < k, so the second term is Exercise 9
the dominant term in the cross section formula.
A good approximation to the cross section is,
a(  ) d d =
2 k0  sin d d Write a Monte Carlo program that generates
2m2 k Compton scattering events.
2 k ;1 The program should ask for the number of events
= 2m2 1+ m u du d to generate and the photon energy. Show the
distribution of the scattering angle of the photon
where u = (1 ; cos ). (compared to the Klein Nishina formula) and give
the total cross section (ie. use the same program to
u is generated according to: evaluate the integral and its uncertainty) for the
" 2 #
u = mk k following four cases:
1 + 2m ; 1
k=5 keV, k=2 MeV, k=1 GeV, k=1 TeV

Be careful when k  m this procedure would not generate in each case generate 10000 events.
u properly, due to roundo errors. Similarly, it is much
better to generate u = (1 ; cos ) than cos , when there is a

& %& %
pole at = 0.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502

Solution to Exercise 9:
Multidimensional Simulation
$'
186 Physics 75.502 Multidimensional Simulation
$
187

xsec=6.525+/-0.012 x 10-25 cm**2


Photon transport in matter
160

140
With this program, and others that simulate the
120
photoelectric effect, pair production, etc., you
100 could produce a program that simulates the
80 interaction of photons with matter:
60

40

20

0
Algorithm:
0 0.4 0.8 1.2 1.6 2 2.4 2.8

angular dist 5 KeV

xsec=1.117+/-0.001 x 10-27 cm**2


Break path into small steps:
1200
For each step decide if an interaction takes place
1000
(given the total cross section for each possible
interaction).
800

Simulate the interaction, ie. give photon new


momentum vector or possibly produce an e+e-
600

400 pair, which then would be followed, etc.

& %& %
200

0
0 0.4 0.8 1.2 1.6 2 2.4 2.8

angular dist 1 GeV

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99
'
Physics 75.502 Multidimensional Simulation

Such programs already exist. For example:


$'
188 Physics 75.502 Multidimensional Simulation
$
189

For example:
EGS (SLAC) Once ε is known,
GEANT (CERN) select events that are
ε missed with a
probabilty of ε:
You may use these to simulate photon transport If λ>ε then event is
in a particular sample you are testing or to not observed.
simulate the response of your detector. E

Resolution, offset

Detector response Again, these can be


measured using well Eres
understood sources:
It is often sufficient to simulate the general
properties of your detector: efficiency, resolution,
bias, offset. Eoffset Emeas - Etrue
Emeas = Etrue + Eres Gλ + Eoffset
Efficiency Gaussian random number
Background, noise

& %& %
From measurements from well understood
Simulate the observed energy distribution
sources, the effiency as a function of energy when no source is present.
(and maybe position) can be found.

Dean Karlen/Carleton University Rev. 1.3 1998/99 Dean Karlen/Carleton University Rev. 1.3 1998/99

You might also like