Importance sampling

· math, monte carlo

You want an expectation under p, but you can only draw samples from q. Reweight each sample and the estimate stays unbiased. The catch is in the variance.

Suppose we want the expectation of ff under pp, but sampling from pp is awkward: it is expensive, it is only known up to a constant, or we already have samples from something else. Multiply and divide by any qq that covers the support of pp and the expectation does not change:

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[f(x)\,w(x)],

with the importance weight w(x)=p(x)/q(x)w(x) = p(x)/q(x).

Every sample carries the weight w(x)=p(x)/q(x)w(x) = p(x)/q(x). When qq puts little mass where pp puts a lot, a rare sample lands with a huge weight and the variance of the estimate explodes. The animation below draws samples one at a time and tracks the running estimate.

Samples arriving one at a time on the left, the running estimate of Pp(x>1)P_p(x > 1) on the right.

The same two densities drawn with manim, rendered once per theme.