Tutorials › Exam ASTAM - Advanced Short-Term Actuarial Mathematics › Normal Sampling With Box-Muller

Exam ASTAM · Random Variate Generation · Tutorial 599 of 979

Normal Sampling With Box-Muller

Learn to convert pairs of independent uniform draws into independent standard normal variates using the Box-Muller transformation.

Beginner 10 min read

What You'll Learn

  • Derive the Box-Muller radius and angle from two independent uniform variates.
  • Show from the joint density why the transformed outputs are independent standard normal variates.
  • Apply the transformation step by step to exact uniform inputs.
  • Use the radius and squared-output identities to check calculations.
  • Avoid endpoint, scaling, and uniform-reuse errors when generating normal variates.

Why Generate Normal Variates From Uniforms?

Simulation methods often begin with uniform variates and transform them into draws from a target distribution. The Box-Muller transformation is a direct way to turn two independent uniform variates into two independent standard normal variates. It is useful, for example, when a simulation model requires normally distributed random inputs but the available random-number generator supplies uniforms.

The key is to use both uniforms together: one determines a radius and the other an angle. The resulting pair is not merely two numbers with mean zero and variance one. Under the model assumptions, the pair has the joint standard normal distribution, so its components are independent.

Definition: Let \(U_1\) and \(U_2\) be independent random variables, each uniformly distributed on the open interval \((0,1)\). Define $$ R=\sqrt{-2\ln U_1}, \qquad \Theta=2\pi U_2, $$ and then define $$ Z_1=R\cos\Theta, \qquad Z_2=R\sin\Theta. $$ The Box-Muller result is that \(Z_1\) and \(Z_2\) are independent standard normal variates: \(Z_1\sim N(0,1)\), \(Z_2\sim N(0,1)\), and \(Z_1\) and \(Z_2\) are independent.

The assumption that the uniforms are independent matters. It ensures that the radius and angle are independent, which is part of the argument establishing the joint distribution of the outputs. In simulation, the uniforms are usually produced by a pseudo-random number generator; the derivation treats them as independent ideal uniforms.

Why the Transformation Works

The radius has a convenient distribution because if \(U_1\) is uniform on \((0,1)\), then \(T=-\ln U_1\) has an exponential distribution with rate 1. Indeed, for \(t\geq 0\), $$ P(T>t)=P(-\ln U_1>t)=P(U_1<e^{-t})=e^{-t}. $$ Thus \(T\) has density \(e^{-t}\) for \(t>0\). Since \(R=\sqrt{2T}\), the transformation \(t=r^2/2\) has derivative \(dt/dr=r\), giving \(f_R(r)=r e^{-r^2/2}\) for \(r>0\). Independently, \(\Theta=2\pi U_2\) is uniform on \((0,2\pi)\), with density \(1/(2\pi)\).

1
Find the radius density.
From \(T=-\ln U_1\), \(T\) has density \(e^{-t}\) for \(t>0\). Setting \(T=R^2/2\) gives \(f_R(r)=r e^{-r^2/2}\) for \(r>0\).
2
Combine radius and angle.
The radius and angle are independent, so their joint density is \(f_{R,\Theta}(r,\theta)=\frac{r}{2\pi}e^{-r^2/2}\) for \(r>0\) and \(0<\theta<2\pi\).
3
Convert to Cartesian coordinates.
The transformation is \(Z_1=r\cos\theta\), \(Z_2=r\sin\theta\). Its forward Jacobian determinant has absolute value \(r\), so the density in the \(Z_1,Z_2\) coordinates is the radius-angle density divided by \(r\).
4
Factor the resulting density.
Since \(r^2=Z_1^2+Z_2^2\), the joint density becomes \(\frac{1}{2\pi}e^{-(Z_1^2+Z_2^2)/2}\), which is the product of two standard normal densities.

In full, the change-of-variables calculation is

$$ f_{Z_1,Z_2}(z_1,z_2) = \frac{f_{R,\Theta}(r,\theta)}{r} = \frac{1}{2\pi}\exp\left(-\frac{r^2}{2}\right) = \frac{1}{2\pi}\exp\left(-\frac{z_1^2}{2}\right) \exp\left(-\frac{z_2^2}{2}\right). $$

The final expression factors as \(\phi(z_1)\phi(z_2)\), where \(\phi(z)=\frac{1}{\sqrt{2\pi}}e^{-z^2/2}\) is the standard normal density. A joint density that factors into the product of its marginal densities establishes independence; each marginal is standard normal. This proves both the claimed distributions and independence. The argument applies to ideal independent continuous uniforms and does not mean that any two computed numerical outputs must look unrelated in a particular run.

Applying the Transformation

For each pair of uniforms, calculate the radius and angle first, then apply cosine and sine. Keep the pairing consistent: the same \(R\) and \(\Theta\) produce both \(Z_1\) and \(Z_2\). The identity $$ Z_1^2+Z_2^2=R^2=-2\ln U_1 $$ is a useful arithmetic check. It checks the squared distance from the origin, though it does not by itself prove that the values are normally distributed or independent.

Worked Example: A Pair With a One-Unit Radius

A simulation uses the independent uniform inputs \(U_1=e^{-1/2}\) and \(U_2=1/6\). Use Box-Muller to calculate both normal outputs. State the method and check the result.

Method. Apply the radius-angle form of Box-Muller. The common mistake to avoid is calculating only one output and discarding the second, even though the transformation supplies a pair.

First calculate the radius: $$ R=\sqrt{-2\ln(e^{-1/2})} =\sqrt{-2(-1/2)} =\sqrt{1} =1. $$

Next calculate the angle: $$ \Theta=2\pi(1/6)=\pi/3. $$

Therefore, $$ Z_1=1\cos(\pi/3)=1(0.5)=0.5, \qquad Z_2=1\sin(\pi/3)=1(0.866025\ldots)=0.866025\ldots. $$ Rounded only at the final step to four decimal places, the outputs are \(Z_1=0.5000\) and \(Z_2=0.8660\).

Check. The sum of their unrounded squares is \(0.5^2+(\sqrt{3}/2)^2=0.25+0.75=1\). The radius calculation gives \(R^2=1\), so the check agrees.

Worked Example: An Output Pair in the Second Quadrant

A different simulation step supplies \(U_1=e^{-2}\) and \(U_2=3/8\), independently. Calculate the two Box-Muller outputs and check their squared sum.

Method. Calculate \(R\) and \(\Theta\) from the stated uniforms, then use the two coordinate formulas. A common mistake is to use \(-\ln U_1\) as \(R^2\); the required identity is \(R^2=-2\ln U_1\).

The radius is $$ R=\sqrt{-2\ln(e^{-2})} =\sqrt{-2(-2)} =\sqrt{4} =2. $$ The angle is $$ \Theta=2\pi(3/8)=3\pi/4. $$

At this angle, \(\cos(3\pi/4)=-\sqrt{2}/2\) and \(\sin(3\pi/4)=\sqrt{2}/2\). Thus $$ Z_1=2\left(-\frac{\sqrt{2}}{2}\right)=-\sqrt{2}\approx-1.4142, \qquad Z_2=2\left(\frac{\sqrt{2}}{2}\right)=\sqrt{2}\approx1.4142. $$ Rounded to four decimal places, the pair is \((-1.4142,1.4142)\).

Check. The unrounded squared sum is \((-\sqrt{2})^2+(\sqrt{2})^2=2+2=4\), matching \(R^2=4\). The negative first coordinate is also consistent with an angle in the second quadrant.

Worked Example: Generating Two Pairs From Four Uniforms

A simulation generates two independent uniform pairs. The first is \(U_1=e^{-1/8}\), \(U_2=1/4\), and the second is \(U_1=e^{-9/8}\), \(U_2=1/3\). Calculate all four normal outputs. Assume all four uniforms are mutually independent.

Method. Apply Box-Muller separately to each pair. To obtain four independent normal outputs, use two disjoint pairs of independent uniforms. Reusing one of the first pair's uniforms in the second pair would not meet the stated independence assumption.

For the first pair, $$ R_1=\sqrt{-2\ln(e^{-1/8})}=\sqrt{1/4}=0.5, \qquad \Theta_1=2\pi(1/4)=\pi/2. $$ Thus $$ Z_1=0.5\cos(\pi/2)=0, \qquad Z_2=0.5\sin(\pi/2)=0.5. $$

For the second pair, $$ R_2=\sqrt{-2\ln(e^{-9/8})}=\sqrt{9/4}=1.5, \qquad \Theta_2=2\pi(1/3)=2\pi/3. $$ Since \(\cos(2\pi/3)=-1/2\) and \(\sin(2\pi/3)=\sqrt{3}/2\), $$ Z_3=1.5(-1/2)=-0.75, \qquad Z_4=1.5(\sqrt{3}/2)=\frac{3\sqrt{3}}{4}\approx1.299038\ldots. $$ Rounded to four decimal places, the four outputs are \(0.0000,\ 0.5000,\ -0.7500,\ 1.2990\).

Check. For the first pair, \(0^2+0.5^2=0.25=R_1^2\). For the second, \((-0.75)^2+(3\sqrt{3}/4)^2=0.5625+27/16=0.5625+1.6875=2.25=R_2^2\). Each pair passes the radius check.

Implementation Checks and Common Traps

The transformation is concise, but several details matter in an implementation:

  • Use the factor of 2. The radius is \(\sqrt{-2\ln U_1}\), not \(\sqrt{-\ln U_1}\). Omitting the 2 changes the distribution and makes the squared radius too small by a factor of 2.
  • Use an angle in radians. The formula \(\Theta=2\pi U_2\) produces radians. If a calculator is in degree mode, convert the angle or change the calculator setting before evaluating sine and cosine.
  • Keep the inputs inside \((0,1)\). If \(U_1=0\), the logarithm is undefined; if \(U_1=1\), the radius is zero and the resulting angle does not have the continuous joint distribution derived above. Practical generators should supply values strictly between 0 and 1 for this use.
  • Do not infer independence from a single pair. Independence is a property of the joint distribution under the model assumptions, not a visual feature that can be established by inspecting two realized values.
  • Do not reuse uniforms as if they were independent. Outputs from separate Box-Muller applications are guaranteed independent under the derivation when the input uniforms for those applications are mutually independent.
Key takeaway: With independent \(U_1,U_2\sim \text{Uniform}(0,1)\), set \(R=\sqrt{-2\ln U_1}\) and \(\Theta=2\pi U_2\). Then \(R\cos\Theta\) and \(R\sin\Theta\) are independent standard normal variates. Check arithmetic using \(Z_1^2+Z_2^2=-2\ln U_1\), while remembering that the check alone does not establish the distribution.

Check Your Understanding

For each problem, use the stated independent uniform inputs and round numerical outputs to four decimal places when needed.

  1. For \(U_1=e^{-1/2}\) and \(U_2=1/2\), calculate both normal outputs and verify their squared sum. (Answer: \(Z_1=-1.0000,\ Z_2=0.0000\); squared sum \(=1\).)
  2. For \(U_1=e^{-1/2}\) and \(U_2=1/4\), calculate the radius, angle, and two outputs. (Answer: \(R=1,\ \Theta=\pi/2,\ Z_1=0.0000,\ Z_2=1.0000\).)
  3. For \(U_1=e^{-9/8}\) and \(U_2=1/6\), calculate both outputs and check their squared sum. (Answer: \(Z_1=0.7500,\ Z_2=1.2990\); squared sum \(=2.25\).)
  4. For \(U_1=e^{-2}\) and \(U_2=5/6\), calculate both outputs. Identify the quadrant before evaluating the trigonometric functions. (Answer: \(\Theta=5\pi/3,\ Z_1=1.0000,\ Z_2=-1.7321\).)
  5. A program receives four mutually independent uniforms and uses the first two as one Box-Muller pair and the last two as another. How many independent standard normal outputs does it generate, and what condition makes the outputs from the two pairs independent? (Answer: Four independent standard normal outputs, provided all four uniforms are independent and each pair is transformed by the stated Box-Muller formulas.)
Clinical Biostats