The Marsaglia polar method[1] is a pseudo-random number sampling method for generating a pair of independent standard normal random variables.[2]
Standard normal random variables are frequently used in computer science, computational statistics, and in particular, in applications of the Monte Carlo method.
The polar method works by choosing random points (x, y) in the square -1 < x < 1, -1 < y < 1 until
0<s=x2+y2<1,
and then returning the required pair of normal random variables as
x\sqrt{ | -2ln(s) |
s |
or, equivalently,
x | |
\sqrt{s |
where
x/\sqrt{s}
y/\sqrt{s}
The underlying theory may be summarized as follows:
If u is uniformly distributed in the interval0 ≤ u < 1, then the point(cos(2πu), sin(2πu))is uniformly distributed on the unit circumferencex2 + y2 = 1, and multiplying that point by an independentrandom variable ρ whose distribution is
a | |
\Pr(\rho<a)=\int | |
0 |
-r2/2 | |
re |
dr
will produce a point
\left(\rho\cos(2\piu),\rho\sin(2\piu)\right)
whose coordinates are jointly distributed as two independent standardnormal random variables.
This idea dates back to Laplace, whom Gauss credits with finding the above
infty | |
I=\int | |
-infty |
-x2/2 | |
e |
dx
by taking the square root of
I2=
infty | |
\int | |
-infty |
-(x2+y2)/2 | |
e |
dxdy
2\pi | |
=\int | |
0 |
infty | |
\int | |
0 |
-r2/2 | |
re |
drd\theta.
The transformation to polar coordinates makes evident that θ isuniformly distributed (constant density) from 0 to 2π, and that theradial distance r has density
-r2/2 | |
re |
.
(r2 has the appropriate chi square distribution.)
This method of producing a pair of independent standard normal variates by radially projecting a random point on the unit circumference to a distance given by the square root of a chi-square-2 variate is called the polar method for generating a pair of normal random variables,
A direct application of this idea,
x=\sqrt{-2ln(u1)}\cos(2\piu2), y=\sqrt{-2ln(u1)}\sin(2\piu2)
is called the Box–Muller transform, in which the chi variate is usuallygenerated as
\sqrt{-2ln(u1)},
but that transform requires logarithm, square root, sine and cosine functions. On some processors, the cosine and sine of the same argument can be calculated in parallel using a single instruction.[3] Notably for Intel-based machines, one can use fsincos assembler instruction or the expi instruction (available e.g. in D), to calculate complex
\operatorname{expi}(z)=ei=\cos(z)+i\sin(z),
and just separate the real and imaginary parts.
Note: To explicitly calculate the complex-polar form use the following substitutions in the general form,
Let
r=\sqrt{-2ln(u1)}
z=2\piu2.
rei=\sqrt{-2ln(u1)}
i2\piu2 | |
e |
=\sqrt{-2ln(u1)}\left[\cos(2\piu2)+i\sin(2\piu2)\right].
In contrast, the polar method here removes the need to calculate a cosine and sine. Instead, by solving for a point on the unit circle, these two functions can be replaced with the x and y coordinates normalized to the
\sqrt{x2+y2}
s=x2+y2
\left(
x | |
\sqrt{s |
which is a faster procedure than calculating the cosine and sine. Some researchers argue that the conditional if instruction (for rejecting a point outside of the unit circle), can make programs slower on modern processors equipped with pipelining and branch prediction.[4] Also this procedure requires about 27% more evaluations of the underlying random number generator (only
\pi/4 ≈ 79\%
That random point on the circumference is then radially projected the required random distance by means of
\sqrt{-2ln(s)},
using the same s because that s is independent of the random point on the circumference and is itself uniformly distributed from 0 to 1.
Simple implementation in Java using the mean and standard deviation:
public static synchronized double generateGaussian(double mean, double stdDev)
A non-thread safe implementation in C++ using the mean and standard deviation:
C++11 GNU GCC libstdc++'s implementation of std::normal_distribution uses the Marsaglia polar method, as quoted from herein.
A simple Julia implementation:
Generate `2N` samples from the standard normal distribution using the Marsaglia method."""function marsagliasample(N) z = Array(undef,N,2); for i in axes(z,1) s = Inf; while s > 1 z[i,:] .= 2rand(2) .- 1; s = sum(abs2.(z[i,:])) end z[i,:] .*= sqrt(-2log(s)/s); end vec(z)end
""" marsagliasample(n,μ,σ)
Generate `n` samples from the normal distribution with mean `μ` and standard deviation `σ` using the Marsaglia method."""function marsagliasample(n,μ,σ) μ .+ σ*marsagliasample(cld(n,2))[1:n];endThe for loop can be parallelized by using the Threads.@threads
macro.