Importance sampling

On this page 7

Importance sampling is a Monte Carlo method for estimating expectations under a target distribution by drawing samples from a different proposal distribution and correcting them with weights. Each sample is multiplied by the ratio of the target density to the proposal density, so that the weighted average remains an unbiased estimate of the quantity of interest.

In probabilistic models, the technique is used when the target, often a posterior distribution, is hard to sample directly but easy to evaluate up to a constant. It underpins marginal likelihood estimation, particle filters, off-policy evaluation in reinforcement learning and several variational objectives across Artificial intelligence and statistics.

How importance sampling reweights samples

The method rewrites an expectation so that it can be computed with samples from a convenient distribution. For a function f and target density p, the expectation of f under p equals the expectation of f(x) times p(x)/q(x) under the proposal q, provided q is positive wherever p times f is nonzero. The ratio w(x) = p(x)/q(x) is the importance weight.

In practice, analysts draw N samples from q, compute each weight, and average the weighted function values. When the target is known only up to a normalizing constant, as in Bayesian inference, the self-normalized estimator divides by the sum of the weights. This version is slightly biased for finite N but consistent, and it is the standard choice for posterior expectations.

The table below lists the main quantities involved and their role in the estimator.

QuantityDefinitionRole
Target p(x)Distribution of interestDefines the expectation to estimate
Proposal q(x)Distribution actually sampledMust cover the support of the target
Weight w(x)Ratio p(x)/q(x)Corrects for sampling from q
Normalized weightWeight divided by sum of weightsHandles unnormalized targets
Effective sample sizeSquared weight sum over sum of squared weightsDiagnoses weight degeneracy
Components of the importance sampling estimator

Variance and the choice of proposal

The quality of an importance sampling estimate depends almost entirely on how well the proposal matches the target. When q places little mass where p times the absolute value of f is large, a few samples receive enormous weights, and the estimator’s variance can become very large or even infinite.

A central rule is that the proposal should have heavier tails than the target. A Gaussian proposal for a Student-t target, for example, can yield infinite variance. The theoretically optimal proposal is proportional to the absolute value of f times p, which is rarely available but guides design: concentrate samples where the integrand matters.

The following table compares common proposal strategies used in practice.

Proposal typeTypical constructionStrengthWeakness
Prior as proposalSample from the model priorSimple and always availablePoor when data are informative
Laplace-based GaussianMode and curvature of posteriorCheap, often reasonableLight tails, misses skew
Heavy-tailed Student-tLaplace fit with wider tailsSafer variance behaviorLess efficient in the center
Mixture proposalSeveral components combinedCovers multimodal targetsMore tuning required
Learned variational proposalFitted by variational inferenceAdapts to complex shapesMay underestimate tails
Common proposal choices and their trade-offs

The effective sample size (ESS) is the most widely used diagnostic. An ESS far below N, such as 20 effective samples out of 10,000, signals that the estimate rests on a handful of points. Pareto-smoothed importance sampling adds a further check: a fitted tail shape parameter k above about 0.7 indicates unreliable results.

Variants used in probabilistic modeling

Several extensions of importance sampling address its weaknesses or adapt it to specific model structures. Many sit at the boundary with Markov chain Monte Carlo and variational inference.

VariantKey ideaTypical application
Self-normalized ISNormalize weights by their sumPosterior expectations
Sequential ISBuild weights step by stepParticle filters, state-space models
Annealed ISBridge prior to posterior graduallyMarginal likelihood estimation
Pareto-smoothed ISSmooth the largest weightsLeave-one-out cross-validation
Multiple ISCombine several proposalsRendering, rare event simulation
Importance weighted autoencoderTighter bound with K samplesDeep generative models
Important variants of importance sampling

In variational inference, the importance weighted evidence lower bound uses K samples from the variational distribution and becomes tighter as K grows, linking the two families of approximate inference directly.

Comparison with other sampling methods

Importance sampling differs from Markov chain Monte Carlo because its samples are independent rather than correlated, which makes it easy to parallelize and free of burn-in. MCMC methods such as the Metropolis-Hastings algorithm or Hamiltonian Monte Carlo, however, scale far better to high-dimensional posteriors, where importance weights tend to collapse.

Compared with rejection sampling, importance sampling uses every draw instead of discarding some, and it does not require a known bound on the density ratio. Compared with variational inference, it provides asymptotically exact estimates, whereas variational methods optimize an approximation that remains biased regardless of compute.

Limitations and common mistakes

The main limitation is the curse of dimensionality: as the number of parameters rises, the mismatch between proposal and target compounds across dimensions, and the weights become dominated by a single sample. Plain importance sampling is therefore rarely reliable beyond a few dozen dimensions without careful adaptation.

  • Ignoring support coverage: a proposal that is zero where the target is positive produces a biased estimate.
  • Using light-tailed proposals: Gaussian proposals for heavy-tailed targets can give infinite variance.
  • Skipping diagnostics: reporting an estimate without ESS or Pareto k values hides failure.
  • Computing weights without logarithms: densities underflow, so weights should be computed in log space with the log-sum-exp trick.

The idea in brief

Imagine you want the average height of people in a city. You can only survey people at a basketball club. Tall people are overrepresented there. Importance sampling fixes this by giving each answer a weight. Tall people count a little less, and shorter people count a little more.

The result is a fair estimate for the whole city, even though the survey was taken in the wrong place. It works well only if the club still includes some people of every height. If whole groups are missing, no weighting can recover them.

A practical scenario

A data science team builds a Bayesian logistic regression model with 12 parameters to predict loan default. The team fits it with Hamiltonian Monte Carlo and then wants to check how well the model predicts each of 8,000 borrowers when that borrower is left out of the data.

Refitting the model 8,000 times would take days. Instead, the team applies Pareto-smoothed importance sampling to the existing posterior draws, reweighting them to approximate each leave-one-out posterior. The diagnostics show Pareto k below 0.7 for 7,980 borrowers, so those estimates are trusted.

For the remaining 20 influential observations, the team refits the model exactly. The outcome is a complete cross-validation result in under an hour, with explicit warnings where the cheap approximation was not reliable.