Practice / Gradients of expectations

Importance sampling and Monte Carlo estimators

Ten problems on Monte Carlo estimation: the plain estimator and its 1/√n error, the importance-sampling identity and when it fails, the variance of the weighted estimator, the zero-variance proposal, a Gaussian tail probability where a shifted proposal cuts the variance by a factor of 218, the self-normalised estimator for unnormalised targets and its finite-sample bias, the exponential growth of weight variance with dimension, the effective sample size, control variates and antithetic variates, with worked solutions and the mistakes that swap the weights, call the self-normalised estimator unbiased, use a proposal narrower than the target, pair antithetic samples for a symmetric integrand, or quote the plain Monte Carlo variance for a weighted estimate.

Before you start

Every expectation that cannot be done in closed form is done by averaging samples, and the whole craft is in the variance: an estimate whose error shrinks like 1/n1/\sqrt n is only as good as the constant in front. Importance sampling changes that constant by drawing from a distribution of one's own choosing and correcting with weights, and it can make it smaller by orders of magnitude or, with the wrong choice, infinite. These ten problems derive the plain estimator and its error, prove the importance-sampling identity and find the variance it carries, identify the proposal that drives the variance to zero, work a Gaussian tail probability where a shifted proposal is 218218 times more efficient, handle targets known only up to a constant with the self-normalised estimator and quantify its bias, show why weights degenerate exponentially with dimension and how the effective sample size measures it, and end with the two cheapest variance reductions, control variates and antithetic pairs. The five mistakes at the end are the ones that return a number with no warning: weights with the numerator and denominator swapped, a self-normalised estimate called unbiased, a proposal with lighter tails than the target, antithetic pairs on a symmetric integrand, and the plain Monte Carlo variance quoted for a weighted estimate.

  • The target is a distribution pp and the quantity wanted is μ=Ep[f(X)]\mu = \mathbb{E}_p[f(X)]; sums are written for a finite set of outcomes and integrals for densities, and every argument below works for both. A proposal is another distribution qq with q(x)>0q(x) > 0 wherever f(x)p(x)≠0f(x)p(x) \neq 0. The importance weight is w(x)=p(x)/q(x)w(x) = p(x)/q(x), and Eq\mathbb{E}_q, Var⁡q\operatorname{Var}_q are expectation and variance when X∼qX \sim q.
  • With x1,…,xnx_1, \dots, x_n independent draws, the plain Monte Carlo estimator is μ^n=1n∑if(xi)\hat\mu_n = \tfrac1n\sum_if(x_i) for xi∼px_i \sim p, and the importance-sampling (IS) estimator is μ^IS=1n∑iw(xi)f(xi)\hat\mu_{\mathrm{IS}} = \tfrac1n\sum_iw(x_i)f(x_i) for xi∼qx_i \sim q. The variance page's rules for sums of independent variables are used throughout; Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 and Cov⁡(X,Y)=E[XY]−E[X]E[Y]\operatorname{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y], with correlation ρ=Cov⁡(X,Y)/Var⁡(X)Var⁡(Y)\rho = \operatorname{Cov}(X, Y)/\sqrt{\operatorname{Var}(X)\operatorname{Var}(Y)}.
  • An unnormalised target is p~(x)=Zp(x)\tilde p(x) = Zp(x) with Z=∑xp~(x)Z = \sum_x\tilde p(x) unknown; its weights are w~=p~/q=Zw\tilde w = \tilde p/q = Zw. The self-normalised estimator is μ^SN=∑iw~(xi)f(xi)/∑iw~(xi)\hat\mu_{\mathrm{SN}} = \sum_i\tilde w(x_i)f(x_i)\big/\sum_i\tilde w(x_i). The effective sample size of weights w1,…,wnw_1, \dots, w_n is ESS⁡=(∑iwi)2/∑iwi2\operatorname{ESS} = \big(\sum_iw_i\big)^2\big/\sum_iw_i^2.
  • φ(x)=e−x2/2/2π\varphi(x) = e^{-x^2/2}/\sqrt{2\pi} is the standard normal density and Φ\Phi its distribution function, so P(X>a)=1−Φ(a)\mathbb{P}(X > a) = 1 - \Phi(a) for X∼N(0,1)X \sim \mathcal{N}(0, 1); 1−Φ(3)≈1.350×10−31 - \Phi(3) \approx 1.350\times10^{-3} and 1−Φ(6)≈9.866×10−101 - \Phi(6) \approx 9.866\times10^{-10}. N(m,σ2)\mathcal{N}(m, \sigma^2) has density φ((x−m)/σ)/σ\varphi\big((x - m)/\sigma\big)/\sigma, and N(0,σ2Id)\mathcal{N}(0, \sigma^2I_d) is the dd-dimensional Gaussian with independent coordinates (the Gaussian page). 1{A}\mathbf{1}\{A\} is 11 when AA holds and 00 otherwise. U∼U(0,1)U \sim \mathcal{U}(0, 1) is uniform on [0,1][0, 1], with E[U]=12\mathbb{E}[U] = \tfrac12 and Var⁡(U)=112\operatorname{Var}(U) = \tfrac1{12}.

Builds on: Variance, covariance and correlation, The multivariate Gaussian: gradients and identities

Problems

  1. ·

    Show that μ^n\hat\mu_n is unbiased with Var⁡(μ^n)=Var⁡p(f)/n\operatorname{Var}(\hat\mu_n) = \operatorname{Var}_p(f)/n, so its standard error is Var⁡p(f)/n\sqrt{\operatorname{Var}_p(f)/n}. How many more samples are needed to halve the standard error?

  2. ·

    Prove the importance-sampling identity Ep[f(X)]=Eq[w(X)f(X)]\mathbb{E}_p[f(X)] = \mathbb{E}_q[w(X)f(X)] with w=p/qw = p/q, so that μ^IS\hat\mu_{\mathrm{IS}} is unbiased. Where does the proof use q(x)>0q(x) > 0 wherever f(x)p(x)≠0f(x)p(x) \neq 0, and what happens if that fails?

  3. ··

    Show that Var⁡(μ^IS)=1n(Eq[w2f2]−μ2)\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \dfrac1n\big(\mathbb{E}_q[w^2f^2] - \mu^2\big) and that Eq[w2f2]=Ep[wf2]\mathbb{E}_q[w^2f^2] = \mathbb{E}_p[wf^2]. When is the variance infinite even though the estimator is unbiased?

  4. ···

    Show that Eq[w2f2]≥(Ep∣f∣)2\mathbb{E}_q[w^2f^2] \ge \big(\mathbb{E}_p\lvert f\rvert\big)^2 for every proposal qq, with equality for q∗(x)=∣f(x)∣p(x)/Ep∣f∣q^*(x) = \lvert f(x)\rvert p(x)\big/\mathbb{E}_p\lvert f\rvert. Conclude that for f≥0f \ge 0 the proposal q∗=fp/μq^* = fp/\mu gives an estimator with zero variance, and explain why it cannot be used directly.

  5. ···

    Let X∼N(0,1)X \sim \mathcal{N}(0, 1) and μ=P(X>3)=1−Φ(3)\mu = \mathbb{P}(X > 3) = 1 - \Phi(3). (a) Compute the per-sample variance Var⁡p(1{X>3})\operatorname{Var}_p(\mathbf{1}\{X > 3\}) of plain Monte Carlo and the relative standard error Var⁡p/n/μ\sqrt{\operatorname{Var}_p/n}/\mu for n=1n = 1. (b) With the proposal q=N(3,1)q = \mathcal{N}(3, 1), show that w(x)=e9/2−3xw(x) = e^{9/2 - 3x} and that Eq[w2f2]=e9(1−Φ(6))\mathbb{E}_q[w^2f^2] = e^9\big(1 - \Phi(6)\big). (c) Compute the IS variance per sample and the ratio of the two variances.

  6. ··

    With an unnormalised target p~=Zp\tilde p = Zp and weights w~=p~/q\tilde w = \tilde p/q, show that Eq[w~]=Z\mathbb{E}_q[\tilde w] = Z and Eq[w~f]=Zμ\mathbb{E}_q[\tilde wf] = Z\mu, so the self-normalised estimator μ^SN=∑iw~if(xi)/∑iw~i\hat\mu_{\mathrm{SN}} = \sum_i\tilde w_if(x_i)/\sum_i\tilde w_i converges to μ\mu. Show that for finite nn it is biased in general, by computing E[μ^SN]\mathbb{E}[\hat\mu_{\mathrm{SN}}] for n=1n = 1.

  7. ···

    Let p=N(0,Id)p = \mathcal{N}(0, I_d) and q=N(0,σ2Id)q = \mathcal{N}(0, \sigma^2I_d) with σ2>12\sigma^2 > \tfrac12. Show that in one dimension Eq[w2]=Ep[w]=σ22σ2−1\mathbb{E}_q[w^2] = \mathbb{E}_p[w] = \dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}, hence in dd dimensions Eq[w2]=(σ22σ2−1)d\mathbb{E}_q[w^2] = \Big(\dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}\Big)^d, and evaluate for σ2=2\sigma^2 = 2 and d=10,50d = 10, 50. What happens for σ2≤12\sigma^2 \le \tfrac12?

  8. ··

    For positive weights w1,…,wnw_1, \dots, w_n, show that 1≤ESS⁡=(∑iwi)2∑iwi2≤n1 \le \operatorname{ESS} = \dfrac{(\sum_iw_i)^2}{\sum_iw_i^2} \le n, with ESS⁡=n\operatorname{ESS} = n exactly when all weights are equal and ESS⁡→1\operatorname{ESS} \to 1 when one weight dominates. Show that with normalised weights Wi=wi/∑jwjW_i = w_i/\sum_jw_j, ESS⁡=1/∑iWi2\operatorname{ESS} = 1/\sum_iW_i^2, and that for large nn, ESS⁡/n≈1/Eq[w2]\operatorname{ESS}/n \approx 1/\mathbb{E}_q[w^2] when w=p/qw = p/q with pp and qq normalised.

  9. ··

    Let gg be a function with known mean Ep[g]=γ\mathbb{E}_p[g] = \gamma. Show that the control-variate estimator μ^c=1n∑i(f(xi)−c (g(xi)−γ))\hat\mu_c = \tfrac1n\sum_i\big(f(x_i) - c\,(g(x_i) - \gamma)\big) is unbiased for every cc, that its variance is 1n(Var⁡(f)−2cCov⁡(f,g)+c2Var⁡(g))\tfrac1n\big(\operatorname{Var}(f) - 2c\operatorname{Cov}(f, g) + c^2\operatorname{Var}(g)\big), minimised at c∗=Cov⁡(f,g)/Var⁡(g)c^* = \operatorname{Cov}(f, g)/\operatorname{Var}(g) with value 1nVar⁡(f)(1−ρ2)\tfrac1n\operatorname{Var}(f)(1 - \rho^2). Evaluate for f(U)=eUf(U) = e^U, g(U)=Ug(U) = U with U∼U(0,1)U \sim \mathcal{U}(0, 1).

  10. ··

    For U∼U(0,1)U \sim \mathcal{U}(0, 1), the antithetic pair estimator uses 12(f(U)+f(1−U))\tfrac12\big(f(U) + f(1 - U)\big). Show that it is unbiased and that its variance is 12Var⁡(f(U))(1+ρa)\tfrac12\operatorname{Var}(f(U))(1 + \rho_a) with ρa=Corr⁡(f(U),f(1−U))\rho_a = \operatorname{Corr}\big(f(U), f(1 - U)\big), so it beats two independent samples exactly when ρa<0\rho_a < 0. Evaluate for f(u)=euf(u) = e^u.

Worked solutions

Problem 1

Show that μ^n\hat\mu_n is unbiased with Var⁡(μ^n)=Var⁡p(f)/n\operatorname{Var}(\hat\mu_n) = \operatorname{Var}_p(f)/n, so its standard error is Var⁡p(f)/n\sqrt{\operatorname{Var}_p(f)/n}. How many more samples are needed to halve the standard error?

  1. E[μ^n]=1n∑iEp[f(xi)]=1n⋅nμ=μ\mathbb{E}[\hat\mu_n] = \tfrac1n\sum_i\mathbb{E}_p[f(x_i)] = \tfrac1n\cdot n\mu = \mu.Linearity; every xix_i is a draw from pp.
  2. Var⁡(μ^n)=1n2∑iVar⁡p(f(xi))=Var⁡p(f)n\operatorname{Var}(\hat\mu_n) = \tfrac1{n^2}\sum_i\operatorname{Var}_p(f(x_i)) = \dfrac{\operatorname{Var}_p(f)}n.Independence makes the variance of the sum the sum of the variances; Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2\operatorname{Var}(X) with a=1/na = 1/n.
  3. The standard error Var⁡p(f)/n\sqrt{\operatorname{Var}_p(f)/n} is halved when nn is multiplied by 44.1/(4n)=121/n\sqrt{1/(4n)} = \tfrac12\sqrt{1/n}.
  4. E[μ^n]=μ\mathbb{E}[\hat\mu_n] = \mu, Var⁡(μ^n)=Var⁡p(f)/n\operatorname{Var}(\hat\mu_n) = \operatorname{Var}_p(f)/n, standard error Var⁡p(f)/n\sqrt{\operatorname{Var}_p(f)/n}; halving it takes four times the samplesMonte Carlo converges at the rate 1/n1/\sqrt n in every dimension, which is its whole appeal against quadrature and its whole weakness against anything with a closed form: each extra digit of accuracy costs a hundred times the samples. The constant Var⁡p(f)\operatorname{Var}_p(f) is the only thing that can be changed, and Problems 2 to 10 are all ways of changing it. For a probability μ=Ep[1{A}]\mu = \mathbb{E}_p[\mathbf{1}\{A\}] the variance is μ(1−μ)\mu(1 - \mu) and the relative error (1−μ)/(nμ)\sqrt{(1 - \mu)/(n\mu)}, which for a rare event is enormous; Problem 5 makes this concrete.

Problem 2

Prove the importance-sampling identity Ep[f(X)]=Eq[w(X)f(X)]\mathbb{E}_p[f(X)] = \mathbb{E}_q[w(X)f(X)] with w=p/qw = p/q, so that μ^IS\hat\mu_{\mathrm{IS}} is unbiased. Where does the proof use q(x)>0q(x) > 0 wherever f(x)p(x)≠0f(x)p(x) \neq 0, and what happens if that fails?

  1. Ep[f]=∑xp(x)f(x)=∑x:q(x)>0q(x)p(x)q(x)f(x)\mathbb{E}_p[f] = \sum_xp(x)f(x) = \sum_{x : q(x) > 0}q(x)\dfrac{p(x)}{q(x)}f(x).Multiply and divide by q(x)q(x), which is allowed only where q(x)>0q(x) > 0; the terms with q(x)=0q(x) = 0 are dropped from the sum.
  2. The dropped terms have f(x)p(x)=0f(x)p(x) = 0 by the support condition, so they contributed nothing.The condition says q=0q = 0 only where the summand p(x)f(x)p(x)f(x) is already zero.
  3. ∑x:q(x)>0q(x) w(x)f(x)=Eq[w(X)f(X)]\sum_{x : q(x) > 0}q(x)\,w(x)f(x) = \mathbb{E}_q[w(X)f(X)].A qq-weighted sum over the support of qq is an expectation under qq.
  4. E[μ^IS]=1n∑iEq[w(xi)f(xi)]=μ\mathbb{E}[\hat\mu_{\mathrm{IS}}] = \tfrac1n\sum_i\mathbb{E}_q[w(x_i)f(x_i)] = \mu.Linearity with xi∼qx_i \sim q, then step 3.
  5. Ep[f]=Eq[wf]\mathbb{E}_p[f] = \mathbb{E}_q[wf] and μ^IS\hat\mu_{\mathrm{IS}} is unbiased, provided q>0q > 0 wherever fp≠0fp \neq 0; if the condition fails, the estimator is biased by the missing mass ∑x:q(x)=0p(x)f(x)\sum_{x : q(x) = 0}p(x)f(x)The identity is a change of measure: samples from qq are reweighted to look like samples from pp, and the weight is large where qq undersamples pp and small where it oversamples. The condition is exactly what the ELBO page's w=p(x,z)/q(z)w = p(x, z)/q(z) and the policy-gradient page's behaviour policy need. It fails silently: a proposal that never visits a region where pp has mass returns an estimate that is biased by everything in that region, with no large weight to signal it, because the samples that would have carried the large weight are never drawn. Unbiased is the easy part; Problem 3 is the hard part.

Problem 3

Show that Var⁡(μ^IS)=1n(Eq[w2f2]−μ2)\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \dfrac1n\big(\mathbb{E}_q[w^2f^2] - \mu^2\big) and that Eq[w2f2]=Ep[wf2]\mathbb{E}_q[w^2f^2] = \mathbb{E}_p[wf^2]. When is the variance infinite even though the estimator is unbiased?

  1. Var⁡(μ^IS)=1nVar⁡q(wf)=1n(Eq[(wf)2]−(Eq[wf])2)\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \dfrac1n\operatorname{Var}_q(wf) = \dfrac1n\big(\mathbb{E}_q[(wf)^2] - (\mathbb{E}_q[wf])^2\big).Problem 1's argument with wfwf in place of ff and qq in place of pp; the variance as second moment minus squared mean.
  2. Eq[wf]=μ\mathbb{E}_q[wf] = \mu, so Var⁡(μ^IS)=1n(Eq[w2f2]−μ2)\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \dfrac1n\big(\mathbb{E}_q[w^2f^2] - \mu^2\big).Problem 2.
  3. Eq[w2f2]=∑xq(x)p(x)2q(x)2f(x)2=∑xp(x)p(x)q(x)f(x)2=Ep[wf2]\mathbb{E}_q[w^2f^2] = \sum_xq(x)\dfrac{p(x)^2}{q(x)^2}f(x)^2 = \sum_xp(x)\dfrac{p(x)}{q(x)}f(x)^2 = \mathbb{E}_p[wf^2].Cancel one qq against one of the two in w2w^2; what remains is a pp-weighted sum of wf2wf^2.
  4. Var⁡(μ^IS)=1n(Eq[w2f2]−μ2)=1n(Ep[wf2]−μ2)\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \dfrac1n\big(\mathbb{E}_q[w^2f^2] - \mu^2\big) = \dfrac1n\big(\mathbb{E}_p[wf^2] - \mu^2\big); it is infinite when Ep[wf2]=Ep[f2p/q]\mathbb{E}_p[wf^2] = \mathbb{E}_p[f^2p/q] diverges, which happens when qq has lighter tails than f2pf^2pThe variance is controlled by Ep[wf2]\mathbb{E}_p[w f^2]: the weight p/qp/q averaged under pp and amplified by f2f^2. Where qq is much smaller than pp the weight is huge and the rare samples that land there dominate the average; if p/qp/q grows fast enough in the tails the second moment is infinite and the estimator, though unbiased, has no central limit theorem, so the sample average lurches whenever a tail sample arrives and its empirical standard error is meaningless. Problem 7 computes Ep[w]\mathbb{E}_p[w] for Gaussians and finds exactly this: a proposal narrower than the target by a factor of 2\sqrt2 in standard deviation already gives infinite variance (Mistake 3). The rule of thumb is that qq must have tails at least as heavy as pp's, and Problem 4 says what the best qq is.

Problem 4

Show that Eq[w2f2]≥(Ep∣f∣)2\mathbb{E}_q[w^2f^2] \ge \big(\mathbb{E}_p\lvert f\rvert\big)^2 for every proposal qq, with equality for q∗(x)=∣f(x)∣p(x)/Ep∣f∣q^*(x) = \lvert f(x)\rvert p(x)\big/\mathbb{E}_p\lvert f\rvert. Conclude that for f≥0f \ge 0 the proposal q∗=fp/μq^* = fp/\mu gives an estimator with zero variance, and explain why it cannot be used directly.

  1. Eq[w2f2]=Eq[(w∣f∣)2]≥(Eq[w∣f∣])2\mathbb{E}_q[w^2f^2] = \mathbb{E}_q\big[(w\lvert f\rvert)^2\big] \ge \big(\mathbb{E}_q[w\lvert f\rvert]\big)^2.f2=∣f∣2f^2 = \lvert f\rvert^2; then Jensen's inequality for the convex function t↦t2t \mapsto t^2, the Jensen page's Problem 5(a): a second moment is at least the squared mean.
  2. Eq[w∣f∣]=Ep∣f∣\mathbb{E}_q[w\lvert f\rvert] = \mathbb{E}_p\lvert f\rvert.Problem 2's identity applied to ∣f∣\lvert f\rvert.
  3. For q∗=∣f∣p/Ep∣f∣q^* = \lvert f\rvert p/\mathbb{E}_p\lvert f\rvert: w∗∣f∣=pq∗∣f∣=Ep∣f∣w^*\lvert f\rvert = \dfrac{p}{q^*}\lvert f\rvert = \mathbb{E}_p\lvert f\rvert, a constant, so equality holds in step 1.p/q∗=Ep∣f∣/∣f∣p/q^* = \mathbb{E}_p\lvert f\rvert/\lvert f\rvert wherever f≠0f \neq 0, and the ∣f∣\lvert f\rvert cancels; a constant has zero Jensen gap. q∗q^* is a valid proposal: nonnegative, sums to one, and positive wherever fp≠0fp \neq 0.
  4. For f≥0f \ge 0: Ep∣f∣=μ\mathbb{E}_p\lvert f\rvert = \mu, q∗=fp/μq^* = fp/\mu, and every sample gives w∗(x)f(x)=μw^*(x)f(x) = \mu exactly, so Var⁡(μ^IS)=0\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = 0.Steps 2 and 3 with ∣f∣=f\lvert f\rvert = f; the estimator is the constant μ\mu whatever is drawn.
  5. Eq[w2f2]≥(Ep∣f∣)2\mathbb{E}_q[w^2f^2] \ge (\mathbb{E}_p\lvert f\rvert)^2 with equality at q∗∝∣f∣pq^* \propto \lvert f\rvert p; for f≥0f \ge 0, q∗=fp/μq^* = fp/\mu has zero variance; it cannot be used because normalising it requires μ\mu, the unknownThe best proposal is not the target but the target reshaped by the integrand: sample where ∣f∣p\lvert f\rvert p is large, which for a tail probability means sampling in the tail (Problem 5) and for the ELBO means sampling near the posterior. The zero-variance proposal is a statement about direction rather than a recipe, since writing it down solves the problem, but every good proposal is an approximation to it, and the bound in step 1 gives the floor that any approximation is measured against. For ff taking both signs the minimum variance is (Ep∣f∣)2−μ2>0(\mathbb{E}_p\lvert f\rvert)^2 - \mu^2 > 0, so the floor is not zero. Mistake 4 on the Jensen page is the reason q=pq = p is usually far from optimal, which Problem 5 quantifies.

Problem 5

Let X∼N(0,1)X \sim \mathcal{N}(0, 1) and μ=P(X>3)=1−Φ(3)\mu = \mathbb{P}(X > 3) = 1 - \Phi(3). (a) Compute the per-sample variance Var⁡p(1{X>3})\operatorname{Var}_p(\mathbf{1}\{X > 3\}) of plain Monte Carlo and the relative standard error Var⁡p/n/μ\sqrt{\operatorname{Var}_p/n}/\mu for n=1n = 1. (b) With the proposal q=N(3,1)q = \mathcal{N}(3, 1), show that w(x)=e9/2−3xw(x) = e^{9/2 - 3x} and that Eq[w2f2]=e9(1−Φ(6))\mathbb{E}_q[w^2f^2] = e^9\big(1 - \Phi(6)\big). (c) Compute the IS variance per sample and the ratio of the two variances.

  1. (a) f=1{X>3}f = \mathbf{1}\{X > 3\} has f2=ff^2 = f, so Var⁡p(f)=μ−μ2=μ(1−μ)≈1.348×10−3\operatorname{Var}_p(f) = \mu - \mu^2 = \mu(1 - \mu) \approx 1.348\times10^{-3}, and the relative standard error at n=1n = 1 is (1−μ)/μ≈27.2\sqrt{(1 - \mu)/\mu} \approx 27.2.An indicator is Bernoulli with success probability μ≈1.350×10−3\mu \approx 1.350\times10^{-3}; divide the standard error by μ\mu.
  2. (b) w(x)=φ(x)φ(x−3)=exp⁡(−x22+(x−3)22)=exp⁡(−6x+92)=e9/2−3xw(x) = \dfrac{\varphi(x)}{\varphi(x - 3)} = \exp\Big(-\dfrac{x^2}2 + \dfrac{(x - 3)^2}2\Big) = \exp\Big(\dfrac{-6x + 9}2\Big) = e^{9/2 - 3x}.The two densities share the 1/2π1/\sqrt{2\pi}; expand (x−3)2=x2−6x+9(x - 3)^2 = x^2 - 6x + 9 and the x2x^2 cancels.
  3. Eq[w2f2]=∫3∞φ(x)2φ(x−3) dx=∫3∞12πexp⁡(−x2+(x−3)22)dx\mathbb{E}_q[w^2f^2] = \int_3^\infty\dfrac{\varphi(x)^2}{\varphi(x - 3)}\,dx = \int_3^\infty\dfrac1{\sqrt{2\pi}}\exp\Big(-x^2 + \dfrac{(x - 3)^2}2\Big)dx.Problem 3's Eq[w2f2]=∫q (p/q)2f2=∫p2/q\mathbb{E}_q[w^2f^2] = \int q\,(p/q)^2f^2 = \int p^2/q over the region where f=1f = 1; one factor of 2π\sqrt{2\pi} survives.
  4. −x2+x2−6x+92=−x22−3x+92=−(x+3)22+9-x^2 + \dfrac{x^2 - 6x + 9}2 = -\dfrac{x^2}2 - 3x + \dfrac92 = -\dfrac{(x + 3)^2}2 + 9.Complete the square: −12(x2+6x)=−12(x+3)2+92-\tfrac12(x^2 + 6x) = -\tfrac12(x + 3)^2 + \tfrac92, plus the 92\tfrac92 already there.
  5. Eq[w2f2]=e9∫3∞φ(x+3) dx=e9∫6∞φ(u) du=e9(1−Φ(6))≈8103.1×9.866×10−10≈7.994×10−6\mathbb{E}_q[w^2f^2] = e^9\int_3^\infty\varphi(x + 3)\,dx = e^9\int_6^\infty\varphi(u)\,du = e^9\big(1 - \Phi(6)\big) \approx 8103.1\times 9.866\times10^{-10} \approx 7.994\times10^{-6}.Substitute u=x+3u = x + 3; the integral of φ\varphi from 66 is the upper tail.
  6. (c) Var⁡q(wf)=7.994×10−6−μ2≈7.994×10−6−1.822×10−6=6.17×10−6\operatorname{Var}_q(wf) = 7.994\times10^{-6} - \mu^2 \approx 7.994\times10^{-6} - 1.822\times10^{-6} = 6.17\times10^{-6}, and the ratio is 1.348×10−3/6.17×10−6≈2181.348\times10^{-3}/6.17\times10^{-6} \approx 218; the relative standard error at n=1n = 1 is 6.17×10−6/μ≈1.84\sqrt{6.17\times10^{-6}}/\mu \approx 1.84.Problem 3; μ2≈(1.350×10−3)2\mu^2 \approx (1.350\times10^{-3})^2.
  7. (a) Var⁡p=μ(1−μ)≈1.348×10−3\operatorname{Var}_p = \mu(1 - \mu) \approx 1.348\times10^{-3}, relative error ≈27.2\approx 27.2 per sample; (b) w(x)=e9/2−3xw(x) = e^{9/2 - 3x} and Eq[w2f2]=e9(1−Φ(6))≈7.994×10−6\mathbb{E}_q[w^2f^2] = e^9(1 - \Phi(6)) \approx 7.994\times10^{-6}; (c) IS variance ≈6.17×10−6\approx 6.17\times10^{-6} per sample, 218218 times smaller, relative error ≈1.84\approx 1.84 per samplePlain Monte Carlo sees a success once in 741741 draws and needs about n≈74,000n \approx 74{,}000 samples for a 10%10\% relative error; the shifted proposal lands half its draws in the tail with weights no larger than e9/2−9=e−4.5e^{9/2 - 9} = e^{-4.5}, and reaches the same accuracy with n≈340n \approx 340. The gain comes from sampling where fpfp is large, Problem 4's prescription, and the shift to the threshold rather than beyond it is the standard choice because it keeps the weights bounded on the region that matters. The check evaluates both variances by quadrature and confirms the factor of 218218.

Problem 6

With an unnormalised target p~=Zp\tilde p = Zp and weights w~=p~/q\tilde w = \tilde p/q, show that Eq[w~]=Z\mathbb{E}_q[\tilde w] = Z and Eq[w~f]=Zμ\mathbb{E}_q[\tilde wf] = Z\mu, so the self-normalised estimator μ^SN=∑iw~if(xi)/∑iw~i\hat\mu_{\mathrm{SN}} = \sum_i\tilde w_if(x_i)/\sum_i\tilde w_i converges to μ\mu. Show that for finite nn it is biased in general, by computing E[μ^SN]\mathbb{E}[\hat\mu_{\mathrm{SN}}] for n=1n = 1.

  1. Eq[w~]=∑xq(x)Zp(x)q(x)=Z∑xp(x)=Z\mathbb{E}_q[\tilde w] = \sum_xq(x)\dfrac{Zp(x)}{q(x)} = Z\sum_xp(x) = Z.The qq cancels and pp sums to one.
  2. Eq[w~f]=Z Eq[wf]=Zμ\mathbb{E}_q[\tilde wf] = Z\,\mathbb{E}_q[wf] = Z\mu.w~=Zw\tilde w = Zw and Problem 2.
  3. μ^SN=1n∑iw~if(xi)1n∑iw~i\hat\mu_{\mathrm{SN}} = \dfrac{\tfrac1n\sum_i\tilde w_if(x_i)}{\tfrac1n\sum_i\tilde w_i}, and as n→∞n \to \infty the numerator tends to ZμZ\mu and the denominator to ZZ, so the ratio tends to μ\mu.Divide top and bottom by nn; each is a plain Monte Carlo average under qq and converges to its mean (the law of large numbers); the ratio of the limits is the limit of the ratio because Z>0Z > 0.
  4. For n=1n = 1: μ^SN=w~(x1)f(x1)w~(x1)=f(x1)\hat\mu_{\mathrm{SN}} = \dfrac{\tilde w(x_1)f(x_1)}{\tilde w(x_1)} = f(x_1) with x1∼qx_1 \sim q, so E[μ^SN]=Eq[f]≠μ\mathbb{E}[\hat\mu_{\mathrm{SN}}] = \mathbb{E}_q[f] \neq \mu in general.The single weight cancels; the estimate is the integrand at a draw from the proposal, not from the target.
  5. Eq[w~]=Z\mathbb{E}_q[\tilde w] = Z, Eq[w~f]=Zμ\mathbb{E}_q[\tilde wf] = Z\mu, so μ^SN→μ\hat\mu_{\mathrm{SN}} \to \mu; but E[μ^SN]≠μ\mathbb{E}[\hat\mu_{\mathrm{SN}}] \neq \mu for finite nn, and for n=1n = 1 it equals Eq[f]\mathbb{E}_q[f]Self-normalisation is what makes importance sampling usable for posteriors, energy-based models and any p~\tilde p whose normaliser is unknown: the unknown ZZ cancels in the ratio. The price is bias of order 1/n1/n, because the ratio of two unbiased estimates is not unbiased (the Jensen page's Mistake 3: the denominator's fluctuations do not average out through a reciprocal), and the check enumerates every pair of draws for n=2n = 2 to measure it. The bias vanishes faster than the standard error, which is O(1/n)O(1/\sqrt n), so it is harmless for large nn and the variance of Problem 3, with ff replaced by f−μf - \mu, is the right measure of quality; Mistake 2 overstates the guarantee.

Problem 7

Let p=N(0,Id)p = \mathcal{N}(0, I_d) and q=N(0,σ2Id)q = \mathcal{N}(0, \sigma^2I_d) with σ2>12\sigma^2 > \tfrac12. Show that in one dimension Eq[w2]=Ep[w]=σ22σ2−1\mathbb{E}_q[w^2] = \mathbb{E}_p[w] = \dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}, hence in dd dimensions Eq[w2]=(σ22σ2−1)d\mathbb{E}_q[w^2] = \Big(\dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}\Big)^d, and evaluate for σ2=2\sigma^2 = 2 and d=10,50d = 10, 50. What happens for σ2≤12\sigma^2 \le \tfrac12?

  1. In one dimension, Eq[w2]=∫p(x)2q(x) dx=∫(2π)−1e−x2(2πσ2)−1/2e−x2/(2σ2) dx=σ2π∫exp⁡(−x2(1−12σ2))dx\mathbb{E}_q[w^2] = \int\dfrac{p(x)^2}{q(x)}\,dx = \int\dfrac{(2\pi)^{-1}e^{-x^2}}{(2\pi\sigma^2)^{-1/2}e^{-x^2/(2\sigma^2)}}\,dx = \dfrac{\sigma}{\sqrt{2\pi}}\int\exp\Big(-x^2\Big(1 - \dfrac1{2\sigma^2}\Big)\Big)dx.Problem 3's Eq[w2]=Ep[w]=∫p2/q\mathbb{E}_q[w^2] = \mathbb{E}_p[w] = \int p^2/q; collect the constants and the exponents.
  2. ∫e−αx2 dx=π/α\int e^{-\alpha x^2}\,dx = \sqrt{\pi/\alpha} for α>0\alpha > 0, with α=1−12σ2=2σ2−12σ2\alpha = 1 - \dfrac1{2\sigma^2} = \dfrac{2\sigma^2 - 1}{2\sigma^2}.The Gaussian integral; it converges only when α>0\alpha > 0, that is σ2>12\sigma^2 > \tfrac12.
  3. Eq[w2]=σ2π2πσ22σ2−1=σ22σ2−1\mathbb{E}_q[w^2] = \dfrac{\sigma}{\sqrt{2\pi}}\sqrt{\dfrac{2\pi\sigma^2}{2\sigma^2 - 1}} = \dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}.Multiply out; π/α=2πσ2/(2σ2−1)\sqrt{\pi/\alpha} = \sqrt{2\pi\sigma^2/(2\sigma^2 - 1)}.
  4. In dd dimensions pp and qq factor over coordinates, so w(x)=∏j=1dwj(xj)w(x) = \prod_{j=1}^dw_j(x_j) and Eq[w2]=∏jEq[wj2]=(σ22σ2−1)d\mathbb{E}_q[w^2] = \prod_j\mathbb{E}_q[w_j^2] = \Big(\dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}\Big)^d.Independent coordinates: the expectation of a product of independent factors is the product of the expectations (the variance page).
  5. σ2=2\sigma^2 = 2: 23≈1.1547\dfrac{2}{\sqrt3} \approx 1.1547, so Eq[w2]≈4.2\mathbb{E}_q[w^2] \approx 4.2 for d=10d = 10 and ≈1.3×103\approx 1.3\times10^3 for d=50d = 50.1.154710≈4.211.1547^{10} \approx 4.21 and 1.154750≈13291.1547^{50} \approx 1329.
  6. Eq[w2]=(σ22σ2−1)d\mathbb{E}_q[w^2] = \Big(\dfrac{\sigma^2}{\sqrt{2\sigma^2 - 1}}\Big)^d for σ2>12\sigma^2 > \tfrac12; for σ2=2\sigma^2 = 2 it is ≈4.2\approx 4.2 at d=10d = 10 and ≈1.3×103\approx 1.3\times10^3 at d=50d = 50; for σ2≤12\sigma^2 \le \tfrac12 it is infiniteThe base σ2/2σ2−1\sigma^2/\sqrt{2\sigma^2 - 1} equals 11 only at σ2=1\sigma^2 = 1 and exceeds 11 on both sides, so any mismatch between proposal and target is raised to the power dd: the weights' variance, Eq[w2]−1\mathbb{E}_q[w^2] - 1, grows exponentially with dimension, and with it the variance of every importance-sampling estimate (Problem 3). Problem 8 turns this into an effective sample size of roughly n/1329n/1329 at d=50d = 50, so a million samples are worth about 750750. Below σ2=12\sigma^2 = \tfrac12 the proposal's tails are too light and the integral diverges: unbiased, infinite variance, Mistake 3. This is why importance sampling is a low-dimensional tool, why the ELBO page's single-sample bound is loose in high dimensions, and why Markov chain methods replace it when dd is large.

Problem 8

For positive weights w1,…,wnw_1, \dots, w_n, show that 1≤ESS⁡=(∑iwi)2∑iwi2≤n1 \le \operatorname{ESS} = \dfrac{(\sum_iw_i)^2}{\sum_iw_i^2} \le n, with ESS⁡=n\operatorname{ESS} = n exactly when all weights are equal and ESS⁡→1\operatorname{ESS} \to 1 when one weight dominates. Show that with normalised weights Wi=wi/∑jwjW_i = w_i/\sum_jw_j, ESS⁡=1/∑iWi2\operatorname{ESS} = 1/\sum_iW_i^2, and that for large nn, ESS⁡/n≈1/Eq[w2]\operatorname{ESS}/n \approx 1/\mathbb{E}_q[w^2] when w=p/qw = p/q with pp and qq normalised.

  1. (∑iwi)2=∑iwi2+∑i≠jwiwj≥∑iwi2\big(\sum_iw_i\big)^2 = \sum_iw_i^2 + \sum_{i \neq j}w_iw_j \ge \sum_iw_i^2, so ESS⁡≥1\operatorname{ESS} \ge 1.Expand the square; the cross terms are positive.
  2. (∑iwi)2=(∑i1⋅wi)2≤(∑i12)(∑iwi2)=n∑iwi2\big(\sum_iw_i\big)^2 = \big(\sum_i 1\cdot w_i\big)^2 \le \big(\sum_i1^2\big)\big(\sum_iw_i^2\big) = n\sum_iw_i^2, so ESS⁡≤n\operatorname{ESS} \le n.Cauchy–Schwarz for the vectors 1\mathbf{1} and ww; equivalently the Jensen page's (E[W])2≤E[W2](\mathbb{E}[W])^2 \le \mathbb{E}[W^2] for the uniform distribution on the wiw_i.
  3. Equality in step 2 holds when ww is proportional to 1\mathbf{1}, all weights equal; and if w1≫wiw_1 \gg w_i for i>1i > 1 then both (∑iwi)2(\sum_iw_i)^2 and ∑iwi2\sum_iw_i^2 are ≈w12\approx w_1^2, so ESS⁡≈1\operatorname{ESS} \approx 1.The Cauchy–Schwarz equality case; dominant-term approximation.
  4. ∑iWi2=∑iwi2(∑jwj)2=1ESS⁡\sum_iW_i^2 = \dfrac{\sum_iw_i^2}{(\sum_jw_j)^2} = \dfrac1{\operatorname{ESS}}.Substitute Wi=wi/∑jwjW_i = w_i/\sum_jw_j.
  5. ESS⁡n=(1n∑iwi)21n∑iwi2→(Eq[w])2Eq[w2]=1Eq[w2]\dfrac{\operatorname{ESS}}n = \dfrac{\big(\tfrac1n\sum_iw_i\big)^2}{\tfrac1n\sum_iw_i^2} \to \dfrac{(\mathbb{E}_q[w])^2}{\mathbb{E}_q[w^2]} = \dfrac1{\mathbb{E}_q[w^2]}.Divide top and bottom by n2n^2 and nn; each average converges to its mean by the law of large numbers, and Eq[w]=1\mathbb{E}_q[w] = 1 for normalised pp and qq (Problem 6 with Z=1Z = 1).
  6. 1≤ESS⁡≤n1 \le \operatorname{ESS} \le n, equal to nn for equal weights and near 11 for one dominant weight; ESS⁡=1/∑iWi2\operatorname{ESS} = 1/\sum_iW_i^2; ESS⁡/n≈1/Eq[w2]\operatorname{ESS}/n \approx 1/\mathbb{E}_q[w^2]The effective sample size is the number of plain Monte Carlo samples the weighted sample is worth, in the sense that the self-normalised estimator's variance is approximately Var⁡p(f)/ESS⁡\operatorname{Var}_p(f)/\operatorname{ESS} rather than Var⁡p(f)/n\operatorname{Var}_p(f)/n. It is the diagnostic for Problem 7's degeneracy: with Eq[w2]≈1329\mathbb{E}_q[w^2] \approx 1329 at d=50d = 50, a million draws have an ESS near 750750, and a histogram of the normalised weights shows a handful of samples carrying almost all the mass. Sequential Monte Carlo resamples whenever ESS⁡\operatorname{ESS} falls below a threshold such as n/2n/2. The ESS uses only the weights, not ff, so it can report a healthy sample for an integrand that is badly estimated, which is the limitation Problem 4's ∣f∣p\lvert f\rvert p points at.

Problem 9

Let gg be a function with known mean Ep[g]=γ\mathbb{E}_p[g] = \gamma. Show that the control-variate estimator μ^c=1n∑i(f(xi)−c (g(xi)−γ))\hat\mu_c = \tfrac1n\sum_i\big(f(x_i) - c\,(g(x_i) - \gamma)\big) is unbiased for every cc, that its variance is 1n(Var⁡(f)−2cCov⁡(f,g)+c2Var⁡(g))\tfrac1n\big(\operatorname{Var}(f) - 2c\operatorname{Cov}(f, g) + c^2\operatorname{Var}(g)\big), minimised at c∗=Cov⁡(f,g)/Var⁡(g)c^* = \operatorname{Cov}(f, g)/\operatorname{Var}(g) with value 1nVar⁡(f)(1−ρ2)\tfrac1n\operatorname{Var}(f)(1 - \rho^2). Evaluate for f(U)=eUf(U) = e^U, g(U)=Ug(U) = U with U∼U(0,1)U \sim \mathcal{U}(0, 1).

  1. E[f(xi)−c(g(xi)−γ)]=μ−c(γ−γ)=μ\mathbb{E}[f(x_i) - c(g(x_i) - \gamma)] = \mu - c(\gamma - \gamma) = \mu.Linearity; Ep[g]=γ\mathbb{E}_p[g] = \gamma by assumption, so the correction has mean zero for every cc.
  2. Var⁡(f−c(g−γ))=Var⁡(f)−2cCov⁡(f,g)+c2Var⁡(g)\operatorname{Var}\big(f - c(g - \gamma)\big) = \operatorname{Var}(f) - 2c\operatorname{Cov}(f, g) + c^2\operatorname{Var}(g), and dividing by nn gives the variance of the average.The variance page's Var⁡(X−cY)=Var⁡(X)−2cCov⁡(X,Y)+c2Var⁡(Y)\operatorname{Var}(X - cY) = \operatorname{Var}(X) - 2c\operatorname{Cov}(X, Y) + c^2\operatorname{Var}(Y); the constant γ\gamma changes nothing; Problem 1 for the 1/n1/n.
  3. ddc(⋯)=−2Cov⁡(f,g)+2cVar⁡(g)=0  ⟺  c∗=Cov⁡(f,g)Var⁡(g)\dfrac{d}{dc}\big(\cdots\big) = -2\operatorname{Cov}(f, g) + 2c\operatorname{Var}(g) = 0 \iff c^* = \dfrac{\operatorname{Cov}(f, g)}{\operatorname{Var}(g)}, a minimum since the coefficient of c2c^2 is positive.Differentiate the quadratic in cc.
  4. At c∗c^*: Var⁡(f)−Cov⁡(f,g)2Var⁡(g)=Var⁡(f)(1−Cov⁡(f,g)2Var⁡(f)Var⁡(g))=Var⁡(f)(1−ρ2)\operatorname{Var}(f) - \dfrac{\operatorname{Cov}(f, g)^2}{\operatorname{Var}(g)} = \operatorname{Var}(f)\Big(1 - \dfrac{\operatorname{Cov}(f, g)^2}{\operatorname{Var}(f)\operatorname{Var}(g)}\Big) = \operatorname{Var}(f)(1 - \rho^2).Substitute c∗c^*; the bracket is 1−ρ21 - \rho^2 by the definition of correlation.
  5. U∼U(0,1)U \sim \mathcal{U}(0, 1): E[eU]=e−1\mathbb{E}[e^U] = e - 1, E[e2U]=12(e2−1)\mathbb{E}[e^{2U}] = \tfrac12(e^2 - 1), so Var⁡(eU)=12(e2−1)−(e−1)2≈0.2420\operatorname{Var}(e^U) = \tfrac12(e^2 - 1) - (e - 1)^2 \approx 0.2420; E[UeU]=1\mathbb{E}[Ue^U] = 1, so Cov⁡(eU,U)=1−12(e−1)≈0.1409\operatorname{Cov}(e^U, U) = 1 - \tfrac12(e - 1) \approx 0.1409; Var⁡(U)=112\operatorname{Var}(U) = \tfrac1{12}.∫01eu du=e−1\int_0^1e^u\,du = e - 1, ∫01e2u du=12(e2−1)\int_0^1e^{2u}\,du = \tfrac12(e^2 - 1), ∫01ueu du=[ueu−eu]01=1\int_0^1ue^u\,du = [ue^u - e^u]_0^1 = 1 by parts (the integration page).
  6. c∗=0.1409⋅12≈1.690c^* = 0.1409\cdot 12 \approx 1.690, ρ2=0.140920.2420/12≈0.984\rho^2 = \dfrac{0.1409^2}{0.2420/12} \approx 0.984, and the variance falls from 0.24200.2420 to 0.2420 (1−0.984)≈0.00390.2420\,(1 - 0.984) \approx 0.0039, a factor of about 6161.Steps 3 and 4 with the numbers of step 5.
  7. μ^c\hat\mu_c is unbiased for every cc; Var⁡(μ^c)=1n(Var⁡(f)−2cCov⁡(f,g)+c2Var⁡(g))\operatorname{Var}(\hat\mu_c) = \tfrac1n\big(\operatorname{Var}(f) - 2c\operatorname{Cov}(f, g) + c^2\operatorname{Var}(g)\big), minimised at c∗=Cov⁡(f,g)/Var⁡(g)c^* = \operatorname{Cov}(f, g)/\operatorname{Var}(g) with value 1nVar⁡(f)(1−ρ2)\tfrac1n\operatorname{Var}(f)(1 - \rho^2); for eUe^U with control UU: c∗≈1.690c^* \approx 1.690, ρ2≈0.984\rho^2 \approx 0.984, variance 0.2420→0.00390.2420 \to 0.0039A control variate subtracts the part of ff that a known-mean function can explain, which is a regression of ff on gg: c∗c^* is the least-squares slope and 1−ρ21 - \rho^2 the unexplained fraction. Because cc does not affect the mean, it can be estimated from the same samples at a cost of O(1/n)O(1/n) bias, which is how it is done in practice. This is the policy-gradient page's baseline in disguise: there gg is the score function, whose mean is known to be zero, and c∗c^* is the optimal baseline. Any cc strictly between 00 and 2c∗2c^* helps and any cc outside that range hurts, so a default of c=1c = 1 is right only when c∗≈1c^* \approx 1.

Problem 10

For U∼U(0,1)U \sim \mathcal{U}(0, 1), the antithetic pair estimator uses 12(f(U)+f(1−U))\tfrac12\big(f(U) + f(1 - U)\big). Show that it is unbiased and that its variance is 12Var⁡(f(U))(1+ρa)\tfrac12\operatorname{Var}(f(U))(1 + \rho_a) with ρa=Corr⁡(f(U),f(1−U))\rho_a = \operatorname{Corr}\big(f(U), f(1 - U)\big), so it beats two independent samples exactly when ρa<0\rho_a < 0. Evaluate for f(u)=euf(u) = e^u.

  1. 1−U∼U(0,1)1 - U \sim \mathcal{U}(0, 1), so E[f(1−U)]=E[f(U)]=μ\mathbb{E}[f(1 - U)] = \mathbb{E}[f(U)] = \mu and the pair average has mean 12(μ+μ)=μ\tfrac12(\mu + \mu) = \mu.Reflecting a uniform variable about 12\tfrac12 gives a uniform variable; linearity.
  2. Var⁡(f(U)+f(1−U)2)=14(Var⁡(f(U))+Var⁡(f(1−U))+2Cov⁡(f(U),f(1−U)))=14(2v+2ρav)=v2(1+ρa)\operatorname{Var}\Big(\dfrac{f(U) + f(1 - U)}2\Big) = \dfrac14\big(\operatorname{Var}(f(U)) + \operatorname{Var}(f(1 - U)) + 2\operatorname{Cov}(f(U), f(1 - U))\big) = \dfrac14\big(2v + 2\rho_av\big) = \dfrac v2(1 + \rho_a) with v=Var⁡(f(U))v = \operatorname{Var}(f(U)).The variance page's variance of a sum; both terms have variance vv by step 1, and the covariance is ρav\rho_av by the definition of correlation.
  3. Two independent samples have variance v/2v/2, so the pair is better exactly when 1+ρa<11 + \rho_a < 1, that is ρa<0\rho_a < 0.Compare v2(1+ρa)\tfrac v2(1 + \rho_a) with v2\tfrac v2.
  4. For f(u)=euf(u) = e^u: v=12(e2−1)−(e−1)2≈0.2420v = \tfrac12(e^2 - 1) - (e - 1)^2 \approx 0.2420 and Cov⁡(eU,e1−U)=E[eUe1−U]−μ2=e−(e−1)2≈−0.2342\operatorname{Cov}(e^U, e^{1 - U}) = \mathbb{E}[e^Ue^{1 - U}] - \mu^2 = e - (e - 1)^2 \approx -0.2342, so ρa≈−0.968\rho_a \approx -0.968.eUe1−U=ee^Ue^{1 - U} = e is constant; Problem 9's moments for vv and μ=e−1\mu = e - 1.
  5. The pair's variance is 12(0.2420)(1−0.968)≈0.0039\tfrac12(0.2420)(1 - 0.968) \approx 0.0039, against 0.12100.1210 for two independent samples: a factor of about 3131.Step 2 with the numbers of step 4.
  6. The antithetic pair is unbiased with variance 12Var⁡(f(U))(1+ρa)\tfrac12\operatorname{Var}(f(U))(1 + \rho_a), better than two independent samples exactly when ρa<0\rho_a < 0; for eue^u, ρa≈−0.968\rho_a \approx -0.968 and the variance per pair is ≈0.0039\approx 0.0039 against 0.12100.1210For a monotone ff, f(U)f(U) and f(1−U)f(1 - U) move in opposite directions, so ρa<0\rho_a < 0 and the pair's errors partly cancel; for eue^u they cancel almost entirely because the product f(U)f(1−U)f(U)f(1 - U) is constant. For a symmetric ff with f(u)=f(1−u)f(u) = f(1 - u) the pair is one sample counted twice, ρa=1\rho_a = 1, and the pair's variance is vv, twice that of two independent samples (Mistake 4). The Gaussian version pairs ε\varepsilon with −ε-\varepsilon, and it is why the ELBO page's reparameterised gradient estimates are sometimes computed on symmetric pairs; the mechanism, inducing negative correlation between samples that are averaged, is also what stratified and quasi-Monte Carlo sampling do more systematically.

Where this goes wrong

1. Weights with the numerator and denominator swapped

Samples come from qq and the weight corrects for qq, so qq goes on top.

  1. Ep[f]=Eq[wf]\mathbb{E}_p[f] = \mathbb{E}_q[wf] with xi∼qx_i \sim qRight so far: Problem 2.
  2. “The weight is the proposal over the target: the samples are from qq, so divide out qq.”The habit that causes the mistake: Problem 2's proof multiplied and divided by qq, leaving p/qp/q under Eq\mathbb{E}_q; the proposal's density goes in the denominator because it was put into the expectation, and the target's goes in the numerator because it is what is wanted.
  3. μ^=1n∑iq(xi)p(xi)f(xi)\hat\mu = \tfrac1n\sum_i\dfrac{q(x_i)}{p(x_i)}f(x_i) with xi∼qx_i \sim qIts mean is Eq[(q/p)f]=∑xq(x)2f(x)/p(x)\mathbb{E}_q[(q/p)f] = \sum_xq(x)^2f(x)/p(x), which is not μ\mu and need not be close to it. In Problem 5 the swapped weight is e3x−9/2e^{3x - 9/2}, which exceeds e4.5≈90e^{4.5} \approx 90 at x=3x = 3 and grows from there, so the estimate of a probability of 0.001350.00135 comes out in the hundreds. The check for the orientation is Problem 6's Eq[w]=1\mathbb{E}_q[w] = 1: the weights of a correctly oriented normalised pair average to one over the samples, and the swapped ones average to Eq[q/p]≥1\mathbb{E}_q[q/p] \ge 1 with equality only when p=qp = q, by the Jensen page's Problem 5.

2. Calling the self-normalised estimator unbiased

The weighted average with normalised weights looks exactly like Problem 2's estimator, which is unbiased.

  1. μ^SN=∑iw~if(xi)/∑iw~i→μ\hat\mu_{\mathrm{SN}} = \sum_i\tilde w_if(x_i)/\sum_i\tilde w_i \to \muRight so far: Problem 6, step 3.
  2. “The numerator is unbiased for ZμZ\mu and the denominator for ZZ, so the ratio is unbiased for μ\mu.”The shortcut that causes the mistake: the expectation of a ratio is not the ratio of expectations (the Jensen page's Mistake 3).
  3. E[μ^SN]=μ\mathbb{E}[\hat\mu_{\mathrm{SN}}] = \mu for every nnFor n=1n = 1 the estimator is f(x1)f(x_1) with x1∼qx_1 \sim q, whose mean is Eq[f]\mathbb{E}_q[f], the integrand averaged under the wrong distribution (Problem 6); the check enumerates n=2n = 2 and finds the mean still off. The bias is O(1/n)O(1/n), so the self-normalised estimator is consistent, and in practice its bias is swamped by its O(1/n)O(1/\sqrt n) standard error, but it is not unbiased, and the difference matters when nn is small or when many such estimates are averaged, because averaging reduces variance and not bias (the bias–variance page). When ZZ is known, Problem 2's estimator with the normalised weights is the unbiased one.

3. A proposal narrower than the target

A proposal concentrated on the region where the target is large seems like the efficient choice.

  1. Var⁡(μ^IS)=1n(Ep[wf2]−μ2)\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \tfrac1n\big(\mathbb{E}_p[wf^2] - \mu^2\big) with w=p/qw = p/qRight so far: Problem 3.
  2. “q=N(0,14)q = \mathcal{N}(0, \tfrac14) covers the mode of p=N(0,1)p = \mathcal{N}(0, 1) and wastes no samples on the tails.”The habit that causes the mistake: the variance is an expectation under pp of p/qp/q, so what qq does in the tails of pp, where p/qp/q can be huge, is what decides it.
  3. q=N(0,14)q = \mathcal{N}(0, \tfrac14) is a valid proposal with finite variance for f=1f = 1Problem 7 with σ2=14<12\sigma^2 = \tfrac14 < \tfrac12: Eq[w2]=∫p2/q\mathbb{E}_q[w^2] = \int p^2/q diverges, because p2/q∝e−x2+2x2=ex2p^2/q \propto e^{-x^2 + 2x^2} = e^{x^2}. The estimator is still unbiased, so a run looks fine, with the weights small and the average stable, until a sample from the tail of pp arrives with a weight of ex2/2e^{x^2/2}-scale size and moves the average by more than everything before it; the empirical variance never settles, and the usual error bars are fiction. Any qq whose tails are lighter than those of ∣f∣p\lvert f\rvert p has this problem, which is why heavy-tailed proposals (a Student-tt around the mode, or a mixture with a wide component) are the standard safe choice, and why, in Problem 7's family, σ2\sigma^2 slightly above 11 is better than slightly below.

4. Antithetic pairs on a symmetric integrand

Antithetic sampling is a free variance reduction, so it is switched on by default.

  1. Var⁡(f(U)+f(1−U)2)=v2(1+ρa)\operatorname{Var}\Big(\dfrac{f(U) + f(1 - U)}2\Big) = \dfrac v2(1 + \rho_a)Right so far: Problem 10.
  2. “f(U)f(U) and f(1−U)f(1 - U) are negatively correlated because UU and 1−U1 - U are.”The habit that causes the mistake: UU and 1−U1 - U have correlation −1-1, and ff may not preserve that; Problem 10 needed ρa<0\rho_a < 0, which holds for monotone ff and not in general.
  3. For f(u)=(u−12)2f(u) = (u - \tfrac12)^2, the antithetic pair halves the variancef(1−u)=f(u)f(1 - u) = f(u), so the pair is f(U)f(U) twice, ρa=1\rho_a = 1, and the pair average has variance vv where two independent samples would have v/2v/2: the “free reduction” doubles the variance per sample. Any ff symmetric about 12\tfrac12 does this, and any ff with a symmetric component gains less than the monotone case suggests. The test is the sign of ρa\rho_a, which can be estimated from a pilot run; for the Gaussian version with ε→−ε\varepsilon \to -\varepsilon, an even integrand such as ε2\varepsilon^2 or ∥ε∥2\|\varepsilon\|^2 is the same failure, which is relevant to the ELBO page, where the KL term is even in ε\varepsilon and the antithetic pair does nothing for it.

5. Quoting the plain Monte Carlo variance for the weighted estimate

The importance-sampling estimator is unbiased for the same μ\mu, and its error is reported as if it were the plain one.

  1. E[μ^IS]=μ=E[μ^n]\mathbb{E}[\hat\mu_{\mathrm{IS}}] = \mu = \mathbb{E}[\hat\mu_n]Right so far: Problems 1 and 2.
  2. “Same target, same mean, same error bars: Var⁡p(f)/n\operatorname{Var}_p(f)/n.”The shortcut that causes the mistake: the two estimators average different random variables, f(X)f(X) under pp and w(X)f(X)w(X)f(X) under qq, and Problem 3's variance depends on qq through Ep[wf2]\mathbb{E}_p[wf^2].
  3. Var⁡(μ^IS)=Var⁡p(f)/n\operatorname{Var}(\hat\mu_{\mathrm{IS}}) = \operatorname{Var}_p(f)/n whatever the proposalIn Problem 5 the true variance is 218218 times smaller than that, and with the proposal of Mistake 3 it is infinite; neither is visible from Var⁡p(f)\operatorname{Var}_p(f). The right error bar is the empirical variance of the weighted terms w(xi)f(xi)w(x_i)f(x_i) divided by nn, and the right diagnostic for whether that empirical variance can be trusted is the ESS of Problem 8. The same slip appears with the self-normalised estimator, whose variance uses f−μf - \mu in place of ff and ESS⁡\operatorname{ESS} in place of nn, and with the policy-gradient page's importance-weighted surrogate, whose variance grows with the ratios as the policy moves away from the one that generated the data.

Print this set: importance-sampling-and-monte-carlo-estimators.pdf (problems, answers, and worked solutions on separate pages).