TL;DR
Importance sampling estimates an average under a target distribution by drawing samples from a different distribution that is easier to sample from. Each sample is multiplied by the weight , which corrects for the fact that it came from the wrong place. Done well, the estimate is accurate with far fewer samples than drawing from directly.
The trick only works if covers the regions where has probability and is not too thin in the tails. Otherwise a few huge weights dominate the answer.
Where you'll see it: in estimating rare-event probabilities, in off-policy reinforcement learning where data comes from an older policy, and in the importance ratio that keeps PPO updates honest.
Intuition
Asking a different crowd
Suppose you want the average wealth of a country's residents, but you can only phone people at random. Almost everyone you reach is middling, while the few billionaires who dominate the total are almost never called. A better plan is to oversample the wealthy on purpose. The raw average of that sample is too high, because the rich are over-represented compared with the population. To fix it, you give each person a weight that undoes the difference in how likely you were to reach them.
That is the whole method. You sample where it is convenient or informative, and then reweight so the answer is the one you would have got from the right population. The weight is a likelihood ratio: how much more likely a sample is under the target than under the place it was drawn from.
This matters because Monte Carlo estimates depend on samples landing in the places that matter. If the quantity you need is driven by a tiny region of the distribution, plain sampling spends almost every draw outside it. Importance sampling moves the draws into that region and pays for it with weights.
- target p = N(0, 1)
- proposal q = N(4, 1)
The math
Definition
Let be the target density and a proposal density with wherever . For a function , the expectation under can be written as an expectation under :
where is the importance weight. Drawing from , the estimator is
Read the first line as a change of variables. The integral is unchanged if we multiply and divide the integrand by , and the rearranged integral is an average under . The estimator is unbiased: its expected value is exactly , by the same algebra.
Why the variance matters
The estimator is unbiased for any valid , but its variance depends strongly on the choice. For independent samples,
The first term is the one to minimise. The best possible proposal, which makes every term constant, is . That is rarely available, because computing it needs the answer. In practice you choose a that is roughly shaped like and then check the weights.
Self-normalised form
Often is known only up to a constant, , and is hard to compute. Replace the weights by and divide by their sum:
The unknown constant cancels. This form is biased for finite but consistent, and it is what most practical code uses.
The effective sample size
A quick diagnostic for weight trouble is the effective sample size,
If all weights are equal, . If one weight dominates, falls toward 1, meaning the estimate rests on a single sample despite draws.
Worked examples
Let be the target distribution over three outcomes , , , with . Let be the proposal, which samples far more often. We want for .
- The true value is .
- The weights are .
- Check the identity: . The weighted average under recovers the target value exactly.
- Now simulate. Draw four samples from and get .
- The values are , , , and again.
- The estimate is .
- Interpretation: 3.042 is close to 3.1 from only four draws. The rare but important outcome was sampled often, and its small target weight pulled its contribution back to the right size.
The effective sample size of these four draws is . Four draws are worth about two and a half equally weighted ones, a mild loss caused by the weights ranging from 0.33 to 2.5.
Let , and suppose we want . The exact value is about , roughly one in 31,600.
- Plain Monte Carlo with draws from expects hits. Most of the time it sees none and reports zero.
- Use the proposal , which sits right on the event. The weight is where is the standard normal density. The algebra is .
- Roughly half the draws from land above 4, so about 5,000 of the 10,000 samples contribute. Two examples of their weights: a draw at has , and a draw at has .
- The estimate is the average of the indicator times the weight, . It converges to with much lower variance than plain sampling.
Interpretation: the samples that matter are common under and rare under , and the weights are small but well-behaved. This is why importance sampling is the standard tool for tail probabilities in risk and reliability work.
Tradeoffs and failure modes
Importance sampling is only as good as its proposal. The main failures are below.
| Situation | Problem | Better choice |
|---|---|---|
| where | Estimator is biased, and the missing region is never sampled | Choose with support covering |
| has thinner tails than | Weights explode in the tail, variance is huge or infinite | Use a heavier-tailed , such as a Student-t |
| Weights vary by orders of magnitude | Effective sample size collapses | Adapt , or use sequential methods such as particle filters |
| known only up to a constant | Cannot compute exactly | Self-normalised estimator |
| Samples come from an old policy in RL | Ratio drifts away from one | Clip the ratio, as PPO does, or limit the update size |
A practical rule is to look at before trusting an answer. A reported estimate with of 3 out of 10,000 should not be believed, however precise its digits look.
History
Importance sampling came out of the early work on Monte Carlo methods at Los Alamos and the RAND Corporation, where physicists and mathematicians needed to estimate rare events such as particle penetration through shielding. Herman Kahn is closely associated with the variance-reduction ideas that include it, and his paper with Andy Marshall on reducing sample size in Monte Carlo computations (early 1950s) is a common reference for the reweighting trick.
The method spread into statistics and then into computing. Hammersley and Handscomb's 1964 monograph on Monte Carlo methods, and later Art Owen's textbook, made it standard. In the 2010s and 2020s it became central to machine learning: off-policy reinforcement learning reuses data from older policies through importance ratios, and the clipped ratio in PPO is a direct descendant of the same idea.
Check yourself
QuestionWhy is the importance sampling estimator unbiased, given samples drawn from ?Show answer
Because . The cancels inside the integral, so the expected value of each weighted term is the target expectation.
QuestionUsing the discrete example above, with f = (1, 2, 10), p = (0.5, 0.3, 0.2) and q = (0.2, 0.2, 0.6), what is the weight for outcome B?Show answer
The weight is .
QuestionA proposal assigns probability zero to a region where the target has positive probability. What goes wrong?Show answer
Samples are never drawn from that region, so its contribution to the expectation is missing. The estimator is biased, and no number of samples fixes it, because the weight there is undefined.
QuestionApplied scenario: an RL agent reuses 1,000 transitions collected by an old policy. For a few transitions the ratio of new to old action probabilities is 40. What should you check and why?Show answer
Check the effective sample size and the spread of ratios. A ratio of 40 means those transitions dominate the gradient, which makes the update noisy and can push the policy too far. Clipping the ratio, as PPO does, or shrinking the step size limits that damage.
QuestionWhy does the self-normalised estimator still work when the normalising constant of the target is unknown?Show answer
The constant appears in every weight, so it cancels between the numerator and the denominator of the ratio. The estimate depends only on the unnormalised density, so never needs to be computed. The price is a small bias at finite .