STAT 536 — Statistical Computing
Fall 2026
The Problem. How do we generate samples from arbitrary distributions?
Standing assumption
We can generate \(U \sim \Uniform(0,1)\) using pseudo-random number generators.
The first two are this week. Accept-reject is next week.
Theorem (Probability Integral Transform)
Let \(F\) be any CDF and let \(F^{-}(u) = \inf\{x : F(x) \geq u\}\). Then:
Essentially, this says that the CDF is a two-way bridge between uniform draws and target draws: part (1) turns a uniform into a draw from any distribution by way of its generalized inverse, and part (2) reverses the process when \(F\) is continuous.
The theorem needs no inverse function in the usual sense. Define
\[F^{-}(u) = \inf\{x : F(x) \geq u\}, \qquad 0 < u < 1 .\]
Right-continuity of \(F\) gives the equivalence that drives everything below:
\[F^{-}(u) \leq x \iff u \leq F(x) .\]
We can think of this as saying: asking whether the generated value is at most \(x\) is the same as asking whether the uniform draw is at most \(F(x)\), which is why the proof of part (1) is one line.
When \(F\) is continuous and strictly increasing, \(F^{-} = F^{-1}\). Otherwise \(F^{-}\) is what the algorithm actually computes, and the discrete case is the example.
For any \(x \in \mathbb{R}\), the equivalence of the previous slide gives
\[P[F^{-}(U) \leq x] = P[U \leq F(x)] \quad (\text{right-continuity of } F)\]
\[P[U \leq F(x)] = F(x) \quad (\text{since } U \sim \Uniform(0,1))\]
Therefore \(F^{-}(U)\) has CDF \(F\).
Generate \(X \sim \Exp(\lambda)\).
CDF: \(F(x) = 1 - e^{-\lambda x}\) for \(x \geq 0\)
Set \(u = F(x) = 1 - e^{-\lambda x}\)
Solve for \(x\):
\[ \begin{aligned} u &= 1 - e^{-\lambda x}\\ 1 - u &= e^{-\lambda x}\\ \log(1-u) &= -\lambda x\\ x &= -\frac{1}{\lambda}\log(1-u) \end{aligned} \]
Since \(1-U \sim \Uniform(0,1)\) when \(U \sim \Uniform(0,1)\):
\[X = -\frac{1}{\lambda}\log(U) \sim \Exp(\lambda)\]
Given: \(P[X = x_i] = p_i\) for \(x_1 < x_2 < \cdots < x_k\), with \(\sum p_i = 1\).
Method 1: Linear Search
Complexity: \(O(k)\) per sample
Method 2: Binary Search
Complexity: \(O(\log k)\) per sample
The indexing rule \(F_{i-1} < U \leq F_i\) is exactly \(X = F^{-}(U)\).
Limitations.
\[F(x) = \Phi(x) = \int_{-\infty}^x \frac{1}{\sqrt{2\pi}} e^{-t^2/2}\, dt\]
Solution. Use transformation methods or accept-reject algorithms.
The third panel uses accept-reject, which is next week.
Key idea. Use known relationships between distributions to generate complex from simple.
Theorem (General Transformation). If \(X\) has PDF \(f_X\) and \(Y = g(X)\) where \(g\) is strictly monotone and continuously differentiable with \(g' \neq 0\) on the support of \(X\), then
\[f_Y(y) = f_X(g^{-1}(y)) \left|\frac{d}{dy}g^{-1}(y)\right| .\]
Essentially, this says that the density of a transformed variable is the original density evaluated at the pre-image, scaled by how much \(g^{-1}\) stretches or compresses locally.
Theorem (Sum of Exponentials \(\to\) Gamma). If \(X_1, \ldots, X_\alpha \iid \Exp(\beta)\), then
\[Y = \sum_{i=1}^\alpha X_i \sim \Gam(\alpha, \beta)\]
when \(\alpha\) is a positive integer (rate parameterization throughout: \(\E[Y] = \alpha/\beta\)).
We can think of this as saying that a \(\Gam(\alpha, \beta)\) draw, when \(\alpha\) is a positive integer, is the sum of \(\alpha\) consecutive independent exponential waiting times: the time to the \(\alpha\)-th arrival of a rate-\(\beta\) Poisson process.
Algorithm for \(\Gam(\alpha, \beta)\), \(\alpha \in \mathbb{N}\).
Special cases. \(\chi^2_k = \Gam(k/2, 1/2)\), and this algorithm applies when \(k\) is even; \(\Exp(\lambda) = \Gam(1, \lambda)\).
Theorem (Gamma Ratio \(\to\) Beta). If \(X \sim \Gam(a, \lambda)\) and \(Y \sim \Gam(b, \lambda)\) are independent, then
\[\frac{X}{X + Y} \sim \Betad(a, b) .\]
Essentially, this says that the share of the total contributed by one of two independent gamma variables, when they share the same rate, follows a beta distribution.
Proof sketch. Use the transformation \((U,V) = (X/(X+Y), X+Y)\) with Jacobian \(J = V\).
Algorithm for \(\Betad(a, b)\), \(a, b \in \mathbb{N}\).
Theorem (Box–Muller)
If \(U_1, U_2 \iid \Uniform(0,1)\), then
\[ \begin{aligned} X_1 &= \sqrt{-2\log U_1}\, \cos(2\pi U_2) \\ X_2 &= \sqrt{-2\log U_1}\, \sin(2\pi U_2) \end{aligned} \]
are independent \(\Normal(0,1)\) random variables.
We can think of this as saying that two uniform draws, passed through the right polar-coordinate recipe, produce two independent standard normals.
Recall the \(\Exp(\lambda)\) inverse transform from the first act of this lecture? At \(\lambda = 1/2\), it gives \(R^2 = -2\log U_1 \sim \chi^2_2\), so the squared radius is an inverse-transform exponential draw.
Define the transformation:
\[ \begin{aligned} R &= \sqrt{X_1^2 + X_2^2} = \sqrt{-2\log U_1}\\ \Theta &= \operatorname{atan2}(X_2, X_1) = 2\pi U_2 \end{aligned} \]
Inverting: \(u_1 = e^{-(x_1^2 + x_2^2)/2}\) and \(u_2 = \frac{1}{2\pi}\operatorname{atan2}(x_2, x_1)\).
Compute the Jacobian:
\[J = \det \begin{pmatrix} \frac{\partial u_1}{\partial x_1} & \frac{\partial u_1}{\partial x_2}\\[2pt] \frac{\partial u_2}{\partial x_1} & \frac{\partial u_2}{\partial x_2} \end{pmatrix} = -\frac{1}{2\pi} e^{-(x_1^2 + x_2^2)/2}\]
Joint density of \((X_1, X_2)\):
\[f_{X_1,X_2}(x_1,x_2) = |J| \cdot 1 = \frac{1}{2\pi} e^{-(x_1^2 + x_2^2)/2}\]
This factors as two independent \(\Normal(0,1)\) densities.
Motivation. Avoid expensive trigonometric functions in Box–Muller.
Algorithm.
Repeat:
until \(0 < S \leq 1\).
Set
\[ \begin{aligned} X_1 &= V_1\sqrt{\frac{-2\log S}{S}}\\ X_2 &= V_2\sqrt{\frac{-2\log S}{S}} \end{aligned} \]
The number comes from area: the disc occupies \(\pi/4 \approx 0.785\) of the square, so that is the acceptance probability.
To generate from a nonstandard target distribution \(F\):
R’s built-in generators already cover the standard families, each with a tuned algorithm of its own: rnorm() inverts \(\Phi\) numerically, and rgamma() uses accept-reject, next week’s method. The recipe matters when the target is nonstandard: a truncated or mixture distribution, a posterior you derived yourself, or any density without a built-in generator.
Theorem. If \(\mathbf{z} = (z_1, \ldots, z_p)' \sim \Normal_p(\mathbf{0}, \mathbf{I}_p)\) and \(\boldsymbol{\Sigma} = \mathbf{L}\mathbf{L}'\) is positive definite (Cholesky, \(\mathbf{L}\) lower triangular), then
\[\mathbf{x} = \boldsymbol{\mu} + \mathbf{L}\mathbf{z} \sim \Normal_p(\boldsymbol{\mu}, \boldsymbol{\Sigma}) .\]
Essentially, this says that multiplying independent standard normals by the Cholesky factor of \(\boldsymbol{\Sigma}\) and shifting by \(\boldsymbol{\mu}\) produces a draw with the desired mean and covariance structure.
A linear transformation of a normal vector is normal, so only the mean and covariance need checking.
\[\E[\mathbf{x}] = \E[\boldsymbol{\mu} + \mathbf{L}\mathbf{z}] = \boldsymbol{\mu} + \mathbf{L}\,\E[\mathbf{z}] = \boldsymbol{\mu}\]
\[\Cov(\mathbf{x}) = \Cov(\mathbf{L}\mathbf{z}) = \mathbf{L}\,\Cov(\mathbf{z})\,\mathbf{L}' = \mathbf{L}\mathbf{I}_p\mathbf{L}' = \boldsymbol{\Sigma}\]
Algorithm.
In R
chol() returns the upper triangular factor \(\mathbf{R}\) with \(\boldsymbol{\Sigma} = \mathbf{R}'\mathbf{R}\), so take L <- t(chol(Sigma)).
| Result | Statement | Role |
|---|---|---|
| PIT (part 1) | \(F^{-}(U) \sim F\) | Uniform to any distribution with a tractable \(F^{-}\) |
| General Transformation | \(f_Y(y) = f_X(g^{-1}(y))\,\lvert(g^{-1})'(y)\rvert\) | A known draw to a related distribution |
| MVN Theorem | \(\boldsymbol{\mu} + \mathbf{L}\mathbf{z} \sim \Normal_p(\boldsymbol{\mu}, \boldsymbol{\Sigma})\) | Independent normals to a correlated normal vector |
Every method this week traces back to a uniform draw, and all three need structure you can exploit: a tractable inverse, or a relationship to a distribution you can already sample.
Next: when neither \(F^{-1}\) nor a transformation is available, accept-reject.