Skip to main content
Open In Colab Importance sampling estimates an expectation under one distribution from samples of another. Each sample is weighted by the ratio of the two densities. Resampling in proportion to the weights gives an unweighted sample set that follows the first distribution. The example in this section is one-dimensional.

Target and proposal

Suppose you want the expectation of a function hh under a density ff: Ef[h(x)]=∫h(x) f(x) dx.\mathbb{E}_f[h(x)] = \int h(x) \, f(x) \, dx. With samples x[1],…,x[M]x^{[1]}, \ldots, x^{[M]} drawn from ff, the average of h(x[m])h(x^{[m]}) estimates this integral. Often there is no direct way to sample from ff, for example when ff is the posterior belief of a Bayes filter. Importance sampling draws the samples from another density gg, the proposal distribution. ff is the target distribution. gg must be positive wherever ff is positive, so that every value ff can produce can also be drawn from gg. Multiplying and dividing by gg gives Ef[h(x)]=∫h(x) f(x)g(x) g(x) dx=Eg[h(x) w(x)],w(x)=f(x)g(x).\mathbb{E}_f[h(x)] = \int h(x) \, \frac{f(x)}{g(x)} \, g(x) \, dx = \mathbb{E}_g\left[h(x) \, w(x)\right], \qquad w(x) = \frac{f(x)}{g(x)}. The ratio w(x)w(x) is the importance weight. With samples x[m]∼gx^{[m]} \sim g, the estimate is Ef[h(x)]≈1M∑m=1Mw(x[m]) h(x[m]).\mathbb{E}_f[h(x)] \approx \frac{1}{M} \sum_{m=1}^{M} w(x^{[m]}) \, h(x^{[m]}). Where f>gf > g, the proposal undersamples and the weight is above 1. Where f<gf < g, the weight is below 1. The example target has two modes, like a localization belief when two places match the robot’s observations: f(x)=0.3 N(x;2,0.52)+0.7 N(x;7,12).f(x) = 0.3 \, \mathcal{N}(x; 2, 0.5^2) + 0.7 \, \mathcal{N}(x; 7, 1^2). The proposal is a single wide Gaussian, g(x)=N(x;5,32)g(x) = \mathcal{N}(x; 5, 3^2). The mean of the target is 0.3⋅2+0.7⋅7=5.50.3 \cdot 2 + 0.7 \cdot 7 = 5.5.
Output from cell 3 The samples in the top plot cover the range of the proposal, and their unweighted average estimates the mean of gg. The weights in the bottom plot are large under the two modes of ff and near zero elsewhere. The weighted average is 5.63, against the true mean of 5.5.

Self-normalized weights

In filtering, the target is known only up to a constant. Bayes’ rule gives the posterior as η p(z∣x) p(x)\eta \, p(z \mid x) \, p(x), and the normalizer η\eta is an integral over the state space that the filter does not compute. Write the target as f(x)=ηf~(x)f(x) = \eta \tilde{f}(x), where f~\tilde{f} can be evaluated and η\eta is unknown. Compute the weights from f~\tilde{f} and divide by their sum: w~[m]=f~(x[m])g(x[m]),Ef[h(x)]≈∑m=1Mw~[m] h(x[m])∑m=1Mw~[m].\tilde{w}^{[m]} = \frac{\tilde{f}(x^{[m]})}{g(x^{[m]})}, \qquad \mathbb{E}_f[h(x)] \approx \frac{\sum_{m=1}^{M} \tilde{w}^{[m]} \, h(x^{[m]})}{\sum_{m=1}^{M} \tilde{w}^{[m]}}. η\eta cancels between numerator and denominator. This self-normalized estimate is slightly biased for finite MM, and the bias vanishes as MM grows. Its error typically shrinks as 1/M1/\sqrt{M}, with a constant that depends on how similar gg is to ff (Thrun, Burgard and Fox, 2005, Section 4.2.2). The code below scales the target by an arbitrary factor, as if η\eta were unknown, and repeats the estimate 500 times for each sample size.
Each tenfold increase in MM reduces the error by about 10≈3.2\sqrt{10} \approx 3.2. The factor 17 does not change the estimate.

Choosing the proposal

When gg is close to ff, the weights are nearly equal and every sample contributes. When gg puts little mass where ff is large, the few samples that land there get very large weights, and the estimate depends mostly on them. The effective sample size, computed with normalized weights, N^eff=1∑m=1M(w[m])2,\hat{N}_{\mathrm{eff}} = \frac{1}{\sum_{m=1}^{M} \left(w^{[m]}\right)^2}, measures this. It equals MM when all weights are equal and 1 when a single sample holds all the weight. The code compares three proposals with M=1000M = 1000 samples each: the wide Gaussian used so far, a narrower Gaussian centered between the modes, and a Gaussian that covers only the right mode.
The wide proposal has an effective sample size of about 500 of 1000, and its estimate is close to 5.5. The narrow proposal has little mass near x=2x = 2, where the left mode is, so the few samples there get large weights. Its effective sample size is about 290 and its error is larger. The proposal centered on the right mode almost never produces a sample near x=2x = 2, so the left mode is missing from the weighted set. The weights of its samples are nearly equal, the effective sample size is about 900, and the estimate is 6.9 instead of 5.5. The effective sample size measures how even the weights of the drawn samples are. It cannot reveal a region of the target that the proposal never reaches.

Resampling

Some algorithms need an unweighted sample set, for example to pass the samples through another sampling step. Resampling produces one from the weighted set. Draw MM samples with replacement from the weighted set, each with probability equal to its normalized weight. Samples with large weights are copied several times and samples with small weights tend to disappear. The new set is approximately distributed according to ff, and every sample in it has the same weight.
Output from cell 7 The histogram of the resampled set follows the target. Fewer than half of the original samples survive, many of them as several copies. The mean of the resampled set is no more accurate than the weighted mean before it, and the random draw adds variance. Resampling moves the samples into the regions where the target has mass, where a later step can use them.

Importance ratios in policy optimization

PPO collects actions with an old policy πold\pi_{\text{old}} and updates a new policy πθ\pi_\theta. The ratio πθ(at∣st)/πold(at∣st)\pi_\theta(a_t \mid s_t) / \pi_{\text{old}}(a_t \mid s_t) in its objective is the importance weight that corrects for the samples coming from the old policy, and the clipping limits how large that weight can get.

References

  • Thrun, S., Burgard, W., and Fox, D. (2005). Probabilistic Robotics. MIT Press.

Found an error on this page? Report an issue.