Skip to content
mlmentorship

Monte Carlo and importance sampling

Estimate expectations by averaging over random samples. The simplest way to compute integrals you can't compute analytically.

Published · 7 min read ·Core ·Foundation
Reading depth

Summary

Monte Carlo estimates an expectation by averaging sampled contributions; importance sampling changes the sampling law and corrects those contributions. The proposal must cover the relevant target contribution, and useful uncertainty calculations require suitable moment and dependence conditions. Normalized weights, unbiased expectations, and diagnostic effective sample size are different objects.

Plain Monte Carlo

For iid and integrable f, is unbiased. If contribution variance is finite, and the iid CLT supports asymptotic normal approximations. The standard-error rate does not remove dimension-dependent variance constants or guarantee superiority over deterministic integration.

Let outcomes be zero and one, target , and . Then target mean is one and direct-sampling contribution variance is .

Importance sampling

For iid proposal draws from q and known normalized p, use:

Choose for the fixture. Weights become and corrected contributions , each appearing with probability one-half. Their mean remains one but variance becomes . Draws [0,1,0,1] give ordinary estimate one; exact four-draw estimator variance is , versus under direct sampling. This particular proposal improves variance; other proposals can worsen it or give infinite variance.

Require q positive wherever contributes, and integrability of the weighted contribution. Proposal (1,0) misses the nonzero contribution entirely: every estimate is zero although the target mean is one.

Lose the Target Normalizing Constant

New task: Evaluate only .

Picture and change: Use self-normalized estimate so the common factor cancels. This requires target coverage and a positive denominator, plus appropriate convergence conditions.

Check: The four fixture weights are ; numerator and denominator both equal twelve. Dividing only that numerator by four gives three, not the desired expectation one.

Use when: The target density is known only up to a common factor. The ratio is generally finite-sample biased, although consistent under suitable conditions.

Move Proposal Mass toward the Integrand

New task: Replace q with , keeping p and f fixed.

Picture and change: Recompute weights as and corrected contributions as .

Check: Contribution mean is still one and variance is . Old proposal weights cannot be reused.

Use when: Proposal draws can concentrate where the integrand contributes; validate coverage and weighted moments rather than weights alone.

Effective sample size

A diagnostic of nonnegative-weight concentration is:

For illustrative weights [1,1,1,1,6], normalized shares are and ESS is . These are sample weight shares, not each sample’s fraction of a general estimate, which also depends on f.

Importance weights

Five draws remain five draws even when unequal weights yield ESS 2.5

Reweighting does not create observationsFive weights (6,1,1,1,1) sum to 10. ESS is 10 squared divided by 40, or 2.5; the actual draw count stays five.Observation identity does notmultiply#16#21#31#41#515 observed draws; total weight 10ESS = 10 squared / 40 = 2.5
Derivation
Five proposal samples become unequal weighted contributions Five samples drawn from proposal q initially count once each. Their p over q importance ratios are 1, 1, 1, 1, and 6. After normalization, the first four samples each contribute 10 percent and the fifth contributes 60 percent. The effective sample size is 10 squared divided by 40, or 2.5, despite a nominal sample count of five. DRAW FROM PROPOSAL q · FIVE SAMPLES, EACH COUNTED ONCE 1 2 3 4 5 MULTIPLY BY IMPORTANCE RATIO w = p(x) / q(x) 1 1 1 1 6 NORMALIZE · EACH BAR'S SHARE OF TOTAL WEIGHT 10% 10% 10% 10% 60% sample 1 2 3 4 5 nominal n = 5, but ESS = 10² / (1² + 1² + 1² + 1² + 6²) = 2.5 effective samples
Read it this way: draw from q, then compute importance ratios. The fifth sample has 60% of total weight, not necessarily 60% of the final estimate. ESS is a concentration diagnostic, not a literal independent sample count. Original illustration follows Owen, Vehtari et al., and Elvira et al..
Check: Why are there five actual draws but only 2.5 effective samples?
Read it this way: Five weights (6,1,1,1,1) sum to 10. ESS is 10 squared divided by 40, or 2.5; the actual draw count stays five.
Does enlarging the first draw create another independent observation?

Equal weights give ESS n; one dominating weight pushes it toward one. This diagnostic cannot reveal missing support, guarantee error bars, or replace weighted-integrand variance checks. It is distinct from autocorrelation-based ESS for dependent Monte Carlo chains.

Where it shows up in ML

Use caseWhat’s the integral
REINFORCE policy gradient
Variational inference (ELBO gradient via reparam alternatives)
Off-policy RL (importance sampling correction)
Bayesian posterior predictive
Importance-weighted autoencoders (IWAE)tighter ELBO via -sample IS

Variance reduction

  • Control variates: subtract while preserving expectation; select c and account for its estimation.
  • Antithetic variates: preserve each sample’s marginal law; benefit requires suitable negative contribution covariance.
  • Stratification: sample strata and combine with target mass weights, not an arbitrary unweighted average.
  • Rao-Blackwellization: conditional expectations reduce variance when their required moments exist.

Common pitfalls

  • Heavy tails: finite target expectation does not imply finite weighted-contribution variance.
  • Changing proposals: reuse requires the correct proposal accounting; trajectory likelihood ratios need their specified sequential factorization.
  • False normal coverage: is an asymptotic iid approximation under suitable moments, not universal finite-sample coverage.
  • Finite-sample bias: with one draw, self-normalization returns f(X), whose q-expectation need not match the p-expectation.