Neha Patil (Editor)

Marsaglia polar method

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

The polar method (attributed to George Marsaglia, 1964) is a pseudo-random number sampling method for generating a pair of independent standard normal random variables. While it is superior to the Box–Muller transform, the Ziggurat algorithm is even more efficient.

Contents

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 (xy) in the square −1 < x < 1, −1 < y < 1 until

s = x 2 + y 2 < 1 ,

and then returning the required pair of normal random variables as

x 2 ln ( s ) s ,     y 2 ln ( s ) s ,

or, equivalently,

x s 2 ln ( s ) ,     y s 2 ln ( s ) ,

where x / s and y / s represent the cosine and sine of the angle that the vector (x, y) makes with x axis.

Theoretical basis

The underlying theory may be summarized as follows:

If u is uniformly distributed in the interval 0 ≤ u < 1, then the point (cos(2πu), sin(2πu)) is uniformly distributed on the unit circumference x2 + y2 = 1, and multiplying that point by an independent random variable ρ whose distribution is

Pr ( ρ < a ) = 0 a r e r 2 / 2 d r

will produce a point

( ρ cos ( 2 π u ) , ρ sin ( 2 π u ) )

whose coordinates are jointly distributed as two independent standard normal random variables.

History

This idea dates back to Laplace, whom Gauss credits with finding the above

I = e x 2 / 2 d x

by taking the square root of

I 2 = e ( x 2 + y 2 ) / 2 d x d y = 0 2 π 0 r e r 2 / 2 d r d θ .

The transformation to polar coordinates makes evident that θ is uniformly distributed (constant density) from 0 to 2π, and that the radial distance r has density

r e r 2 / 2 .

(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,

Practical considerations

A direct application of this idea,

x = 2 ln ( u 1 ) cos ( 2 π u 2 ) , y = 2 ln ( u 1 ) sin ( 2 π u 2 )

is called the Box Muller transform, in which the chi variate is usually generated as

2 ln ( u 1 ) ,

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. Notably for Intel-based machines, one can use fsincos assembler instruction or the expi instruction (available e.g. in D), to calculate complex

expi ( z ) = e i z = cos ( z ) + i sin ( z ) ,

and just separate the real and imaginary parts.

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 x 2 + y 2 radius. In particular, a random point (xy) inside the unit circle is projected onto the unit circumference by setting s2 = x2 + y2 and forming the point

( x s , y 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. Also this procedure requires about 27% more evaluations of the underlying random number generator (only π / 4 79 % of generated points lie inside of unit circle).

That random point on the circumference is then radially projected the required random distance by means of

2 ln ( 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.

Implementation

Simple implementation in Java using the mean and standard deviation:

An implementation, not thread safe, in C++ using the mean and standard deviation:

References

Marsaglia polar method Wikipedia