Monte Carlo Techniques: 32.1. Sampling The Uniform Distribution
Monte Carlo Techniques: 32.1. Sampling The Uniform Distribution
(32.1)
(32.2)
This method is shown in Fig. 32.1a. It is most convenient when one can calculate by
hand the inverse function of the indefinite integral of f . This is the case for some common
functions f (x) such as exp(x), (1 x)n , and 1/(1 + x2 ) (Cauchy or Breit-Wigner),
although it does not necessarily produce the fastest generator. CERNLIB contains
routines to implement this method numerically, working from functions or histograms.
http://pdg.lbl.gov/)
13:58
(a)
Continuous
distribution
F(x)
u
0
x = F 1(u)
(b)
F(x)
u
Discrete
distribution
} f(xk)
xk xk+1
Figure 32.1: Use of a random number u chosen from a uniform distribution (0,1)
to find a random number x from a distribution with cumulative distribution function
F (x).
For a discrete distribution, F (x) will have a discontinuous jump of size f (xk ) at each
allowed xk , k = 1, 2, . Choose u from a uniform distribution on (0,1) as before. Find
xk such that
k
X
F (xk1 ) < u F (xk ) Prob (x xk ) =
f (xi ) ;
(32.3)
i=1
then xk is the value we seek (note: F (x0 ) 0). This algorithm is illustrated in Fig. 32.1b.
13:58
C h(x)
f(x)
(b)
C h(x)
f(x)
32.4. Algorithms
Algorithms for generating random numbers belonging to many different distributions
are given by Press [5], Ahrens and Dieter [6], Rubinstein [7], Everett and Cashwell [8],
Devroye [9], and Walck [10]. For many distributions alternative algorithms exist, varying
in complexity, speed, and accuracy. For time-critical applications, these algorithms may
be coded in-line to remove the significant overhead often encountered in making function
calls. Variables named u are assumed to be independent and uniform on (0,1). (Hence,
u must be verified to be non-zero where relevant.)
In the examples given below, we use the notation for the variables and parameters
given in Table 30.1.
13:58
(32.4)
and
(32.5)
2 ln u2
2 ln u2
(32.6)
2 ln r2
r2
s
and z2 = v2
2 ln r2
r2
(32.7)
are independent numbers chosen from a normal distribution with mean 0 and variance 1.
zi0 = + zi distributes with mean and variance 2 .
A recent implementation of the fast algorithm of Leva Ref. 11 is in CERNLIB.
For a multivariate Gaussian, see the algorithm in Ref. 12.
13:58
n/2
Y
y = 2 ln
ui is 2 (n) .
(32.8)
i=1
For n odd, generate (n 1)/2 uniform numbers ui and one Gaussian z as in Sec. 32.4.4;
then
(n1)/2
Y
y = 2 ln
ui + z 2 is 2 (n) .
(32.9)
i=1
For n & 30 the much faster Gaussian approximation for the 2 may be preferable:
generate z as in Sec. 32.4.4 and use
2
13:58
then accept x = max(0, [ + z + 0.5]) where [ ] signifies the greatest integer the
expression. [13]
32.4.9. Students t distribution:
For n > 0 degrees of freedom (n not necessarily integer), generate x from a
Gaussian with mean 0 and 2 = 1 according to the method of 32.4.4. Next generate
y, an independent gamma random variate with k = n/2 degrees of freedom. Then
13:58
13:58