Elements of Statistical Computation

Rejection Sampling

Longhai Li

2026-09-07

1. Motivation

Review: Monte Carlo Integration

To compute I = \int a(\theta)\,f(\theta)\,d\theta = E_f\big[a(\theta)\big], \qquad \theta\sim f(\theta)

draw \theta_1,\ldots,\theta_N \overset{iid}{\sim} f and use \hat I = \frac1N\sum_{i=1}^N a(\theta_i)

The catch: we need to draw from f. For standard distributions R has rnorm, rgamma, etc. For a posterior f(\theta)\propto L(\theta)\pi(\theta) we typically know f only up to a normalizing constant and there is no built-in sampler.

Rejection sampling produces exact iid draws from f using draws from an easier distribution g: reject, reweight, repeat.

The Envelope

Suppose we can find a function g(x) such that

  • g(x)\ge f(x) for all x (an envelope of f), and
  • we can sample from the density g(x)\big/\!\int g(x)\,dx

Neither f nor g needs to be normalized: f may be an unnormalized posterior and g a scaled density M\,g_0(x).

Overall acceptance rate \frac{\int_{-\infty}^{\infty} f(x)\,dx}{\int_{-\infty}^{\infty} g(x)\,dx} = \frac{\text{area under } f}{\text{area under } g}

A tight envelope wastes few draws; a loose one wastes many.

The Envelope (Continued)

At each x, the ratio r = f(x)/g(x)\in[0,1] is the fraction of the green segment lying under f; a proposal at x is kept with probability r.

2. The Algorithm

Rejection Sampling

Input: target f, envelope g\ge f with a sampler for g(x)\big/\!\int g(x)\,dx.

For i = 1,\ldots,n, repeat until acceptance:

  1. Draw x\sim g(x)\big/\!\int g(x)\,dx
  2. Compute r = f(x)/g(x)
  3. Draw U\sim\text{Unif}(0,1)
  4. If U < r, accept: set x_i = x; otherwise return to step 1

Output: x_1,\ldots,x_n \overset{iid}{\sim} f(x)\big/\!\int f(x)\,dx.

  • The number of proposals per accepted draw is Geometric with mean \int g(x)\,dx\big/\!\int f(x)\,dx
  • Only ratios f/g are needed, so unknown normalizing constants cancel as long as g\ge f holds for the versions used

Alternative Description: Points Under a Curve

Repeat until acceptance:

  1. Draw x\sim g
  2. Draw V\sim\text{Unif}\big(0,\,g(x)\big), i.e. V = U\cdot g(x) with U\sim\text{Unif}(0,1)
  3. If V < f(x), accept x; otherwise go back to 1

This is the same algorithm, since V = U g(x) < f(x) \iff U < f(x)/g(x).

Why it works. Steps 1–2 produce a point uniformly distributed in the region under g: (X,V)\sim\text{Unif}(G), \qquad G = \{(x,v): 0<v<g(x)\}

Step 3 keeps the point only if it also lies in the region under f: F = \{(x,v): 0<v<f(x)\}\subset G

A uniform point on G, conditioned on lying in F, is uniform on F. The x-coordinate of a uniform point on F has density proportional to the height f(x). \blacksquare

Alternative Description (Continued)

Points are uniform under g (all markers); the accepted ones (red) are uniform under f, and their x-coordinates are draws from f.

Proof via the Distribution Function

For any t, \begin{aligned} P(X\le t\mid \text{accept}) &= \frac{P(X\le t,\ U<f(X)/g(X))}{P(U<f(X)/g(X))} \\[4pt] &= \frac{\displaystyle\int_{-\infty}^t \frac{f(x)}{g(x)}\cdot\frac{g(x)}{\int g(x)\,dx}\,dx} {\displaystyle\int_{-\infty}^{\infty} \frac{f(x)}{g(x)}\cdot\frac{g(x)}{\int g(x)\,dx}\,dx} = \frac{\int_{-\infty}^t f(x)\,dx}{\int_{-\infty}^{\infty} f(x)\,dx} \end{aligned}

which is the distribution function of the normalized f. The denominator, P(\text{accept}) = \int f(x)\,dx\big/\!\int g(x)\,dx, is the acceptance rate stated earlier.

Example 1: A Uniform Target

f(x) = 2\,I\big(0\le x\le\tfrac12\big), \qquad g(x) = 2\,I(0\le x\le1)

Steps:

  1. X\sim\text{Unif}(0,1) (the density g(x)\big/\!\int g(x)\,dx)
  2. U\sim\text{Unif}(0,1)
  3. r = f(X)/g(X) = \begin{cases}1, & X\le\tfrac12\\ 0, & X>\tfrac12\end{cases}; accept if U<r

Since r\in\{0,1\}, step 3 reduces to: accept if X\le\tfrac12, otherwise redraw. The accepted draws are \text{Unif}(0,\tfrac12), as they should be.

Acceptance rate: \dfrac{\int f(x)\,dx}{\int g(x)\,dx} = \dfrac{1}{2}.

A looser envelope, e.g. g_2(x) = 2\,I(0\le x\le 2), still works but accepts only 1/4 of the proposals.

Example 1 (Continued)

Example 2: Normal from a Double Exponential Envelope

Target f(x) = e^{-x^2/2} (unnormalized N(0,1)). Proposal density g_0(x) = \tfrac12 e^{-|x|} (Laplace), easy to sample by inversion. Since e^{-x^2/2}\le e^{1/2 - |x|} (equality at |x|=1), the envelope g(x) = 2e^{1/2}\,g_0(x) = e^{1/2-|x|} \ \ge\ f(x)

works, with acceptance rate \int f(x)\,dx\big/\!\int g(x)\,dx = \sqrt{2\pi}\big/(2e^{1/2}) \approx 0.76.

rlaplace <- function(n) { u <- runif(n); ifelse(u < 0.5, log(2 * u), -log(2 * (1 - u))) }
f <- function(x) exp(-x^2 / 2); g <- function(x) exp(0.5 - abs(x))
rnorm_reject <- function(n) { out <- numeric(0); tries <- 0
  while (length(out) < n) { x <- rlaplace(n); tries <- tries + n
    out <- c(out, x[runif(n) < f(x) / g(x)]) }
  list(x = out[1:n], acceptance = n / tries) }
res <- rnorm_reject(1e5); c(mean = mean(res$x), sd = sd(res$x), acceptance = res$acceptance)
       mean          sd  acceptance 
0.001898247 0.996775757 0.500000000 

Example 2 (Continued)

3. Adaptive Rejection Sampling

Log-Concave Targets

Finding a good envelope by hand is the hard part of rejection sampling. For a large class of targets it can be automated.

f is log-concave if \log f(x) is a concave function. Many posteriors are: normal, gamma (\alpha\ge1), beta (\alpha,\beta\ge1), logistic-regression posteriors with normal priors, and any product of log-concave factors.

Key fact: a concave function lies below every tangent line. So for tangent lines \ell_1(x),\ell_2(x),\ldots to \log f at points x_1<x_2<\cdots, the piecewise-linear upper hull \log g(x) = \min_j \ell_j(x) = \begin{cases}\ell_1(x), & x\in I_1\\ \ell_2(x), & x\in I_2\\ \ \vdots\end{cases}

satisfies \log g(x)\ge\log f(x), hence g(x) = e^{\ell_j(x)}\ge f(x) on each piece I_j.

Log-Concave Targets (Continued)

Left: tangents at three points bound the concave \log f from above. Right: exponentiating gives a piecewise-exponential envelope g\ge f.

Sampling from the Piecewise-Exponential Envelope

On piece I_j = (z_{j-1}, z_j) the envelope is g(x) = e^{a_j + b_j x}, so its integral and CDF are closed-form: \int_{z_{j-1}}^{z_j} e^{a_j+b_j t}\,dt = \frac{e^{a_j}}{b_j}\big(e^{b_j z_j} - e^{b_j z_{j-1}}\big), \qquad F_g(x) = \frac{\int_{-\infty}^x g(t)\,dt}{\int_{-\infty}^{\infty} g(t)\,dt}

Draw from g(x)\big/\!\int g(x)\,dx by inverse CDF: pick a piece with probability proportional to its area, then invert the exponential CDF within the piece.

Adaptive rejection sampling (Gilks & Wild, 1992)

  1. Start with a few tangent points x_1<\cdots<x_k (at least one on each side of the mode)
  2. Draw x\sim g, U\sim\text{Unif}(0,1); accept if U<f(x)/g(x)
  3. Whenever a point is rejected, add it to the tangent set and rebuild the hull

Each rejection tightens the envelope where it was loose, so the acceptance rate rises toward 1 as sampling proceeds. Only \log f and its derivative are required (a derivative-free version uses secants).

Summary and Limitations

  • Rejection sampling turns draws from an envelope g\ge f into exact iid draws from f, using only the ratio f/g; normalizing constants are not needed
  • Geometric view: uniform points under g, keep those under f
  • Efficiency =\int f(x)\,dx\big/\!\int g(x)\,dx; the envelope should be as tight as possible while remaining easy to sample
  • Adaptive rejection sampling builds and refines the envelope automatically for log-concave f
  • Limitations: an envelope with a bounded ratio g/f may not exist (heavier tails in f than in g); in high dimensions the acceptance rate typically decays exponentially with dimension. These limitations motivate importance sampling and Markov chain Monte Carlo (next lectures)