Importance sampling

Intermediate

What you need first. Nothing else on this path comes first. This page is part of Bayesian statistics.

In Bayesian statistics, Importance sampling is a Monte Carlo method that draws from a proposal distribution and reweights each draw by the ratio of the target density to the proposal density. Importance sampling is a Monte Carlo method for estimating an expectation under a distribution that is hard to sample from, or under which the quantity of interest is rarely seen, by sampling instead from a second, more convenient distribution and correcting each draw with a weight. If pp is the density of the target distribution, hh is the function whose expectation is wanted, and qq is the density of the proposal distribution actually sampled, the correction is the ratio w(x)=p(x)/q(x)w(x) = p(x)/q(x), and the estimate is a weighted average of the values h(xi)h(x_i) at the proposal draws. The method appears as the second of the general strategies listed by Hastings in 1970 for problems in which direct sampling from pp is difficult: sample from qq instead, and estimate the integral ∫h(x)p(x) dx\int h(x) p(x)\, dx by averaging h(xi)p(xi)/q(xi)h(x_i) p(x_i)/q(x_i) .[1] It is used in two distinct situations. In the first, pp is fully known and sampling from qq is a device for reducing variance, as when a rare event is made common under qq so that it is actually observed. In the second, pp is known only up to a normalising constant — the usual situation for a Bayesian posterior — and a ratio form, the self-normalised estimator, is required. Tokdar and Kass describe importance sampling as a collection of Monte Carlo methods built on this single identity, used together with Markov chain Monte Carlo as a foundation for simulation-based numerical integration .[2]

Importance sampling

Importance sampling
Fact Value
Topic Bayesian statistics
Listed on Bayesian statistics
Worked example 1
Question using it 1

The identity and the support condition

Let XX be a random quantity with density pp with respect to a common reference measure (Lebesgue measure for a continuous variable, counting measure for a discrete one), and let hh be a real-valued function. The target is

μ=Ep[h(X)]=∫h(x)p(x) dx,\mu = \mathbb{E}_p[h(X)] = \int h(x) p(x)\, dx,

where EpE_p denotes expectation under pp and the integral is over the whole space of possible values of XX. Let qq be a second density, the proposal. Wherever q(x)>0q(x) > 0, the integrand can be multiplied and divided by q(x)q(x) without changing its value:

μ=∫h(x)p(x)q(x)q(x) dx=Eq[h(X)w(X)],\mu = \int h(x) \frac{p(x)}{q(x)} q(x)\, dx = \mathbb{E}_q[h(X) w(X)],

where EqE_q denotes expectation under qq and w(x)=p(x)/q(x)w(x) = p(x)/q(x) is the importance weight at xx. The identity is exact; no approximation has been made. What has changed is the distribution from which draws are taken.

The division is legitimate only where it is needed. The precise requirement is the support condition: q(x)>0q(x) > 0 wherever ∣h(x)∣p(x)>0|h(x)| p(x) > 0. If qq assigns density zero to a set on which h(x)p(x)h(x) p(x) is nonzero, draws from qq never fall in that set, its contribution to μ\mu is never seen, and the estimator converges to the integral over the part of the space that qq does reach — a wrong value, reached with no warning from the sample itself. Tokdar and Kass state the condition in the form q(x)>0q(x) > 0 whenever f(x)p(x)≠0f(x) p(x) \ne 0, with ff in the role of hh here .[2] Owen gives the same condition and notes the safer sufficient version: choose qq positive wherever pp is positive, which covers every hh at once .[3] For the self-normalised estimator (treated under ‘The self-normalised estimator’ below) the stronger version is the one required, because its denominator estimates an integral of pp itself, for which the relevant function is the constant 11, not hh .[3]

The plain estimator, its variance, the proposal, and the self-normalised form

The estimator when pp is normalised

Suppose first that pp is a fully specified, normalised density, so that w(x)w(x) can be computed exactly. Draw x1,…,xNx_1, \dots, x_N independently from qq. The importance sampling estimator is

μ^=1N∑i=1Nh(xi)w(xi).\widehat{\mu} = \frac{1}{N} \sum_{i=1}^{N} h(x_i) w(x_i).

It is exactly unbiased, not merely approximately: each term has expectation Eq[h(X)w(X)]=μ\mathbb{E}_q[h(X) w(X)] = \mu by the identity above, provided the support condition holds and Ep[∣h(X)∣]\mathbb{E}_p[|h(X)|] is finite. Hastings writes the estimator in exactly this form, as the average of h(xi)p(xi)/q(xi)h(x_i) p(x_i)/q(x_i) over a sample drawn from qq .[1] By the strong law of large numbers μ^\widehat{\mu} converges to μ\mu with probability one as N→∞N \to \infty, and by the central limit theorem its error is approximately normal with variance given in the next section, whenever the second moment Eq[h(X)2w(X)2]\mathbb{E}_q[h(X)^2 w(X)^2] is finite. That moment condition belongs to this estimator. The self-normalised ratio form has its own central limit theorem, with a different, centred moment condition, stated in its section below; neither condition covers both estimators. Tokdar and Kass record the strong law and the central limit results for the two forms separately .[2]

Two special cases fix the meaning of the weights. If q=pq = p, then w(x)=1w(x) = 1 for all xx and the estimator is the ordinary Monte Carlo average of hh over draws from pp. If hh is the indicator of a set AA, the estimator is the average of the weights of the draws that fall in AA; draws outside AA contribute exactly zero, however large their weights.

Variance: the weights decide everything

Unbiasedness says nothing about precision. Because the terms h(xi)w(xi)h(x_i) w(x_i) are independent draws from qq, the exact variance of the estimator, when it is finite, is

Varq(μ^)=1N(∫h(x)2p(x)2q(x) dx−μ2)=1N(Eq[h(X)2w(X)2]−μ2).\text{Var}_q(\widehat{\mu}) = \frac{1}{N}\left( \int \frac{h(x)^2 p(x)^2}{q(x)}\, dx - \mu^2 \right) = \frac{1}{N}\left( \mathbb{E}_q[h(X)^2 w(X)^2] - \mu^2 \right).

Owen writes the per-draw variance in this form and draws the two practical conclusions from it: the numerator rewards a proposal close to ∣h∣p|h| p in shape, and the denominator punishes a proposal that comes close to zero anywhere that ∣h∣p|h| p does not .[3] The variance is small when qq places its draws where ∣h(x)∣p(x)|h(x)| p(x) is large and when the weights of the drawn points are nearly constant. It is large — and can be infinite — when some draws carry weights vastly larger than others.

The benchmark case h≡1h \equiv 1, for which μ=1\mu = 1 exactly, isolates the role of the weights. The per-draw variance is then Eq[(w(X)−1)2]=Eq[w(X)2]−1\mathbb{E}_q[(w(X) - 1)^2] = \mathbb{E}_q[w(X)^2] - 1, since Eq[w(X)]=∫p(x) dx=1\mathbb{E}_q[w(X)] = \int p(x)\, dx = 1. Tokdar and Kass give this as Var(μ^f)=Eq[(w(X)−1)2]/m\text{Var}(\widehat{\mu}_f) = \mathbb{E}_q[(w(X)-1)^2]/m for a sample of size mm when the function is identically 11 .[2] The quantity Eq[w(X)2]−1\mathbb{E}_q[w(X)^2] - 1 is the chi-squared divergence between pp and qq,

Dχ2(p ∥ q)=∫(p(x)−q(x))2q(x) dx=∫p(x)2q(x) dx−1,D_{\chi^2}(p \, \| \, q) = \int \frac{(p(x) - q(x))^2}{q(x)}\, dx = \int \frac{p(x)^2}{q(x)}\, dx - 1,

so the difficulty of estimating even a constant under pp by sampling from qq is exactly a divergence between the two densities. For a general hh the relevant second moment is Eq[h(X)2w(X)2]\mathbb{E}_q[h(X)^2 w(X)^2], which can be much smaller than the worst case suggested by the divergence when hh is small where the weights are large — the first worked example below turns on exactly this point — or much larger, when hh is large where the weights are large. A small chi-squared divergence is a guarantee for all hh that are nearly constant under pp; Tokdar and Kass use the same quantity, written cv2=Eq[(w(X)−1)2]cv^2 = \mathbb{E}_q[(w(X)-1)^2], as the efficiency criterion to be minimised when choosing among parametric proposals .[2]

For nonnegative hh there is an optimal proposal, q∗(x)=h(x)p(x)/μq^*(x) = h(x) p(x)/\mu, proportional to hph p. Under q∗q^* every term h(xi)w(xi)h(x_i) w(x_i) equals μ\mu exactly, so the estimator has variance zero. This is a benchmark, not a method: q∗q^* depends on the unknown μ\mu, and sampling from it is usually as hard as the original problem. Its practical content is directional — a good proposal mimics the shape of ∣h∣p|h| p, not the shape of pp alone .[3] For an indicator hh, q∗q^* is the distribution of XX under pp conditional on X∈AX \in A: the ideal way to estimate a rare-event probability is to sample from the event itself, reweighted.

Choosing the proposal: tails, tilting and defensive mixtures

Three requirements compete. The proposal must be easy to sample from; its density must be computable at the drawn points; and its shape must follow ∣h∣p|h| p closely enough that the weights do not vary wildly. The precise form of the third requirement is a moment condition, not a tail comparison: the variance is finite exactly when Eq[h(X)2w(X)2]=∫h(x)2p(x)2/q(x) dx\mathbb{E}_q[h(X)^2 w(X)^2] = \int h(x)^2 p(x)^2/q(x)\, dx is finite. Tail thickness is a heuristic for that integral, no more. A proposal with thinner tails than the target can still satisfy it: for h≡1h \equiv 1, a N(0,1)N(0,1) target and a N(0,3/4)N(0, 3/4) proposal, p2/qp^2/q is proportional to exp⁡(−x2/3)\exp(-x^2/3) and is integrable, even though the proposal’s tails are thinner; the same comparison with proposal variance at most 1/21/2 makes the integral diverge. What is fatal is not thinness itself but a ratio p2/qp^2/q that grows fast enough, in a region qq still visits, to defeat integrability. Owen’s tail comparison is the practical guidance: a normal target with a Student tt proposal is safe, while a tt target with a normal proposal is problematic .[3]

In Bayesian problems a standard construction takes qq to be a normal or Student tt density centred at the posterior mode, with scale from the curvature of the log posterior at the mode — an importance-sampling version of the Laplace approximation. Tokdar and Kass describe this construction, with the mode found by minimising −log⁡p(x)-\log p(x) and the scale from the inverse Hessian there, and record the usual preference for the Student version: its heavier tails give greater assurance that Eq[w(X)2]\mathbb{E}_q[w(X)^2] is finite .[2] Owen likewise recommends a tt distribution rather than a normal for safety when a Gaussian proposal is built from a mode and a Hessian .[3]

A second construction changes a parameter of the target family itself. Exponential tilting takes a proposal from the same family as pp with a shifted parameter — the N(0,1)N(0,1) target and N(3,1)N(3,1) proposal of the first worked example are the simplest instance — and Owen treats it as the standard device for rare-event problems .[3]

A third construction insures against a proposal that is wrong somewhere. A defensive mixture combines a best-guess proposal q2q_2 with a safe component q1q_1 — in the original defensive construction, the target pp itself — as qα=α1q1+α2q2q_\alpha = \alpha_1 q_1 + \alpha_2 q_2 with α1+α2=1\alpha_1 + \alpha_2 = 1 and, for example, α1=1/10\alpha_1 = 1/10. Because qα(x)≥α1p(x)q_\alpha(x) \ge \alpha_1 p(x) everywhere, the weight satisfies w(x)≤1/α1w(x) \le 1/\alpha_1 for all xx: the weights are bounded, and the second moment of the weights themselves, Eq[w(X)2]\mathbb{E}_q[w(X)^2], is at most 1/α11/\alpha_1. That bound is a statement about the weights, and it secures finite variance for the plain estimator of a bounded integrand — in particular for the constant 11, the normalising-constant problem. It does not by itself control Eq[w(X)2h(X)2]\mathbb{E}_q[w(X)^2 h(X)^2] for an unbounded hh: with p=qp = q uniform on (0,1)(0,1) the weights are identically 11, yet h(x)=x−3/4h(x) = x^{-3/4} has finite mean 44 while Eq[w(X)2h(X)2]=∫01x−3/2 dx\mathbb{E}_q[w(X)^2 h(X)^2] = \int_0^1 x^{-3/2}\, dx is infinite, so that estimator has infinite variance despite perfectly bounded weights. The finite-variance condition remains the moment condition on the product, Eq[w(X)2h(X)2]<∞\mathbb{E}_q[w(X)^2 h(X)^2] < \infty; bounded weights are a way to meet it for bounded hh, not a substitute for it. The price of the insurance is spending a fraction α1\alpha_1 of the draws on the safe component. Owen presents defensive mixtures, attributed there to Hesterberg (1995), as the way to bound the importance ratio while keeping a tuned component .[3] Mixtures serve a second purpose as well: one component can be placed at each mode, singularity or failure mechanism that is known about, so that no important region depends on a single component having found it .[3]

The self-normalised estimator

In Bayesian inference the target is a posterior density, and Bayes’ rule supplies it only as an unnormalised density p~(x)=L(x)π(x)\tilde{p}(x) = L(x) \pi(x), the product of a likelihood LL and a prior density π\pi, with p(x)=p~(x)/Zp(x) = \tilde{p}(x)/Z and Z=∫p~(x) dxZ = \int \tilde{p}(x)\, dx the marginal likelihood of the data under the model. The constant ZZ is generally unknown — computing it is itself one of the hard integrals of the subject — so the exact weights p(x)/q(x)p(x)/q(x) cannot be formed. What can be formed is the unnormalised weight w~(x)=p~(x)/q(x)=Zw(x)\tilde{w}(x) = \tilde{p}(x)/q(x) = Z w(x), which differs from the exact weight by the same unknown factor ZZ at every point.

The factor cancels in a ratio — but the derivation needs the stronger support condition, and needs it here, not as a later caveat: q(x)>0q(x) > 0 wherever p~(x)>0\tilde{p}(x) > 0, equivalently wherever p(x)>0p(x) > 0. The denominator of the ratio estimates Z=∫p~(x) dxZ = \int \tilde{p}(x)\, dx, the expectation of the constant function 11 against the unnormalised target, and the identity for that expectation requires qq positive wherever p~\tilde{p} is — including where hh vanishes. Under only the weaker, hh-dependent condition of the first section, the numerator could still target ZμZ\mu while the denominator targets the integral of p~\tilde{p} over just the part of the space qq reaches, and the ratio would converge to a wrong value. Impose the strong condition from the start. Both μ=Ep[h(X)]\mu = \mathbb{E}_p[h(X)] and 11 are then expectations that the unnormalised weights can estimate: applied to hh, the plain estimator with w~\tilde{w} in place of ww estimates ZμZ\mu; applied to the constant function 11, it estimates ZZ. Dividing the first by the second gives the self-normalised estimator

μ~=∑i=1Nw~(xi)h(xi)∑i=1Nw~(xi)=∑i=1Nw(xi)h(xi)∑i=1Nw(xi),\widetilde{\mu} = \frac{\sum_{i=1}^{N} \tilde{w}(x_i) h(x_i)}{\sum_{i=1}^{N} \tilde{w}(x_i)} = \frac{\sum_{i=1}^{N} w(x_i) h(x_i)}{\sum_{i=1}^{N} w(x_i)},

a weighted average of the values h(xi)h(x_i) with normalised weights wi/∑jwjw_i / \sum_j w_j that sum to one — defined only when the denominator is positive. The weights are nonnegative, so the denominator vanishes exactly when every drawn point has w~(xi)=0\tilde{w}(x_i) = 0, an event of positive probability whenever qq can draw points at which p~\tilde{p} is zero. Every statement about μ~\widetilde{\mu} below is conditional on the denominator being positive; under the support condition that event has probability tending to one, but it is not certain at any finite NN. Tokdar and Kass give this ratio estimate as the standard replacement when the density is known only up to a multiplicative constant ,[2] and Owen derives it the same way, as the cancellation of the unknown constants of both pp and qq in a ratio of two importance sampling estimates .[3]

The ratio form changes the estimator’s properties in three ways, and the differences from the plain estimator are exact statements, not matters of taste. First, μ~\widetilde{\mu} is generally biased at finite NN: it is a ratio of two random averages, and the expectation of a ratio is not in general the ratio of the expectations. ‘Generally’ is the correct qualification, not ‘always’: if hh is a constant function, h≡ch \equiv c, the ratio equals cc exactly on every sample with a positive denominator, so that estimator is exact rather than biased. For non-constant hh the bias is typically nonzero. A Taylor expansion of the ratio about the limits of its numerator and denominator — the delta method — produces a formal first-order bias term of order 1/N1/N, with a coefficient depending on the covariance, under qq, of w~(X)\tilde{w}(X) and w~(X)h(X)\tilde{w}(X) h(X); but that term is an asymptotic approximation, and the second moments it needs — Eq[w~(X)2]\mathbb{E}_q[\tilde{w}(X)^2] and Eq[w~(X)2h(X)2]\mathbb{E}_q[\tilde{w}(X)^2 h(X)^2] finite, with Z>0Z > 0 — do not by themselves make the bias genuinely of order 1/N1/N. They do not even guarantee that the bias exists: take qq uniform on (0,1)(0,1), w~(x)∝e−1/x\tilde{w}(x) \propto e^{-1/x} and h(x)=e1/(2x)h(x) = e^{1/(2x)}, where both displayed moments and ZZ are finite, yet on the event that all NN draws fall below ε\varepsilon — an event of probability εN\varepsilon^N — the ratio is at least e1/(2ε)e^{1/(2\varepsilon)}, so its expectation is infinite at every finite NN. A genuine 1/N1/N bias expansion needs stronger hypotheses, such as bounded weights, or higher moments together with control of the denominator near zero; without them, the consistency below is the exact statement and no rate for the bias is claimed. Second, the estimator is consistent: numerator and denominator each obey the strong law of large numbers, converging to ZμZ\mu and ZZ respectively, so their ratio converges to μ\mu with probability one .[2] Third, its large-sample variance is not the plain estimator’s variance. The delta method gives the approximation

Varq(μ~)≈1NEq[w(X)2(h(X)−μ)2],\text{Var}_q(\widetilde{\mu}) \approx \frac{1}{N} \mathbb{E}_q\left[ w(X)^2 (h(X) - \mu)^2 \right],

in which the weights multiply the centred values h(X)−μh(X) - \mu rather than h(X)h(X) itself; Owen obtains this expression by Taylor expansion of the ratio .[3] The central limit theorem for the ratio is therefore stated with the centred moment condition: when Eq[w(X)2(h(X)−μ)2]\mathbb{E}_q[w(X)^2 (h(X) - \mu)^2] is finite, N(μ~−μ)\sqrt{N}(\widetilde{\mu} - \mu) is approximately normal with variance Eq[w(X)2(h(X)−μ)2]\mathbb{E}_q[w(X)^2 (h(X) - \mu)^2] — the centred moment itself. The factor 1/N1/N belongs to the variance of μ~\widetilde{\mu}, not to the variance of its root-NN scaling. This is a condition on the centred product w(X)(h(X)−μ)w(X)(h(X) - \mu), distinct from the plain estimator’s condition Eq[h(X)2w(X)2]<∞\mathbb{E}_q[h(X)^2 w(X)^2] < \infty, and neither should be quoted as sufficient for both estimators. One consequence is that the optimal proposal also changes: for the self-normalised estimator it is proportional to p(x)∣h(x)−μ∣p(x) |h(x) - \mu|, not to p(x)∣h(x)∣p(x) |h(x)|, so zero variance is unattainable even in the benchmark sense — the best proposal still leaves variance of the order of Ep[∣h(X)−μ∣]2/N\mathbb{E}_p[|h(X) - \mu|]^2 / N .[3]

The denominator of the ratio is an estimate in its own right. The average unnormalised weight, (1/N)∑iw~(xi)(1/N) \sum_i \tilde{w}(x_i), is an unbiased and consistent estimate of ZZ, because Eq[w~(X)]=∫p~(x) dx=Z\mathbb{E}_q[\tilde{w}(X)] = \int \tilde{p}(x)\, dx = Z. In the Bayesian setting ZZ is the marginal likelihood, the quantity that enters Bayes factors between models; importance sampling therefore supplies, from one set of draws, both posterior expectations (through the ratio) and an estimate of the marginal likelihood (through the mean unnormalised weight). The second worked example computes both from the same five draws. The estimate of ZZ inherits the fragility of the plain estimator — it is a plain estimator, of the constant function — and can be dominated by a handful of large weights when the proposal is poorly matched.

Effective sample size and the diagnosis of degeneracy

A set of weighted draws carries less information than the same number of unweighted draws. Given weights w1,…,wNw_1, \dots, w_N at the drawn points, the effective sample size of the weighted sample is

Neff=(∑i=1Nwi)2∑i=1Nwi2.N_{\text{eff}} = \frac{\left(\sum_{i=1}^{N} w_i\right)^2}{\sum_{i=1}^{N} w_i^2}.

The formula is a variance comparison, not an arbitrary index. If values YiY_i with common variance σ2\sigma^2 are combined in a weighted average with fixed weights, the variance of the weighted average is σ2∑iwi2/(∑iwi)2=σ2/Neff\sigma^2 \sum_i w_i^2 / (\sum_i w_i)^2 = \sigma^2 / N_{\text{eff}}: exactly the variance that NeffN_{\text{eff}} equally weighted independent values would give. Owen presents the measure in this form, attributed there to Kong (1992) and to Evans and Swartz (1995), under the name effective sample size nen_e .[3] It satisfies 1≤Neff≤N1 \le N_{\text{eff}} \le N, with Neff=NN_{\text{eff}} = N only when all the weights are equal and NeffN_{\text{eff}} near 11 when a single weight dominates the sum. It is unchanged if every weight is multiplied by the same constant, so it can be computed from unnormalised weights, which is what makes it available in the self-normalised setting. The repo’s article on effective sample size treats the corresponding notion for dependent, unweighted draws, such as Markov chain output; the present formula is the weighted, independent-draw version, and the two should not be interchanged.

The effective sample size is a diagnostic, and its limits should be stated plainly. It summarises the variability of the weights actually drawn. It cannot detect a region the proposal never visited: if qq misses part of the support of ∣h∣p|h| p entirely, the drawn weights can be comfortably equal, NeffN_{\text{eff}} comfortably large, and the estimate wrong by the whole contribution of the missed region. It is also a statement about the weights alone, not about hh, because what matters for μ\mu is the variability of h(xi)w(xi)h(x_i) w(x_i), not of w(xi)w(x_i) alone. Used with those limits in mind — and reported alongside the largest normalised weight — it is the standard first check on an importance sampling calculation .[3]

Where importance sampling is used

Importance sampling is used in three broad settings. The first is rare-event simulation: failure probabilities of engineered systems, extreme losses in finance, and small tail probabilities generally, where crude Monte Carlo would need an impractical number of draws to see the event at all. The second is Bayesian inference, where the self-normalised estimator turns draws from a tractable proposal — a Laplace-style normal or tt approximation, a prior, or a fitted approximation such as the output of variational inference — into estimates of posterior expectations, and the mean unnormalised weight estimates the marginal likelihood. The third is reuse: one set of draws, reweighted, can estimate expectations under a range of nearby targets, as in sensitivity analyses that vary a parameter of the model. Its competitors clarify what it is. Against rejection sampling it keeps every draw: rejection sampling discards proposals and returns unweighted draws from pp itself, but requires a known bound on w(x)=p(x)/q(x)w(x) = p(x)/q(x), whereas importance sampling requires no bound, works when the ratio is unbounded, and pays by returning weighted draws whose quality varies .[3] Against Markov chain Monte Carlo, including Hamiltonian Monte Carlo, it offers independent draws and an estimate of the normalising constant, but no mechanism for discovering regions of the target that the proposal did not already cover.

Worked example

A rare-event probability

Let ZZ be standard normal and estimate μ=P(Z>3)\mu = \mathbb{P}(Z > 3) by importance sampling. The exact value is μ=1−Φ(3)=0.001349898\mu = 1 - \Phi(3) = 0.001349898, where Φ\Phi is the standard normal distribution function. Under crude Monte Carlo — sampling from pp, the N(0,1)N(0,1) density itself — the per-draw contribution is a Bernoulli variable with success probability μ\mu, so the per-draw variance is exactly μ(1−μ)=1.348076×10−3\mu(1-\mu) = 1.348076 \times 10^{-3}, and almost every draw contributes zero.

Take instead the proposal qq to be the N(3,1)N(3,1) density: the same family, tilted so that its centre sits on the boundary of the rare set. Writing ϕ\phi for the standard normal density, the weight function is

w(x)=ϕ(x)ϕ(x−3)=exp⁡(92−3x),w(x) = \frac{\phi(x)}{\phi(x-3)} = \exp\left(\frac{9}{2} - 3x\right),

an exact closed form: the normalising constants and the quadratic terms partly cancel, leaving a decreasing exponential. On the rare set itself the weights are small and shrink rapidly: w(3)=e−9/2≈0.011109w(3) = e^{-9/2} \approx 0.011109 and w(4)≈0.000553w(4) \approx 0.000553. Half of all proposal draws fall in the set {x>3}\{x > 3\}, by symmetry of N(3,1)N(3,1) about 33, and each is downweighted to compensate.

The per-draw second moment of the importance sampling contribution w(X)1{X>3}w(X) \mathbf{1}\{X > 3\} under qq also has an exact closed form. Squaring the target density and dividing by the proposal density gives ϕ(x)2/ϕ(x−3)=e9ϕ(x+3)\phi(x)^2/\phi(x-3) = e^{9} \phi(x+3), so

Eq[w(X)21{X>3}]=e9∫3∞ϕ(x+3) dx=e9(1−Φ(6))=8103.083928×9.865876×10−10≈7.994402×10−6.\mathbb{E}_q\left[w(X)^2 \mathbf{1}\{X > 3\}\right] = e^{9} \int_3^{\infty} \phi(x+3)\, dx = e^{9} \left(1 - \Phi(6)\right) = 8103.083928 \times 9.865876 \times 10^{-10} \approx 7.994402 \times 10^{-6}.

The per-draw variance is therefore exactly this second moment minus μ2\mu^2:

7.994402×10−6−(0.001349898)2≈6.172178×10−6,7.994402 \times 10^{-6} - (0.001349898)^2 \approx 6.172178 \times 10^{-6},

against the crude per-draw variance 1.348076×10−31.348076 \times 10^{-3}. The tilted proposal reduces the variance per draw by a factor of about 218218. In sample-size terms, a relative standard error of 10%10\% requires N=(per-draw variance/μ2)/0.01N = (\text{per-draw variance}/\mu^2)/0.01 draws: about 73,98073{,}980 draws by crude Monte Carlo, against 339339 draws by importance sampling with this proposal. Both figures are exact consequences of the two per-draw variances, not simulation results.

The example also disciplines the chi-squared discussion in ‘Variance: the weights decide everything’ above. For this pair, Eq[w(X)2]=e9≈8103.08\mathbb{E}_q[w(X)^2] = e^{9} \approx 8103.08, so the chi-squared divergence Dχ2(p ∥ q)=e9−1≈8102.08D_{\chi^2}(p \, \| \, q) = e^9 - 1 \approx 8102.08 is enormous: estimating the constant 1=Ep[1]1 = \mathbb{E}_p[1] from N(3,1)N(3,1) draws would be very poor, because draws far below 33 carry weights like e9/2≈90e^{9/2} \approx 90 and larger. The rare-event estimate is nevertheless excellent, because h(x)=1{x>3}h(x) = \mathbf{1}\{x > 3\} is zero exactly where the weights explode, and the second moment that governs it, Eq[w(X)2h(X)2]\mathbb{E}_q[w(X)^2 h(X)^2], is the small number computed above. A divergence is a property of the pair of densities; the variance of an estimate is a property of the triple (p,q,h)(p, q, h).

A self-normalised posterior mean in five draws

A coin is tossed four times with three heads and one tail. With a uniform prior on the success probability θ∈(0,1)\theta \in (0,1), the posterior density is proportional to p~(θ)=θ3(1−θ)\tilde{p}(\theta) = \theta^3 (1-\theta); the normalised posterior is the Beta(4,2)(4,2) distribution, whose mean is exactly 4/6=2/3≈0.6666674/6 = 2/3 \approx 0.666667 and whose normalising constant is exactly Z=∫01θ3(1−θ) dθ=1/20=0.05Z = \int_0^1 \theta^3(1-\theta)\, d\theta = 1/20 = 0.05. Treat p~\tilde{p} as known only up to its constant, as a posterior ordinarily is, and take the proposal qq to be the uniform density on (0,1)(0,1), which equals 11 everywhere, so the unnormalised weight of a draw θ\theta is simply w~(θ)=θ3(1−θ)\tilde{w}(\theta) = \theta^3 (1-\theta). Suppose the five proposal draws are 0.20.2, 0.40.4, 0.50.5, 0.60.6 and 0.80.8 — a stated, deterministic set, so that every number below is exact arithmetic, not a simulation report.

A self-normalised posterior mean in five draws: Draw, Unnormalised weight, Normalised weight
Draw θi\theta_i Unnormalised weight w~i=θi3(1−θi)\tilde{w}_i = \theta_i^3 (1-\theta_i) Normalised weight w~i/∑jw~j\tilde{w}_i / \sum_j \tilde{w}_j
0.2 4/625=0.00644/625 = 0.0064 64/2961≈0.02161464/2961 \approx 0.021614
0.4 24/625=0.038424/625 = 0.0384 128/987≈0.129686128/987 \approx 0.129686
0.5 1/16=0.06251/16 = 0.0625 625/2961≈0.211077625/2961 \approx 0.211077
0.6 54/625=0.086454/625 = 0.0864 96/329≈0.29179396/329 \approx 0.291793
0.8 64/625=0.102464/625 = 0.1024 1024/2961≈0.3458291024/2961 \approx 0.345829

The weights sum to ∑iw~i=2961/10000=0.2961\sum_i \tilde{w}_i = 2961/10000 = 0.2961. The self-normalised estimate of the posterior mean is the weighted average of the draws:

μ~=∑iw~iθi∑iw~i=0.181650.2961=173282≈0.613475,\widetilde{\mu} = \frac{\sum_i \tilde{w}_i \theta_i}{\sum_i \tilde{w}_i} = \frac{0.18165}{0.2961} = \frac{173}{282} \approx 0.613475,

where the numerator is 0.0064×0.2+0.0384×0.4+0.0625×0.5+0.0864×0.6+0.1024×0.8=0.181650.0064 \times 0.2 + 0.0384 \times 0.4 + 0.0625 \times 0.5 + 0.0864 \times 0.6 + 0.1024 \times 0.8 = 0.18165 exactly. The estimate 0.61350.6135 is not the exact posterior mean 0.66670.6667, and the gap should be read correctly: it is the sampling variability of the ratio at N=5N = 5 on display. A single sample cannot demonstrate bias — bias is a property of the estimator’s average over repeated samples — and nothing in the method promises closeness at this sample size. The same weights estimate the normalising constant: the mean unnormalised weight is 0.2961/5=0.059220.2961/5 = 0.05922, against the exact Z=0.05Z = 0.05 — the marginal-likelihood estimate from this calculation, obtained from the same draws at no extra cost.

The effective sample size of the five weighted draws is

Neff=(0.2961)20.00642+0.03842+0.06252+0.08642+0.10242=0.087675210.02337249≈3.7512.N_{\text{eff}} = \frac{(0.2961)^2}{0.0064^2 + 0.0384^2 + 0.0625^2 + 0.0864^2 + 0.1024^2} = \frac{0.08767521}{0.02337249} \approx 3.7512.

That number is a statement about the weights alone: they are as uneven as about 3.753.75 equal weights would be. It is not a measure of the information this sample carries about the posterior mean specifically — accuracy for that integrand depends on the products w~iθi\tilde{w}_i \theta_i, not on the weights in isolation. The largest normalised weight is 0.3460.346: no single draw dominates, which is why the effective sample size is a substantial fraction of 55 rather than a number near 11.

Common misconceptions

“Importance sampling draws from the target distribution.” It does not. Every draw comes from the proposal qq; the target pp enters only through the weights. The output is a weighted sample, and treating the draws as an unweighted sample from pp — averaging the h(xi)h(x_i) without weights, for instance — estimates Eq[h(X)]\mathbb{E}_q[h(X)], not Ep[h(X)]\mathbb{E}_p[h(X)].

“An unbiased estimator cannot be badly wrong.” The plain estimator is exactly unbiased under the support condition, including when its variance is infinite. Unbiasedness is a statement about the average over repeated runs; a single run of an infinite-variance estimator can be far from μ\mu, and the runs that correct the average are those containing a rare, enormous weight.

“The self-normalised estimator is unbiased because the constant cancels.” The constant cancels exactly; unbiasedness does not follow. The estimator is a ratio of two random quantities, its bias is in general nonzero at finite NN and vanishes as NN grows, with no universal rate (a constant hh is the exception: its ratio is exact whenever the denominator is positive), and only its consistency — convergence to μ\mu as NN grows — is exact. In the second worked example the estimate from five draws is 0.61350.6135 against an exact mean of 0.66670.6667; that gap is the sampling variability of the ratio at small NN, not an error in the arithmetic, and a single sample could not exhibit bias in any case.

“A large effective sample size certifies the estimate.” The effective sample size summarises the weights that were drawn. It is silent about regions the proposal never reached and about functions hh other than the one implicitly weighted. It is a check on degeneracy, not a certificate of correctness.

“The proposal should approximate the target as closely as possible.” The variance is governed by ∣h∣p|h| p, not by pp alone. For a rare-event probability the best proposals are centred on the rare set and can be far from pp as densities — in the first worked example the chi-squared divergence between target and proposal is about 81028102, yet the variance falls by a factor of about 218218. Closeness to pp is the right goal only when hh is nearly constant.

“More draws fix a bad proposal.” More draws reduce variance at the rate 1/N1/N when the variance is finite. They do not repair a support violation at any NN, they do not make an infinite variance finite, and in high dimensions the number of draws needed to overcome weight degeneracy grows exponentially with the discrepancy between pp and qq.

Support, moments, and weight degeneracy

Its assumptions are few and sharp. The support condition must hold: q(x)>0q(x) > 0 wherever ∣h(x)∣p(x)|h(x)| p(x) is nonzero, and, for the self-normalised estimator, wherever p(x)>0p(x) > 0. Both densities must be evaluable at the drawn points, the target up to the constant the setting allows. For the variance formulas and the normal approximation to the error, the second moment Eq[h(X)2w(X)2]\mathbb{E}_q[h(X)^2 w(X)^2] must be finite; unbiasedness and consistency do not need it, which is why an estimate can be consistent, apparently stable over long runs, and still have infinite variance — the sample variance then understates the true variability, because the draws that would reveal it are those the proposal produces too rarely.

The limits follow from the same formulas. A proposal with thinner tails than the target can give infinite variance. A proposal that misses a mode gives an estimate wrong by that mode’s contribution, with no internal signal of the omission: the weights of the draws that occurred cannot report on draws that never occurred. In high dimensions the weights degenerate unless the proposal is exponentially close to the target. And the self-normalised estimator is generally biased at finite NN — exact only in special cases such as a constant integrand, when its denominator is positive — so reporting it as if it were the unbiased plain estimator misstates its properties. Diagnostics based on the drawn weights — the effective sample size and the largest normalised weight — are necessary checks, not sufficient ones.

High dimensions: weight degeneracy

The method’s characteristic failure is already visible in Hastings’s 1970 account: importance sampling “is a difficult method to use in a large number of dimensions, since the values of the weights w(xi)=p(xi)/q(xi)w(x_i) = p(x_i)/q(x_i) for reasonable values of NN may all be extremely small, or a few may be extremely large” .[1] The mechanism is multiplicative. In dd dimensions a density ratio is typically a product of dd one-dimensional factors; the logarithm of the weight is a sum of dd terms, with variance growing proportionally to dd, so the weights vary over a range exponential in dd, and the chi-squared divergence between product densities compounds across coordinates. The consequence is weight degeneracy: as the dimension grows, the largest normalised weight tends towards one and the effective sample size towards one, however large NN is, unless NN grows exponentially with the discrepancy between pp and qq. A nominal sample of ten thousand draws can be, in effect, a sample of three.

Tokdar and Kass locate the same difficulty from the approximation side: for a high-dimensional target, a proposal that is easy to sample from and yet close to pp is hard to identify, because the region where pp is large occupies an extremely small fraction of the volume .[2] Their review’s response is structural: where the target factorises along a chain — as in state-space models — the proposal can be built sequentially, one conditional factor at a time, with the weight accumulated as a product of one-step ratios. That construction is sequential importance sampling, and with resampling added it is the basis of particle filtering .[2] It manages degeneracy by rebuilding the sample as it goes; it does not repeal the multiplicative mechanism, and the plain, one-shot estimator of this article remains a low-to-moderate-dimensional tool unless the proposal is very close to the target.

Connections

  • Monte Carlo method — the general method of estimating expectations by sampling, of which importance sampling is the weighted form; that article mentions importance sampling in passing, and the present article is the full treatment.
  • Effective sample size — the number of independent unweighted draws a sample is worth; there it is developed for dependent draws, here in the weighted form (∑iwi)2/∑iwi2(\sum_i w_i)^2 / \sum_i w_i^2 for independent weighted draws.
  • Bayesian inference — the setting in which the target is known only up to a normalising constant, the self-normalised estimator is the standard tool, and the mean unnormalised weight estimates the marginal likelihood.
  • Variational inference — a fitted variational approximation is a natural proposal distribution: draws from it, reweighted by the ratio of the unnormalised posterior density to the proposal density, give consistent estimates of posterior expectations under the support condition — the approximation’s error is corrected in the limit, not removed at any finite sample size.
  • Hamiltonian Monte Carlo — the Markov chain alternative for posterior expectations, which needs no proposal covering the whole target but returns dependent, unweighted draws and no estimate of the normalising constant.
  • Dirichlet process — inference for Dirichlet process mixtures is a setting in which weighted and sequential forms of importance sampling handle posteriors that direct sampling cannot reach.

Check yourself

  1. Let XX take the values 11 and 22 with p(1)=p(2)=1/2p(1) = p(2) = 1/2, let h(x)=xh(x) = x, and let the proposal be q(1)=1q(1) = 1, q(2)=0q(2) = 0. What value does the importance sampling estimator take on every run, what is the true μ\mu, and which condition has failed?

Every draw equals 11, its weight is w(1)=p(1)/q(1)=1/2w(1) = p(1)/q(1) = 1/2, so the estimator is identically h(1)w(1)=1/2h(1) w(1) = 1/2, whatever NN is. The true value is μ=(1/2)(1)+(1/2)(2)=3/2\mu = (1/2)(1) + (1/2)(2) = 3/2. The support condition has failed: q(2)=0q(2) = 0 while ∣h(2)∣p(2)=1>0|h(2)| p(2) = 1 > 0, so the contribution of the value 22 — which is 11, two-thirds of μ\mu — is never sampled. No increase in NN repairs this, and the drawn weights, all equal to 1/21/2, give no indication that anything is missing.

  1. Four draws have unnormalised weights 11, 11, 22 and 66, and the corresponding values of hh are 00, 11, 22 and 33. Compute the self-normalised estimate of μ\mu, the effective sample size, and the largest normalised weight, and comment on the sample.

The weights sum to 1010, so the self-normalised estimate is (1×0+1×1+2×2+6×3)/10=23/10=2.3(1 \times 0 + 1 \times 1 + 2 \times 2 + 6 \times 3)/10 = 23/10 = 2.3. The sum of squared weights is 1+1+4+36=421 + 1 + 4 + 36 = 42, so the effective sample size is 102/42=50/21≈2.38110^2/42 = 50/21 \approx 2.381: four weighted draws worth about 2.382.38 unweighted ones. The largest normalised weight is 6/10=0.66/10 = 0.6: a single draw supplies three-fifths of the estimate, which is the degeneracy the effective sample size is reporting.

  1. Let pp be the N(0,1)N(0,1) density and qq the N(0,s2)N(0, s^2) density. For which values of ss is Eq[w(X)2]\mathbb{E}_q[w(X)^2] finite? What happens for s=0.6s = 0.6, and why does no finite sample reveal the problem directly?

The integrand of Eq[w(X)2]=∫p(x)2/q(x) dx\mathbb{E}_q[w(X)^2] = \int p(x)^2/q(x)\, dx is proportional to exp⁡(−x2+x2/(2s2))\exp(-x^2 + x^2/(2s^2)), which is integrable exactly when the exponent’s coefficient is negative: −1+1/(2s2)<0-1 + 1/(2s^2) < 0, that is, s2>1/2s^2 > 1/2, or s>1/2≈0.707s > 1/\sqrt{2} \approx 0.707. For s=0.6s = 0.6 the second moment is infinite, so the plain estimator of any hh whose integrand shares this tail behaviour has infinite variance even though qq is a normal density with the same centre and a similar spread. A finite sample does not reveal it directly because the draws responsible — points far in the tails, where the weight exp⁡(x2(1/(2s2)−1/2))\exp(x^2(1/(2s^2) - 1/2))-type growth is largest — are rare under qq itself; a run that omits them looks stable, and its sample variance understates the true variability.

History

The weighting identity is older than its name in statistics. Importance sampling was introduced as a variance reduction device in statistical physics in the 1950s — Tokdar and Kass trace its introduction to work of Hammersley and Morton (1954) and of Rosenbluth and Rosenbluth (1955) on Monte Carlo calculation in physics, and describe it as a variance reduction technique from that setting .[2] Hastings’s 1970 exposition brought the method into the statistician’s standard toolkit as one of the general strategies for expectations under hard distributions, alongside the Markov chain methods that are that paper’s main subject, and already contained the warning about weights in high dimensions quoted above .[1] The self-normalised, ratio form became central with the growth of Bayesian computation, where unnormalised posteriors are the rule; Tokdar and Kass record the strong law and central limit results for both forms and cite Geweke (1989) for their development in the Bayesian econometric setting .[2] Later development ran in the directions their review organises: adaptive parametric proposals fitted by optimising an efficiency criterion, sequential constructions with resampling for chain-structured targets, and combinations with Markov chain sampling .[2] Owen gives the method a chapter of its own .[3]

See also

Line chart · Apriori algorithm · Asymptotic analysis · Average treatment effect · Base rate · Bayesian inference · Bayesian model · Bayesian probability

Glossary and index

Further reading and use

References

  1. ^ a b c d a. (§1, p. 97) b. (§1, p. 97) c. (§1, p. 97) d. (§1, p. 97) Hastings, W. K. (1970). Monte Carlo sampling methods using Markov chains and their applications. Biometrika, 57(1), 97–109. https://doi.org/10.1093/biomet/57.1.97
  2. ^ a b c d e f g h i j k l m a. (p. 54) b. (p. 54, eq. (2)) c. (p. 54) d. (p. 54, eq. (5)) e. (pp. 55–56, eq. (6)) f. (p. 55) g. (p. 54, eq. (4)) h. (p. 54) i. (p. 55) j. (pp. 56–58) k. (p. 54) l. (p. 54) m. (pp. 55–59) Tokdar, S. T. & Kass, R. E. (2010). Importance Sampling: A Review. Wiley Interdisciplinary Reviews: Computational Statistics, 2(1), 54–60. https://doi.org/10.1002/wics.56
  3. ^ a b c d e f g h i j k l m n o p a. (§9.1) b. (§9.2) c. (§9.1) d. (§9.1) e. (§9.1) f. (§9.7) g. (§9.6) h. (§9.10) i. (§9.10) j. (§9.2) k. (§9.2) l. (§9.2) m. (§9.3) n. (§9.3) o. (§9.5) p. (ch. 9) Owen, A. B. (2013). Monte Carlo Theory, Methods and Examples. Stanford University. https://artowen.su.domains/mc/
Cite this page
Plain text
StatWiki (2026). "Importance sampling." StatWiki. Last updated 2 December 2026. https://www.statwiki.io/articles/importance-sampling.html
BibTeX
@misc{statwiki_importance_sampling,
  author = {{StatWiki}},
  title = {{Importance sampling}},
  year = {2026},
  url = {https://www.statwiki.io/articles/importance-sampling.html},
  urldate = {2026-12-02},
  note = {StatWiki, last updated 2 December 2026}
}