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 . 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.
- Draw from the proposal
- Account for the changed law
- Average weighted contributions
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.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
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?
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?
Need a hint?
Hint 1
Write the expectation as an integral under q.
Hint 2
Insert the ratio that cancels q and restores p.