Monte Carlo Estimation and Importance Sampling
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 : estimate for (true value: 1.0) using sample averages, plot the error vs sample size, and confirm the convergence rate
- A linearity of expectation verifier: for dependent and (where ), verify empirically that even though and are not independent
- An importance sampling estimator: estimate for a target using proposal , compare variance to direct sampling from , and plot the weight distribution
- An ESS calculator: compute effective sample size for several pairs with varying overlap, and plot ESS as a function of the KL divergence between and
- 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.