Importance sampling
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 under , but sampling from 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 that covers the support of and the expectation does not change:
with the importance weight .
Samples are drawn from (ticks) and reweighted by . Here is the indicator of the shaded region, so is placed where is nonzero.
Every sample carries the weight . When puts little mass where 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 on the right.
The same two densities drawn with manim, rendered once per theme.