Box–Muller transform

Visualisation of the Box–Muller transform — the coloured points in the unit square (u1, u2), drawn as circles, are mapped to a 2D Gaussian (z0, z1), drawn as crosses. The plots at the margins are the probability distribution functions of z0 and z1. Note that z0 and z1 are unbounded; they appear to be in [-3,3] due to the choice of the illustrated points.

The Box–Muller transform, by George Edward Pelham Box and Mervin Edgar Muller 1958,[1] is a pseudo-random number sampling method for generating pairs of independent, standard, normally distributed (zero expectation, unit variance) random numbers, given a source of uniformly distributed random numbers.

It is commonly expressed in two forms. The basic form as given by Box and Muller takes two samples from the uniform distribution on the interval (0, 1] and maps them to two standard, normally distributed samples. The polar form takes two samples from a different interval, [−1, +1], and maps them to two normally distributed samples without the use of sine or cosine functions.

The Box–Muller transform was developed as a more computationally efficient alternative to the inverse transform sampling method.[2] The Ziggurat algorithm gives an even more efficient method. Furthermore, the Box–Muller transform can be also employed from drawing from truncated bivariate Gaussian densities.[3]

Basic form

Suppose U1 and U2 are independent random variables that are uniformly distributed in the interval (0, 1). Let

Z_0 = R \cos(\Theta) =\sqrt{-2 \ln U_1} \cos(2 \pi U_2)\,

and

Z_1 = R \sin(\Theta) = \sqrt{-2 \ln U_1} \sin(2 \pi U_2).\,

Then Z0 and Z1 are independent random variables with a standard normal distribution.

The derivation[4] is based on a property of a two-dimensional Cartesian system, where X and Y coordinates are described by two independent and normally distributed random variables, the random variables for R2 and Θ (shown above) in the corresponding polar coordinates are also independent and can be expressed as

R^2 = -2\cdot\ln U_1\,

and

\Theta = 2\pi U_2. \,

Because R2 is the square of the norm of the standard bivariate normal variable (X, Y), it has the chi-squared distribution with two degrees of freedom. In the special case of two degrees of freedom, the chi-squared distribution coincides with the exponential distribution, and the equation for R2 above is a simple way of generating the required exponential variate.

Polar form

Two uniformly distributed values, u and v are used to produce the value s = R2, which is likewise uniformly distributed. The definitions of the sine and cosine are then applied to the basic form of the Box–Muller transform to avoid using trigonometric functions.

The polar form was first proposed by J. Bell[5] and then modified by R. Knop.[6] While several different versions of the polar method have been described, the version of R. Knop will be described here because it is the most widely used, in part due to its inclusion in Numerical Recipes.

Given u and v, independent and uniformly distributed in the closed interval [−1, +1], set s = R2 = u2 + v2. (Clearly \scriptstyle R = \sqrt{s}). If s = 0 or s ≥ 1, discard u and v, and try another pair (u, v). Because u and v are uniformly distributed and because only points within the unit circle have been admitted, the values of s will be uniformly distributed in the open interval (0, 1), too. The latter can be seen by calculating the cumulative distribution function for s in the interval (0, 1). This is the area of a circle with radius \scriptstyle \sqrt{s}, divided by \scriptstyle\pi. From this we find the probability density function to have the constant value 1 on the interval (0, 1). Equally so, the angle θ divided by \scriptstyle 2 \pi is uniformly distributed in the interval [0, 1) and independent of s.

We now identify the value of s with that of U1 and \scriptstyle \theta/(2 \pi) with that of U2 in the basic form. As shown in the figure, the values of \scriptstyle \cos \theta = \cos 2 \pi U_2 and \scriptstyle \sin \theta = \sin 2 \pi U_2 in the basic form can be replaced with the ratios \scriptstyle\cos \theta = u/R = u/\sqrt{s} and \scriptstyle\sin \theta = v/R = v/\sqrt{s}, respectively. The advantage is that calculating the trigonometric functions directly can be avoided. This is helpful when trigonometric functions are more expensive to compute than the single division that replaces each one.

Just as the basic form produces two standard normal deviates, so does this alternate calculation.

z_0 = \sqrt{-2 \ln U_1} \cos(2 \pi U_2) = \sqrt{-2 \ln s} \left(\frac{u}{\sqrt{s}}\right) = u \cdot \sqrt{\frac{-2 \ln s}{s}}

and

z_1 = \sqrt{-2 \ln U_1} \sin(2 \pi U_2) = \sqrt{-2 \ln s}\left( \frac{v}{\sqrt{s}}\right) = v \cdot \sqrt{\frac{-2 \ln s}{s}}.

Contrasting the two forms

The polar method differs from the basic method in that it is a type of rejection sampling. It discards some generated random numbers, but it is typically faster than the basic method because it is simpler to compute (provided that the random number generator is relatively fast) and is more numerically robust.[7] It avoids the use of trigonometric functions, which are comparatively expensive in many computing environments. It discards 1 − π/4 ≈ 21.46% of the total input uniformly distributed random number pairs generated, i.e. discards 4/π − 1 ≈ 27.32% uniformly distributed random number pairs per Gaussian random number pair generated, requiring 4/π ≈ 1.2732 input random numbers per output random number.

The basic form requires two multiplications, 1/2 logarithm, 1/2 square root, and one trigonometric function for each normal variate.[8] On some processors, the cosine and sine of the same argument can be calculated in parallel using a single instruction. Notably for Intel-based machines, one can use the fsincos assembler instruction or the expi instruction (usually available from C as an intrinsic function), to calculate complex

\mathrm{exp}(iz) = e^{i z} = \cos(z) + i \sin(z), \,

and just separate the real and imaginary parts.

The polar form requires 3/2 multiplications, 1/2 logarithm, 1/2 square root, and 1/2 division for each normal variate. The effect is to replace one multiplication and one trigonometric function with a single division.

Tails truncation

When a computer is used to produce a uniform random variable it will inevitably have some inaccuracies because there is a lower bound on how close numbers can be to 0. If the generator uses 32 bits per output value, the smallest non-zero number that can be generated is 2^{-32}. When U_1 and U_2 are equal to this the Box–Muller transform produces a normal random variable equal to \sqrt{-2 \ln(2^{-32})} \cos(2 \pi 2^{-32})\approx 6.66 This means that the algorithm will not produce random variables more than 6.66 standard deviations from the mean. This corresponds to a proportion of 2.74 \times 10^{-11} lost due to the truncation.

Implementation

The standard Box–Muller transform generates values from the standard normal distribution (i.e. standard normal deviates) with mean 0 and standard deviation 1. The implementation below in standard C++ generates values from any normal distribution with mean \mu and variance \sigma^2. If Z is a standard normal deviate, then X = Z\sigma + \mu will have a normal distribution with mean \mu and standard deviation \sigma. Note that because the random number generator rand has not been seeded, the same series of values will always be returned from the generateGaussianNoise function.

#include <cstdlib>
#include <cmath>
#include <limits>
double generateGaussianNoise(double mu, double sigma)
{
	const double epsilon = std::numeric_limits<double>::min();
	const double two_pi = 2.0*3.14159265358979323846;

	static double z0, z1;
	static bool generate;
	generate = !generate;

	if (!generate)
	   return z1 * sigma + mu;

	double u1, u2;
	do
	 {
	   u1 = rand() * (1.0 / RAND_MAX);
	   u2 = rand() * (1.0 / RAND_MAX);
	 }
	while ( u1 <= epsilon );

	z0 = sqrt(-2.0 * log(u1)) * cos(two_pi * u2);
	z1 = sqrt(-2.0 * log(u1)) * sin(two_pi * u2);
	return z0 * sigma + mu;
}

See also

References

  1. G. E. P. Box and Mervin E. Muller, A Note on the Generation of Random Normal Deviates, The Annals of Mathematical Statistics (1958), Vol. 29, No. 2 pp. 610–611
  2. Kloeden and Platen, Numerical Solutions of Stochastic Differential Equations, pp. 11–12
  3. Martino, L.; Luengo, D.; Míguez, J. "Efficient sampling from truncated bivariate Gaussians via Box-Muller transformation". Electronics Letters 48 (24): 1533–1534. doi:10.1049/el.2012.2816.
  4. Sheldon Ross, A First Course in Probability, (2002), pp. 279–281
  5. J. Bell: 'Algorithm 334: Normal random deviates', Communications of the ACM, vol. 11, No. 7. 1968
  6. R. Knopp: 'Remark on algorithm 334 [G5]: normal random deviates', Communications of the ACM, vol. 12, No. 5. 1969
  7. Everett F. Carter, Jr., The Generation and Application of Random Numbers, Forth Dimensions (1994), Vol. 16, No. 1 & 2.
  8. Note that the evaluation of 2πU1 is counted as one multiplication because the value of 2π can be computed in advance and used repeatedly.

External links

This article is issued from Wikipedia - version of the Wednesday, January 06, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.