Download Introduction to the Monte Carlo Method

Document related concepts

Nuclear drip line wikipedia , lookup

P-nuclei wikipedia , lookup

Transcript
Monte Carlo course
Introduction to the Monte Carlo Method
- The analog method -
Pablo Botas
[email protected]
[email protected]
Heidelberg University
Massachusetts General Hospital & Harvard Medical School
May 18, 2017
1 / 45
Monte Carlo course
Outline
1 Introduction
Motivation
Definition
One, two, three!! Analog
method
Fancy examples
History, applications and
simple examples
Examples: Buffon and π
2 Pseudo-random numbers
Linear Congruential
Generator
Recommended RNG
Tests for RNGs
3 Probability density function
sampling
Inverse method
Uniform distribution in
interval [a, b]
Photon step
4 Practical example: proton
simulation
2 / 45
Monte Carlo course
Introduction
Outline
Examples: Buffon and π
1 Introduction
Motivation
Definition
One, two, three!! Analog
method
Fancy examples
History, applications and
simple examples
2 Pseudo-random numbers
3 Probability density function
sampling
4 Practical example: proton
simulation
3 / 45
Monte Carlo course
Introduction
Motivation
Motivation
Linear Boltzmann transport equation, describes the trajectories of
particles in a medium:
Z
Z
p ∂
∂
0
+
+ µ(x, p) ψ(x, p, s) = dx
dp 0 µ(x, p, p 0 )ψ(x 0 , p 0 , s 0 )
∂s
|p| ∂x
where s is the particle path length, µ(x, p, p 0 ) is scattering from
momentum p 0 to p at position x, ψ(x, p, s) dx dp is the probability
of having a particle at x with p and at path length s. A general
solution can be stated as:
Z
Z
ψ(x, p, s) =
dx 0
dp 0 G (x, p, x 0 , s)Q(x 0 , p 0 )
where G (x, p, x 0 , s) is the Green function and Q(x 0 , p 0 ) is a source.
4 / 45
Monte Carlo course
Introduction
Definition
Monte Carlo method definition
General definition
The Monte Carlo method is a collection of techniques that allow
the estimation of the moments of probability density functions
(PDFs) through statistical sampling.
5 / 45
Monte Carlo course
Introduction
Definition
Monte Carlo method definition
General definition
The Monte Carlo method is a collection of techniques that allow
the estimation of the moments of probability density functions
(PDFs) through statistical sampling.
. . . for our purposes
The Monte Carlo method allows the statistical estimation of
macroscopic quantities (such as deposited energy inside a
patient) arising from the step-by-step evaluation of interaction
probabilities of projectiles with their microscopic
environment.
5 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
One, two, three!!
Get context.
6 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
One, two, three!!
Sample step lengths to next events.
6 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
One, two, three!!
Decide what happens.
6 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
One, two, three!!
Run interaction and sample results.
6 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
One, two, three!!
Repeat with every available particle.
6 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
One, two, three!!
This is some more detail about (0), 1, 2, 3:
0
1
Get context: particle type, material information, geometry
constrains and mean free path
Sample step length:
Get distance to geometry sgeo and any constrain sc
Sample mean distance to interaction sλ = − λ1 ln (η), using a
random number.
3 Get the minimum: s = min (sgeo , sc , sλ )
1
2
2
Move1 to next position, sample interaction and produce
secondaries using a random number
1
We will see that the concept of moving can be extended from a change of
position to momentum (~
p ) changes, depending on the particle type
7 / 45
Monte Carlo course
Introduction
One, two, three!! Analog method
Basic Monte Carlo ingredients
Necessary knowledge:
Physics
Today we will learn. . .
Random numbers: uniform random
η = [0, 1]
Probability Density Function
(PDF) sampling
Statistics
Fig.: Photon track.
8 / 45
Monte Carlo course
Introduction
Fancy examples
Fancy (real life) examples: proton therapy
Pencil beam
scanning
system in ion
therapy.
The outlined
volumes are the
components of a
delivery nozzle.
Protons are
blue, electrons
are red, photons
are green.
9 / 45
Monte Carlo course
Introduction
Fancy examples
Fancy (real life) examples
Fig.: Dose distributions in patient geometry.
10 / 45
Monte Carlo course
Introduction
Fancy examples
Fig.: CMS detector geometry.
http://cms.web.cern.ch/news/zooom-cms-visualised-3d
11 / 45
Monte Carlo course
Introduction
History, applications and simple examples
Brief history from our point of view
Comte de Buffon (1777): needle tossing experiment
Laplace (1886): random points in a rectangle to calculate π
Fermi (1930): random method to calculate the properties of
the newly discovered neutron
Manhattan project (40’s): neutron transport (von Neumann,
Richtmyer). Ulam, Metropolis coined the term ”Monte Carlo”
Exponential growth with digital computers and the
improvement of the algorithms
Berger (1963): first complete coupled electron-photon
transport code, ETRAN
Exponential growth in Medical Physics since the 80’s:
Accuracy
12 / 45
Monte Carlo course
Introduction
History, applications and simple examples
Applications
This is a list with random things I found interesting:
Financial market: thanks Chicago school!
Traffic flow: traffic-jam dynamics
Environmental sciences
Particle physics
Medical physics (these are us ,)
Quantum field theory
Astrophysics
Molecular modeling
Semiconductor devices
Optimization problems
...
13 / 45
Monte Carlo course
Introduction
Examples: Buffon and π
Compte de Buffon’s needle tossing
First application of the Monte Carlo method.
Not yet called Monte Carlo method.
Wanted to calculate the probability of a
needle touching a stripe, not π.
Random event generator: how random is
the needle distribution?
Resources (click on them): simulator,
nice analysis, wurst throwing.
Probability with needles of length l ≤ d:
2l
# of overlaps
P=
=
πd
# of trials
14 / 45
Monte Carlo course
Introduction
Examples: Buffon and π
π calculation example (v1)
Let’s calculate π by throwing chalks to a circle. But:
Is your arm strong enough to throw 106 darts? No, use
computers.
Would you anyway be random enough?
Can computers generate random numbers? No.
. . . but we can generate: pseudo-random numbers
15 / 45
Monte Carlo course
Pseudo-random numbers
Outline
Tests for RNGs
1 Introduction
3 Probability density function
sampling
2 Pseudo-random numbers
Linear Congruential
Generator
Recommended RNG
4 Practical example: proton
simulation
16 / 45
Monte Carlo course
Pseudo-random numbers
One, two, three!!
17 / 45
Monte Carlo course
Pseudo-random numbers
Basic Monte Carlo ingredients
Necessary knowledge:
Physics
Today we will learn. . .
Random numbers: uniform random
η = [0, 1]
Probability Density Function
(PDF) sampling
Statistics
Fig.: Photon track.
18 / 45
Monte Carlo course
Pseudo-random numbers
Pseudo-Random Numbers
A list of pseudo-random numbers is a list of numbers that appear
statistically uncorrelated. That is, there is no way to tell that they
are not random. Goals of a generator:
Goal 1: create lists of statistically uncorrelated numbers.
Goal 2: list length long enough to cover your purposes.
Problem? Computers are deterministic
Advantage? Computers are deterministic
Definition: the length of the list is known as period (P).
How many particles are you simulating?
How many randoms will you need per particle?
RNG used in the π calculation: Linear Congruential Generator.
19 / 45
Monte Carlo course
Pseudo-random numbers
General random number generator pseudo-code
1
2
Set initial state: seed.
Get random number
by:
1:
Execute a function
on the current
state.
2 Update the state.
3 Return the result
of the function.
5:
1
3
RAND(){
global state ← update()
3:
return f (state)
4: }
2:
6:
7:
state ← setInitialState(seed)
η ← RAND()
Get another
All numbers depend on the initial seed!!
20 / 45
Monte Carlo course
Pseudo-random numbers
Linear Congruential Generator
Linear Congruential Generator (LCG)
One of the simplest generators, of the form:
ηi+1 = aηi + c mod m
Dependence on previous values.
Predictable.
Period of m at most.
√
Recommended not to use more than P numbers for a
simulation.
√ An otherwise good2 LCG starts to fail tests when
about P numbers are used.
Tend to fail high-dimensional tests.
2
Hellekalek and L’Ecuyer (1998)
21 / 45
Monte Carlo course
Pseudo-random numbers
Linear Congruential Generator
Limitations of LCG
This is why LCG are not used in practice for random generation,
they can be used to generate seeds for other algorithms.
22 / 45
Monte Carlo course
Pseudo-random numbers
Linear Congruential Generator
π calculation example (v2)
Repeating the π example changing the random number generator:
ηi+1 = aηi mod m ; with a = 663608941, m = 232 , η0 = 3
⇓
ηi+1 = aηi
mod m
; with a = 26590841, m = 226 − 5, η0 = 3
23 / 45
Monte Carlo course
Pseudo-random numbers
Linear Congruential Generator
π calculation example (v2)
Repeating the π example changing the random number generator:
ηi+1 = aηi mod m ; with a = 663608941, m = 232 , η0 = 3
⇓
ηi+1 = aηi
mod m
; with a = 26590841, m = 226 − 5, η0 = 3
The period is too short and the calculation becomes biased!
23 / 45
Monte Carlo course
Pseudo-random numbers
Recommended RNG
Recommended RNG
There are many codes out there already written if you were to
write your own MC, unless you become/are an expert on the field,
please, use one of the many functions publicly available. One of
the most common choice is the Mersenne-twister algorithm:
Based on Mersenne primes (prime number of the form 2k − 1)
and a twisted version of generalized feedback shift registers.
Period of P = 219937 − 1
A practical tip is that for some distributions, sampling when η = 0
or η = 1 is problematic: −log (1 − η), 1.0/η or sin (η)/η. The
result is not biased if you omit them.3
3
η ∈ [0, 1] can in practice be η ∈ (0, 1] or ∈ [0, 1) or ∈ (0, 1)
24 / 45
Monte Carlo course
Pseudo-random numbers
Tests for RNGs
Tests for RNG
Tests (reference: NIST):
Frequency
Spectral
Maurer’s Universal Statistics
Linear Complexity
Approximate Entropy
Cumulative sum
Random Excursions
Interesting sites: TestU01,
Dieharder
25 / 45
Monte Carlo course
Probability density function sampling
Outline
3 Probability density function
1 Introduction
2 Pseudo-random numbers
sampling
Inverse method
4 Practical example: proton
simulation
26 / 45
Monte Carlo course
Probability density function sampling
One, two, three!!
27 / 45
Monte Carlo course
Probability density function sampling
Basic Monte Carlo ingredients
Necessary knowledge:
Physics
Today we will learn. . .
Random numbers: uniform random
η = [0, 1]
Probability Density Function
(PDF) sampling
Statistics
Fig.: Photon track.
28 / 45
Monte Carlo course
Probability density function sampling
Sampling from a probability density function (PDF)
Sampling from an PDF is in practice the process of taking a
uniform η ∈ [0, 1] and use it to get another number ξ following the
intended PDF in the intended interval.
We want to sample values from PDFs representing physical
processes
Why did we need pseudo random numbers between [0, 1]?
Because we can use them to sample from any distribution in
any interval [a, b].
How?
Inverse method
Rejection method
Others: importance sampling, Metropolis sampling, Gibbs
sampling. . . and many others
29 / 45
Monte Carlo course
Probability density function sampling
Inverse method
Inverse Method
Which important features does it have?
Easy to sample from conditional distribution (from here to
here: this, from here to here: that. . . )
Useful for implementation of variance reduction techniques
(next day’s topic).
It uses just one uniform random value.
We are going to see two examples and one of you should try to do
the second ,!
30 / 45
Monte Carlo course
Probability density function sampling
Inverse method
Inverse Method
How to sample ξ from a PDF p(z) with the inverse method?
Using the Cumulative Density Function (CDF) P(x):
Z x
P(x) =
dzp(z), a ≤ x ≤ b, P(a) = 0, P(b) = 1
a
It has these properties:
P(x) monotonically increases in interval [a, b]
0≤y ≤1
|
∀y = P(x), ∀x ∈ [a, b]
P−1 (y)
If x =
can be calculated, then ξ = P −1 (η) is distributed
according to p(x). 4
4
For proof, see: Nelson et al. 1985. The EGS4 Code System, SLAC Report
No. SLAC-265, Stanford Linear Accelerator Center.
31 / 45
Monte Carlo course
Probability density function sampling
Inverse method
Inverse method: Uniform distribution in interval [a, b]
Simple case:
PDF:
CDF:
1
dx
p(x) dx =
b
−
a
Z x
P(x) =
p(z) dz =
1
b
−
a
a
ξ−a
x −a
−→ η =
P(x) =
b−a
b−a
ξ = (b − a)η + a
Z
x
dz =
a
x −a
b−a
32 / 45
Monte Carlo course
Probability density function sampling
Inverse method
Inverse method: photon step
This is the first physics case!!
PDF:
p(s) ds = µe −µs ds
Who volunteers to solve it?
Some help:
Rx
1
Calculate CDF: P(x) =
2
η = CDF (ξ)
3
Solve for ξ inverting the CDF
4
Done!
a
dzp(z)
33 / 45
Monte Carlo course
Probability density function sampling
Inverse method
Inverse method: photon step
PDF:
CDF:
p(s) ds = µe −µs ds
Z s
P(s) =
p(s 0 ) ds 0 = 1 − e −µs
0
η = 1 − e −µξ
1
ξ = − ln (1 − η)
µ
34 / 45
Monte Carlo course
Probability density function sampling
Inverse method
Inverse method: photon step
PDF:
CDF:
p(s) ds = µe −µs ds
Z s
P(s) =
p(s 0 ) ds 0 = 1 − e −µs
0
η = 1 − e −µξ
1
ξ = − ln (1 − η)
µ
Would this be correct?
1
ξ = − ln (η)
µ
Why?
34 / 45
Monte Carlo course
Practical example: proton simulation
Outline
3 Probability density function
1 Introduction
sampling
4 Practical example: proton
2 Pseudo-random numbers
simulation
35 / 45
Monte Carlo course
Practical example: proton simulation
1, 2, 3!!
36 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
We have to ask ourselves, who am I?
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Mamma, do I have enough energy? To matter or not to
matter (Monte Carlo version of: To Be or not To Be)
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
3
Let’s decide what happens to me. What can happen to me?
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
3
Compute total mean free path λ and distance to boundary
(sgeo ), sample step sλ and get maximum allowed step (smax )
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
3
Compute total mean free path λ and distance to boundary
(sgeo ), sample step sλ and get maximum allowed step (smax )
4
Action:
◦ interact
if

sλ ,

◦ move to boundary if sgeo ,
= min (sλ , sgeo , smax )


◦ move step max
if smax
In this case sλ = min (sλ , sgeo , smax ). This is what we call a
hard event. What would you do if option 2 or 3 happened?
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Keep this in mind forever!
Assume you are a particle inside a patient:
The fact that the particle is charged is
1 I’m positive I’m proton!!
very important. It changes what we
2 Yes, I am energetic enough to pass the threshold!!
understand by moving in a simulation.
3 ComputeIn
total
meanmoving
free path
λ and
general,
is not
justdistance
a changeto boundary
(sgeo ), sample
step sλbut
anda get
maximum
allowed step (smax )
of position,
change
in momentum
4 Action: too. Since charged particles interact so

much it is unfeasible
simulate
◦ interact
if sto

λ ,
every
single
interaction
point.
Those
◦ move to boundary if sgeo , = min
(sλ , sgeo , smax )

interactions considered
soft are
◦ move step max
if smax
condensed into single
steps.
In this case
s
=
min
(s
,
s
,
s
). simulation
This is what we call a
geo
max
λ
λ
Specially complicated is the
hard event.
What would you do if option 2 or 3 happened?
of electrons.
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
3
Compute total mean free path λ and distance to boundary
(sgeo ), sample step sλ and get maximum allowed step (smax )
4
Action: it is a hard interaction
5
What can happen to a proton?
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
3
Compute total mean free path λ and distance to boundary
(sgeo ), sample step sλ and get maximum allowed step (smax )
4
Action: it is a hard interaction
5
What can happen to a proton? Ionization, elastic and inelastic
nuclear reactions

σ

Ionization, if η <


Σ

σ
probabilities → Nuclear E., if η < + κ


Σ


Nuclear I., otherwise
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
Assume you are a particle inside a patient:
1
I’m positive I’m proton!!
2
Yes, I am energetic enough to pass the threshold!!
3
Compute total mean free path λ and distance to boundary
(sgeo ), sample step sλ and get maximum allowed step (smax )
4
Action: it is a hard interaction
5
Roll the dice: η = 0.123456789 −→ Ionization!! We are
having a δ-electron.
37 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
What is the kinetic energy of an electron kicked out by a
proton? Probability of producing an electron of kinetic energy
T > Tmin , with a given a proton (E , β). [Geant4, equation 12.19
for f (T ) is wrong if different from the one here].
zp2
1
T2
dσ
2
2
2 T
= 2πre mc Z 2 × 2 1 − β
+
dT
β
T
Tmax
2E 2
Could be factorized as follows:
dσ
= f (T )g (T ) with T ∈ [Tmin , Tmax ]
dT
If f (T ) is a PDF between [Tmin , Tmax ] and g (T ) ∈ [0, 1], then we
can sample T from dσ/dT using ξ = f −1 (η) and accept T = ξ if
η 0 < g (ξ).
38 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
What is the kinetic energy of an electron kicked out by a
proton? Probability of producing an electron of kinetic energy
T > Tmin , with a given a proton (E , β). [Geant4, equation 12.19
for f (T ) is wrong if different from the one here].
zp2
1
T2
dσ
2
2
2 T
= 2πre mc Z 2 × 2 1 − β
+
dT
β
T
Tmax
2E 2
In other words, if f and g are:
Tmin Tmax
1
f (T ) =
Tmin − Tmax T 2
T
T2
g (T ) = 1 − β 2
+
Tmax
2E 2
39 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
To obtain T , we sample from f (T ) using the inverse method:
Z T
1
1
Tmin Tmax
−
η=
f (t) dt =
Tmin − Tmax
Tmin T
Tmin
Tmin Tmax
ξ=
(1 − η)Tmax + ηTmin
and accept T = ξ if
η2 < g (T ) = 1 − β 2
T
T2
+
Tmax
2E 2
40 / 45
Monte Carlo course
Practical example: proton simulation
Proton produced δ-electron kinetic energy
We already know T , so to get the secondary δ:
From kinematics derive the polar scattering angle
The azimutal scattering angle is uniformly sampled
(η ∈ [0, 2π])
Since mp me , the direction change of the proton can be
neglected.
DONE!!
41 / 45
Monte Carlo course
Practical example: proton simulation
Last 1, 2, 3!! of the day
Very nice link for more advance information:
http://statweb.stanford.edu/~owen/mc/
42 / 45
Monte Carlo course
Practical example: proton simulation
End of part 1, let’s take a
deserved break!
43 / 45
Monte Carlo course
Practical example: proton simulation
Here is some additional vocabulary that I might have been using.
Each exact definition depends on the jargon each particular code
uses as it usually represents it’s underlying algorithm architecture:
History May refer to each one of the primary particles
launched to simulate or may include the secondaries
Event Each one of the interactions/steps
Track Usually refers to the trajectory of a given particle.
May contain information of past events
Phase space In our scope, it is a collection of particles at a given
location. For example: a phase space may hold the
information of all the particles that crossed a given
surface, storing the 3D position and direction, energy,
statistical weight and particle type
44 / 45
Monte Carlo course
Practical example: proton simulation
From Geant4: Suppose we wish to sample x in the interval [x1 , x2 ]
from the distribution f (x) and the PDF can be written as:
n
X
f (x) =
Ni fi (x)gi (x)
i=1
where Ni > 0, fi (x) are normalized PDFs on [x1 , x2 ], and
0 ≤ gi (x) ≤ 1. Then x can be sampled in the following way:
1
select an integer i ∈ {1, 2, . . . n} with probability proportional
to Ni
2
select a value x0 from the distribution fi (x)
3
calculate gi (x) and accept x = x0 with probability gi (x0 )
4
if x0 is rejected restart from 1
45 / 45