Lecture 09 Randomnumbers
Lecture 09 Randomnumbers
Training UKNum
Random numbers, Monte Carlo methods
H. Klahr
Max Planck Institute for Astronomy, Heidelberg
Program:
1) Random Number Generator
2) Transformation Method
3) Monte Carlo Integration
1.0 Random Numbers
Applications
‣Gambling
‣Physics Simulations
‣Monte Carlo Methods
‣Kryptography
‣...
Generating Random I
•Text
‣Text
text
A page chosen at random from the 1955 book A Million Random Digits.
Photo: Garry McLeod
Generating Random II
•First call
You‣Initialisation
initialize withiseed to aDepends
arbitrary seed. (usually)
on arbi
algorithm, sometimes neg. Odd number. For same
initializing value will typically return
seed same sequence. Returns new random number a
east a different
x and new value forsubsequence
iseed. of some on
•Next call:
izing
‣Use value
last of iseed
returned iseed. (!) will always return
Now our first, and perhaps most import
PRNG Algorithmen
•Widely distributed
‣Not perfect but simple, quality depends on used
parameters, can be improved by some tricks to a
useful RNG.
•Very fast.
•Small demand in memory (cache).
Chapter 7. Random Numbers
Linear Kongruence generator
of•integers I 1 , I2 , Iof
Generates sequence 3 ,integers:
. . . , each
I1, I2, between
I3 ..., all 0a
between 0 and m-1 with m a “large” number.
urrence relation
•Recursive definition:
Ij+1 = aIj + c (mod m)
•m=Modulus (Integer, m>0)
• a=Multiplier/Faktor (Integer, 0< a < m)
modulus, and a and c are positive integers c
•c=Increment (Integer, 0<= c <m)
spectively. The recurrence (7.1.1) will even
obviously no greater than m. If m, a, and c a
Linear Kongruence generator II
•Drawbacks
‣Sequential correlation (relation between RN
pairs), manifested in hyper planes(Satz von
Marsaglia):
Plotting a sequence of k numbers in a k dimensional space (k1,k2,k3),
then the points do not fill the volume, but lie on k-1 dimensional hyper
planes. Maximally m1/k Hyper planes, for “bad” values of m,a and c
even less.
•Sobycalled
posed Lewis,“Minimal
Goodman,Standard”
and Miller MINSTD Generator.
in 1969, this generator has
nta•=Widely distributed,
7 = 16807
years m but
= 2should
5passed all new theoretical
− 1 =not be used for
2147483647
31 tests, and (perhaps more importantl
(7.
professional
mulated application.
a large amount of successful use. Park and Miller do not claim th
ator
by isLewis,
“perfect” (we will see
Goodman, below
and that it
Miller inis1969,
not), but only
this that it is ahas
generator go
Implementation of MINST
Press et al.
MINSTD: sample output
seed =1
Call No. x idum
1 7.82636926E-06 16807
2 0.13153780 282475249
3 0.75560534 1622650073
....
2147483645 0.65550071 1407677000
231-2 2147483646 4.65661287E-10
2147483647 7.82636926E-06
1
16807
231≈2 109
•MINSTD Problems:
•Low order serial correlations. Example:
Once in 106 calls, x < 10-6 ausgegeben (which is OK), Once in 106 calls,
x < 10-6 ausgegeben (which is OK), but next number is always < 0.0168,
which is not OK.
MINSTD with BD-shuffling
•Simple method after Bays and Durham, to prevent
2
low order serial correlations:
Chapter 7. Random Numbers
iy
ure 7.1.1. Shuffling procedure used in ran1 to break up sequential correlations in the Minimal
Press
ndard generator. et al.
Circled numbers indicate the sequence of events: On each call, the random number
iy is used to choose a random element in the array iv. That element becomes the output random
MINSTD mit BD-shuffling 7.1 Uniform Deviates 271
FUNCTION ran1(idum)
INTEGER idum,IA,IM,IQ,IR,NTAB,NDIV
REAL ran1,AM,EPS,RNMX
PARAMETER (IA=16807,IM=2147483647,AM=1./IM,IQ=127773,IR=2836,
* NTAB=32,NDIV=1+(IM-1)/NTAB,EPS=1.2e-7,RNMX=1.-EPS)
“Minimal” random number generator of Park and Miller with Bays-Durham shuffle and
added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of
the endpoint values). Call with idum a negative integer to initialize; thereafter, do not
Press et al.
END
MINSTD with BD-shuffling
•Then
xk+2 = 6xk+1 9xk
Oops!!!
2. Non equal RNG:
Transformation Method
Statistical Distribution
bility distributions
or •
population
X is givensynthesis calculations
via (cumulative is the generation
distribution of initial cond
function,
robability
CDF) F(x)distributions, as explained in the introduction to Paper I
beschrieben.
a short overview on probability distributions in general.
•Then F(x) is probability, that X has value between
iate X is
-∞ and x:described with the cumulative distribution function (CD
obability that the random deviate X takes a values between ⇤ a
•Requirement
lfill for CDF: namely (1) F ( ⇤) = 0, (2) F (⇤
an number of requirements,
•F(-∞)=0,
notonically F(∞)=1 and (4) F (x) must rightwards continuous
increasing,
•Monotonically raising
•Continuous
y that a continuous random deviate takes a given value xi is zero. T
Statistical Distribution
F (x) = P (X ⇥ x) for ⇤<x<⇤
The
The mean
CDF PDF is0.6is
is obtained ofsetting
obtained
by course
asdFthe=simply
fderivative,
(x)dx = k1 dx 0.6
and determining k1 by the conditions
0.6
F (a) = 0 and 0.4F (b) = 1. The PDF is obtained as the 0.4 derivative, 0.4
0.2 1
if a ⇥ x ⇥ b ⇧ b
f (x) = 0 b a 1
if a ⇥
0.2
x ⇥ b 1 (3.8)
0.2
a + b
.5 1 1.5 2 0 !0.5 otherwise
!1 0 f (x)1 = 1.5 E(X)
0.5
0
b a
2 = µ 0
= xdx
otherwisex!1 !0.5b 0 a0.5 1 1.5 2
= 0 (3.8)
ne in fig. 3.1. Figure 3.1: PDF (left) and CDF (right) for a standard un
2
•
a)2
.
distributions
Simple
This distribution is
are shown
case:
The mean is of course simply
mply implemented as
in fig. 3.1.
(a+b)/2,
No. 1 in
but then we can test the general Eq.:
exostat. (b a)2
niform deviate in the unit interval (i.e. between 0.0 and 1.0) which and the variance 2
x= 12 . This distribution is implemented as
er generators. Called the standard uniform deviate, it has a PDF ⇧ b A special case is the uniform deviate in the unit interval (i.e. b
n the unit interval, and 0 otherwise. In ⇧the practical application for1 a+
is given by b number generators. Called the standard unif
random
1 from
wants to calculate random deviates E(X) b
some given x =a + b
= µdistribution, equal=
xdx (3.9) In t
1, i.e. f (x)dx = dx in the unit interval, and 0 otherwise.
r generator=
E(X) µx = which gives a standard
at disposition xdx =deviate.
uniform b a a a computer 2program, one wants to calculate (3.9)random deviates from
Equal distribution in Intervall [0,1]
is •simple
Solution if D.E. isequation
differential x=F(y), with F(y)
is just x= theF integral of f(y).
(y), where F (y)
eherefore,
differential equation is just x = F (y), where
the transformation of a uniform deviate x to y distr
F
e, the transformation of a uniform deviate x to y
• Transformation of SUD from X to Y with f(y) :
y(x) = F 1
(x),
• MC
estimatestheorem
the says
integral of we
a can
function then
f overestimate
the the
multidimensional vo
Integral as:
Suppose that we pick N random points, uniformly distributed in a multidimen-
⇧
nal volume V . Call them x1 , . . . , xN . Then the basic theorem of Monte Carlo
⇥
egration estimates the integral of a function f over2the 2
multidimensional volume,
⇧f ⌥ ⇥ ⇧f⌥
f dV ⌃ V ⇧f⌥ ± V ⇧ (
⇥ 2 N
⇧f ⌥ ⇥ ⇧f⌥ 2
f dV ⌃ V ⇧f⌥ ± V (7.6.1)
gle brackets denote taking the arithmeticNmean over the N sample p
• With <> the arithmetic mean over N sample points.
re the angle brackets denote taking the arithmetic mean over the N sample points,
⌅N ⌅N
1 N
⌃ 2⌥ 1⌅
N 2
⇧f⌥ ⌅
1 f(xi ) ⌃ f2 ⌥ 1 2 f (xi ) (
⇧f⌥N f(xi ) f N f (xi ) (7.6.2)
N
i=1 i=1
N i=1
i=1
Monte Carlo Integration II
area A
∫fdx
Press et al.
Example ∫fdx
2
Torus, with certain cuts…
x
Difficult to integrate
0 1 2 4
analytically.
For MC simple.
Z
1
Press et al.
~rs = ⇢(~r)~rdV
M
, (see
butbuta§7.8),
a worked
worked example will show the underlying simplicity
but a worked example will show the underlying simplicity of the method.m
example will show the underlying simplicity of theof
hat
at weExample
we
Suppose want
want to
that we to find
find
want
II
the
to the
find theweight
weight and
weight and
and thethe
the position
position
position of of of
thethe the
center
center center
of
of mass
object of complicated shape, namely the intersection of a torus with the edge of a
anof
ofmas
complicated
omplicated shape,
shape, namely
namely thethe intersection
intersection of aoftorus
a torus
withwith
the ed th
large box. In particular let the object be defined by the three simultaneous conditions
nInparticular
particular letlet
thethe
object be
object defined
⇤ be definedby 2the three
by the simultaneous
three simultaneou con
•Body defined as⇤z2 +⇤ x2 + y2 ⇥ 3 2 ⇤ 1 (7.6.3)
2
2 2 2 +2 y2 ⇥23
z z+ + x x + y ⇥ 3⇤
(torus centered on the origin with major radius = 4, minor 1⇤radius
1 = 2)
x ⌅ 1radius
y ⌅=⇥34, minor radius = 2)
(7.6.4)
(Torus
ered on
tered onouter
the radius with
origin = 4, inner
with
the origin majorradius
major =3)=and
radius planes radius = 2)
4, minor
(two faces of the box, see Figure 7.6.2). Suppose for the moment that the object
has a constant density ⇧. x ⌅ x⌅11 y ⌅ ⇥3
y ⌅ ⇥3
We want to estimate the following integrals over the interior of the complicated
of• Find
the
object: the
box, Volume
see Figure for the
7.6.2). Integrals
Suppose
s of ⇥the box, see ⇥Figure 7.6.2). ⇥Suppose for the for the moment
⇥ moment tha
that the
tant density ⇧.
nstant ⇧density
dx dy dz ⇧. x⇧ dx dy dz y⇧ dx dy dz z⇧ dx dy dz
ant to estimate the following integrals over the interior of the comp
want to estimate the following integrals over the interior of(7.6.5)
the c
The coordinates of the center of mass will be the ratio of the latter three integrals
• Assume
(linear moments)⇥ constant
⇥
density
to the first one ⇥ρ.
(the weight).
⇥
⇥
⇥
In the following
x dy dz x⇧fragment,
dx dy dz the region V , y⇧
enclosing
dx dythedz
piece-of-torusz⇧
W dx
, is the
dy
Example III 306 Chapter 7. Random Numbers
area A
in z. Figure 7.6.1. Monte Carlo integration. Random points are chosen within the area A. The integral of the
function f is estimated as the area of A multiplied by the fraction of random points that fall below the
curve f . Refinements on this procedure can improve the accuracy of the method; see text.
x
0 1 2 4
Z
1
~rs = ⇢(~r)~rdV
M
(see Lecture for details). Then to obtain a random number xi drawn from the interval
(0, 1) use the normalisation xi = ui /231 . Create some 100 000 random number,
starting with an initial seed u0 = 1 and plot the consecutive triples (xi , xi+1 , xi+2 )
in a 3-dimensional plot, e.g., using the splot command from gnuplot. Count the
number of 2D planes by viewing the data in di↵erent projections. What is the
number of planes for randu?
• Exercise 2, 8 points: Transformation method.
Write a program code to generate random numbers with an exponential probability
distribution function (PDF) ⇢(y) = e y in the interval ymin = 0 to ymax = 5 using
the transformation:
y= ln (1 x) , x = e y.
Use an random number generator with uniform PDF of your choice or the one
o↵ered in the Lecture. Show that your resulting distribution of random numbers
indeed follows an exponential one.
• Exercise 3, 6 points: Monte-Carlo Integration.
Approximate the value of ⇡ using the Monte-Carlo technique by integrating the area
of a square with side length a and a circle of radius 1/2a. Use the equation:
Ac Nc
⇡=4 ⇡4 ,
As Ns
where Ac and As is the area of the square and the circle, respectively. How does the
precision of the result scale with the number of points used in the integration?