Advanced sampling and rare eventsLesson 63 of 76 · 9 min

Weight samples from another distribution

Use a target-to-proposal density ratio to estimate a target expectation.

Importance sampling estimates an expectation under a target distribution p while drawing from a proposal q. Multiply the function value by p(x)/q(x), then average under q. The density ratio corrects for sampling some regions more or less often.

The target here is standard normal and the function is eXe^X. We draw from a normal proposal shifted towards larger X, where the function contributes more. Using the unweighted proposal average would answer a different question.

Work in log densities when convenient, then exponentiate the combined contribution. The proposal must cover every region where the target integrand contributes. Ordinary MCSE also needs independent weighted contributions with finite variance; it is the variance of those contributions, not just of raw X, that matters. Poor overlap can make a modest run misleading.

Return proposal draws to the target
  1. Draw from the proposal
    Xi∼qX_i\sim q
  2. Account for the changed law
    wi=p(Xi)/q(Xi)w_i=p(X_i)/q(X_i)
  3. Average weighted contributions
    μ^=1n∑iwif(Xi)\widehat\mu=\frac1n\sum_iw_if(X_i)
Reference illustration. Ordinary importance sampling here uses a normalised target, full proposal support where the integrand matters, and valid weighted variance. The unweighted proposal average answers a different question.

Does moving the proposal mean change the target expectation when weights are correct?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Weighted target expectation estimate: 1.64994
Exact target E[exp(X)]: 1.64872
Unweighted proposal average: 2.70882
Weighted estimated MCSE: 0.006148

Read the first result

The target stays standard normal. The visibly different unweighted average estimates the proposal expectation.

Try an edit

Replace proposal_mean = .5 with proposal_mean = 0 and select Run Python. Then use proposal_mean = 1. Select Run Python again. Compare Weighted target expectation estimate, Unweighted proposal average and Weighted estimated MCSE across the three runs. Which quantity changes target with the proposal?

Back to the Python editor

Scope. The example uses known normal densities and a well-supported target. Weighting does not repair an incorrect target model.

Try a fresh case

Which ratio corrects a draw from proposal q to target p?

Choose an answer
Need a hint?
Hint 1

Write the expectation as an integral under q.

Hint 2

Insert the ratio that cancels q and restores p.

Your notes