Rejection sampling: drawing from a density you can only evaluate
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 SeeBuild a roof and run the sampler.
- 2 TryPredict which proposals can work.
- 3 ProveBuild the proof, line by line.
- 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 ideasA 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: 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.
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.
Build a roof and run the sampler
- Drag each round handle up or down to set the height of a step of your roof, or focus a handle and use the arrow keys.
- Aim for a roof that stays on or above f with as small an area as you can, then press Run 2,000 proposals.
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.
Raise at least one step above 0 to run the sampler.
| Round | Your M | Covers f? | Accepted of 2,000 | Lowest M |
|---|
Choosing the proposal
A prediction first. The explorer below it opens once it is answered.
Explorer: a Laplace roof over the normal density
Move the scale \(b\). The tiles show the smallest constant \(M(b)\) and the acceptance rate \(1/M(b)\).
Goals
Swap the roles
The lesson on heavy tails compares how fast different densities fall off.
Why the kept draws follow f
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.
Problems
Each problem has hints and a full solution.
Further reading
- S. M. Ross, Simulation, 5th ed. (Academic Press, 2013), chapter 5, "Generating Continuous Random Variables," the section on the rejection method.
- L. Devroye, Non-Uniform Random Variate Generation (Springer, 1986), chapter II, section 3, "The rejection method."
- C. P. Robert and G. Casella, Monte Carlo Statistical Methods, 2nd ed. (Springer, 2004), chapter 2, section 2.3, "Accept-Reject Methods."
- C. P. Robert, "Simulation of truncated normal variables," Statistics and Computing 5 (1995), on exponential proposals for normal tails.