Sampling from the Normal Distribution

by Weidong Liang

Beijing, 2014.4


Introduction

The Box-Muller Transform transforms a two-dimentional constinuous uniform distribution to a two dimensional bivariate normal distribution using: $$ z_1 = \sqrt{-2 ln u_1} cos(2 \pi u_2) $$ $$ z_2 = \sqrt{-2 ln u_1} sin(2 \pi u_1) $$ where \( Z_1, Z_2 \sim N(0, 1) \) and \( U_1, U_2 \sim U(0, 1) \).

This can be verified by using the above equation, solve for \( u_1, u_2 \) to obtain: $$ u_1 = e ^ {- \frac{(z_1 ^ 2 + z_2 ^ 2)}{2} } $$ $$ u_2 = \frac{1}{(2 \pi)} tan^{-1}(\frac{z_2}{z_1}) $$ taking the Jacobian to obtain: $$ \frac{ \partial p_{(U_1, U_2)}(u_1, u_2) }{ \partial p_{(Z_1, Z_2)}(z_1, z_2) } = { \begin{bmatrix} \ \frac{ \partial p_{U_1}(u_1) }{ \partial p_{Z_1}(z_1) } & \frac{ \partial p_{U_1}(u_1) }{ \partial p_{Z_1}(z_2) } \\ \ \frac{ \partial p_{U_2}(u_2) }{ \partial p_{Z_1}(z_1) } & \frac{ \partial p_{U_2}(u_2) }{ \partial p_{Z_2}{z_2} } \end{bmatrix} } = -( \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ z_1 ^ 2 }{ 2 }} )( \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ z_2 ^ 2 }{ 2 }} ) $$ Using the change of variable rules of density function, we have $$ p_{(Z_1, Z_2)}(z_1, z_2) = p_{(U_1, U_2)}(u_1, u_2) \left| \frac{ \partial p_{(U_1, U_2)}(u_1, u_2) }{ \partial p_{(Z_1, Z_2)}(z_1, z_2) } \right| = p_{(U_1, U_2)}(u_1, u_2) ( \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ Z_1 ^ 2 }{ 2 }} )( \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ Z_2 ^ 2 }{ 2 }} ) $$ since the probability density function of uniform distribution is 1, i.e. \( p_{(U_1, U_2)}(u_1, u_2) = p_{U_1}(u_1) \cdot p_{U_2}(u_2) = 1 \), therefore $$ p_{(Z_1, Z_2)}(z_1, z_2) = ( \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ Z_1 ^ 2 }{ 2 }} ) \cdot ( \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ Z_2 ^ 2 }{ 2 }} ) = p_{Z_1}(z_1) \cdot p_{Z_2}(z_2) \\ p_{Z_1}(z_1) = \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ Z_1 ^ 2 }{ 2 }} \\ p_{Z_2}(z_2) = \frac{ 1 }{ \sqrt{2 \pi} } e^{-\frac{ Z_2 ^ 2 }{ 2 }} $$ hence, \( Z_1 \sim N(0, 1), Z_2 \sim N(0, 1) \).


Simulation

For the following simulation, we use Box-Muller Transform to generate samples from the Normal distribution, and then compare the histogram of these samples against the theoretical probability density function (pdf).

Parameters:

NumberOfBuckets: NumberOfSamples: Lambda:


Algorithm

GaussianSamplerWithBoxMullerTransform()   // Draw a sample from a standard Normal distribution.

  1. u_1 <- U(0, 1)   // generate u_1 from a uniform random distribution [0, 1]
  2. u_2 <- U(0, 1)   // generate u_2 from a uniform random distribution [0, 1]
  3. z_1 = \( \sqrt{-2 ln u_1} cos(2 \pi u_2) \)   // Apply Box-Muller transform
  4. return z_1


Implementation


Note

The Change of Variable Technique

Consider a change of variables \(x = g(y) \), then a function \( f(x) \) becomes \( \tilde{f}(x) = f(g(x)) \). Let \( p_x(x) \) corresponds to a density \( p_y(y) \) with respect to the new variable \( y \), where the suffices denote that they are different densities. Observations falling in the range \( (x, x + \delta x ) \) will, for small values of \( \delta x \), be transformed into the range of \( (y, y + \delta y ) \) where \( p_x(x) \delta x \approx p_y(y) \delta y \) and hence: $$ p_y(y) = p_x(x) \cdot \left|{ \frac{dx}{dy} }\right| $$


Reference


comments powered by Disqus