Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF007 · Algorithm

Importance sampling and self-normalized importance sampling

Estimates target expectations using draws from a different, easier proposal distribution. Ordinary and self-normalized estimators are treated as one family because they share the same sampling mechanism;

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through bandit.ope. The reference's example ran here in one language.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Estimates target expectations using draws from a different, easier proposal distribution. Ordinary and self-normalized estimators are treated as one family because they share the same sampling mechanism; their normalization, finite-sample bias, and requirements differ.

Question / hypotheses

Can proposal samples cover the important regions of a target integral, with density ratios correcting the change in sampling distribution?

Architecture / computational structure

Draw independently from q, evaluate target and proposal log densities, stabilize weights numerically, and aggregate h. For self-normalization, subtracting a common log-weight maximum is harmless; ordinary estimates must restore the discarded scale. Error assessment should consider the particular integrand, not weights alone.

Assumptions

The proposal must be positive wherever the target integrand contributes. Ordinary unbiased estimation requires a known normalized target density and integrability; a finite Monte Carlo variance requires stronger tail conditions. Self-normalized estimation allows an unknown target normalizer but still needs support coverage and appropriate consistency conditions.

Formula / computational definition

xi∼q,wi=p~(xi)/q(xi),μ^SN=∑iwih(xi)∑iwi;μ^IS=N−1∑ip(xi)q(xi)h(xi).x_i\sim q,\quad w_i=\tilde p(x_i)/q(x_i),\quad \hat\mu_{SN}=\frac{\sum_iw_ih(x_i)}{\sum_iw_i};\quad \hat\mu_{IS}=N^{-1}\sum_i\frac{p(x_i)}{q(x_i)}h(x_i).

Parameters and notation

The target is fixed; proposal parameters may be fitted on separate pilot draws or updated by a justified adaptive scheme. Hyperparameters include proposal family, tail scale, sample count, defensive mixtures, and adaptation schedule. Weight ESS=(sum w)^2/sum w^2 is a concentration diagnostic, not a universal error certificate.

Fitting, fine-tuning, and adaptation

Fit a broader proposal to pilot posterior information and then obtain evaluation draws under the documented scheme. Refit after a changed target. A neural density can provide q, but fine-tuning it must preserve evaluable normalized proposal densities and support; a sampler that cannot evaluate q is insufficient for ordinary importance weighting.

How to use and calibrate

Inspect log-weight histograms, maximum normalized weight, repeated-run estimates, and tail sensitivity. Compare several proposals and known integrals. Plot estimate versus sample size; agreement across a few runs cannot reveal an important region that none of them visited.

Example application

Estimate posterior moments or rare-event probabilities from a tractable Gaussian proposal, or reweight draws under a modest prior change without rerunning a full sampler when overlap is adequate.

Interpretation / pitfalls

Watch out

A few enormous weights can dominate estimates, and increasing dimension often destroys overlap. Self-normalized estimates are generally biased at finite N even when consistent. Weight clipping or smoothing trades bias for stability and must not be reported as the original estimator without qualification.

Python and R code examples

Scope: Ordinary and self-normalized estimates of E[X^2] for N(0,1), using 10,000 N(0,4) proposal draws; results vary by language RNG.

Implementation route: NumPy random draws and base R rnorm provide native implementations. For production, retain log densities and use stable log-sum-exp arithmetic, with diagnostics appropriate to the target and integrand.

Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Ordinary and self-normalized estimates of E[X^2] for N(0,1), using 10,000 N(0,4) proposal draws; results vary by language RNG. · needs numpy

import numpy as np
rng=np.random.default_rng(7); x=rng.normal(0,2,10000)
w=2*np.exp(-.5*x*x+.5*(x/2)**2)
ordinary=np.mean(w*x*x); normalized=np.sum(w*x*x)/w.sum(); ess=w.sum()**2/(w@w)
assert abs(ordinary-1)<.1 and abs(normalized-1)<.1
print('ordinary',ordinary,'self-normalized',normalized,'weight ESS',ess)
R

Component demo, not a trained model — Ordinary and self-normalized estimates of E[X^2] for N(0,1), using 10,000 N(0,4) proposal draws; results vary by language RNG.

set.seed(7); x <- rnorm(10000,0,2)
w <- 2*exp(-.5*x*x+.5*(x/2)^2)
ordinary <- mean(w*x*x); normalized <- sum(w*x*x)/sum(w); ess <- sum(w)^2/sum(w*w)
print(c(ordinary=ordinary,self.normalized=normalized,weight.ESS=ess))

Sources

From the CodeSplash reference, entry INF007.