HOME

TheInfoList



OR:

The
Marsaglia Marsaglia is a ''comune'' (municipality) in the Province of Cuneo in the Italian region Piedmont, located about southeast of Turin and about east of Cuneo Cuneo (; pms, Coni ; oc, Coni/Couni ; french: Coni ) is a city and ''comune'' in Pied ...
polar method is a
pseudo-random number sampling Non-uniform random variate generation or pseudo-random number sampling is the numerical practice of generating pseudo-random numbers (PRN) that follow a given probability distribution. Methods are typically based on the availability of a unifo ...
method for generating a pair of independent standard normal random variables. Standard normal random variables are frequently used in
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
,
computational statistics Computational statistics, or statistical computing, is the bond between statistics and computer science. It means statistical methods that are enabled by using computational methods. It is the area of computational science (or scientific computi ...
, and in particular, in applications of the
Monte Carlo method Monte Carlo methods, or Monte Carlo experiments, are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. The underlying concept is to use randomness to solve problems that might be determi ...
. The polar method works by choosing random points (''x'', ''y'') in the square −1 < ''x'' < 1, −1 < ''y'' < 1 until : 0 < s=x^2+y^2 < 1, \, and then returning the required pair of normal
random variable A random variable (also called random quantity, aleatory variable, or stochastic variable) is a mathematical formalization of a quantity or object which depends on random events. It is a mapping or a function from possible outcomes (e.g., the po ...
s as : x\sqrt\,,\ \ y\sqrt, or, equivalently, : \frac \sqrt\,,\ \ \frac \sqrt, where x/\sqrt and y/\sqrt represent the cosine and
sine In mathematics, sine and cosine are trigonometric functions of an angle. The sine and cosine of an acute angle are defined in the context of a right triangle: for the specified angle, its sine is the ratio of the length of the side that is oppo ...
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 ''x''2 + ''y''2 = 1, and multiplying that point by an independent random variable ρ whose distribution is :\Pr(\rho 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 Johann Carl Friedrich Gauss (; german: Gauß ; la, Carolus Fridericus Gauss; 30 April 177723 February 1855) was a German mathematician and physicist who made significant contributions to many fields in mathematics and science. Sometimes refer ...
credits with finding the above :I=\int_^\infty e^\,dx by taking the square root of :I^2 = \int_^\infty\int_^\infty e^\,dx\,dy =\int_0^\int_0^\infty re^ \, dr \, d\theta. 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 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\cos(2\pi u_2),\quad y=\sqrt\sin(2\pi u_2) is called the
Box–Muller transform The Box–Muller transform, by George Edward Pelham Box and Mervin Edgar Muller, is a random number sampling method for generating pairs of independent, standard, normally distributed (zero expectation, unit variance) random numbers, given a ...
, in which the chi variate is usually generated as :\sqrt, 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 : \operatorname(z) = e^ = \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 and z = 2 \pi u_2. Then : \ re^ = \sqrt e^ =\sqrt\left \cos(2 \pi u_2) + i \sin(2 \pi u_2)\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 radius. In particular, a random point (''x'', ''y'') inside the unit circle is projected onto the unit circumference by setting s=x^2+y^2 and forming the point :\left( \frac, \frac \right), \, 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.This effect can be heightened in a GPU generating many variates in parallel, where a rejection on one processor can slow down many other processors. See section 7 of . Also this procedure requires about 27% more evaluations of the underlying random number generator (only \pi/4 \approx 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, \, 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 Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
using the mean and standard deviation: private static double spare; private static boolean hasSpare = false; public static synchronized double generateGaussian(double mean, double stdDev) A non-
thread safe Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without uni ...
implementation in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
using the mean and standard deviation: double generateGaussian(double mean, double stdDev)
C++11 C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
GNU GCC
libstdc++ #REDIRECT C++ Standard Library The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the or ...
's implementation of std::normal_distributio
uses
the Marsaglia polar method, as quoted fro


References

{{Reflist Monte Carlo methods Pseudorandom number generators Non-uniform random numbers