Importance sampling
Last edited
Fact-checked
In review queue
Sources
24 citations
Revision
v4 · 4,179 words
Fact-checks are independent of edits: a reviewer re-verifies the article against its sources and stamps the date. How we verify
Importance sampling (often abbreviated IS) is a Monte Carlo method for estimating the expectation of a function under a target probability distribution by drawing samples from a different proposal distribution and reweighting each sample by the likelihood ratio . It lets you compute an average under a distribution you cannot easily sample from, using samples from one you can, and it is the standard tool for variance reduction in rare-event estimation and for off-policy correction in reinforcement learning. The technique is one of the oldest in computational statistics, with roots in 1940s rare-event simulation work at Los Alamos and a textbook treatment in Hammersley and Handscomb's 1964 monograph Monte Carlo Methods [1]. It now sits at the centre of off-policy reinforcement learning, variational inference, particle filtering, simulation-based Bayesian inference, and counterfactual evaluation in industrial recommender systems.
Given a target density , a proposal density with wherever , and a function whose expectation we want, the basic identity is
where the importance weight corrects for the mismatch between sampling distribution and target. The standard estimator is
When the supports are correct the estimator is unbiased; in practice the choice of controls almost everything that can go right or wrong with the method.
What is importance sampling used for?
Three forces keep importance sampling at the centre of modern probabilistic computing:
- Sampling from may be impossible or expensive. might be an unnormalised posterior, the distribution induced by a target reinforcement learning policy, or the conditional path measure of a stochastic differential equation. Drawing from a tractable and reweighting is often the only feasible option.
- Variance reduction. When concentrates in a region where puts little mass (rare-event probabilities, tail integrals, expected loss under adversarial inputs), naive Monte Carlo wastes nearly all of its samples. A well-chosen shifts samples into the region that matters, reducing the variance of the estimate.
- It is the mathematical glue under modern algorithms. PPO clipping, V-trace, Retrace, IWAE bounds, particle filters, annealed importance sampling for marginal likelihoods, and counterfactual estimators in advertising and recommendation systems are all dressed-up importance samplers.
Different fields rediscovered the same equation under different names. Statisticians call the importance weight. Causal inference calls it the inverse propensity score. Survey methodologists call it a sampling weight. Reinforcement learning calls the importance ratio. The arithmetic is identical.
How does importance sampling work?
Unbiasedness and variance
When dominates (that is, wherever ), is unbiased for . Its variance is
The second moment can be infinite even when is finite. Owen's Monte Carlo theory, methods and examples (Chapter 9) gives precise conditions: a sufficient finite-variance condition is that is bounded, i.e. [11]. Owen warns that the proposal must have heavier tails than the target, noting that "if has lighter tails than then can be unbounded and the variance of might be infinite" [11]. When this fails the estimator can still be unbiased but its variance is infinite, the central limit theorem does not apply, and confidence intervals built from sample variance are meaningless.
The optimal proposal
Minimising the second moment over subject to gives the optimal proposal
For non-negative this achieves zero variance; a single sample suffices. The catch is that 's normalising constant is exactly , the quantity we wanted in the first place. The optimal proposal is therefore unattainable, but it is a useful target: a good should look roughly like , putting mass where is large and is non-negligible.
What is the effective sample size in importance sampling?
The most common diagnostic for an importance sample is the effective sample size (ESS), introduced by Augustine Kong in 1992 [22]:
Its value lies between (one weight dominates) and (uniform weights). An ESS of from samples means that the weighted estimator carries about as much information as direct draws from . ESS is widely used in sequential Monte Carlo to decide when to resample particles. It is a necessary diagnostic but not a sufficient one: a high ESS can still hide catastrophic tail behaviour if misses an important mode entirely.
What is self-normalised importance sampling?
When is known only up to a normalising constant (the usual situation in Bayesian inference where ), the unnormalised weights are still computable but is not. The standard fix is the self-normalised estimator (SNIS):
SNIS is biased (the ratio of two random variables) but consistent, and it often has lower mean-squared error than the vanilla estimator because of the cancellation between numerator and denominator fluctuations. The bias scales as while the variance scales as , so the bias becomes negligible quickly. Robert and Casella's Monte Carlo Statistical Methods (Chapter 3) treats SNIS as the default importance sampler in Bayesian practice [8]. Recent work such as Cardoso et al.'s 2022 BR-SNIS uses iterated sampling-importance-resampling to reduce the bias further at essentially the same cost [19].
What variance reduction techniques relate to importance sampling?
Vanilla importance sampling is only one of a family of variance reduction tools, and it is often combined with the others.
| Technique | Idea | Typical use |
|---|---|---|
| Antithetic variables | Pair each with (or another negatively correlated draw) | Symmetric integrands, simulation studies |
| Control variates | Subtract a known-mean function correlated with | Pricing financial derivatives, RL baselines |
| Stratified sampling | Partition the domain and sample within each stratum | Survey statistics, low-discrepancy QMC |
| Multiple importance sampling (Veach 1995) | Combine samples from several proposals via the balance heuristic | Path-traced rendering, bidirectional path tracing |
| Adaptive importance sampling | Refine from past samples (population Monte Carlo, AMIS) | Bayesian model evidence, simulation-based inference |
| Annealed importance sampling (Neal 2001) | Move along a tempered sequence via MCMC kernels and accumulate weights | Marginal likelihood estimation, hard posteriors |
Multiple importance sampling deserves a closer look. Eric Veach and Leonidas Guibas introduced it at SIGGRAPH 1995 in Optimally Combining Sampling Techniques for Monte Carlo Rendering, and it remains the workhorse of production renderers [5]. Their balance heuristic weights each sample by the proposal density that produced it relative to the sum of all proposal densities, which is provably close to optimal in the single-sample case.
How is importance sampling used in reinforcement learning?
Importance sampling is the standard correction in off-policy reinforcement learning, where a behaviour policy generates trajectories and a different target policy is evaluated or improved. The setting is unavoidable in three places: replay buffers in deep RL, observational data in healthcare and recommendation systems, and parallel actor-learner architectures where the actors run a stale policy by the time the learner uses their data.
The trajectory ratio
For a single full trajectory generated by , the trajectory importance ratio is
An unbiased estimator of the value for an episodic Markov decision process is
where is the discounted return of the -th trajectory. Sutton and Barto's Reinforcement Learning: An Introduction (Chapter 5) develops this ordinary and the corresponding weighted importance sampling estimator and works out their bias and variance trade-off [18]. As they summarise it, "ordinary importance sampling produces unbiased estimates, but has larger, possibly infinite, variance, whereas weighted importance sampling always has finite variance" [18]. The trouble is the product: a long trajectory multiplies many ratios, the variance of explodes exponentially in , and a single lucky episode can swing the average by orders of magnitude. With a greedy target and an exploratory behaviour policy, Sutton and Barto note that "this method only learns from the tails of episodes, when all of the remaining actions are greedy," so most weights are negligible [18].
Per-decision importance sampling
Doina Precup, Richard Sutton and Satinder Singh introduced per-decision importance sampling in their ICML 2000 paper Eligibility Traces for Off-Policy Policy Evaluation [6]. The trick is that the reward at time only depends on the actions up to time , so its expectation needs only the partial product , not the full trajectory ratio. This shrinks the effective variance considerably and is the foundation of every modern off-policy return estimator. The same paper analyses five eligibility-trace algorithms that combine importance sampling with TD learning and proves their consistency.
Truncated and clipped ratios
More recent work tames the variance by truncating the ratios at the cost of some bias.
-
Tree backup. Replaces the importance ratio with the target-policy probability, eliminating variance from the behaviour policy at the cost of off-policyness.
-
Retrace() (Munos et al., NeurIPS 2016). Uses the truncated ratio in a return-based off-policy update [14]. Munos and colleagues proved Retrace is the first return-based off-policy control algorithm that converges almost surely to without a Greedy-in-the-Limit-with-Infinite-Exploration assumption, and it solved a long-standing open question about Watkins's .
-
V-trace (Espeholt et al., IMPALA, ICML 2018). Uses two clipping constants and on per-step ratios so a distributed actor-learner architecture can absorb policy lag from thousands of asynchronous workers [17]. IMPALA achieves a throughput of about 250,000 frames per second on a single learner with hundreds of actors, and is the workhorse of large-scale on-policy-style training [17].
-
PPO clipping (Schulman et al. 2017). Proximal policy optimization's flagship objective is
where is a one-step importance ratio [15]. The clip constant (the value recommended by Schulman et al. is ) prevents the ratio from blowing up when the new policy strays from the old, which would otherwise destabilise gradient updates [15]. PPO is now the default policy-gradient method in most deep-RL libraries.
The earlier trust region policy optimization (TRPO, Schulman et al. 2015) maximises the same importance-weighted surrogate but constrains the KL divergence between successive policies instead of clipping the ratio [23]. PPO replaced the hard KL constraint with the simpler clipped ratio above.
How is importance sampling used in variational inference and generative models?
Kingma and Welling's 2013 Variational Autoencoder maximises the evidence lower bound (ELBO) on using a single sample from the recognition network [12]. The ELBO can be tightened with importance weighting.
Burda, Grosse and Salakhutdinov's Importance Weighted Autoencoders (ICLR 2016) introduced the IWAE bound
By Jensen's inequality this is a lower bound on for every , with , and the bound is monotonically tighter as grows, converging to in the limit [13]. The trick is the position of the logarithm: the VAE takes the average of logs, the IWAE takes the log of an average of importance-weighted likelihood ratios. IWAE-style -sample bounds are now the standard way to evaluate the marginal likelihood of normalising flows, diffusion models, and other deep generative models.
Closely related techniques include Reweighted Wake-Sleep (Bornschein and Bengio, ICLR 2015), which uses importance-weighted wake-phase updates to train discrete generative models, and Annealed Importance Sampling (Neal 2001), which constructs a chain of intermediate distributions between a tractable proposal and the target and accumulates importance weights along an MCMC trajectory [7]. AIS is the standard estimator of marginal likelihoods for restricted Boltzmann machines, deep belief networks, and recently for evaluating diffusion-model log-likelihoods.
How do particle filters use sequential importance sampling?
In state-space models the target is a sequence of posteriors that grows in dimension over time. Sequential importance sampling (SIS) extends an importance sample one time step at a time, multiplying weights by .
SIS suffers from weight degeneracy: after a few time steps almost all probability mass concentrates on a single particle. The fix is resampling: at each step (or whenever the ESS falls below a threshold), draw a new particle population proportional to current weights. This Sampling-Importance-Resampling (SIR) algorithm is what Gordon, Salmond and Smith introduced in their 1993 IEE paper Novel approach to nonlinear/non-Gaussian Bayesian state estimation, popularising the bootstrap filter [4]. The bootstrap filter and its descendants underpin robot localisation (FastSLAM), object tracking, target tracking in radar, epidemic forecasting, and probabilistic programming languages such as Anglican and Pyro.
Where else is importance sampling used in machine learning?
| Application area | How importance sampling is used | Representative reference |
|---|---|---|
| Off-policy value estimation | Reweight returns by trajectory or per-decision ratios | Precup, Sutton and Singh 2000 |
| Distributed deep RL | V-trace clipping in actor-learner architectures | Espeholt et al. (IMPALA) 2018 |
| Policy optimisation | PPO clipped surrogate, TRPO surrogate | Schulman et al. 2017 |
| Variational inference | IWAE tighter ELBO, Reweighted Wake-Sleep | Burda et al. 2016 |
| Marginal likelihood | Annealed importance sampling along tempered chain | Neal 2001 |
| Bayesian model comparison | PSIS-LOO leave-one-out cross-validation | Vehtari, Gelman, Gabry 2017 |
| Particle filters | SIR/bootstrap filter for state-space models | Gordon, Salmond and Smith 1993 |
| Computer graphics rendering | Multiple importance sampling for path tracing | Veach and Guibas 1995 |
| Counterfactual policy evaluation | Inverse propensity scoring for ad/recommendation systems | Bottou et al. 2013 |
| Causal inference | Inverse probability of treatment weighting | Horvitz and Thompson 1952 |
| Survey statistics | Sampling weights to correct stratified surveys | Hansen and Hurwitz 1943 |
| Likelihood-free inference (ABC) | Reweight simulations by approximate likelihood | Sisson et al. 2007 |
| Diffusion model evaluation | IWAE-style upper bound on negative log-likelihood | Song et al. 2021 |
| Neural rendering and inverse graphics | Multiple importance sampling within differentiable renderers | Müller et al. 2019 |
What are the pitfalls and limitations of importance sampling?
Importance sampling is unforgiving when the proposal is wrong. The classical failure modes are:
- Heavy-tailed weight distributions. When has a heavy right tail, a handful of samples dominate the average and the estimator behaves erratically. The Pareto-shape parameter of the weights is the standard diagnostic; values above are typically considered unreliable [21].
- Mismatched supports. If at some where , the estimator is biased and the bias may be silently catastrophic. This is the most common bug in implementations: a softmax target policy that puts mass on actions that the behaviour policy excluded outright.
- Infinite variance. Whenever , the variance estimate from a finite sample is misleading and the central limit theorem does not apply. Owen's Chapter 9 discusses sufficient conditions for finite variance [11].
- Curse of dimensionality. The variance of the weight in dimension typically grows exponentially with unless matches along most coordinates. As a result, vanilla importance sampling is rarely effective in moderate-to-high-dimensional problems without structural help (sequential structure, annealing, normalising-flow proposals, or learned proposals).
- Counterfactual brittleness. In off-policy evaluation for recommender systems and ads, a target policy that puts probability on an item that the logging policy showed with probability produces a weight on every observed click of that item. A single click can dominate the entire estimate.
How do you diagnose a bad importance sampler?
Three diagnostics are standard practice:
- Effective sample size. Computed continuously during a run; small ESS indicates a poor proposal or weight degeneracy.
- Pareto-smoothed importance sampling (PSIS). Vehtari, Simpson, Gelman, Yao and Gabry's 2024 JMLR paper Pareto Smoothed Importance Sampling fits a generalised Pareto distribution to the upper tail of the importance ratios, smooths the largest weights with the fitted quantiles, and reports the shape parameter as a finite-sample convergence diagnostic [21]. The recommended threshold depends on sample size: for samples. PSIS is implemented in the R package
looand the Python packagearviz. - Visual inspection of weight histograms. A long, sparse right tail signals a problem that ESS may not reveal.
Which libraries implement importance sampling?
Importance sampling is built into most probabilistic programming languages and reinforcement learning frameworks:
| System | Where importance sampling appears |
|---|---|
| PyMC | SMC sampler, variational SVGD, PSIS-LOO |
| Pyro / NumPyro | infer.SMCFilter, infer.MCMC, IWAE example |
| TensorFlow Probability | tfp.mcmc, tfp.experimental.distribute, importance-weighted ELBO |
| Edward2 | Importance-weighted training and evaluation |
Stan + loo | PSIS leave-one-out cross-validation |
| Stable-Baselines3 | PPO clipped objective, off-policy SAC importance corrections |
| RLlib | IMPALA V-trace, PPO clipping |
| Tianshou | PPO and off-policy actor-critic algorithms with IS corrections |
| Open Source Image rendering (Mitsuba, PBRT) | Multiple importance sampling in path tracers |
What are recent developments in importance sampling?
Research interest in importance sampling has stayed steady because the technique sits at the intersection of so many fields. A few directions stand out in the past decade.
Pareto-smoothed importance sampling. Vehtari et al.'s PSIS gave the field a practical, sample-size-aware diagnostic and is now the default for Bayesian leave-one-out cross-validation [21].
Differentiable Monte Carlo. Treating importance-weighted estimators as differentiable graphs (with the reparameterisation trick where possible) lets practitioners back-propagate through Monte Carlo objectives. This is the technical core of the IWAE bound, of differentiable rendering, and of recent work on differentiable annealed importance sampling.
Off-policy evaluation in offline reinforcement learning. The shift from online RL to offline RL on logged data made off-policy evaluation a first-class problem. Self-normalised importance sampling, doubly robust estimators, and clipped IPS are routine in the offline-RL literature.
LLM evaluation and inference-time scaling. Importance sampling has become a core tool for evaluating large language models on rare prompts, for re-weighting samples from proposal LLMs to estimate behaviour under a target policy (used in distillation and red-teaming), and for re-scoring beam search candidates with auxiliary models.
Neural proposals. Normalising-flow and energy-based models are now used as learned importance-sampling proposals in physics simulation and lattice quantum chromodynamics, where a hand-designed would be hopeless.
How does importance sampling compare with other Monte Carlo methods?
| Method | Assumptions on target | When to use | Cost per sample | Output |
|---|---|---|---|---|
| Vanilla Monte Carlo | Can sample directly from | is easy to draw from and integrand is well-behaved | Very low | i.i.d. samples from , unbiased |
| Importance sampling | Can evaluate (up to constant) and have a covering proposal | Sampling from is hard, or concentrates in a rare region | Low | Weighted samples, unbiased (or consistent) |
| Markov Chain Monte Carlo (Metropolis-Hastings, Gibbs) | Can evaluate up to constant | Posterior sampling, unimodal or moderately multimodal targets | Moderate, correlated samples | Approximate samples after burn-in |
| Hamiltonian Monte Carlo | available | Continuous high-dimensional posteriors | Higher per step, much better mixing | Approximate samples with low autocorrelation |
| Sequential Monte Carlo / particle filters | State-space structure or sequence of intermediate targets | Nonlinear, non-Gaussian filtering, evidence estimation | Moderate, parallel | Particle approximation of , evidence estimate |
| Variational inference | Choose a tractable family | Speed-critical Bayesian inference, large-scale latent-variable models | Optimisation cost | Parametric approximation |
| Annealed importance sampling | Tempered chain | Marginal likelihood / evidence estimation | Higher (chain of MCMC steps) | Unbiased weight, evidence estimate |
A short history
While precursors of importance sampling appear in 1949 statistical-physics work by Kahn and Marshall (and in Hansen and Hurwitz's 1943 stratified survey weights), the explicit formulation as a variance-reduction technique is generally credited to the early Los Alamos Monte Carlo group. Hammersley and Handscomb's 1964 Monte Carlo Methods (Methuen Monographs on Applied Statistics and Probability) gave the first textbook treatment, and is still cited as the canonical reference for the classical results [1]. Teun Kloek and Herman van Dijk's 1978 Econometrica paper introduced importance sampling to Bayesian econometrics [2]. Rubinstein's 1981 Simulation and the Monte Carlo Method and Robert and Casella's 2004 Monte Carlo Statistical Methods (especially Chapter 3) are the standard graduate references in statistics [8]; Bishop's Pattern Recognition and Machine Learning (2006) and Murphy's Machine Learning: A Probabilistic Perspective (2012) and Probabilistic Machine Learning (2022) cover the technique for ML audiences [9][10][20]. Owen's online textbook Monte Carlo theory, methods and examples (2013, in progress) gives the most complete modern variance-reduction treatment, with Chapter 9 devoted entirely to importance sampling [11]. The reinforcement-learning treatment is concentrated in Sutton and Barto's Reinforcement Learning: An Introduction (2018, 2nd edition), Chapters 5 and 7 [18]. A concise general overview is also maintained on Wikipedia [24].
ELI5: importance sampling in plain language
Imagine you want to know the average height of giraffes, but giraffes are rare and you mostly bump into horses. If you measure horses instead, your answer will be wrong because horses are shorter. Importance sampling fixes this by giving every horse a "weight": the rarer that kind of animal is among horses compared to giraffes, the more its measurement counts. Add up the weighted measurements and you get the giraffe average, even though you only ever looked at horses. The catch is that if one weird horse gets a giant weight, it can throw off the whole average, which is exactly the high-variance problem that makes choosing the right "stand-in" distribution so important.
See also
- Markov Chain Monte Carlo (MCMC)
- Monte Carlo method
- Monte Carlo Tree Search
- Reinforcement Learning
- Proximal Policy Optimization (PPO)
- Reinforcement Learning from Human Feedback (RLHF)
- Rejection sampling
- Sampling Bias
- Variance
- Trajectory (Reinforcement Learning)
- Return (Reinforcement Learning)
- Episode (Reinforcement Learning)
References
- Hammersley, J. M. and Handscomb, D. C. (1964). *Monte Carlo Methods*. Methuen, London. Monographs on Applied Statistics and Probability. ↩
- Kloek, T. and van Dijk, H. K. (1978). Bayesian Estimates of Equation System Parameters: An Application of Integration by Monte Carlo. *Econometrica* 46(1):1-19. ↩
- Geweke, J. (1989). Bayesian Inference in Econometric Models Using Monte Carlo Integration. *Econometrica* 57(6):1317-1339.
- Gordon, N. J., Salmond, D. J., and Smith, A. F. M. (1993). Novel approach to nonlinear/non-Gaussian Bayesian state estimation. *IEE Proceedings F (Radar and Signal Processing)* 140(2):107-113. ↩
- Veach, E. and Guibas, L. J. (1995). Optimally Combining Sampling Techniques for Monte Carlo Rendering. *SIGGRAPH '95*. ↩
- Precup, D., Sutton, R. S., and Singh, S. (2000). Eligibility Traces for Off-Policy Policy Evaluation. *ICML 2000*. ↩
- Neal, R. M. (2001). Annealed importance sampling. *Statistics and Computing* 11:125-139. ↩
- Robert, C. P. and Casella, G. (2004). *Monte Carlo Statistical Methods* (2nd ed.). Springer Texts in Statistics. Chapter 3 covers importance sampling. ↩
- Bishop, C. M. (2006). *Pattern Recognition and Machine Learning*. Springer. ↩
- Murphy, K. P. (2012). *Machine Learning: A Probabilistic Perspective*. MIT Press. ↩
- Owen, A. B. (2013). *Monte Carlo theory, methods and examples*. Online textbook. Chapter 9: Importance Sampling. ↩
- Kingma, D. P. and Welling, M. (2013). Auto-Encoding Variational Bayes. arXiv:1312.6114. ↩
- Burda, Y., Grosse, R. and Salakhutdinov, R. (2016). Importance Weighted Autoencoders. *ICLR 2016*. arXiv:1509.00519. ↩
- Munos, R., Stepleton, T., Harutyunyan, A. and Bellemare, M. G. (2016). Safe and Efficient Off-Policy Reinforcement Learning. *NeurIPS 2016*. ↩
- Schulman, J., Wolski, F., Dhariwal, P., Radford, A. and Klimov, O. (2017). Proximal Policy Optimization Algorithms. arXiv:1707.06347. ↩
- Vehtari, A., Gelman, A. and Gabry, J. (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. *Statistics and Computing* 27:1413-1432.
- Espeholt, L., Soyer, H., Munos, R., Simonyan, K., et al. (2018). IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures. *ICML 2018*. arXiv:1802.01561. ↩
- Sutton, R. S. and Barto, A. G. (2018). *Reinforcement Learning: An Introduction* (2nd ed.). MIT Press. Chapters 5 (off-policy MC) and 7 (off-policy n-step). ↩
- Cardoso, G., Idrissi, Y. J. E., Le Corff, S., Moulines, E. (2022). BR-SNIS: Bias Reduced Self-Normalized Importance Sampling. *NeurIPS 2022*. arXiv:2207.06364. ↩
- Murphy, K. P. (2022). *Probabilistic Machine Learning: An Introduction*. MIT Press. ↩
- Vehtari, A., Simpson, D., Gelman, A., Yao, Y. and Gabry, J. (2024). Pareto Smoothed Importance Sampling. *Journal of Machine Learning Research* 25:1-58. ↩
- Kong, A. (1992). A Note on Importance Sampling using Standardized Weights. Technical Report 348, Department of Statistics, University of Chicago. ↩
- Schulman, J., Levine, S., Abbeel, P., Jordan, M. and Moritz, P. (2015). Trust Region Policy Optimization. *ICML 2015*. arXiv:1502.05477. ↩
- Wikipedia contributors. *Importance sampling*. Wikipedia. ↩
Improve this article
Add missing citations, update stale details, or suggest a clearer explanation. Every suggestion is reviewed for sourcing before it goes live.
3 revisions by 1 contributors · full history