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/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 218 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 p and the quantity wanted is ;μ=Ep[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 q with q(x)>0 wherever .f(x)p(x)=0. The importance weight is ,w(x)=p(x)/q(x), and ,Eq,Varq are expectation and variance when .X∼q.
With x1,…,xn independent draws, the plain Monte Carlo estimator is μ^n=n1∑if(xi) for ,xi∼p, and the importance-sampling (IS) estimator is μ^IS=n1∑iw(xi)f(xi) for .xi∼q. The variance page's rules for sums of independent variables are used throughout; Var(X)=E[X2]−(E[X])2 and ,Cov(X,Y)=E[XY]−E[X]E[Y], with correlation .ρ=Cov(X,Y)/Var(X)Var(Y).
An unnormalised target is p~(x)=Zp(x) with Z=∑xp~(x) unknown; its weights are .w~=p~/q=Zw. The self-normalised estimator is .μ^SN=∑iw~(xi)f(xi)/∑iw~(xi). The effective sample size of weights w1,…,wn is .ESS=(∑iwi)2/∑iwi2.
φ(x)=e−x2/2/2π is the standard normal density and Φ its distribution function, so P(X>a)=1−Φ(a) for ;X∼N(0,1);1−Φ(3)≈1.350×10−3 and .1−Φ(6)≈9.866×10−10.N(m,σ2) has density ,φ((x−m)/σ)/σ, and N(0,σ2Id) is the d-dimensional Gaussian with independent coordinates (the Gaussian page). 1{A} is 1 when A holds and 0 otherwise. U∼U(0,1) is uniform on ,[0,1], with E[U]=21 and .Var(U)=121.
Show that μ^n is unbiased with ,Var(μ^n)=Varp(f)/n, so its standard error is .Varp(f)/n. How many more samples are needed to halve the standard error?
·
Prove the importance-sampling identity Ep[f(X)]=Eq[w(X)f(X)] with ,w=p/q, so that μ^IS is unbiased. Where does the proof use q(x)>0 wherever ,f(x)p(x)=0, and what happens if that fails?
··
Show that Var(μ^IS)=n1(Eq[w2f2]−μ2) and that .Eq[w2f2]=Ep[wf2]. When is the variance infinite even though the estimator is unbiased?
···
Show that Eq[w2f2]≥(Ep∣f∣)2 for every proposal ,q, with equality for .q∗(x)=∣f(x)∣p(x)/Ep∣f∣. Conclude that for f≥0 the proposal q∗=fp/μ gives an estimator with zero variance, and explain why it cannot be used directly.
···
Let X∼N(0,1) and .μ=P(X>3)=1−Φ(3). (a) Compute the per-sample variance Varp(1{X>3}) of plain Monte Carlo and the relative standard error Varp/n/μ for .n=1. (b) With the proposal ,q=N(3,1), show that w(x)=e9/2−3x and that .Eq[w2f2]=e9(1−Φ(6)). (c) Compute the IS variance per sample and the ratio of the two variances.
··
With an unnormalised target p~=Zp and weights ,w~=p~/q, show that Eq[w~]=Z and ,Eq[w~f]=Zμ, so the self-normalised estimator μ^SN=∑iw~if(xi)/∑iw~i converges to .μ. Show that for finite n it is biased in general, by computing E[μ^SN] for .n=1.
···
Let p=N(0,Id) and q=N(0,σ2Id) with .σ2>21. Show that in one dimension ,Eq[w2]=Ep[w]=2σ2−1σ2, hence in d dimensions ,Eq[w2]=(2σ2−1σ2)d, and evaluate for σ2=2 and .d=10,50. What happens for ?σ2≤21?
··
For positive weights ,w1,…,wn, show that ,1≤ESS=∑iwi2(∑iwi)2≤n, with ESS=n exactly when all weights are equal and ESS→1 when one weight dominates. Show that with normalised weights ,Wi=wi/∑jwj,,ESS=1/∑iWi2, and that for large ,n,ESS/n≈1/Eq[w2] when w=p/q with p and q normalised.
··
Let g be a function with known mean .Ep[g]=γ. Show that the control-variate estimator μ^c=n1∑i(f(xi)−c(g(xi)−γ)) is unbiased for every ,c, that its variance is ,n1(Var(f)−2cCov(f,g)+c2Var(g)), minimised at c∗=Cov(f,g)/Var(g) with value .n1Var(f)(1−ρ2). Evaluate for ,f(U)=eU,g(U)=U with .U∼U(0,1).
··
For ,U∼U(0,1), the antithetic pair estimator uses .21(f(U)+f(1−U)). Show that it is unbiased and that its variance is 21Var(f(U))(1+ρa) with ,ρa=Corr(f(U),f(1−U)), so it beats two independent samples exactly when .ρa<0. Evaluate for .f(u)=eu.
Answers
,E[μ^n]=μ,,Var(μ^n)=Varp(f)/n, standard error ;Varp(f)/n; halving it takes four times the samples
Ep[f]=Eq[wf] and μ^IS is unbiased, provided q>0 wherever ;fp=0; if the condition fails, the estimator is biased by the missing mass ∑x:q(x)=0p(x)f(x)
;Var(μ^IS)=n1(Eq[w2f2]−μ2)=n1(Ep[wf2]−μ2); it is infinite when Ep[wf2]=Ep[f2p/q] diverges, which happens when q has lighter tails than f2p
Eq[w2f2]≥(Ep∣f∣)2 with equality at ;q∗∝∣f∣p; for ,f≥0,q∗=fp/μ has zero variance; it cannot be used because normalising it requires ,μ, the unknown
(a) ,Varp=μ(1−μ)≈1.348×10−3, relative error ≈27.2 per sample; (b) w(x)=e9/2−3x and ;Eq[w2f2]=e9(1−Φ(6))≈7.994×10−6; (c) IS variance ≈6.17×10−6 per sample, 218 times smaller, relative error ≈1.84 per sample
,Eq[w~]=Z,,Eq[w~f]=Zμ, so ;μ^SN→μ; but E[μ^SN]=μ for finite ,n, and for n=1 it equals Eq[f]
Eq[w2]=(2σ2−1σ2)d for ;σ2>21; for σ2=2 it is ≈4.2 at d=10 and ≈1.3×103 at ;d=50; for σ2≤21 it is infinite
,1≤ESS≤n, equal to n for equal weights and near 1 for one dominant weight; ;ESS=1/∑iWi2;ESS/n≈1/Eq[w2]
μ^c is unbiased for every ;c;,Var(μ^c)=n1(Var(f)−2cCov(f,g)+c2Var(g)), minimised at c∗=Cov(f,g)/Var(g) with value ;n1Var(f)(1−ρ2); for eU with control :U:,c∗≈1.690,,ρ2≈0.984, variance 0.2420→0.0039
The antithetic pair is unbiased with variance ,21Var(f(U))(1+ρa), better than two independent samples exactly when ;ρa<0; for ,eu,ρa≈−0.968 and the variance per pair is ≈0.0039 against 0.1210
Worked solutions
Problem 1
Show that μ^n is unbiased with ,Var(μ^n)=Varp(f)/n, so its standard error is .Varp(f)/n. How many more samples are needed to halve the standard error?
.E[μ^n]=n1∑iEp[f(xi)]=n1⋅nμ=μ.Linearity; every xi is a draw from .p.
.Var(μ^n)=n21∑iVarp(f(xi))=nVarp(f).Independence makes the variance of the sum the sum of the variances; Var(aX)=a2Var(X) with .a=1/n.
The standard error Varp(f)/n is halved when n is multiplied by .4..1/(4n)=211/n.
,E[μ^n]=μ,,Var(μ^n)=Varp(f)/n, standard error ;Varp(f)/n; halving it takes four times the samplesMonte Carlo converges at the rate 1/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 Varp(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}] the variance is μ(1−μ) and the relative error ,(1−μ)/(nμ), 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)] with ,w=p/q, so that μ^IS is unbiased. Where does the proof use q(x)>0 wherever ,f(x)p(x)=0, and what happens if that fails?
.Ep[f]=∑xp(x)f(x)=∑x:q(x)>0q(x)q(x)p(x)f(x).Multiply and divide by ,q(x), which is allowed only where ;q(x)>0; the terms with q(x)=0 are dropped from the sum.
The dropped terms have f(x)p(x)=0 by the support condition, so they contributed nothing.The condition says q=0 only where the summand p(x)f(x) is already zero.
.∑x:q(x)>0q(x)w(x)f(x)=Eq[w(X)f(X)].A q-weighted sum over the support of q is an expectation under .q.
.E[μ^IS]=n1∑iEq[w(xi)f(xi)]=μ.Linearity with ,xi∼q, then step 3.
Ep[f]=Eq[wf] and μ^IS is unbiased, provided q>0 wherever ;fp=0; if the condition fails, the estimator is biased by the missing mass ∑x:q(x)=0p(x)f(x)The identity is a change of measure: samples from q are reweighted to look like samples from ,p, and the weight is large where q undersamples p and small where it oversamples. The condition is exactly what the ELBO page's 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 p 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)=n1(Eq[w2f2]−μ2) and that .Eq[w2f2]=Ep[wf2]. When is the variance infinite even though the estimator is unbiased?
.Var(μ^IS)=n1Varq(wf)=n1(Eq[(wf)2]−(Eq[wf])2).Problem 1's argument with wf in place of f and q in place of ;p; the variance as second moment minus squared mean.
,Eq[wf]=μ, so .Var(μ^IS)=n1(Eq[w2f2]−μ2).Problem 2.
.Eq[w2f2]=∑xq(x)q(x)2p(x)2f(x)2=∑xp(x)q(x)p(x)f(x)2=Ep[wf2].Cancel one q against one of the two in ;w2; what remains is a p-weighted sum of .wf2.
;Var(μ^IS)=n1(Eq[w2f2]−μ2)=n1(Ep[wf2]−μ2); it is infinite when Ep[wf2]=Ep[f2p/q] diverges, which happens when q has lighter tails than f2pThe variance is controlled by :Ep[wf2]: the weight p/q averaged under p and amplified by .f2. Where q is much smaller than p the weight is huge and the rare samples that land there dominate the average; if p/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] for Gaussians and finds exactly this: a proposal narrower than the target by a factor of 2 in standard deviation already gives infinite variance (Mistake 3). The rule of thumb is that q must have tails at least as heavy as p's, and Problem 4 says what the best q is.
Problem 4
Show that Eq[w2f2]≥(Ep∣f∣)2 for every proposal ,q, with equality for .q∗(x)=∣f(x)∣p(x)/Ep∣f∣. Conclude that for f≥0 the proposal q∗=fp/μ gives an estimator with zero variance, and explain why it cannot be used directly.
.Eq[w2f2]=Eq[(w∣f∣)2]≥(Eq[w∣f∣])2.;f2=∣f∣2; then Jensen's inequality for the convex function ,t↦t2, the Jensen page's Problem 5(a): a second moment is at least the squared mean.
.Eq[w∣f∣]=Ep∣f∣.Problem 2's identity applied to .∣f∣.
For :q∗=∣f∣p/Ep∣f∣:,w∗∣f∣=q∗p∣f∣=Ep∣f∣, a constant, so equality holds in step 1.p/q∗=Ep∣f∣/∣f∣ wherever ,f=0, and the ∣f∣ cancels; a constant has zero Jensen gap. q∗ is a valid proposal: nonnegative, sums to one, and positive wherever .fp=0.
For :f≥0:,Ep∣f∣=μ,,q∗=fp/μ, and every sample gives w∗(x)f(x)=μ exactly, so .Var(μ^IS)=0.Steps 2 and 3 with ;∣f∣=f; the estimator is the constant μ whatever is drawn.
Eq[w2f2]≥(Ep∣f∣)2 with equality at ;q∗∝∣f∣p; for ,f≥0,q∗=fp/μ has zero variance; it cannot be used because normalising it requires ,μ, the unknownThe best proposal is not the target but the target reshaped by the integrand: sample where ∣f∣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 f taking both signs the minimum variance is ,(Ep∣f∣)2−μ2>0, so the floor is not zero. Mistake 4 on the Jensen page is the reason q=p is usually far from optimal, which Problem 5 quantifies.
Problem 5
Let X∼N(0,1) and .μ=P(X>3)=1−Φ(3). (a) Compute the per-sample variance Varp(1{X>3}) of plain Monte Carlo and the relative standard error Varp/n/μ for .n=1. (b) With the proposal ,q=N(3,1), show that w(x)=e9/2−3x and that .Eq[w2f2]=e9(1−Φ(6)). (c) Compute the IS variance per sample and the ratio of the two variances.
(a) f=1{X>3} has ,f2=f, so ,Varp(f)=μ−μ2=μ(1−μ)≈1.348×10−3, and the relative standard error at n=1 is .(1−μ)/μ≈27.2.An indicator is Bernoulli with success probability ;μ≈1.350×10−3; divide the standard error by .μ.
(b) .w(x)=φ(x−3)φ(x)=exp(−2x2+2(x−3)2)=exp(2−6x+9)=e9/2−3x.The two densities share the ;1/2π; expand (x−3)2=x2−6x+9 and the x2 cancels.
.Eq[w2f2]=∫3∞φ(x−3)φ(x)2dx=∫3∞2π1exp(−x2+2(x−3)2)dx.Problem 3's Eq[w2f2]=∫q(p/q)2f2=∫p2/q over the region where ;f=1; one factor of 2π survives.
.−x2+2x2−6x+9=−2x2−3x+29=−2(x+3)2+9.Complete the square: ,−21(x2+6x)=−21(x+3)2+29, plus the 29 already there.
.Eq[w2f2]=e9∫3∞φ(x+3)dx=e9∫6∞φ(u)du=e9(1−Φ(6))≈8103.1×9.866×10−10≈7.994×10−6.Substitute ;u=x+3; the integral of φ from 6 is the upper tail.
(c) ,Varq(wf)=7.994×10−6−μ2≈7.994×10−6−1.822×10−6=6.17×10−6, and the ratio is ;1.348×10−3/6.17×10−6≈218; the relative standard error at n=1 is .6.17×10−6/μ≈1.84.Problem 3; .μ2≈(1.350×10−3)2.
(a) ,Varp=μ(1−μ)≈1.348×10−3, relative error ≈27.2 per sample; (b) w(x)=e9/2−3x and ;Eq[w2f2]=e9(1−Φ(6))≈7.994×10−6; (c) IS variance ≈6.17×10−6 per sample, 218 times smaller, relative error ≈1.84 per samplePlain Monte Carlo sees a success once in 741 draws and needs about n≈74,000 samples for a 10% relative error; the shifted proposal lands half its draws in the tail with weights no larger than ,e9/2−9=e−4.5, and reaches the same accuracy with .n≈340. The gain comes from sampling where fp 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 .218.
Problem 6
With an unnormalised target p~=Zp and weights ,w~=p~/q, show that Eq[w~]=Z and ,Eq[w~f]=Zμ, so the self-normalised estimator μ^SN=∑iw~if(xi)/∑iw~i converges to .μ. Show that for finite n it is biased in general, by computing E[μ^SN] for .n=1.
.Eq[w~]=∑xq(x)q(x)Zp(x)=Z∑xp(x)=Z.The q cancels and p sums to one.
.Eq[w~f]=ZEq[wf]=Zμ.w~=Zw and Problem 2.
,μ^SN=n1∑iw~in1∑iw~if(xi), and as n→∞ the numerator tends to Zμ and the denominator to ,Z, so the ratio tends to .μ.Divide top and bottom by ;n; each is a plain Monte Carlo average under q and converges to its mean (the law of large numbers); the ratio of the limits is the limit of the ratio because .Z>0.
For :n=1:μ^SN=w~(x1)w~(x1)f(x1)=f(x1) with ,x1∼q, so E[μ^SN]=Eq[f]=μ in general.The single weight cancels; the estimate is the integrand at a draw from the proposal, not from the target.
,Eq[w~]=Z,,Eq[w~f]=Zμ, so ;μ^SN→μ; but E[μ^SN]=μ for finite ,n, and for n=1 it equals Eq[f]Self-normalisation is what makes importance sampling usable for posteriors, energy-based models and any p~ whose normaliser is unknown: the unknown Z cancels in the ratio. The price is bias of order ,1/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=2 to measure it. The bias vanishes faster than the standard error, which is ,O(1/n), so it is harmless for large n and the variance of Problem 3, with f replaced by ,f−μ, is the right measure of quality; Mistake 2 overstates the guarantee.
Problem 7
Let p=N(0,Id) and q=N(0,σ2Id) with .σ2>21. Show that in one dimension ,Eq[w2]=Ep[w]=2σ2−1σ2, hence in d dimensions ,Eq[w2]=(2σ2−1σ2)d, and evaluate for σ2=2 and .d=10,50. What happens for ?σ2≤21?
In one dimension, .Eq[w2]=∫q(x)p(x)2dx=∫(2πσ2)−1/2e−x2/(2σ2)(2π)−1e−x2dx=2πσ∫exp(−x2(1−2σ21))dx.Problem 3's ;Eq[w2]=Ep[w]=∫p2/q; collect the constants and the exponents.
∫e−αx2dx=π/α for ,α>0, with .α=1−2σ21=2σ22σ2−1.The Gaussian integral; it converges only when ,α>0, that is .σ2>21.
In d dimensions p and q factor over coordinates, so w(x)=∏j=1dwj(xj) and .Eq[w2]=∏jEq[wj2]=(2σ2−1σ2)d.Independent coordinates: the expectation of a product of independent factors is the product of the expectations (the variance page).
:σ2=2:,32≈1.1547, so Eq[w2]≈4.2 for d=10 and ≈1.3×103 for .d=50.1.154710≈4.21 and .1.154750≈1329.
Eq[w2]=(2σ2−1σ2)d for ;σ2>21; for σ2=2 it is ≈4.2 at d=10 and ≈1.3×103 at ;d=50; for σ2≤21 it is infiniteThe base σ2/2σ2−1 equals 1 only at σ2=1 and exceeds 1 on both sides, so any mismatch between proposal and target is raised to the power :d: the weights' variance, ,Eq[w2]−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/1329 at ,d=50, so a million samples are worth about .750. Below σ2=21 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 d is large.
Problem 8
For positive weights ,w1,…,wn, show that ,1≤ESS=∑iwi2(∑iwi)2≤n, with ESS=n exactly when all weights are equal and ESS→1 when one weight dominates. Show that with normalised weights ,Wi=wi/∑jwj,,ESS=1/∑iWi2, and that for large ,n,ESS/n≈1/Eq[w2] when w=p/q with p and q normalised.
,(∑iwi)2=∑iwi2+∑i=jwiwj≥∑iwi2, so .ESS≥1.Expand the square; the cross terms are positive.
,(∑iwi)2=(∑i1⋅wi)2≤(∑i12)(∑iwi2)=n∑iwi2, so .ESS≤n.Cauchy–Schwarz for the vectors 1 and ;w; equivalently the Jensen page's (E[W])2≤E[W2] for the uniform distribution on the .wi.
Equality in step 2 holds when w is proportional to ,1, all weights equal; and if w1≫wi for i>1 then both (∑iwi)2 and ∑iwi2 are ,≈w12, so .ESS≈1.The Cauchy–Schwarz equality case; dominant-term approximation.
.nESS=n1∑iwi2(n1∑iwi)2→Eq[w2](Eq[w])2=Eq[w2]1.Divide top and bottom by n2 and ;n; each average converges to its mean by the law of large numbers, and Eq[w]=1 for normalised p and q (Problem 6 with ).Z=1).
,1≤ESS≤n, equal to n for equal weights and near 1 for one dominant weight; ;ESS=1/∑iWi2;ESS/n≈1/Eq[w2]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 Varp(f)/ESS rather than .Varp(f)/n. It is the diagnostic for Problem 7's degeneracy: with Eq[w2]≈1329 at ,d=50, a million draws have an ESS near ,750, and a histogram of the normalised weights shows a handful of samples carrying almost all the mass. Sequential Monte Carlo resamples whenever ESS falls below a threshold such as .n/2. The ESS uses only the weights, not ,f, so it can report a healthy sample for an integrand that is badly estimated, which is the limitation Problem 4's ∣f∣p points at.
Problem 9
Let g be a function with known mean .Ep[g]=γ. Show that the control-variate estimator μ^c=n1∑i(f(xi)−c(g(xi)−γ)) is unbiased for every ,c, that its variance is ,n1(Var(f)−2cCov(f,g)+c2Var(g)), minimised at c∗=Cov(f,g)/Var(g) with value .n1Var(f)(1−ρ2). Evaluate for ,f(U)=eU,g(U)=U with .U∼U(0,1).
.E[f(xi)−c(g(xi)−γ)]=μ−c(γ−γ)=μ.Linearity; Ep[g]=γ by assumption, so the correction has mean zero for every .c.
,Var(f−c(g−γ))=Var(f)−2cCov(f,g)+c2Var(g), and dividing by n gives the variance of the average.The variance page's ;Var(X−cY)=Var(X)−2cCov(X,Y)+c2Var(Y); the constant γ changes nothing; Problem 1 for the .1/n.
,dcd(⋯)=−2Cov(f,g)+2cVar(g)=0⟺c∗=Var(g)Cov(f,g), a minimum since the coefficient of c2 is positive.Differentiate the quadratic in .c.
At :c∗:.Var(f)−Var(g)Cov(f,g)2=Var(f)(1−Var(f)Var(g)Cov(f,g)2)=Var(f)(1−ρ2).Substitute ;c∗; the bracket is 1−ρ2 by the definition of correlation.
:U∼U(0,1):,E[eU]=e−1,,E[e2U]=21(e2−1), so ;Var(eU)=21(e2−1)−(e−1)2≈0.2420;,E[UeU]=1, so ;Cov(eU,U)=1−21(e−1)≈0.1409;.Var(U)=121.,∫01eudu=e−1,,∫01e2udu=21(e2−1),∫01ueudu=[ueu−eu]01=1 by parts (the integration page).
,c∗=0.1409⋅12≈1.690,,ρ2=0.2420/120.14092≈0.984, and the variance falls from 0.2420 to ,0.2420(1−0.984)≈0.0039, a factor of about .61.Steps 3 and 4 with the numbers of step 5.
μ^c is unbiased for every ;c;,Var(μ^c)=n1(Var(f)−2cCov(f,g)+c2Var(g)), minimised at c∗=Cov(f,g)/Var(g) with value ;n1Var(f)(1−ρ2); for eU with control :U:,c∗≈1.690,,ρ2≈0.984, variance 0.2420→0.0039A control variate subtracts the part of f that a known-mean function can explain, which is a regression of f on :g:c∗ is the least-squares slope and 1−ρ2 the unexplained fraction. Because c does not affect the mean, it can be estimated from the same samples at a cost of O(1/n) bias, which is how it is done in practice. This is the policy-gradient page's baseline in disguise: there g is the score function, whose mean is known to be zero, and c∗ is the optimal baseline. Any c strictly between 0 and 2c∗ helps and any c outside that range hurts, so a default of c=1 is right only when .c∗≈1.
Problem 10
For ,U∼U(0,1), the antithetic pair estimator uses .21(f(U)+f(1−U)). Show that it is unbiased and that its variance is 21Var(f(U))(1+ρa) with ,ρa=Corr(f(U),f(1−U)), so it beats two independent samples exactly when .ρa<0. Evaluate for .f(u)=eu.
,1−U∼U(0,1), so E[f(1−U)]=E[f(U)]=μ and the pair average has mean .21(μ+μ)=μ.Reflecting a uniform variable about 21 gives a uniform variable; linearity.
Var(2f(U)+f(1−U))=41(Var(f(U))+Var(f(1−U))+2Cov(f(U),f(1−U)))=41(2v+2ρav)=2v(1+ρa) with .v=Var(f(U)).The variance page's variance of a sum; both terms have variance v by step 1, and the covariance is ρav by the definition of correlation.
Two independent samples have variance ,v/2, so the pair is better exactly when ,1+ρa<1, that is .ρa<0.Compare 2v(1+ρa) with .2v.
For :f(u)=eu:v=21(e2−1)−(e−1)2≈0.2420 and ,Cov(eU,e1−U)=E[eUe1−U]−μ2=e−(e−1)2≈−0.2342, so .ρa≈−0.968.eUe1−U=e is constant; Problem 9's moments for v and .μ=e−1.
The pair's variance is ,21(0.2420)(1−0.968)≈0.0039, against 0.1210 for two independent samples: a factor of about .31.Step 2 with the numbers of step 4.
The antithetic pair is unbiased with variance ,21Var(f(U))(1+ρa), better than two independent samples exactly when ;ρa<0; for ,eu,ρa≈−0.968 and the variance per pair is ≈0.0039 against 0.1210For a monotone ,f,f(U) and f(1−U) move in opposite directions, so ρa<0 and the pair's errors partly cancel; for eu they cancel almost entirely because the product f(U)f(1−U) is constant. For a symmetric f with f(u)=f(1−u) the pair is one sample counted twice, ,ρa=1, and the pair's variance is ,v, twice that of two independent samples (Mistake 4). The Gaussian version pairs ε with ,−ε, 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 q and the weight corrects for ,q, so q goes on top.
Ep[f]=Eq[wf] with xi∼qRight so far: Problem 2.
“The weight is the proposal over the target: the samples are from ,q, so divide out .q.”The habit that causes the mistake: Problem 2's proof multiplied and divided by ,q, leaving p/q under ;Eq; 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.
μ^=n1∑ip(xi)q(xi)f(xi) with xi∼qIts mean is ,Eq[(q/p)f]=∑xq(x)2f(x)/p(x), which is not μ and need not be close to it. In Problem 5 the swapped weight is ,e3x−9/2, which exceeds e4.5≈90 at x=3 and grows from there, so the estimate of a probability of 0.00135 comes out in the hundreds. The check for the orientation is Problem 6's :Eq[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 with equality only when ,p=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.
μ^SN=∑iw~if(xi)/∑iw~i→μRight so far: Problem 6, step 3.
“The numerator is unbiased for Zμ and the denominator for ,Z, so the ratio is unbiased for .μ.”The shortcut that causes the mistake: the expectation of a ratio is not the ratio of expectations (the Jensen page's Mistake 3).
E[μ^SN]=μ for every nFor n=1 the estimator is f(x1) with ,x1∼q, whose mean is ,Eq[f], the integrand averaged under the wrong distribution (Problem 6); the check enumerates n=2 and finds the mean still off. The bias is ,O(1/n), so the self-normalised estimator is consistent, and in practice its bias is swamped by its O(1/n) standard error, but it is not unbiased, and the difference matters when n is small or when many such estimates are averaged, because averaging reduces variance and not bias (the bias–variance page). When Z 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.
Var(μ^IS)=n1(Ep[wf2]−μ2) with w=p/qRight so far: Problem 3.
“q=N(0,41) covers the mode of p=N(0,1) and wastes no samples on the tails.”The habit that causes the mistake: the variance is an expectation under p of ,p/q, so what q does in the tails of ,p, where p/q can be huge, is what decides it.
q=N(0,41) is a valid proposal with finite variance for f=1Problem 7 with :σ2=41<21:Eq[w2]=∫p2/q diverges, because .p2/q∝e−x2+2x2=ex2. 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 p arrives with a weight of ex2/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 q whose tails are lighter than those of ∣f∣p has this problem, which is why heavy-tailed proposals (a Student-t around the mode, or a mixture with a wide component) are the standard safe choice, and why, in Problem 7's family, σ2 slightly above 1 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.
Var(2f(U)+f(1−U))=2v(1+ρa)Right so far: Problem 10.
“f(U) and f(1−U) are negatively correlated because U and 1−U are.”The habit that causes the mistake: U and 1−U have correlation ,−1, and f may not preserve that; Problem 10 needed ,ρa<0, which holds for monotone f and not in general.
For ,f(u)=(u−21)2, the antithetic pair halves the variance,f(1−u)=f(u), so the pair is f(U) twice, ,ρa=1, and the pair average has variance v where two independent samples would have :v/2: the “free reduction” doubles the variance per sample. Any f symmetric about 21 does this, and any f with a symmetric component gains less than the monotone case suggests. The test is the sign of ,ρa, which can be estimated from a pilot run; for the Gaussian version with ,ε→−ε, an even integrand such as ε2 or ∥ε∥2 is the same failure, which is relevant to the ELBO page, where the KL term is even in ε 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 ,μ, and its error is reported as if it were the plain one.
E[μ^IS]=μ=E[μ^n]Right so far: Problems 1 and 2.
“Same target, same mean, same error bars: .Varp(f)/n.”The shortcut that causes the mistake: the two estimators average different random variables, f(X) under p and w(X)f(X) under ,q, and Problem 3's variance depends on q through .Ep[wf2].
Var(μ^IS)=Varp(f)/n whatever the proposalIn Problem 5 the true variance is 218 times smaller than that, and with the proposal of Mistake 3 it is infinite; neither is visible from .Varp(f). The right error bar is the empirical variance of the weighted terms w(xi)f(xi) divided by ,n, 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−μ in place of f and ESS in place of ,n, 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.