Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF008 · Algorithm
Rejection sampling
Converts draws from a proposal into independent target draws by accepting each with a density-ratio probability. With a valid global envelope it is exact up to numerical and random-number implementation;
In CodeSplash
No method yet, but the reference's example runs here as a notebook.
Do it in CodeSplash Open the reference's example as a Python or R notebook.
- Availability
- Runs as a notebook
- Code example
- Complete calculation
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- The reference's example ran here in one language.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Converts draws from a proposal into independent target draws by accepting each with a density-ratio probability. With a valid global envelope it is exact up to numerical and random-number implementation; computational cost depends on how tight that envelope is.
Question / hypotheses
Can an easily sampled proposal envelope the entire target, allowing an acceptance decision that removes proposal bias?
Architecture / computational structure
Generate independent proposal/uniform pairs, retain successful candidates, and repeat until enough samples are collected. The example targets a Beta(2,2) density with a uniform proposal; its maximum is analytically 1.5, giving acceptance 4x(1-x) and mean acceptance two thirds.
Assumptions
A finite bound M must satisfy unnormalized target density f(x)<=M q(x) everywhere on its support. Proposal sampling and density evaluation must be correct. A bound estimated only on a finite grid is not automatically a valid global envelope.
Formula / computational definition
Parameters and notation
There are no learned posterior parameters. Proposal parameters and the envelope M are algorithm design quantities; a target normalizer is unnecessary if f and M use compatible scale. Hyperparameters include proposal family, batch size, and stopping rule. M must be validated, not tuned below the required bound to improve acceptance.
Fitting, fine-tuning, and adaptation
An improved envelope can reduce cost, but adaptive rejection methods require their own validity conditions. Changing the target requires checking the envelope again. Neural proposal fitting is optional infrastructure, not part of basic rejection sampling, and does not remove the need for an upper bound.
How to use and calibrate
Check the analytic bound, acceptance rate, support, and known moments. Plot accepted-sample histograms against the target and compare independent batches. Report how many proposals were required per accepted draw; count all proposal evaluations when comparing efficiency.
Example application
Generate exact draws from a simple custom prior or conditional distribution for use inside a larger latent-variable algorithm when a tight envelope is available.
Interpretation / pitfalls
Watch out
Replacing ratios above one with one does not repair an invalid envelope; it generally changes the target. High-dimensional envelopes may have vanishing acceptance. Fixed proposal batches return a random number of draws, unlike stopping after a requested accepted count.
Python and R code examples
Scope: Complete rejection rule on a finite proposal batch for Beta(2,2), not a generic automatic-envelope sampler.
Implementation route: Both versions use native random generators and vector filtering. For complex densities, derive a valid envelope or use a specialized method whose log-concavity or bounding assumptions actually apply.
Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Complete rejection rule on a finite proposal batch for Beta(2,2), not a generic automatic-envelope sampler. · needs numpy
import numpy as np
rng=np.random.default_rng(7); x=rng.uniform(size=10000); u=rng.uniform(size=10000)
prob=4*x*(1-x); assert np.all((prob>=0)&(prob<=1))
accepted=x[u<=prob]
print('draws',len(accepted),'acceptance',len(accepted)/len(x),'mean',accepted.mean())
Complete calculation — Complete rejection rule on a finite proposal batch for Beta(2,2), not a generic automatic-envelope sampler.
set.seed(7); x <- runif(10000); u <- runif(10000)
prob <- 4*x*(1-x); stopifnot(all(prob>=0 & prob<=1))
accepted <- x[u<=prob]
print(c(draws=length(accepted),acceptance=length(accepted)/length(x),mean=mean(accepted)))
Sources
- Primary/official source 1luc.devroye.org
- Primary/official source 2deeplearningbook.org
From the CodeSplash reference, entry INF008.