MathalyseResearch

Learn AI · Chapter 17 · Monte Carlo Methods

Importance Sampling

Rewrite a sum as an average under a different distribution, weight each sample to correct for the change, and choose that distribution so the estimate stays steady, including when the normalising constant is unknown.

Book §17.2, pages 592–595Equations 17.8–17.16About 40 minutes
Section 1

Choosing which part plays the distribution

How to use this page

Click the colored symbols. Clicking a symbol explains it and lights up the same symbol in the other equations. Commit to a guess before each reveal. Each demo shows the result once you have chosen.

p target distribution x, f states and function estimates variances q and weights

Look back at the sum from the last module. A Monte Carlo estimate needs samples from , and sometimes that is impossible. Importance sampling asks a different question. Can we move the work into a different distribution , which is easy to sample, and correct the answer with a factor for each sample?

The book makes the point that there is no unique split of a sum into a distribution and a function. The same product can be written in many ways. Predict the best place for the proposal , then check.

Pick a guess.

The task in the demo is the tail probability under the standard normal , so is the indicator of . This worked task is added here and is not in the book. Equation 17.8 below is the identity behind the method. Only the proposal changes, and the value does not.

(17.8)
Click any coloured symbol to see what it means.

Read it from left to right. The right side multiplies and divides by , so the product is unchanged. Averaging over with the ratio in place of gives the same answer. Try three proposals in the demo below. The area under the product is the same in every case, because only the proposal changes.

Same integrand, different proposal

target pproposal qp f, the integrandweight p f / q

The log scale makes the tail visible. The integrand is zero below 3, and at 3 the target is already small. A proposal that puts its mass over keeps the weight small and smooth there. A proposal far from the tail gives a weight that is huge where the proposal is thin and zero where it is not needed.

Section 2

The importance sampling estimator

The naive estimator draws from and averages :

(17.9)
Click any coloured symbol to see what it means.

Draw samples from instead, and average the weighted values. The result is the importance sampling estimator:

(17.10)
Click any coloured symbol to see what it means.

Both estimators have the same expected value, because the ratio cancels the change of distribution on average:

(17.11)
Click any coloured symbol to see what it means.

Pick a guess.

The two estimators differ in their variance, not their mean. The demo runs both on the same tail probability. The naive estimator often sees nothing at all, because the event is rare, while the importance estimator puts many of its draws where the answer lives.

Naive or importance: which one finds the tail?

estimate, with error bartrue value, dashed
Challenge +8 XP

Choose a proposal that makes the importance standard error at least 5 times smaller than the naive one at the same n.

The standard error of each estimator is the square root of its variance divided by . The naive estimator has per-sample variance . The importance estimator depends on the proposal, and the readout reports its ratio exactly. Both error bars come from the samples, so they wobble from one draw to the next.

Check

With the proposal equal to the target ( = 0, = 1), the weight is 1 wherever , so both estimators are the same method. The challenge needs a different proposal.

Section 3

Variance and the best proposal

For the importance estimator the variance is

(17.12)
Click any coloured symbol to see what it means.

The variance is set by the spread of . The estimator is good when this ratio is nearly constant. The best proposal makes it constant:

(17.13)
Click any coloured symbol to see what it means.

Here is the normalising constant, which makes sum or integrate to 1. When does not change sign, a sample from gives zero variance. The book adds that this is no help in practice, because building already requires knowing the answer. The book also notes that better proposals put more weight where the integrand is larger, and that other choices of q can still reduce the variance somewhat even when is out of reach.

Per-sample variance as the proposal moves

variance with this proposalvariance from sampling p
Challenge +8 XP

Find a proposal whose per-sample variance is at least 100 times smaller than the variance from sampling the target.

The formula for the variance of the weighted indicator is worked out here and is not printed in the book. The demo uses it in closed form, so the numbers are exact. The curve has its lowest value near the tail, and it rises steeply when the proposal is too far away or too narrow.

Check

A proposal centred at 3 with = 1 cuts the variance by a factor of about 200 compared with sampling . The best proposal would cut it to zero, but it cannot be used because it needs the answer.

Section 4

Biased importance sampling

Often the normalising constant of is unknown, so we only have , which equals up to a constant and is not a probability. The same is true of the proposal. The book's remedy is the biased estimator, which divides by the sum of the weights, so the unknown constants cancel:

(17.14)
Click any coloured symbol to see what it means.

Equation 17.14 uses normalised and . Equations 17.15 and 17.16 write the same estimator with unnormalised forms, the last one with both and . Each weight is a ratio, so an unknown constant in or multiplies every weight by the same factor, and the division by the sum of the weights removes that factor.

(17.15)
Click any coloured symbol to see what it means.
(17.16)
Click any coloured symbol to see what it means.

The estimator is biased for any finite , but the bias disappears as grows, which is why the book calls it asymptotically unbiased. The demo below uses a small discrete example added here. The target is = 1, 4, 6, 4, 1 on the states 1 to 5, and the proposal is uniform. The chart plots both estimates against the number of samples n.

Normalised against unnormalised weights

normalised estimateunnormalised estimate
Challenge +8 XP

With 2000 samples, bring the normalised estimate to within 0.05 of the true value 3.

The unnormalised estimate does not settle at the true value. It settles at the target's total mass times the true value, which is here, because 16 is the total mass of the unnormalised target. Without knowing that total, the unnormalised estimate is useless. The normalised one does not need it.

Section 5

When the weights run wild

The proposal is usually chosen to be a simple distribution, so that it is easy to sample from. Simplicity can make it match or poorly, and then the estimate can be much worse than a good choice. When is thin where is large, the weights become enormous there, and most samples land where the weights are small. The book describes the result as typical underestimation, with rare large overestimates that compensate on average.

This happens more often in high dimensions, where the joint probabilities have a huge dynamic range. The book names several places where importance sampling still helps. It speeds up training of neural language models with a large vocabulary, estimates partition functions (section 18.7), and estimates the log-likelihood of directed models such as the variational autoencoder (section 20.10.3). It can also focus the gradient on hard examples, which dominate the cost of a classifier.

One hundred samples, repeated two hundred times

one run eachtrue value, dashed
Challenge +8 XP

Set the proposal spread so that the variance becomes infinite. The readout says so when it happens.

The estimates form a row of dots. Most runs return zero, because no sample falls in the tail. A few runs land far above the truth. An average of many runs is reliable only when the variance is finite, and the readout shows when it is not.

Check

With a narrow proposal, the average of the squared weights diverges, so the variance of the estimate is infinite. The readout reports this as a condition on the spread, not as a number.

  • Importance sampling rewrites a sum as an average under a proposal and corrects it with the weight (17.8 to 17.10).
  • Both estimators are unbiased, and the choice of proposal only changes the variance (17.11 and 17.12).
  • The best proposal is proportional to (17.13), which is not usable in practice, but it shows where the proposal should put its mass.
  • When the normalising constants are unknown, the biased estimator (17.14 to 17.16) divides by the sum of the weights, so the constants cancel.
  • A proposal with thin tails can make the variance infinite, so the estimate can be wrong on most runs and far off on a few.
Final quiz

Check yourself

Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.

Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 17.2 (pages 592–595), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.