The Box–Muller transform, by George Edward Pelham Box and Mervin Edgar Muller,[1] is a 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. The method was first mentioned explicitly by Raymond E. A. C. Paley and Norbert Wiener in their 1934 treatise on Fourier transforms in the complex domain.[2] Given the status of these latter authors and the widespread availability and use of their treatise, it is almost certain that Box and Muller were well aware of its contents.
The Box–Muller transform 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 and maps them to two standard, normally distributed samples. The polar form takes two samples from a different interval,, 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.[3] The ziggurat algorithm gives a more efficient method for scalar processors (e.g. old CPUs), while the Box–Muller transform is superior for processors with vector units (e.g. GPUs or modern CPUs).[4]
Suppose and are independent samples chosen from the uniform distribution on the unit interval . Letand
Then Z0 and Z1 are independent random variables with a standard normal distribution.
The derivation[5] 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 and (shown above) in the corresponding polar coordinates are also independent and can be expressed asand
Because is the square of the norm of the standard bivariate normal variable, 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 above is a simple way of generating the required exponential variate.
See main article: Marsaglia polar method. The polar form was first proposed by J. Bell[6] and then modified by R. Knop.[7] 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. A slightly different form is described as "Algorithm P" by D. Knuth in The Art of Computer Programming.[8]
Given and, independent and uniformly distributed in the closed interval, set . If or, discard u and v, and try another pair . Because and 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, too. The latter can be seen by calculating the cumulative distribution function for s in the interval . This is the area of a circle with radius , divided by
\pi
2\pi
We now identify the value of s with that of U1 and
\theta/(2\pi)
\cos\theta=\cos2\piU2
\sin\theta=\sin2\piU2
\cos\theta=u/R=u/\sqrt{s}
Just as the basic form produces two standard normal deviates, so does this alternate calculation.and
The polar method differs from the basic method in that it is a type of rejection sampling. It discards some generated random numbers, but can be 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.[9] Avoiding the use of expensive trigonometric functions improves speed over the basic form.[6] It discards of the total input uniformly distributed random number pairs generated, i.e. discards uniformly distributed random number pairs per Gaussian random number pair generated, requiring 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.[10] 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 complexand just separate the real and imaginary parts.
Note: To explicitly calculate the complex-polar form use the following substitutions in the general form,
Let and Then
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 and a conditional loop.
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
U1
U2
2(1-\Phi(\delta))\simeq2.738 x 10-11
\Phi(\delta)
\delta=9.419
2(1-\Phi(\delta))<5 x 10-21
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
\sigma2
Z
X=Z\sigma+\mu
\mu
\sigma
generateGaussianNoise
function.//"mu" is the mean of the distribution, and "sigma" is the standard deviation.std::pair
function rand_normal
Generate `2N` samples from the standard normal distribution using the Box-Muller method."""function boxmullersample(N) z = Array(undef,N,2); for i in axes(z,1) z[i,:] .= sincospi(2 * rand); z[i,:] .*= sqrt(-2 * log(rand)); end vec(z)end
""" boxmullersample(n,μ,σ)
Generate `n` samples from the normal distribution with mean `μ` and standard deviation `σ` using the Box-Muller method."""function boxmullersample(n,μ,σ) μ .+ σ*boxmullersample(cld(n,2))[1:n];end