Skip to content
QFT.org

Search QFT.org

Enter a word or phrase.

    You can also browse the main navigation and chapter contents.

    Importance Sampling and Reweighting

    An expectation value may be difficult to sample even when its integrand is easy to evaluate. Importance sampling draws from a convenient distribution and corrects the mismatch with weights. The correction is an exact integral identity, but a finite collection of weights can fail badly if important configurations are almost never proposed.

    We derive ordinary and self-normalized estimates, then test both a Gaussian moment and a double-well integral. The double well illustrates a striking failure: thousands of apparently effective samples can still miss a quarter of the target probability.

    Preparation: Probability densities, expectation values, and variance. All draws on this page are independent; the additional errors of correlated draws are treated in Autocorrelation and Effective Sample Size.

    Change the sampling distribution

    Let p(x)=p~(x)/Zp(x)=\widetilde p(x)/Z be a normalized target density, where Z=∫p~(x) dxZ=\int\widetilde p(x)\,\mathrm dx is finite and positive. A normalized proposal q(x)q(x) must be positive wherever the target contributes. For independent Xi∼qX_i\sim q, write

    I=Ep[f]=∫q(x)p(x)q(x)f(x) dx.I=\mathbb E_p[f]=\int q(x)\frac{p(x)}{q(x)}f(x)\,\mathrm dx.

    If ZZ is known, the ordinary estimator

    I^=1N∑i=1Np(Xi)q(Xi)f(Xi)\widehat I=\frac1N\sum_{i=1}^{N}\frac{p(X_i)}{q(X_i)}f(X_i)

    is unbiased whenever ff is integrable under pp. Its variance, when finite, is

    Var⁡(I^)=1N[∫p(x)2f(x)2q(x) dx−I2].\operatorname{Var}(\widehat I)=\frac1N \left[\int\frac{p(x)^2f(x)^2}{q(x)}\,\mathrm dx-I^2\right].

    This follows by computing the second moment of one weighted draw and dividing by NN. The denominator reveals the danger: a proposal that is very small where p∣f∣p|f| matters can produce enormous fluctuations.

    The variance-minimizing density is proportional to p∣f∣p|f|, when this can be normalized and sampled. To see this, apply Cauchy–Schwarz to p∣f∣/qp|f|/\sqrt q and q\sqrt q:

    (∫p∣f∣)2≤∫p2f2q.\left(\int p|f|\right)^2\leq\int\frac{p^2f^2}{q}.

    Equality requires q∝p∣f∣q\propto p|f|. If ff has one sign this would make every weighted contribution identical, but constructing that proposal may require the very integral or sampling problem we are trying to solve. The result still gives practical guidance: proposal quality depends on the observable as well as the target.

    An unknown partition function gives a ratio

    Field-theory weights usually have a known action and an unknown partition function. Define w(x)=p~(x)/q(x)w(x)=\widetilde p(x)/q(x). Then Eq[w]=Z\mathbb E_q[w]=Z and Eq[wf]=ZI\mathbb E_q[wf]=ZI, so use

    I^SN=∑iw(Xi)f(Xi)∑iw(Xi).\widehat I_{\mathrm{SN}}= \frac{\sum_i w(X_i)f(X_i)}{\sum_i w(X_i)}.

    Laws of large numbers for the two integrable variables give consistency as N→∞N\to\infty. At finite NN, however, the expectation of a ratio is generally not the ratio of expectations.

    Put A=N−1∑iwifiA=N^{-1}\sum_iw_if_i and B=N−1∑iwiB=N^{-1}\sum_iw_i. Expanding A/BA/B about (ZI,Z)(ZI,Z) gives the leading fluctuation

    I^SN−I≃1NZ∑iwi(fi−I).\widehat I_{\mathrm{SN}}-I \simeq\frac{1}{NZ}\sum_iw_i(f_i-I).

    Provided this expansion and the relevant second moments are controlled,

    Var⁡(I^SN)≃Var⁡q[w(f−I)]NZ2.\operatorname{Var}(\widehat I_{\mathrm{SN}}) \simeq\frac{\operatorname{Var}_q[w(f-I)]}{NZ^2}.

    The numerator and denominator fluctuate together. Treating their errors as independent drops their covariance. Keeping the quadratic terms also gives the leading bias, under the stronger moment and remainder assumptions needed for an expectation expansion:

    E[I^SN]−I≃IVar⁡q(w)−Cov⁡q(wf,w)NZ2.\mathbb E[\widehat I_{\mathrm{SN}}]-I \simeq\frac{I\operatorname{Var}_q(w)-\operatorname{Cov}_q(wf,w)}{NZ^2}.

    These are large-sample approximations, not promises that an available run has reached that regime. For example, with only one sample, the self-normalized estimate is simply f(X1)f(X_1) and its expectation is under qq, regardless of the nonzero weight.

    Reweighting between actions is the same identity. Samples from p0∝e−S0p_0\propto e^{-S_0} can estimate a target with S1S_1 using w=e−(S1−S0)w=e^{-(S_1-S_0)} in the ratio. This requires target support and integrable weights. This page assumes positive weights; complex phases and sign cancellations require separate analysis.

    A Gaussian can have infinite sampling variance

    Take p=N(0,1)p=N(0,1), f(x)=x2f(x)=x^2, and q=N(0,σ2)q=N(0,\sigma^2). The desired answer is I=1I=1. Both densities are positive on the whole real line, but support alone does not guarantee a finite variance. The ordinary estimator's second-moment integrand is proportional to

    x4exp⁡ ⁣[−12(2−1σ2)x2].x^4\exp\!\left[-\frac12\left(2-\frac1{\sigma^2}\right)x^2\right].

    It is integrable exactly when σ2>1/2\sigma^2>1/2. In that regime, differentiating the Gaussian integral twice gives

    Var⁡(I^)=1N[3σ(2−σ−2)5/2−1].\operatorname{Var}(\widehat I) =\frac1N\left[\frac{3\sigma}{(2-\sigma^{-2})^{5/2}}-1\right].

    At σ=1\sigma=1 this is 2/N2/N, as expected for the mean of X2X^2. At σ=1/2\sigma=1/2 the mean remains well defined and the ordinary estimator remains unbiased, but its variance is infinite. A finite run still prints a finite sample variance; that output cannot establish the usual normal error scaling. The self-normalized estimator also lacks the finite influence variance used in its ordinary central-limit error formula here.

    The supplied experiment uses 512 runs of 8,192 draws. For self-normalized estimates of II, it obtains:

    Proposal standard deviationAverage estimateRoot mean square errorNominal 95% interval coverage
    0.50.92510.372151.0%
    10.99940.0156495.3%
    21.00080.0128094.7%

    The broad proposal improves this moment despite differing more from the target near its peak. The narrow proposal's rare large contributions make averages and uncertainty estimates unstable. An empirical root mean square error from 512 runs is not a finite theoretical variance where the latter does not exist.

    A double well with deceptive weight statistics

    Now use the dimensionless zero-dimensional scalar action

    S(x)=14(x2−4)2−0.3x,p(x)=e−S(x)Z,f(x)=x.S(x)=\frac14(x^2-4)^2-0.3x, \qquad p(x)=\frac{e^{-S(x)}}{Z}, \qquad f(x)=x.

    Adaptive quadrature gives Z=2.21898116644Z=2.21898116644, I=1.00233906397I=1.00233906397, and Pp(x<0)=0.24625412864P_p(x<0)=0.24625412864. Repeating the integrations over [−6,6][-6,6] with tighter tolerances changes the mean by about 2.2×10−162.2\times10^{-16} in the retained double-precision run. That agreement checks this integral; it is not an analytic proof of the quadrature error bound.

    Compare three proposals: a Gaussian centered on the right well, an equal mixture centered on both wells, and a broad centered Gaussian. Here N(m,s2)N(m,s^2) denotes mean mm and standard deviation ss.

    ProposalAverage estimateRoot mean square errorMedian weight ESSNominal 95% coverage
    N(2,0.42)N(2,0.4^2)1.91930.91776,8600.2%
    12N(−2,0.42)+12N(2,0.42)\tfrac12N(-2,0.4^2)+\tfrac12N(2,0.4^2)1.00310.027865,22593.6%
    N(0,22)N(0,2^2)1.00100.028092,10795.5%

    The weight diagnostic in the fourth column is

    Neff,w=(∑iwi)2∑iwi2=1∑iui2,ui=wi∑jwj.N_{\mathrm{eff},w}=\frac{(\sum_iw_i)^2}{\sum_iw_i^2} =\frac1{\sum_i u_i^2},\qquad u_i=\frac{w_i}{\sum_jw_j}.

    For the one-well proposal, 511 of 512 runs contain no negative draw, although the negative half-line carries almost a quarter of the target probability. The weights can look well distributed among sampled positive configurations and still omit essential contributions. A large weight ESS therefore cannot establish overlap. The broader proposal has a lower weight ESS yet dramatically better accuracy for the desired mean.

    The two-well proposal improves the result, but the retained root mean square error of 0.02786 exceeds its median reported standard error of 0.01703. Rare contributions between the narrow proposal peaks still matter. This comparison is a reason to examine repeated runs and different proposal widths, rather than declaring a sampler optimal from its weight ESS.

    The normal intervals here use the plug-in influence variance

    σ^SN2=∑iui2(fi−I^SN)2.\widehat\sigma_{\mathrm{SN}}^2 =\sum_i u_i^2(f_i-\widehat I_{\mathrm{SN}})^2.

    It is asymptotically useful under finite-moment conditions; it is not an exact finite-sample interval. For 512 repetitions, a coverage near 95% itself has a sampling uncertainty of about one percentage point. Difficulties diagnosing importance-sampling convergence from a sample's own fluctuations are discussed in [1, §2].

    Reproduce and improve the calculation

    Run the retained source with Python 3, NumPy, and SciPy:

    python3 examples/computational-qft/importance-sampling/check.py
    

    The seed is 20261022; the source records all proposal parameters and saves results.json. It compares the ordinary Gaussian estimator with its analytic variance, checks the double-well integral with two domains and tolerance settings, and preserves the poor-overlap result as a deliberate failure example. It uses log weights and log-sum-exp normalization to avoid preventable overflow, without truncating or altering weights.

    For a new target, compare proposals that cover physically distinct regions, repeat runs, increase sample size, and inspect the observable's weighted contributions as well as the weights alone. Proposal adaptation should be designed separately from evaluation, or its effect on the estimator must be included. Stable arithmetic cannot compensate for unsampled probability.

    Summary

    Importance sampling changes where points are drawn, not the desired integral. Ordinary weights give an unbiased estimator when normalization is known; self-normalization removes an unknown partition function at the price of finite-sample bias. Support, moment conditions, observable dependence, and actual overlap all matter. The weight ESS is a diagnostic of the observed weights, not a guarantee of accuracy.

    Exercises

    1. Expose ratio bias at one sample. For the Gaussian target and f=x2f=x^2, calculate the expectation of the self-normalized estimate at N=1N=1.

    Solution

    The weight cancels between numerator and denominator, giving I^SN=X12\widehat I_{\mathrm{SN}}=X_1^2. Since X1∼N(0,σ2)X_1\sim N(0,\sigma^2), its expectation is σ2\sigma^2, not the target value one unless σ=1\sigma=1. Consistency as sample size grows does not imply finite-sample unbiasedness.

    2. Change an overall normalization. If p~\widetilde p is multiplied by an unknown positive constant cc, which quantities change?

    Solution

    ZZ and every raw weight multiply by cc. The normalized target pp, normalized weights uiu_i, ratio estimate, weight ESS, and plug-in variance remain unchanged. An estimate of the unnormalized partition function N−1∑iwiN^{-1}\sum_iw_i does multiply by cc; its absolute value requires a specified measure and normalization.

    References

    1. S. Chatterjee and P. Diaconis, “The sample size required in importance sampling,” Annals of Applied Probability 28 (2018), 1099–1135. Full text, arXiv version 3. Section 1 sets out ordinary and self-normalized estimators; §2 analyzes convergence diagnostics.

    Scrollable formula. Use Left and Right arrow keys to reveal hidden content.

    Scrollable table. Use Left and Right arrow keys to reveal hidden columns.

    Scrollable figure. Use Left and Right arrow keys to reveal hidden content.