Search

Search papers and lessons

Importance Sampling

Level 3 · 9 min read

Importance sampling estimates an average under one distribution by drawing samples from another and reweighting each one by the ratio of the two densities.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Sampling
  • Discrete math
  • Sign in to see your progress

TL;DR

Importance sampling estimates an average under a target distribution pp by drawing samples from a different distribution qq that is easier to sample from. Each sample is multiplied by the weight p(x)/q(x)p(x)/q(x), 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 pp directly.

The trick only works if qq covers the regions where pp 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.

−2024600.10.20.30.4xdensity
  • target p = N(0, 1)
  • proposal q = N(4, 1)
To estimate the tail beyond x = 4 under p, drawing from q puts most samples where the tail is. The weights then bring the average back to p.

The math

Definition

Definition: Importance sampling estimator

Let pp be the target density and qq a proposal density with q(x)>0q(x) \gt 0 wherever p(x)>0p(x) \gt 0. For a function ff, the expectation under pp can be written as an expectation under qq:

Ep[f(X)]=∫f(x) p(x) dx=∫f(x) p(x)q(x) q(x) dx=Eq ⁣[f(X) w(X)],\mathbb{E}_p[f(X)] = \int f(x)\, p(x)\, dx = \int f(x)\, \frac{p(x)}{q(x)}\, q(x)\, dx = \mathbb{E}_q\!\left[f(X)\, w(X)\right],

where w(x)=p(x)/q(x)w(x) = p(x)/q(x) is the importance weight. Drawing X1,…,XNX_1, \dots, X_N from qq, the estimator is

μ^=1N∑n=1Nf(Xn) w(Xn).\hat{\mu} = \frac{1}{N} \sum_{n=1}^{N} f(X_n)\, w(X_n).

Read the first line as a change of variables. The integral is unchanged if we multiply and divide the integrand by q(x)q(x), and the rearranged integral is an average under qq. The estimator is unbiased: its expected value is exactly Ep[f]\mathbb{E}_p[f], by the same algebra.

Why the variance matters

The estimator is unbiased for any valid qq, but its variance depends strongly on the choice. For NN independent samples,

Var(μ^)=1N(Ep ⁣[f(X)2 w(X)]−Ep[f]2).\mathrm{Var}(\hat{\mu}) = \frac{1}{N}\left( \mathbb{E}_p\!\left[f(X)^2\, w(X)\right] - \mathbb{E}_p[f]^2 \right).

The first term is the one to minimise. The best possible proposal, which makes every term constant, is q⋆(x)∝∣f(x)∣ p(x)q^\star(x) \propto |f(x)|\, p(x). That is rarely available, because computing it needs the answer. In practice you choose a qq that is roughly shaped like ∣f∣p|f| p and then check the weights.

Self-normalised form

Often pp is known only up to a constant, p(x)=p~(x)/Zp(x) = \tilde{p}(x)/Z, and ZZ is hard to compute. Replace the weights by w~=p~(x)/q(x)\tilde{w} = \tilde{p}(x)/q(x) and divide by their sum:

μ^SN=∑nf(Xn) w~(Xn)∑nw~(Xn).\hat{\mu}_{\text{SN}} = \frac{\sum_{n} f(X_n)\, \tilde{w}(X_n)}{\sum_{n} \tilde{w}(X_n)}.

The unknown constant cancels. This form is biased for finite NN 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,

Neff=(∑nwn)2∑nwn2.N_{\text{eff}} = \frac{\left(\sum_n w_n\right)^2}{\sum_n w_n^2}.

If all weights are equal, Neff=NN_{\text{eff}} = N. If one weight dominates, NeffN_{\text{eff}} falls toward 1, meaning the estimate rests on a single sample despite NN draws.

Worked examples

Example
A tiny discrete example you can check by hand

Let pp be the target distribution over three outcomes AA, BB, CC, with p=(0.5, 0.3, 0.2)p = (0.5,\ 0.3,\ 0.2). Let q=(0.2, 0.2, 0.6)q = (0.2,\ 0.2,\ 0.6) be the proposal, which samples CC far more often. We want Ep[f]\mathbb{E}_p[f] for f=(1, 2, 10)f = (1,\ 2,\ 10).

  1. The true value is 0.5⋅1+0.3⋅2+0.2⋅10=3.10.5 \cdot 1 + 0.3 \cdot 2 + 0.2 \cdot 10 = 3.1.
  2. The weights are w=p/q=(0.5/0.2, 0.3/0.2, 0.2/0.6)=(2.5, 1.5, 0.3333)w = p/q = (0.5/0.2,\ 0.3/0.2,\ 0.2/0.6) = (2.5,\ 1.5,\ 0.3333).
  3. Check the identity: Eq[fw]=0.2⋅1⋅2.5+0.2⋅2⋅1.5+0.6⋅10⋅0.3333=0.5+0.6+2.0=3.1\mathbb{E}_q[f w] = 0.2 \cdot 1 \cdot 2.5 + 0.2 \cdot 2 \cdot 1.5 + 0.6 \cdot 10 \cdot 0.3333 = 0.5 + 0.6 + 2.0 = 3.1. The weighted average under qq recovers the target value exactly.
  4. Now simulate. Draw four samples from qq and get A,B,C,CA, B, C, C.
    • The fwf w values are 1⋅2.5=2.51 \cdot 2.5 = 2.5, 2⋅1.5=3.02 \cdot 1.5 = 3.0, 10⋅0.3333=3.33310 \cdot 0.3333 = 3.333, and 3.3333.333 again.
    • The estimate is (2.5+3.0+3.333+3.333)/4=3.042(2.5 + 3.0 + 3.333 + 3.333) / 4 = 3.042.
  5. Interpretation: 3.042 is close to 3.1 from only four draws. The rare but important outcome CC was sampled often, and its small target weight 0.33330.3333 pulled its contribution back to the right size.

The effective sample size of these four draws is Neff=(2.5+1.5+0.333+0.333)2/(2.52+1.52+0.3332+0.3332)=21.78/8.72≈2.5N_{\text{eff}} = (2.5 + 1.5 + 0.333 + 0.333)^2 / (2.5^2 + 1.5^2 + 0.333^2 + 0.333^2) = 21.78 / 8.72 \approx 2.5. 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.

Example
A rare-event probability

Let X∼N(0,1)X \sim \mathcal{N}(0, 1), and suppose we want P(X>4)P(X \gt 4). The exact value is about 3.17×10−53.17 \times 10^{-5}, roughly one in 31,600.

  1. Plain Monte Carlo with N=10,000N = 10{,}000 draws from pp expects 10,000×3.17×10−5=0.3210{,}000 \times 3.17 \times 10^{-5} = 0.32 hits. Most of the time it sees none and reports zero.
  2. Use the proposal q=N(4,1)q = \mathcal{N}(4, 1), which sits right on the event. The weight is w(x)=φ(x)φ(x−4)=exp⁡ ⁣(8−4x),w(x) = \frac{\varphi(x)}{\varphi(x - 4)} = \exp\!\left(8 - 4x\right), where φ\varphi is the standard normal density. The algebra is 12((x−4)2−x2)=8−4x\tfrac{1}{2}\big((x-4)^2 - x^2\big) = 8 - 4x.
  3. Roughly half the draws from qq land above 4, so about 5,000 of the 10,000 samples contribute. Two examples of their weights: a draw at x=4.5x = 4.5 has w=e8−18=e−10=4.5×10−5w = e^{8 - 18} = e^{-10} = 4.5 \times 10^{-5}, and a draw at x=4.2x = 4.2 has w=e8−16.8=e−8.8=1.5×10−4w = e^{8 - 16.8} = e^{-8.8} = 1.5 \times 10^{-4}.
  4. The estimate is the average of the indicator times the weight, 1N∑n1{Xn>4} w(Xn)\frac{1}{N} \sum_n \mathbf{1}\{X_n \gt 4\}\, w(X_n). It converges to 3.17×10−53.17 \times 10^{-5} with much lower variance than plain sampling.

Interpretation: the samples that matter are common under qq and rare under pp, 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.

SituationProblemBetter choice
q(x)=0q(x) = 0 where p(x)>0p(x) \gt 0Estimator is biased, and the missing region is never sampledChoose qq with support covering pp
qq has thinner tails than ppWeights explode in the tail, variance is huge or infiniteUse a heavier-tailed qq, such as a Student-t
Weights vary by orders of magnitudeEffective sample size collapsesAdapt qq, or use sequential methods such as particle filters
pp known only up to a constantCannot compute ww exactlySelf-normalised estimator
Samples come from an old policy in RLRatio drifts away from oneClip the ratio, as PPO does, or limit the update size

A practical rule is to look at NeffN_{\text{eff}} before trusting an answer. A reported estimate with NeffN_{\text{eff}} 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 qq?Show answer

Because Eq[f(X) p(X)/q(X)]=∫f(x) p(x)q(x) q(x) dx=∫f(x) p(x) dx=Ep[f]\mathbb{E}_q[f(X)\, p(X)/q(X)] = \int f(x)\, \frac{p(x)}{q(x)}\, q(x)\, dx = \int f(x)\, p(x)\, dx = \mathbb{E}_p[f]. The q(x)q(x) 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 p(B)/q(B)=0.3/0.2=1.5p(B)/q(B) = 0.3 / 0.2 = 1.5.

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 ZZ 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 ZZ never needs to be computed. The price is a small bias at finite NN.

Sources