TL;DR
Rejection sampling produces samples from a distribution you can only evaluate, not sample from directly. You draw candidates from an easier distribution, then keep each candidate with a probability that is proportional to how much the target likes that value, and throw the rest away. The kept values follow the target exactly.
The method works in any dimension, but the fraction of candidates kept can become tiny, and then the method is far too slow.
Where you'll see it: in sampling from truncated and oddly shaped distributions, in the draft-and-verify step of Speculative Decoding for language models, and as a baseline that more efficient samplers are measured against.
Intuition
Throwing darts at a picture
Suppose you want points spread according to a curved shape, like a hill, drawn on a sheet of paper. You cannot place points by a formula for the hill. But you can throw darts uniformly at a rectangle that covers the hill. Some darts land under the hill and some above. Keep only the darts that land under the curve, and their horizontal positions follow the hill's shape. The darts above the hill are the rejections.
That is the whole method. The rectangle is the easy proposal, the hill is the target, and the darts are candidates. The only subtle part is making sure the hill fits under the rectangle, and making the rectangle as tight as possible so you waste few darts.
Why it exists
Many useful distributions are defined by a formula for their density but have no simple recipe for drawing from them. A posterior distribution in Bayesian inference is an example: you can compute its density up to a constant, but you cannot invert its cumulative function. Rejection sampling needs only the density, a bound, and a sampler for something simpler. It is the first method most students meet for this problem, and it is the building block that Markov chain samplers were designed to improve on in high dimensions.
The math
Setting
Let be the target density we want to sample from. Let be a proposal density that we can sample from. Pick a constant such that
Here and are densities, so each integrates to 1 over its domain. The constant is the envelope: the scaled proposal sits above the target everywhere.
The algorithm
- Draw a candidate .
- Draw an independent uniform number .
- If , accept and output it.
- Otherwise reject , and go back to step 1.
Step 3 is the acceptance test. The ratio is always between 0 and 1 because of the envelope condition, so it can be used as a probability.
Why the accepted samples have density p
Consider one round of the loop. The probability that a candidate lands in a small interval around and is accepted is
Sum over all to get the probability that a single round accepts something:
Dividing the first expression by this total gives the density of an accepted sample:
So the accepted samples have exactly the target density. The proof does not depend on beyond the envelope condition. The choice of only changes the acceptance rate .
Expected number of candidates
Each round is an independent trial that accepts with probability . So the expected number of candidates needed for one accepted sample is . This is the single number to watch when choosing . A tight envelope means small and few wasted draws.
Worked examples
Target: for . This integrates to 1, so it is a valid density. Proposal: on , the uniform distribution.
- Find the smallest envelope: means for all in . The largest value of is 2, so .
- Acceptance test: accept if .
- Expected candidates per sample: . Check with the integral: , which is .
- Trace two candidates. Candidate with : , so accept. Candidate with : , so reject.
- Interpretation: the accepted values pile up near 1, which matches the density rising toward 1. About half of all candidates are wasted, which is exactly .
Target: on , the Beta(3, 1) distribution. Proposal: uniform on , so .
- Envelope: on requires .
- Acceptance test: accept if .
- Take candidate . The acceptance probability is . Suppose : reject. Next, a fresh candidate with : accept.
- Overall acceptance rate: . Check the integral: . Matches.
- Interpretation: one candidate in three is kept. The distribution of kept values has mean , which a histogram of a few thousand accepted samples should approach.
- target p(x) = 3x^2
- envelope M q(x) = 3
Suppose the best envelope has . Then the acceptance rate is , and each accepted sample costs about 1000 candidate draws. Ten thousand samples cost about ten million proposals and evaluations of . In a few dimensions this is acceptable. In a hundred dimensions the best typically grows exponentially with the dimension, so the same method becomes unusable. This illustrates the formula ; the specific number 1000 is chosen for illustration, not measured.
Tradeoffs and failure modes
Rejection sampling is exact, simple and easy to verify, which is why it is taught first. Its main weakness is that the acceptance rate depends entirely on how well matches .
| Situation | What goes wrong | Better choice |
|---|---|---|
| Low dimension, a good envelope exists | Little waste; works well | Rejection sampling |
| Proposal far from the target, so is large | Most candidates thrown away | A closer proposal, or Importance Sampling if you only need averages |
| High dimension | Acceptance rate collapses | Markov chain methods such as [[monte-carlo-method |
| You only need expectations of a function, not samples | Sampling is wasted effort | Importance Sampling or direct averaging |
| You cannot bound | The method is not defined | Adaptive rejection sampling, or a different sampler |
Practical traps: the envelope condition must hold everywhere the target has mass, including the tails; a violated bound silently gives biased samples, so it is worth checking the ratio on a grid. Also, need only be known up to a constant. If you only have an unnormalised , pick with and use the same test; the accepted samples still follow . The acceptance rate is then rather than .
History
The idea goes back to the early Monte Carlo work of the 1940s and 1950s. John von Neumann described an accept-reject scheme for sampling from a density in the early 1950s, in the context of generating random variables from a computer. It caught on because it needs only the density, a bound, and a basic uniform random number generator. It became the standard tool for non-uniform sampling, and later the cheap proposal distributions it relied on, combined with better envelopes, led to adaptive methods used in statistical software.
Check yourself
QuestionRecall: what must be true of the envelope constant for rejection sampling to be correct?Show answer
The inequality must hold for every where . Only then is the acceptance probability at most 1, and only then do the accepted samples have density . If it fails somewhere, samples there are under-represented and the output is biased.
QuestionComputation: target on , uniform proposal. What is the smallest valid , and what is the acceptance rate?Show answer
The largest value of on is 4 at , so . The acceptance rate is . Check: . One candidate in four is kept.
QuestionWhy: why does dividing the joint probability by the total acceptance probability give the target density?Show answer
Accepted candidates in a small interval have probability per round, and the total accepted probability per round is . The conditional density of an accepted sample is therefore the ratio, . Rejection rescales the probability mass without changing its shape, which is exactly what the constant cancels out.
QuestionApplied: you need 10,000 samples from a distribution whose best envelope has . Roughly how many candidate draws do you expect, and is that a problem?Show answer
Expect about candidate draws, since each sample costs candidates on average. Whether that is a problem depends on the cost of evaluating and . For cheap densities it is fine. If each evaluation is expensive, such as a neural network likelihood, look for a tighter envelope or a Markov chain method before committing.