Search

Search papers and lessons

Rejection Sampling

Level 3 · 12 min read

Rejection sampling draws candidates from an easy distribution and keeps each one with a probability proportional to how likely the hard target distribution is at that value.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Sampling
  • Sign in to see your progress

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 p(x)p(x) be the target density we want to sample from. Let q(x)q(x) be a proposal density that we can sample from. Pick a constant MM such that

p(x)≤M q(x)for every x.p(x) \le M\, q(x) \qquad \text{for every } x.

Here pp and qq are densities, so each integrates to 1 over its domain. The constant MM is the envelope: the scaled proposal sits above the target everywhere.

The algorithm

Algorithm
Rejection sampling
  1. Draw a candidate x∼q(x)x \sim q(x).
  2. Draw an independent uniform number u∼Uniform(0,1)u \sim \mathrm{Uniform}(0, 1).
  3. If u≤p(x)M q(x)u \le \dfrac{p(x)}{M\, q(x)}, accept xx and output it.
  4. Otherwise reject xx, and go back to step 1.

Step 3 is the acceptance test. The ratio p(x)/(Mq(x))p(x) / (M q(x)) 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 xx and is accepted is

Pr⁡(candidate in dx and accepted)=q(x) dx⋅p(x)M q(x)=p(x)M dx.\Pr(\text{candidate in } dx \text{ and accepted}) = q(x)\,dx \cdot \frac{p(x)}{M\, q(x)} = \frac{p(x)}{M}\,dx.

Sum over all xx to get the probability that a single round accepts something:

Pr⁡(accept)=∫p(x)M dx=1M.\Pr(\text{accept}) = \int \frac{p(x)}{M}\,dx = \frac{1}{M}.

Dividing the first expression by this total gives the density of an accepted sample:

p(x)/M1/M=p(x).\frac{p(x)/M}{1/M} = p(x).

So the accepted samples have exactly the target density. The proof does not depend on qq beyond the envelope condition. The choice of qq only changes the acceptance rate 1/M1/M.

Expected number of candidates

Each round is an independent trial that accepts with probability 1/M1/M. So the expected number of candidates needed for one accepted sample is MM. This is the single number to watch when choosing qq. A tight envelope means small MM and few wasted draws.

Worked examples

Example
Density 2x on the unit interval

Target: p(x)=2xp(x) = 2x for 0≤x≤10 \le x \le 1. This integrates to 1, so it is a valid density. Proposal: q(x)=1q(x) = 1 on [0,1][0, 1], the uniform distribution.

  1. Find the smallest envelope: p(x)≤Mq(x)p(x) \le M q(x) means 2x≤M2x \le M for all xx in [0,1][0, 1]. The largest value of 2x2x is 2, so M=2M = 2.
  2. Acceptance test: accept if u≤2x2=xu \le \dfrac{2x}{2} = x.
  3. Expected candidates per sample: M=2M = 2. Check with the integral: ∫01p(x)M dx=∫01x dx=12\int_0^1 \dfrac{p(x)}{M}\,dx = \int_0^1 x\,dx = \dfrac{1}{2}, which is 1/M1/M.
  4. Trace two candidates. Candidate x=0.6x = 0.6 with u=0.4u = 0.4: u≤0.6u \le 0.6, so accept. Candidate x=0.3x = 0.3 with u=0.5u = 0.5: u>0.3u > 0.3, so reject.
  5. Interpretation: the accepted values pile up near 1, which matches the density 2x2x rising toward 1. About half of all candidates are wasted, which is exactly 1−1/M1 - 1/M.
Example
Density 3x squared, with a sanity check on the rate

Target: p(x)=3x2p(x) = 3x^2 on [0,1][0, 1], the Beta(3, 1) distribution. Proposal: uniform on [0,1][0, 1], so q(x)=1q(x) = 1.

  1. Envelope: 3x2≤M3x^2 \le M on [0,1][0, 1] requires M=3M = 3.
  2. Acceptance test: accept if u≤x2u \le x^2.
  3. Take candidate x=0.8x = 0.8. The acceptance probability is 0.82=0.640.8^2 = 0.64. Suppose u=0.7u = 0.7: reject. Next, a fresh candidate x=0.8x = 0.8 with u=0.5u = 0.5: accept.
  4. Overall acceptance rate: 1/M=1/31/M = 1/3. Check the integral: ∫013x23 dx=∫01x2 dx=13\int_0^1 \dfrac{3x^2}{3}\,dx = \int_0^1 x^2\,dx = \dfrac{1}{3}. Matches.
  5. Interpretation: one candidate in three is kept. The distribution of kept values has mean ∫01x⋅3x2 dx=3/4\int_0^1 x \cdot 3x^2\,dx = 3/4, which a histogram of a few thousand accepted samples should approach.
00.20.40.60.810123xdensity
  • target p(x) = 3x^2
  • envelope M q(x) = 3
The envelope sits above the target everywhere. The ratio of the target to the envelope is the acceptance probability at each x.
Example
Why high dimensions hurt

Suppose the best envelope has M=1000M = 1000. Then the acceptance rate is 1/1000=0.0011/1000 = 0.001, and each accepted sample costs about 1000 candidate draws. Ten thousand samples cost about ten million proposals and evaluations of pp. In a few dimensions this is acceptable. In a hundred dimensions the best MM typically grows exponentially with the dimension, so the same method becomes unusable. This illustrates the formula E[candidates per sample]=M\mathbb{E}[\text{candidates per sample}] = M; 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 qq matches pp.

SituationWhat goes wrongBetter choice
Low dimension, a good envelope existsLittle waste; works wellRejection sampling
Proposal far from the target, so MM is largeMost candidates thrown awayA closer proposal, or Importance Sampling if you only need averages
High dimensionAcceptance rate collapsesMarkov chain methods such as [[monte-carlo-method
You only need expectations of a function, not samplesSampling is wasted effortImportance Sampling or direct averaging
You cannot bound p/qp / qThe method is not definedAdaptive 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, pp need only be known up to a constant. If you only have an unnormalised p~=Zp\tilde p = Z p, pick MM with p~(x)≤M q(x)\tilde p(x) \le M\, q(x) and use the same test; the accepted samples still follow pp. The acceptance rate is then Z/MZ/M rather than 1/M1/M.

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 MM for rejection sampling to be correct?Show answer

The inequality p(x)≤Mq(x)p(x) \le M q(x) must hold for every xx where p(x)>0p(x) > 0. Only then is the acceptance probability p(x)/(Mq(x))p(x)/(M q(x)) at most 1, and only then do the accepted samples have density pp. If it fails somewhere, samples there are under-represented and the output is biased.

QuestionComputation: target p(x)=4x3p(x) = 4x^3 on [0,1][0, 1], uniform proposal. What is the smallest valid MM, and what is the acceptance rate?Show answer

The largest value of 4x34x^3 on [0,1][0, 1] is 4 at x=1x = 1, so M=4M = 4. The acceptance rate is 1/M=0.251/M = 0.25. Check: ∫014x34 dx=∫01x3 dx=1/4\int_0^1 \dfrac{4x^3}{4}\,dx = \int_0^1 x^3\,dx = 1/4. 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 p(x)M dx\frac{p(x)}{M}\,dx per round, and the total accepted probability per round is 1/M1/M. The conditional density of an accepted sample is therefore the ratio, p(x)p(x). Rejection rescales the probability mass without changing its shape, which is exactly what the constant MM cancels out.

QuestionApplied: you need 10,000 samples from a distribution whose best envelope has M=20M = 20. Roughly how many candidate draws do you expect, and is that a problem?Show answer

Expect about 10,000×20=200,00010{,}000 \times 20 = 200{,}000 candidate draws, since each sample costs MM candidates on average. Whether that is a problem depends on the cost of evaluating pp and qq. 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.

Sources