Marsaglia polar method

Marsaglia polar method


The polar method (attributed to George Marsaglia, 1964[1]) 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[citation needed], the Ziggurat algorithm is even more efficient[citation needed].

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\sqrt{\frac{-2\ln(s)}{s}}\,,\ \ y\sqrt{\frac{-2\ln(s)}{s}}.

Contents

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(\rho<a)=\int_0^a re^{-r^2/2}\,dr

will produce a point

 \left(\rho\cos(2\pi u),\rho\sin(2\pi u)\right)

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=\int_{-\infty}^\infty e^{-x^2/2}\,dx

by taking the square root of

I^2 = \int_{-\infty}^\infty\int_{-\infty}^\infty e^{-(x^2+y^2)/2}\,dx\,dy
    =\int_0^{2\pi}\int_0^\infty re^{-r^2/2} \, d\theta \, dr.

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

re^{-r^2/2}. \,

(Note that 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=\sqrt{-2\ln(u_1)}\cos(2\pi u_2),\quad  y=\sqrt{-2\ln(u_1)}\sin(2\pi u_2)

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

\sqrt{-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[citation needed]. Notably for Intel based machines, one can use fsincos assembler instruction or the expi instruction (available i.e. in D), to calculate complex

\text{expi}(z) = e^{i z} = \cos(z) + i \sin(z), \,

and just separate the real and imaginary parts.

The polar method, in which a random point (xy) inside the unit circle is projected onto the unit circumference by setting s = x2 + y2 and forming the point

\left( \frac{x}{\sqrt{s}}, \frac{y}{\sqrt{s}} \right), \,

is a faster procedure. 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.[citation needed] 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

\sqrt{-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;

private static double spare;
private static boolean spareready = false;
 
public static synchronized double getGaussian(double center, double stdDev) {
        if (spareready) {
                spareready = false;
                return spare * stdDev + center;
        }
        double u, v, s;
        do {
                u = Math.random() * 2 - 1;
                v = Math.random() * 2 - 1;
                s = u * u + v * v;
        } while (s >= 1 || s == 0);
        spare = v * Math.sqrt(-2 * Math.log(s) / s);
        spareready = true;
        return center + stdDev * u * Math.sqrt(-2.0 * Math.log(s) / s);
}

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • George Marsaglia — is a mathematician and computer scientist. He is perhaps best known for establishing the lattice structure [ [http://www.pnas.org/content/61/1/25.full.pdf+html G. Marsaglia, Random numbers fall mainly in the planes , Proc. Natl. Acad. Sci. 61(1) …   Wikipedia

  • Box-Muller transform — A Box Muller transform (by George Edward Pelham Box and Mervin Edgar Muller 1958) [ [http://projecteuclid.org/DPubS/Repository/1.0/Disseminate?view=body id=pdf 1 handle=euclid.aoms/1177706645 G. E. P. Box and Mervin E. Muller, A Note on the… …   Wikipedia

  • Normal distribution — This article is about the univariate normal distribution. For normally distributed vectors, see Multivariate normal distribution. Probability density function The red line is the standard normal distribution Cumulative distribution function …   Wikipedia

  • List of numerical analysis topics — This is a list of numerical analysis topics, by Wikipedia page. Contents 1 General 2 Error 3 Elementary and special functions 4 Numerical linear algebra …   Wikipedia

  • Ziggurat algorithm — The ziggurat algorithm is an algorithm to generate random numbers from a non uniform distribution. It belongs to the class of rejection sampling algorithms and can be used for choosing values from a monotone decreasing probability distribution.… …   Wikipedia

  • NAS Parallel Benchmarks — Тип промышленный бенчмарк Разработчик NASA Advanced Supercomputing Division Написана на Фортран, Си Первый выпуск 1991 (1991) Аппаратная платформа кросс платформенная …   Википедия

  • List of mathematics articles (M) — NOTOC M M estimator M group M matrix M separation M set M. C. Escher s legacy M. Riesz extension theorem M/M/1 model Maass wave form Mac Lane s planarity criterion Macaulay brackets Macbeath surface MacCormack method Macdonald polynomial Machin… …   Wikipedia

  • Pseudo-random number sampling — or non uniform pseudo random variate generation is the numerical practice of generating pseudo random numbers that are distributed according to a given probability distribution. Methods of sampling a non uniform distribution are typically based… …   Wikipedia

  • NAS Parallel Benchmarks — Original author(s) NASA Numerical Aerodynamic Simulation Program Developer(s) NASA Advanced Supercomputing Division Stable release 3.3 Development status Active …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”