Hexa Scholar

Rejection sampling: drawing from a density you can only evaluate

A gold wooden staircase hangs above a smooth brown clay ridge; darts below the ridge line stand upright, and darts in the gap between the ridge and the staircase lie on their sides.
A staircase roof over a ridge, with darts kept below the ridge and laid flat above it.

The question

You can compute f(x) = 20x(1 − x)³ at any x in [0, 1], but its CDF has no simple inverse. How do you draw samples from f?

Build a roof over the curve and let the page throw 2,000 darts.

  1. 1 SeeBuild a roof and run the sampler.
  2. 2 TryPredict which proposals can work.
  3. 3 ProveBuild the proof, line by line.
  4. 4 ApplyProblems to check yourself.
Before you start · 3 quick checks on densities, uniform draws and the constant of a density, about 3 min

Two ideas before you start

2 ideas

A roof over the density

Figure: the density f(x) = 2x on [0, 1] as a rising line, and a dashed flat roof at height M over [0, 1]; where f rises above the roof, the gap is shaded. It needs JavaScript.

Roof height:

Roof: M = 1.5 f(x) > M for x > 0.75 not a valid roof

Proposals come from a density g you can sample, here uniform on [0, 1], so the roof M g(x) is flat at height M. The roof must stay on or above f everywhere; its area is M.

Exact definition

Target, proposal and roof. The target is a density \(f\) that a program can evaluate at any point. The proposal is a density \(g\) that it can sample from, for example with the inverse transform. A roof, or envelope, is a function \(Mg\) with a constant \(M\) such that

\[f(x) \le Mg(x) \text{ for every } x.\]

Its area is \(M\), because \(g\) has area 1. The card's target \(f(x) = 2x\) on \([0, 1]\) is largest at \(x = 1\), where it is 2, so a flat roof over \([0, 1]\) needs \(M \ge 2\).

Keep a draw with chance f(x)/(M g(x))

Figure: the density f(x) = 2x, the dashed roof at height 2, and a vertical segment from 0 up to the roof at one x; the part of the segment under f is drawn thick. It needs JavaScript.

Draw at:

At x = 0.25: f(x) = 0.5 Mg(x) = 2 keep with chance 0.25

Draw x from g, then a height uniformly between 0 and the roof M g(x). Keep x when the height lands under f, which happens with chance f(x)/(M g(x)).

Exact definition

The rejection step. Draw \(X\) with density \(g\) and \(U\) uniform on \((0, 1)\), independent of \(X\). Accept \(X\) when

\[U \le \frac{f(X)}{Mg(X)},\]

that is, when the point \((X, U \cdot Mg(X))\) lies under the graph of \(f\). Otherwise discard both and draw again. The accepted values are the sample. Only the ratio \(f/(Mg)\) is used, so \(f\) is needed only where a proposal lands.

Fine print: names and neighbors

The method is also called accept-reject sampling or von Neumann's method. When the inverse of the CDF is easy to compute, the inverse transform is simpler and wastes no draws; rejection sampling is useful when the CDF, or its inverse, has no usable formula. For how a density, its CDF and probabilities fit together, see the PMF, PDF and CDF.

Rejection sampling makes new draws from a density you write down. The bootstrap instead resamples a data set you already have. The draws on this page come from a seeded generator, so a run can be repeated exactly; the generator stands in for independent uniform draws.

1 See

Build a roof and run the sampler

How the game works

The target is \(f(x) = 20x(1 - x)^3\) on \([0, 1]\). Your roof \(e(x)\) is made of equal steps over \([0, 1]\), each with a height from 0 to 3 in steps of 0.05. Its area is \(M\), and the proposal density is \(g = e/M\): a proposal falls in a step with chance proportional to that step's area, and is uniform within it.

Run makes 2,000 proposals. Each one gets a height drawn uniformly between 0 and the roof, and it is accepted when that height is below \(f\). The page then reports the acceptance rate, compares it with \(1/M\), and shows a histogram of the accepted draws. Round 1 has one step, a flat roof; round 2 has four. Every run of a round uses the same 2,000 pairs of random numbers, so roofs are compared on equal terms. Start over clears both rounds.

2 Try

Choosing the proposal

A prediction first. The explorer below it opens once it is answered.

Swap the roles

3 Prove

Why the kept draws follow f

Theorem

Assumptions. \(f\) and \(g\) are densities on the real line, and \(f\) is not the same density as \(g\): they differ on a set of positive length. \(M\) is a constant with \(f(x) \le Mg(x)\) for every \(x\). Draw \(X\) with density \(g\), and \(U\) uniform on \((0, 1)\) and independent of \(X\). Accept \(X\) when \(U \le f(X)/(Mg(X))\), and write \(A\) for this event; a draw where \(g\) is 0 does not occur. Write \(F(t) = \int_{-\infty}^t f(x)\,dx\) and \(G(t) = \int_{-\infty}^t g(x)\,dx\) for the two CDFs.

Conclusion. \(P(A) = 1/M\), and for every \(t\), \(P(X \le t \mid A) = F(t)\): the accepted draws have density \(f\).

Why \(M \gt 1\). Integrating \(f \le Mg\) over the line gives \(1 \le M\). If \(M\) were 1, then \(g - f\) would never be negative and would have area 0, so \(f\) and \(g\) would be the same density. Since they are not, \(M \gt 1\). The case \(g = f\) is left out because there the proposals already have density \(f\) and no test is needed. The proof below would hold for it too, but several wrong steps offered in the proof builder would then be true.

Facts the proof uses.

  • for \(U\) uniform on \((0, 1)\) and a number \(c\) in \([0, 1]\), the chance that \(U\) is at most \(c\) is \(c\);
  • \(U\) is independent of \(X\), so given \(X = x\) the test compares \(U\) with the fixed number \(f(x)/(Mg(x))\);
  • a probability about \(X\) and \(U\) is the integral over \(x\) of its chance given \(X = x\), weighted by the density \(g\);
  • a conditional probability is a joint probability divided by the probability of the condition.

Each option is one step about the setting of the theorem. Pick the one that is true for every \(f\), \(g\) and \(M\) the theorem covers, and for every \(x\) or \(t\) it names.

What else follows
    4 Apply

    Problems

    Each problem has hints and a full solution.

    Key point Find the smallest \(M\) as the largest value of \(f(x)/g(x)\), often with a derivative. When \(f\) and \(g\) are both densities, on average a fraction \(1/M\) of the proposals is accepted, and \(k\) accepted draws cost \(kM\) proposals on average; for a target known only as \(\tilde{f} = Zf\) with \(\tilde{f} \le \tilde{M}g\), the rate is \(Z/\tilde{M}\) instead. A proposal whose tails fall no faster than the target's keeps \(M\) finite, and a well-matched scale keeps it small; in many dimensions \(M\) can grow exponentially, as in P6.
    Level Undergraduate Prerequisites densities and CDFs (the lesson on the PMF, PDF and CDF), the inverse transform (the lesson on inverse transform sampling), conditional probability (stated with the theorem in stage 3) Time ~35 min

    Further reading