Prerequisite · Probability Foundations

Monte Carlo Estimation and Importance Sampling

Colab Notebook · ~50 min
Google Colab Notebook
Monte Carlo Estimation and Importance Sampling
Python · ~50 min
Open in Colab
Lab Objectives
1
Estimate E[X²] for X ~ N(0,1) via Monte Carlo and confirm the 1/√N convergence rate of estimation error.
2
Verify linearity of expectation empirically for dependent random variables X and Y = X².
3
Implement an importance sampling estimator for E_p[f(x)] using a mismatched proposal q and compare variance to direct sampling.
4
Compute ESS for importance weights from several (p, q) pairs and plot ESS as a function of distributional overlap.
5
Demonstrate importance weight collapse: show that a proposal with lighter tails than the target causes ESS ≈ 1 regardless of sample count.

Lab 2: Monte Carlo Estimation and Importance Sampling

Monte Carlo methods turn intractable integrals into sample averages. This lab implements the estimators from r2 and r5 — expected value, variance, importance sampling, and effective sample size — and builds the intuition for when importance sampling helps and when it catastrophically fails.

What You'll Build

  • A Monte Carlo estimator for E[f(X)]\mathbb{E}[f(X)]: estimate E[X2]\mathbb{E}[X^2] for XN(0,1)X \sim \mathcal{N}(0,1) (true value: 1.0) using sample averages, plot the error vs sample size, and confirm the 1/N1/\sqrt{N} convergence rate
  • A linearity of expectation verifier: for dependent XX and Y=X2Y = X^2 (where XUniform(1,1)X \sim \text{Uniform}(-1,1)), verify empirically that E[X+Y]=E[X]+E[Y]\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y] even though XX and YY are not independent
  • An importance sampling estimator: estimate Ep[f(x)]\mathbb{E}_p[f(x)] for a target p=N(3,1)p = \mathcal{N}(3, 1) using proposal q=N(0,1)q = \mathcal{N}(0, 1), compare variance to direct sampling from pp, and plot the weight distribution
  • An ESS calculator: compute effective sample size (wi)2/wi2(\sum w_i)^2 / \sum w_i^2 for several (p,q)(p, q) pairs with varying overlap, and plot ESS as a function of the KL divergence between pp and qq
  • A weight collapse demonstration: show what happens when the proposal has lighter tails than the target — a handful of samples get enormous weights, ESS collapses to nearly 1, and the estimator becomes unreliable even with many samples

Key Concepts Practiced

By the end you will understand why importance sampling is theoretically valid but practically fragile, what ESS is actually measuring, and why off-policy RL algorithms use clipped importance weights (as in PPO) to prevent collapse.