Foundations to Frontier
Tests
A. Mathematical Foundations › A5 Statistics › Bayesian statistics

Monte Carlo Integration and Importance Sampling

Bayesian answers are integrals: posterior means, probabilities, predictive distributions. Monte Carlo replaces them with averages over random samples, with error ∝ 1/√n in any dimension. When you can't sample the target directly, importance sampling draws from a convenient proposal and reweights; self-normalized weights handle unnormalized posteriors, and the effective sample size warns when a few samples dominate. You will use control variates, antithetic and stratified sampling to cut variance, and compute a satellite collision probability 26× more accurately than naive sampling.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Averaging random tries instead of doing hard maths

How big is a lake on a map? Scatter 1000 random dots over the whole map and count the fraction that land in the lake. Fraction × map area ≈ lake area. That’s Monte Carlo: answer a hard averaging question by trying lots of random cases. More dots means a better answer, but slowly: 100 times more dots gives about 10 times more accuracy.

If the lake is tiny, most dots miss it, so you’d be smarter to scatter your dots near the lake and then correct for having cheated. That’s importance sampling.

Sample from the wrong distribution, then reweight: importance sampling
ESS 317 of 2000 · largest single weight 1.8% · E[x] ≈ 0.136 (true 0.050) · P(x > 3) ≈ 6.95e-2 (true 4.76e-2)
Solid: target p (two bumps). Blue dashed: proposal q. Orange spikes: normalized weights of 400 of the samples. A proposal that covers both bumps and has heavier tails than p gives even weights and a high ESS. A narrow one that misses the right bump gives a few enormous weights, a tiny ESS and confidently wrong answers. To estimate the rare tail P(x > 3), put the proposal in the tail.
Why engineers care

The probability that two satellites collide, a mission’s chance of running out of fuel, a posterior mean in a Bayesian model, the expected return of a policy: all are integrals solved by sampling. Doing it efficiently is the difference between seconds and days of computation.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Monte Carlo estimates an average by…
  2. 2. To get 10× more accuracy you need about…
  3. 3. Importance sampling deliberately samples…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Plain Monte Carlo

Ep[f(X)]=∫f(x)p(x) dx≈1n∑i=1nf(xi),xi∼p\mathbb E_p[f(X)] = \int f(x)p(x)\,dx\approx\frac1n\sum_{i=1}^nf(x_i), \quad x_i\sim p

It’s unbiased, with SE σf/n\sigma_f/\sqrt n (A4.3-4), and the rate doesn’t depend on dimension. Grids cost mdm^d evaluations. That’s why Monte Carlo rules high-dimensional integration. For Bayesian problems, pp is the posterior: sample from it (by MCMC, A5.2-7) and average to get means, intervals and predictive draws.

Importance sampling

If sampling pp is hard, sample qq and reweight:

Ep[f]=Eq[f(X)p(X)q(X)]≈1n∑if(xi)wi,wi=p(xi)q(xi)\mathbb E_p[f] = \mathbb E_q\Big[f(X)\frac{p(X)}{q(X)}\Big]\approx\frac1n\sum_if(x_i)w_i, \quad w_i = \frac{p(x_i)}{q(x_i)}

This needs q>0q > 0 wherever pf≠0pf\ne0 (A4.3-1, Radon–Nikodym). For a posterior known only up to a constant, use self-normalized weights w~i=wi/∑jwj\tilde w_i = w_i/\sum_jw_j. That’s slightly biased but consistent.

📝 Quick check (Level 1)
0/3 answered
  1. 1. The importance weight is…
  2. 2. Self-normalized IS is needed when…
  3. 3. Monte Carlo's error rate depends on dimension…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Effective sample size and choosing q

ESS=(∑wi)2∑wi2∈[1,n]\text{ESS} = \frac{(\sum w_i)^2}{\sum w_i^2}\in[1, n]

It’s roughly the number of i.i.d. target samples that the weighted set is worth. The best qq for E[f]\mathbb E[f] is ∝∣f∣p\propto\lvert f\rvert p. In practice:

  • Match the target’s location and spread (for example, a Laplace approximation: Gaussian at the MAP with the inverse-Hessian covariance).
  • Use heavier tails than pp (Student-t). A proposal narrower than the target gives infinite-variance weights: a few huge weights, and estimates that look precise but are wrong.
  • For rare events, centre the proposal on the event, as in the collision example.

In high dimensions, weights degenerate (the ESS collapses) unless qq is very close to pp. That’s the motivation for MCMC and sequential Monte Carlo.

Variance-reduction tricks

  • Control variates: f−c(g−Eg)f - c(g - \mathbb Eg) for a correlated gg with known mean, using c∗=Cov⁡(f,g)/Var⁡gc^* = \operatorname{Cov}(f, g)/\operatorname{Var}g. Variance drops by a factor of 1−ρ21 - \rho^2.
  • Antithetic variates: pair UU with 1−U1 - U (or ZZ with −Z-Z). It helps for monotone ff.
  • Stratified sampling: a fixed number of samples per stratum removes the between-strata variance. Latin hypercube sampling extends it to many dimensions.
  • Quasi-Monte Carlo: low-discrepancy sequences (Sobol, Halton) reach close to 1/n1/n error for smooth integrands.
🐍 Exercise 1: self-normalized importance sampling for a posterior
Estimate the posterior mean of a success rate (48/50, flat prior: Beta(49, 3) up to a constant) by SNIS with three proposals: the prior, a Laplace-approximation Gaussian, and a too-narrow Gaussian. Compare accuracy and ESS.
📝 Quick check (Level 2)
0/3 answered
  1. 1. ESS of weights (3, 1)?
  2. 2. A proposal narrower than the target leads to…
  3. 3. A control variate with correlation ρ reduces the variance by the factor…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Unbiasedness and variance of IS

Eq[fw]=∫fpqq=∫fp\mathbb E_q[fw] = \int f\frac pqq = \int fp. So IS is unbiased, with variance 1n(∫f2p2q−(Epf)2)\frac1n\big(\int\frac{f^2p^2}q - (\mathbb E_pf)^2\big). By Cauchy–Schwarz, this is minimized by q∗∝∣f∣pq^*\propto\lvert f\rvert p. For f≥0f\ge0 that gives zero variance, because fwfw is then constant. You can’t use q∗q^* directly (its normalizer is the answer you’re seeking), but it tells you where to put samples. ∎

The finite-variance condition

∫f2p2/q<∞\int f^2p^2/q<\infty requires qq’s tails to be at least as heavy as p∣f∣p\lvert f\rvert’s. For a Gaussian target N(0,1)\mathcal N(0, 1) and proposal N(0,s2)\mathcal N(0, s^2), the weight variance is finite iff s2>1/2s^2 > 1/2. With a narrower proposal, the weights look fine on most runs, and then occasionally one is astronomically large.

Self-normalized IS: bias and ESS

μ^SN=∑wif(xi)∑wi\hat\mu_{SN} = \frac{\sum w_if(x_i)}{\sum w_i} is a ratio estimator. Its bias is O(1/n)O(1/n), and its variance is ≈1nEq[w2(f−μ)2]/(Eqw)2\approx\frac1n\mathbb E_q[w^2(f - \mu)^2]/(\mathbb E_qw)^2. Kish’s ESS approximates n⋅Var⁡p(f)/Var⁡(μ^)n\cdot\operatorname{Var}_p(f)/\operatorname{Var}(\hat\mu) under mild conditions. Pareto-smoothed IS (PSIS) diagnoses heavy-tailed weights by fitting a generalized Pareto distribution to the largest weights (Vehtari et al., 2015).

Exercise 3.1: Show that antithetic sampling helps for monotone f

Var⁡f(U)+f(1−U)2=Var⁡f2(1+ρ)\operatorname{Var}\frac{f(U) + f(1 - U)}2 = \frac{\operatorname{Var}f}2(1 + \rho), with ρ=Corr⁡(f(U),f(1−U))\rho = \operatorname{Corr}(f(U), f(1 - U)). For monotone ff, ρ≤0\rho\le0 (Chebyshev’s association inequality), so the variance is at most that of 2 independent samples.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The zero-variance IS proposal is…
  2. 2. For p = N(0,1), q = N(0, s²), weight variance is finite iff…
  3. 3. SNIS bias is of order…

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

Satellite collision probability

At a close approach, the relative position in the encounter plane is Gaussian (from the two orbit covariances), offset by the predicted miss vector. The collision probability PcP_c is the probability mass inside the combined hard-body disc, here about 4×10−44\times10^{-4}. Conjunction screening triggers avoidance manoeuvres at thresholds like 10−410^{-4}. Naive Monte Carlo with 20,000 samples sees only about 8 hits, an error of ±35%. Sampling a proposal centred on the disc and reweighting gives ±1.2% from the same samples. Operational tools use Foster’s 2-D integral or Alfano’s series for such cases, and Monte Carlo with importance sampling when the encounter is nonlinear (Alfano, 2005; Chan, 2008).

🐍 Exercise 2 (research): conjunction collision probability by importance sampling
Compute Pc for a 2-D Gaussian encounter (miss vector, correlated covariance, 20 m hard-body radius) by grid integration, naive Monte Carlo and importance sampling with a proposal centred on the disc. Compare the errors.

Monte Carlo in AI, robotics and aerospace

  • Policy evaluation and policy gradients are Monte Carlo averages over rollouts. Off-policy evaluation uses importance weights with clipping and doubly robust corrections (Precup, Sutton and Singh, 2000; Jiang and Li, 2016).
  • Particle filters are sequential importance sampling with resampling. The ESS triggers resampling (Doucet, de Freitas and Gordon, 2001).
  • Rare-event safety validation of autonomous systems uses adaptive importance sampling and the cross-entropy method to find and weight failures (Rubinstein, 1999; O’Kelly et al., 2018).
  • Mission dispersion analysis (entry, descent and landing; launch) runs thousands of Monte Carlo trajectories. Latin hypercube and stratified designs reduce the runs needed.
  • Variational autoencoders and IWAE use importance-weighted bounds on the marginal likelihood (Burda, Grosse and Salakhutdinov, 2016).

References

  1. C. Robert, G. Casella (2004). Monte Carlo Statistical Methods, 2nd ed. Springer.
  2. A. Owen (2013). Monte Carlo Theory, Methods and Examples. Online book (statweb.stanford.edu/~owen/mc).
  3. L. Kish (1965). Survey Sampling. Wiley (effective sample size).
  4. A. Vehtari, D. Simpson, A. Gelman, Y. Yao, J. Gabry (2015). “Pareto smoothed importance sampling.” arXiv:1507.02646.
  5. R. Rubinstein (1999). “The cross-entropy method for combinatorial and continuous optimization.” Methodology and Computing in Applied Probability, 1.
  6. S. Alfano (2005). “A numerical implementation of spherical object collision probability.” Journal of the Astronautical Sciences, 53(1).
  7. F. K. Chan (2008). Spacecraft Collision Probability. The Aerospace Press.
  8. A. Doucet, N. de Freitas, N. Gordon, eds. (2001). Sequential Monte Carlo Methods in Practice. Springer.
  9. M. O’Kelly, A. Sinha, H. Namkoong, J. Duchi, R. Tedrake (2018). “Scalable end-to-end autonomous vehicle testing via rare-event simulation.” NeurIPS.
  10. Y. Burda, R. Grosse, R. Salakhutdinov (2016). “Importance weighted autoencoders.” ICLR.