7. Variance Reduction Techniques

Ways to get tighter confidence intervals from the same simulation budget: common random numbers, antithetic variates, control variates, indirect estimation, stratified sampling, conditioning, and importance sampling.

Estimate μ=E[X]\mu = \mathbf{E}[X] from nn i.i.d. replications X1,…,XnX_1, \dots, X_n: the point estimator is μ^=Xˉ(n)\hat\mu = \bar X(n), and the 100(1−α)%100(1-\alpha)\% CI is

[μ^−tα/2, n−1σ2n,    μ^+tα/2, n−1σ2n ],σ2=Var⁡(X)\left[\hat\mu - t_{\alpha/2,\,n-1}\sqrt{\frac{\sigma^2}{n}},\;\; \hat\mu + t_{\alpha/2,\,n-1}\sqrt{\frac{\sigma^2}{n}}\,\right], \qquad \sigma^2 = \operatorname{Var}(X)

(in practice σ2\sigma^2 is estimated by S2(n)S^2(n), as in §6.3).

Goal: reduce the variance σ2\sigma^2 at no additional simulation cost. The half-width is proportional to σ/n\sigma/\sqrt n, so there are two ways to make the CI narrower: more replications (a larger nn, which costs computer time; by §6.3.3 the required nn is proportional to σ2\sigma^2), or a smaller σ2\sigma^2 for the same nn. Variance reduction techniques do the second, by using the random numbers more cleverly: halving σ2\sigma^2 is as good as doubling nn, but costs (almost) nothing.

7.1. Common Random Numbers (CRN)

Applicable when comparing two or more alternative systems.

Idea: we want the simulation experiments to be conducted under the same “experimental conditions”, i.e. with the same random numbers, so that an observed difference in performance is due to the difference between the systems and not to the random numbers (“luck”) of each simulation.

Ex: 1. M/M/1, 2. M/M/2.

XiX_i = average delay in queue in system i=1,2i = 1, 2. Goal: estimate ζ=E[Z]=E[X1−X2]\zeta = \mathbf{E}[Z] = \mathbf{E}[X_1 - X_2], where Z=X1−X2Z = X_1 - X_2.

Repeat the experiment nn times (each time simulating both systems) and get Z1,Z2,…,ZnZ_1, Z_2, \dots, Z_n, with Zj=X1j−X2jZ_j = X_{1j} - X_{2j}. The point estimator is

Zˉ(n)=1n∑j=1nZj,\bar Z(n) = \frac{1}{n}\sum_{j=1}^n Z_j ,

and the half-width of the 100(1−α)%100(1-\alpha)\% CI is

tα/2, n−1Var⁡(Z1)nt_{\alpha/2,\,n-1}\sqrt{\frac{\operatorname{Var}(Z_1)}{n}}

(with Var⁡(Z1)\operatorname{Var}(Z_1) estimated by the sample variance of the ZjZ_j’s), where

Var⁡(Z1)=Var⁡(X1−X2)=Var⁡(X1)+Var⁡(X2)−2cov⁡(X1,X2).\operatorname{Var}(Z_1) = \operatorname{Var}(X_1 - X_2) = \operatorname{Var}(X_1) + \operatorname{Var}(X_2) - 2\operatorname{cov}(X_1, X_2) .

If the two systems are simulated with independent random numbers, cov⁡(X1,X2)=0\operatorname{cov}(X_1, X_2) = 0. We want cov⁡(X1,X2)>0\operatorname{cov}(X_1, X_2) > 0: then the last term is subtracted and Var⁡(Z1)\operatorname{Var}(Z_1) is smaller than with independent sampling.

CRN is a technique that tries to induce this positive correlation by using the same random numbers to simulate all alternative systems. (A first example was in §6.3.3: for M/M/1 vs M/D/1, CRN reduced Var⁡(Zj)\operatorname{Var}(Z_j) from 3.79 to 2.23.)

When does CRN work?

u1,u2,…,un  ⟶  simulation program  ⟶  Xu_1, u_2, \dots, u_n \;\longrightarrow\; \boxed{\text{simulation program}} \;\longrightarrow\; X

The simulation program is a (complicated, but deterministic) function of the random numbers: X=X(u1,u2,… )X = X(u_1, u_2, \dots). To picture it, pretend that XX depends on a single random number: X=X(u)X = X(u). CRN feeds the same uu to both systems, so the question is how X1(u)X_1(u) and X2(u)X_2(u) react to uu.

Desired cases: X1X_1 and X2X_2 both increase, or both decrease, in uu.

X₁(u)X₂(u)u01both increase in uX₁(u)X₂(u)u01both decrease in uX₁ and X₂ move together: a large u makes both large (or both small) ⇒ cov(X₁, X₂) > 0

Backfire: one increases while the other decreases. Then CRN induces a negative correlation and increases the variance.

X₁(u)X₂(u)u01X₁ decreases, X₂ increases in uX₁ and X₂ move in opposite directions⇒ cov(X₁, X₂) < 0⇒ Var(X₁ − X₂) = Var(X₁) + Var(X₂) − 2 cov(X₁, X₂) is larger than with independent random numbers: CRN increases the variance

How can we guarantee the desired case? There is no guarantee: the simulation program is far too complicated to check whether it is monotone in every random number. But we can make it very likely, by looking at the two parts that turn random numbers into an output:

flowchart LR
    U(["uᵢ's"]) --> RVG
    subgraph H["X(u₁, u₂, …): monotone if each box is monotone"]
        RVG["RVG: random numbers → random variates"] --> SP["simulation program: random variates → X"]
    end
    SP --> X(["X"])

The composition of two monotone functions is also monotone, so each part has to be monotone:

  • The random variate generator (RVG) is under our control. Use the inverse transform (§4.2.1): X=F−1(u)X = F^{-1}(u) is increasing in uu, so a larger uu always gives a larger variate. Other methods are not monotone; e.g. with acceptance–rejection a slightly different uu can change which candidate is accepted, and even how many uu’s are used. So always use the inverse transform in the CRN approach.
  • The simulation program (the model itself) should react monotonically to the variates. This we cannot control, but it can usually be checked by reasoning about the model, as in the queueing example below.

Queueing simulation (service times in the M/M/1 and the M/M/2 system, both generated by the inverse transform from the same uu):

u↑  ⇒  S1↑ and S2↑  ⇒  X1↑ and X2↑u \uparrow \;\Rightarrow\; S_1 \uparrow \text{ and } S_2 \uparrow \;\Rightarrow\; X_1 \uparrow \text{ and } X_2 \uparrow

(longer service times mean longer delays in both systems). Likewise for interarrival times: u↑⇒A1↑u \uparrow \Rightarrow A_1 \uparrow and A2↑⇒X1↓A_2 \uparrow \Rightarrow X_1 \downarrow and X2↓X_2 \downarrow. Both systems react in the same direction to every random number, which is the desired case.

Synchronization: it is not enough to use the same stream of random numbers to simulate the alternative systems. We must match up (synchronize) the random numbers across the different systems, to ensure that the same uu is used for the same purpose in the different systems.

Ex: one stream u1,u2,…u_1, u_2, \dots is used for everything, each random number being taken when the program needs one (A = used for an interarrival time, S = used for a service time):

random number used in M/M/1 for used in M/M/2 for
u1u_1 A A
u2u_2 A A
⋮\vdots ⋮\vdots ⋮\vdots
u7u_7 S S
u8u_8 A A
→u9\to u_9 A S

Up to u8u_8 the two systems happen to need the random numbers for the same purposes. But the systems are different, so their events eventually happen in a different order: e.g. in the M/M/2 system a waiting customer starts service on the second server (which needs a service time) while in the M/M/1 system the next event is an arrival (which needs an interarrival time). From u9u_9 on, the same uu is used as an interarrival time in one system and as a service time in the other, and all later random numbers are shifted as well. The two systems are no longer driven by the same inputs, and most of the positive correlation is lost.

General synchronization methods:

  • Dedicate a stream of random numbers to a particular source of randomness (§3.3), e.g. stream 1 for all interarrival times and stream 2 for all service times. Then the kk-th interarrival time always uses the kk-th number of stream 1, in both systems, no matter in which order the events happen.
  • Use the inverse transform method to generate random variates: it guarantees monotonicity and is one-to-one (exactly one uu per variate). With acceptance–rejection, the number of uu’s used per variate is random, so the streams drift out of synchronization even when they are dedicated.
  • Generate all random variates beforehand, e.g. generate customer jj’s service time when customer jj arrives and attach it to the customer. Then customer jj has the same service time in both systems, even if it starts service at different times or (with other queue disciplines) in a different order.

E.g. with numbers (my choice): arrival rate 1; system 1 has one server with mean service time 0.9, system 2 has two servers with mean service time 1.8 each (the same total capacity); XiX_i = average delay in queue of the first 100 customers, starting empty; 3000 replications of each version:

random numbers corr⁡(X1,X2)\operatorname{corr}(X_1, X_2) Var⁡(Z)\operatorname{Var}(Z) change
independent for the two systems 0.01 16.4
CRN, one shared stream, not synchronized 0.08 15.7 −4%
CRN, dedicated streams for arrivals and services (synchronized) 0.997 0.057 −99.7%
backfire: system 2 uses 1−u1 - u wherever system 1 uses uu −0.36 23.3 +42%

Here E[Z]≈0.43\mathbf{E}[Z] \approx 0.43 (the M/M/2 system has slightly shorter delays). With n=10n = 10 replications, the 95% CI half-width is 2.26216.4/10=2.92.262\sqrt{16.4/10} = 2.9 with independent random numbers (the CI cannot even tell which system is better), but 2.2620.057/10=0.172.262\sqrt{0.057/10} = 0.17 with synchronized CRN. Note that sharing a stream without synchronization gives almost nothing: CRN only works when the same uu is used for the same purpose.

7.2. Antithetic Variates (AV)

Used when investigating the performance of a single system.

Idea: make the replications in pairs, such that a large observation in the first replication of a pair tends to be matched by a small one in the second; their average is then closer to μ\mu than the average of two independent replications.

  • Use uiu_i’s for the first replication of a pair to get X(1)X^{(1)}.
  • Use 1−ui1 - u_i’s for the second replication, in the same roles, to get X(2)X^{(2)}.

Since 1−U∼U(0,1)1 - U \sim U(0, 1) when U∼U(0,1)U \sim U(0, 1), both are valid replications: E[X(1)]=E[X(2)]=μ\mathbf{E}[X^{(1)}] = \mathbf{E}[X^{(2)}] = \mu and Var⁡(X(1))=Var⁡(X(2))=σ2\operatorname{Var}(X^{(1)}) = \operatorname{Var}(X^{(2)}) = \sigma^2. (X(1),X(2)X^{(1)}, X^{(2)} are the two runs of one pair, not two systems as in §7.1.) Use the pair average as the observation:

X=X(1)+X(2)2,E[X]=μ,X = \frac{X^{(1)} + X^{(2)}}{2}, \qquad \mathbf{E}[X] = \mu,

Var⁡(X)=14[Var⁡(X(1))+Var⁡(X(2))+2cov⁡(X(1),X(2))]=σ2+cov⁡(X(1),X(2))2.\operatorname{Var}(X) = \frac{1}{4}\Big[\operatorname{Var}(X^{(1)}) + \operatorname{Var}(X^{(2)}) + 2\operatorname{cov}(X^{(1)}, X^{(2)})\Big] = \frac{\sigma^2 + \operatorname{cov}(X^{(1)}, X^{(2)})}{2} .

With two independent replications the covariance would be 0 and the variance σ2/2\sigma^2/2. So AV helps exactly when cov⁡(X(1),X(2))<0\operatorname{cov}(X^{(1)}, X^{(2)}) < 0. With nn pairs (different pairs use independent random numbers), the pair averages X1,…,XnX_1, \dots, X_n are i.i.d., and the CI is computed from them as usual, with tα/2, n−1t_{\alpha/2,\,n-1}.

Why the covariance is negative:

ui↑  ⇒  X(1)↑,1−ui↓  ⇒  X(2)↓.u_i \uparrow \;\Rightarrow\; X^{(1)} \uparrow, \qquad 1 - u_i \downarrow \;\Rightarrow\; X^{(2)} \downarrow .

Applicability: the output reacts monotonically to the underlying random numbers (the same condition as for CRN; here it makes the two runs of a pair move in opposite directions).

Practical issues:

  • Synchronization: 1−ui1 - u_i must be used for exactly the same purpose in the second run as uiu_i in the first.
  • Use the inverse transform to generate random variates (monotone, one uu per variate).
  • Use dedicated streams for the different sources of randomness.
  • Do not combine CRN with AV (in a comparison of systems): it may backfire. With both, system 1’s first run (with uu) and system 2’s second run (with 1−u1 - u) are negatively correlated, and these cross-covariances enter Var⁡(Z)\operatorname{Var}(Z) with the wrong sign: they increase it. Whether the net effect is a reduction is not clear in advance.

Ex: θ=E[eU]\theta = \mathbf{E}[e^U], U∼U(0,1)U \sim U(0, 1):

θ=∫01eu⋅1 du=e−1=1.718.\theta = \int_0^1 e^u \cdot 1\, du = e - 1 = 1.718 .

To compare fairly, both approaches use two evaluations of eue^u.

Approach 1: crude Monte Carlo simulation. Generate U1,U2∼U(0,1)U_1, U_2 \sim U(0, 1), independent (U1⊥U2U_1 \perp U_2), and use

θˉ=12(eU1+eU2).\bar\theta = \frac{1}{2}\left(e^{U_1} + e^{U_2}\right).

Var⁡(θˉ)=14⋅2Var⁡(eU)=Var⁡(eU)2=E[e2U]−(e−1)22,\operatorname{Var}(\bar\theta) = \frac{1}{4}\cdot 2\operatorname{Var}(e^{U}) = \frac{\operatorname{Var}(e^U)}{2} = \frac{\mathbf{E}[e^{2U}] - (e - 1)^2}{2},

where

E[e2U]=∫01e2u du=e2−12,\mathbf{E}[e^{2U}] = \int_0^1 e^{2u}\, du = \frac{e^2 - 1}{2},

so

Var⁡(θˉ)=12[e2−12−(e−1)2]=0.24202=0.121.\operatorname{Var}(\bar\theta) = \frac{1}{2}\left[\frac{e^2 - 1}{2} - (e - 1)^2\right] = \frac{0.2420}{2} = 0.121 .

Approach 2: AV. Generate one UU and use

θ^=12(eU+e1−U).\hat\theta = \frac{1}{2}\left(e^U + e^{1-U}\right).

Var⁡(θ^)=14[Var⁡(eU)+Var⁡(e1−U)+2cov⁡(eU,e1−U)]=12Var⁡(eU)+12cov⁡(eU,e1−U)=0.121+12cov⁡(eU,e1−U),\begin{aligned} \operatorname{Var}(\hat\theta) &= \frac{1}{4}\Big[\operatorname{Var}(e^U) + \operatorname{Var}(e^{1-U}) + 2\operatorname{cov}(e^U, e^{1-U})\Big] \\ &= \frac{1}{2}\operatorname{Var}(e^U) + \frac{1}{2}\operatorname{cov}(e^U, e^{1-U}) = 0.121 + \frac{1}{2}\operatorname{cov}(e^U, e^{1-U}), \end{aligned}

using Var⁡(e1−U)=Var⁡(eU)\operatorname{Var}(e^{1-U}) = \operatorname{Var}(e^U) (since 1−U∼U(0,1)1 - U \sim U(0,1)). The covariance:

cov⁡(eU,e1−U)=E[eUe1−U]−E[eU] E[e1−U]=e−(e−1)2=−0.2342,\operatorname{cov}(e^U, e^{1-U}) = \mathbf{E}\big[e^U e^{1-U}\big] - \mathbf{E}[e^U]\,\mathbf{E}[e^{1-U}] = e - (e - 1)^2 = -0.2342,

because eUe1−U=ee^U e^{1-U} = e is a constant and E[e1−U]=E[eU]=e−1\mathbf{E}[e^{1-U}] = \mathbf{E}[e^U] = e - 1. So

Var⁡(θ^)=0.121−0.23422=0.0039.\operatorname{Var}(\hat\theta) = 0.121 - \frac{0.2342}{2} = 0.0039 .

The percentage reduction in variance is (0.121−0.0039)/0.121=96.8%(0.121 - 0.0039)/0.121 = 96.8\%.

Why so much: eue^u is increasing in uu, so eUe^U and e1−Ue^{1-U} move in opposite directions, and their average is almost constant:

1.01.52.02.500.51θ = e − 1 = 1.718eᵘe¹⁻ᵘ½(eᵘ + e¹⁻ᵘ)uone run:eᵘ varies from 1 to 2.72antithetic pair average:almost constant,between 1.65 and 1.86

In a real simulation the reduction is usually much smaller, because the output is far from such a simple, almost linear function of each uu. E.g. the M/M/1 queue above (XX = average delay of the first 100 customers, μ≈4.13\mu \approx 4.13, σ2≈8.67\sigma^2 \approx 8.67), with 1−u1 - u for both the interarrival and the service times in the second run: corr⁡(X(1),X(2))=−0.36\operatorname{corr}(X^{(1)}, X^{(2)}) = -0.36, so the pair average has variance 2.802.80 instead of σ2/2=4.34\sigma^2/2 = 4.34, a reduction of 35%.

7.3. Control Variates (CV)

Goal: estimate μ=E[X]\mu = \mathbf{E}[X], XX = average delay in queue of the first 100 customers.

Suppose there is an intermediate r.v. YY satisfying

  1. the value of YY can be observed (in the same run as XX);
  2. YY and XX are correlated;
  3. ν=E[Y]\nu = \mathbf{E}[Y] is known.

E.g. YY = average service time of the first 99 customers. ν\nu is known: it is the mean of the service-time distribution, which we chose as an input. (99, not 100: the delays of customers 1,…,1001, \dots, 100 depend only on the service times of customers 1,…,991, \dots, 99.)

If Y>νY > \nu (the service times happened to be long in this run) ⇒\Rightarrow probably X>μX > \mu (the delays are long too) ⇒\Rightarrow adjust the value of XX downwards, towards μ\mu.

If Y<νY < \nu ⇒\Rightarrow probably X<μX < \mu ⇒\Rightarrow adjust XX upwards.

YY is called a control variate: it measures how lucky or unlucky the run was, and we correct XX for that luck.

How to decide the adjustment amount?

Let XX = the observed value and XcX_c = the value of XX after the adjustment, so the adjustment amount is X−XcX - X_c. Assume it is proportional to how far YY is from its mean:

X−Xc=a(Y−ν),Xc=X−a(Y−ν).X - X_c = a(Y - \nu), \qquad X_c = X - a(Y - \nu).

E[Xc]=E[X]−a E[Y−ν]=μ−a⋅0=μ,\mathbf{E}[X_c] = \mathbf{E}[X] - a\,\mathbf{E}[Y - \nu] = \mu - a \cdot 0 = \mu,

so XcX_c is unbiased for every constant aa. Its variance (ν\nu is a constant, so it does not affect the variance):

Var⁡(Xc)=Var⁡(X−aY)=Var⁡(X)+{a2Var⁡(Y)−2acov⁡(X,Y)}⏟want ≤ 0.\operatorname{Var}(X_c) = \operatorname{Var}(X - aY) = \operatorname{Var}(X) + \underbrace{\big\{a^2\operatorname{Var}(Y) - 2a\operatorname{cov}(X, Y)\big\}}_{\text{want } \le\, 0} .

Var⁡(Xc)≤Var⁡(X)\operatorname{Var}(X_c) \le \operatorname{Var}(X) exactly when the term in braces is ≤0\le 0.

If a=1a = 1, then YY should be chosen such that Var⁡(Y)≤2cov⁡(X,Y)\operatorname{Var}(Y) \le 2\operatorname{cov}(X, Y).

Idea: find the a∗a^* such that Var⁡(Xc)\operatorname{Var}(X_c) is minimized:

dVar⁡(Xc)da=2aVar⁡(Y)−2cov⁡(X,Y)=0⇒a∗=cov⁡(X,Y)Var⁡(Y)\frac{d\operatorname{Var}(X_c)}{da} = 2a\operatorname{Var}(Y) - 2\operatorname{cov}(X, Y) = 0 \quad\Rightarrow\quad a^* = \frac{\operatorname{cov}(X, Y)}{\operatorname{Var}(Y)}

(a minimum, since the second derivative 2Var⁡(Y)>02\operatorname{Var}(Y) > 0). Plugging a∗a^* in:

Var⁡(Xc∗)=Var⁡(X)−cov⁡(X,Y)2Var⁡(Y)=(1−ρXY2)Var⁡(X),ρXY=corr⁡(X,Y).\operatorname{Var}(X_c^*) = \operatorname{Var}(X) - \frac{\operatorname{cov}(X, Y)^2}{\operatorname{Var}(Y)} = \big(1 - \rho_{XY}^2\big)\operatorname{Var}(X), \qquad \rho_{XY} = \operatorname{corr}(X, Y).

So with the best aa, the variance is reduced by the fraction ρXY2\rho_{XY}^2: the more strongly XX and YY are correlated (positively or negatively), the better. a∗a^* has the sign of cov⁡(X,Y)\operatorname{cov}(X, Y): with a negatively correlated control, a∗<0a^* < 0 and XX is adjusted upwards when Y>νY > \nu.

04812160.60.80.911.2observed (Y, X)Xcslope a* = 20.3ν= 0.9 (known)μY = average service time of the first 99 customersX = average delay of the first 100each point = one replicationXc = X − a*(Y − 0.9):slide the point parallel tothe line until Y = 0.9.The spread of Xc is the spreadof the points around the line,smaller than the spread of X.

E.g. M/M/1 queue (arrival rate 1, mean service time 0.9, so ν=0.9\nu = 0.9), XX and YY as above: Var⁡(X)=8.67\operatorname{Var}(X) = 8.67, Var⁡(Y)=0.00819\operatorname{Var}(Y) = 0.00819, cov⁡(X,Y)=0.1663\operatorname{cov}(X, Y) = 0.1663, so ρXY=0.624\rho_{XY} = 0.624 and a∗=0.1663/0.00819=20.3a^* = 0.1663/0.00819 = 20.3. Then Var⁡(Xc∗)=(1−0.6242)×8.67=5.29\operatorname{Var}(X_c^*) = (1 - 0.624^2) \times 8.67 = 5.29, a reduction of 39%. With a=1a = 1 instead, the condition Var⁡(Y)≤2cov⁡(X,Y)\operatorname{Var}(Y) \le 2\operatorname{cov}(X, Y) holds (0.008≤0.330.008 \le 0.33), but the variance only drops to 8.35 (4%): YY varies much less than XX, so a large factor aa is needed.

One limitation: Var⁡(Y)\operatorname{Var}(Y) and cov⁡(X,Y)\operatorname{cov}(X, Y), and hence a∗a^*, are usually unknown. In practice, perform nn simulation runs, giving (X1,Y1),…,(Xn,Yn)(X_1, Y_1), \dots, (X_n, Y_n), and estimate them from the runs:

sY2(n)=∑i=1n(Yi−Yˉ(n))2n−1≈Var⁡(Y),s_Y^2(n) = \frac{\sum_{i=1}^n \big(Y_i - \bar Y(n)\big)^2}{n-1} \approx \operatorname{Var}(Y),

C^XY(n)=∑i=1n(Xi−Xˉ(n))(Yi−Yˉ(n))n−1≈cov⁡(X,Y),\hat C_{XY}(n) = \frac{\sum_{i=1}^n \big(X_i - \bar X(n)\big)\big(Y_i - \bar Y(n)\big)}{n-1} \approx \operatorname{cov}(X, Y),

a^∗(n)=C^XY(n)sY2(n)\hat a^*(n) = \frac{\hat C_{XY}(n)}{s_Y^2(n)}

(the slope of the least-squares line of XX on YY, the line in the figure above). The control-variate estimator is then

Xˉc(n)=Xˉ(n)−a^∗(n)[Yˉ(n)−ν].\bar X_c(n) = \bar X(n) - \hat a^*(n)\big[\bar Y(n) - \nu\big].

Drawback: a^∗(n)\hat a^*(n) is now a random variable computed from the same YiY_i’s as Yˉ(n)\bar Y(n), so the two are not independent, and

E[a^∗(n)(Yˉ(n)−ν)]  ≠  E[a^∗(n)] E[Yˉ(n)−ν]=0.\mathbf{E}\Big[\hat a^*(n)\big(\bar Y(n) - \nu\big)\Big] \;\ne\; \mathbf{E}\big[\hat a^*(n)\big]\,\mathbf{E}\big[\bar Y(n) - \nu\big] = 0 .

So E[Xˉc(n)]≠μ\mathbf{E}[\bar X_c(n)] \ne \mu: the estimator is biased, and the usual CI formula is no longer exact. (This is the standard drawback; the formula on the board was most likely this one.) The bias goes to 0 as nn grows. It can be avoided by estimating a∗a^* from separate pilot runs: then a^∗\hat a^* is independent of Yˉ(n)\bar Y(n), the expectation above factorizes, and it equals 0. E.g. in the M/M/1 example with n=10n = 10 runs and a^∗\hat a^* estimated from them, the variance of Xˉc(10)\bar X_c(10) was 0.58 instead of 0.86 for Xˉ(10)\bar X(10) (a 33% reduction), with a bias of −0.07-0.07.

Ex. Estimate θ=E[eU]\theta = \mathbf{E}[e^U], where U∼U(0,1)U \sim U(0, 1) (θ=e−1=1.718\theta = e - 1 = 1.718, as in §7.2).

Crude Monte Carlo: generate U∼U(0,1)U \sim U(0, 1) and use θ~=eU\tilde\theta = e^U, with

Var⁡(θ~)=Var⁡(eU)=0.242.\operatorname{Var}(\tilde\theta) = \operatorname{Var}(e^U) = 0.242 .

Control variates: let UU itself be the control variate. It is observed, it is correlated with eUe^U, and its mean E[U]=12\mathbf{E}[U] = \frac{1}{2} is known.

θ^=eU−a∗(U−12),a∗=cov⁡(eU,U)Var⁡(U)=1.690,\hat\theta = e^U - a^*\left(U - \tfrac{1}{2}\right), \qquad a^* = \frac{\operatorname{cov}(e^U, U)}{\operatorname{Var}(U)} = 1.690,

because

cov⁡(eU,U)=E[UeU]−E[U] E[eU]=1−12(e−1)=0.1409,\operatorname{cov}(e^U, U) = \mathbf{E}[Ue^U] - \mathbf{E}[U]\,\mathbf{E}[e^U] = 1 - \tfrac{1}{2}(e - 1) = 0.1409,

where

E[UeU]=∫01ueu du=[(u−1)eu]01=1,\mathbf{E}[Ue^U] = \int_0^1 u e^u\, du = \Big[(u - 1)e^u\Big]_0^1 = 1,

and Var⁡(U)=112\operatorname{Var}(U) = \frac{1}{12}, so a∗=12×0.1409=1.690a^* = 12 \times 0.1409 = 1.690 (1.692 on the board is a small rounding slip). So

θ^=eU−1.690(U−12),\hat\theta = e^U - 1.690\left(U - \tfrac{1}{2}\right),

Var⁡(θ^)=Var⁡(θ~)(1−cov⁡2(eU,U)Var⁡(eU)Var⁡(U))=0.242×(1−0.9837)=0.0039\operatorname{Var}(\hat\theta) = \operatorname{Var}(\tilde\theta)\left(1 - \frac{\operatorname{cov}^2(e^U, U)}{\operatorname{Var}(e^U)\operatorname{Var}(U)}\right) = 0.242 \times (1 - 0.9837) = 0.0039

(this is (1−ρ2)Var⁡(X)(1 - \rho^2)\operatorname{Var}(X) from above, with ρ2=0.14092/(0.242×112)=0.9837\rho^2 = 0.1409^2/(0.242 \times \frac{1}{12}) = 0.9837).

Reduction in variance: (0.242−0.0039)/0.242=98.4%(0.242 - 0.0039)/0.242 = 98.4\%.

Why so much: eue^u is almost a straight line on [0,1][0, 1], and 1.718+1.690(u−12)1.718 + 1.690(u - \frac{1}{2}) is the best straight line through it, so θ^\hat\theta only keeps the small curvature. (Compare §7.2: antithetic variates also reached 0.0039, but with two evaluations of eue^u; here one is enough.)

7.4. Indirect Estimation

Idea: use known relationships, and replace random quantities by constant known quantities wherever possible.

Queueing example:

  • DiD_i = delay in queue of the ii-th customer
  • WiW_i = total waiting time in system of the ii-th customer
  • SiS_i = service time of the ii-th customer
  • Q(t)Q(t) = number of customers in queue at time tt
  • L(t)L(t) = number of customers in system at time tt

Quantities of interest (steady state):

d=lim⁡i→∞E[Di]=lim⁡n→∞1n∑i=1nDi,w=lim⁡i→∞E[Wi]=lim⁡n→∞1n∑i=1nWi,d = \lim_{i \to \infty}\mathbf{E}[D_i] = \lim_{n \to \infty}\frac{1}{n}\sum_{i=1}^n D_i, \qquad w = \lim_{i \to \infty}\mathbf{E}[W_i] = \lim_{n \to \infty}\frac{1}{n}\sum_{i=1}^n W_i,

L=lim⁡t→∞E[L(t)],LQ=lim⁡t→∞E[Q(t)]L = \lim_{t \to \infty}\mathbf{E}[L(t)], \qquad L_Q = \lim_{t \to \infty}\mathbf{E}[Q(t)]

(the second equalities hold with probability 1, by the same long-run-average argument as in Chapter 6, §6.4.4). Their direct estimators, from a run with nn customers that ends at time T(n)T(n) (e.g. when the nn-th customer leaves):

d^(n)=1n∑i=1nDi,w^(n)=1n∑i=1nWi,\hat d(n) = \frac{1}{n}\sum_{i=1}^n D_i, \qquad \hat w(n) = \frac{1}{n}\sum_{i=1}^n W_i,

L^(n)=1T(n)∫0T(n)L(t) dt,Q^(n)=1T(n)∫0T(n)Q(t) dt\hat L(n) = \frac{1}{T(n)}\int_0^{T(n)} L(t)\, dt, \qquad \hat Q(n) = \frac{1}{T(n)}\int_0^{T(n)} Q(t)\, dt

(the last two are time averages, as in Chapter 1).

Since Wi=Di+SiW_i = D_i + S_i,

w^(n)=1n∑i=1n(Di+Si)=1n∑i=1nDi+{1n∑i=1nSi}⏟→ E[S].\hat w(n) = \frac{1}{n}\sum_{i=1}^n (D_i + S_i) = \frac{1}{n}\sum_{i=1}^n D_i + \underbrace{\left\{\frac{1}{n}\sum_{i=1}^n S_i\right\}}_{\to\ \mathbf{E}[S]} .

The term in braces is a random average of the service times, but its mean E[S]\mathbf{E}[S] is known (we chose the service-time distribution). Replace it by its known mean:

w~(n)=1n∑i=1nDi+E[S]=d^(n)+E[S].\tilde w(n) = \frac{1}{n}\sum_{i=1}^n D_i + \mathbf{E}[S] = \hat d(n) + \mathbf{E}[S] .

Both have the same expectation. And

Var⁡(w^(n))=Var⁡(d^(n))+Var⁡(Sˉ(n))+2cov⁡(d^(n),Sˉ(n)),Var⁡(w~(n))=Var⁡(d^(n)),\operatorname{Var}(\hat w(n)) = \operatorname{Var}(\hat d(n)) + \operatorname{Var}(\bar S(n)) + 2\operatorname{cov}(\hat d(n), \bar S(n)), \qquad \operatorname{Var}(\tilde w(n)) = \operatorname{Var}(\hat d(n)),

where Sˉ(n)=1n∑iSi\bar S(n) = \frac{1}{n}\sum_i S_i. In a queue, long service times cause long delays, so cov⁡(d^(n),Sˉ(n))>0\operatorname{cov}(\hat d(n), \bar S(n)) > 0 and Var⁡(w~(n))<Var⁡(w^(n))\operatorname{Var}(\tilde w(n)) < \operatorname{Var}(\hat w(n)). (w~(n)=w^(n)−[Sˉ(n)−E[S]]\tilde w(n) = \hat w(n) - [\bar S(n) - \mathbf{E}[S]] is a control-variate estimator with Y=Sˉ(n)Y = \bar S(n) and a=1a = 1, §7.3.)

Little’s Law

L=λ⋅wL = \lambda \cdot w

where λ\lambda is the arrival rate; likewise LQ=λ⋅dL_Q = \lambda \cdot d.

Why: each customer is charged 1 dollar for every unit of time he spends in the system. Compute the long-run average reward earned per unit time in two ways:

  • By time: at time tt, L(t)L(t) customers are in the system, each paying 1 dollar per unit time, so the reward rate is L(t)L(t). The total reward up to time TT is ∫0TL(t) dt\int_0^T L(t)\, dt, and the average reward per unit time is 1T∫0TL(t) dt→L\frac{1}{T}\int_0^T L(t)\, dt \to L.
  • By customer: customer ii pays WiW_i in total. With N(T)N(T) = the number of customers who arrived by time TT,

1T∑i=1N(T)Wi=N(T)T⋅1N(T)∑i=1N(T)Wi  →  λ⋅w,\frac{1}{T}\sum_{i=1}^{N(T)} W_i = \frac{N(T)}{T}\cdot\frac{1}{N(T)}\sum_{i=1}^{N(T)} W_i \;\to\; \lambda \cdot w,

since N(T)/T→λN(T)/T \to \lambda (arrivals per unit time) and the average of the WiW_i’s →w\to w. (Customers still in the system at time TT have not paid everything yet, but this is negligible as T→∞T \to \infty.)

Both compute the same long-run reward rate, so L=λwL = \lambda w. Charging only for the time spent in the queue gives LQ=λdL_Q = \lambda d in the same way.

customer 1W₁ = 1.5customer 2W₂ = 1.8customer 3W₃ = 0.8customer 4W₄ = 1.6customer 5W₅ = 1.5customer 6W₆ = 0.9light: in queue (Dᵢ)dark: in service (Sᵢ)012L(t)02468tshaded area = ∫ L(t) dt= W₁ + W₂ + … + W₆ = 8.1each customer adds 1 to L(t)while it is in the system

Use in indirect estimation: λ\lambda is a known input, so LL and LQL_Q can also be estimated indirectly, instead of by the time averages L^(n)\hat L(n), Q^(n)\hat Q(n):

L~(n)=λ w~(n)=λ(d^(n)+E[S]),Q~(n)=λ d^(n).\tilde L(n) = \lambda\,\tilde w(n) = \lambda\big(\hat d(n) + \mathbf{E}[S]\big), \qquad \tilde Q(n) = \lambda\,\hat d(n) .

So all four quantities are estimated from d^(n)\hat d(n) alone.

E.g. M/M/1 queue (λ=1\lambda = 1, E[S]=0.9\mathbf{E}[S] = 0.9, empty start), first n=1000n = 1000 customers, 20,000 runs:

quantity direct estimator variance indirect estimator variance reduction
ww w^(n)\hat w(n) 14.48 w~(n)=d^(n)+E[S]\tilde w(n) = \hat d(n) + \mathbf{E}[S] 14.37 0.8%
LL L^(n)\hat L(n) 16.05 L~(n)=λw~(n)\tilde L(n) = \lambda\tilde w(n) 14.37 10.5%
LQL_Q Q^(n)\hat Q(n) 15.83 Q~(n)=λd^(n)\tilde Q(n) = \lambda\hat d(n) 14.37 9.3%

The gains are small here (the delays, d≈8.1d \approx 8.1, dominate WW, so the service times add little noise), but they come for free.

7.5. Stratified Sampling

Goal: estimate θ=E[X]\theta = \mathbf{E}[X], using an intermediate r.v. Y∈{1,2,…,k}Y \in \{1, 2, \dots, k\} with known p.m.f.: P(Y=i)P(Y = i), i=1,…,ki = 1, \dots, k, is given. By the law of total expectation,

θ=E[X]=∑i=1kE[X∣Y=i] P(Y=i).\theta = \mathbf{E}[X] = \sum_{i=1}^k \mathbf{E}[X \mid Y = i]\, P(Y = i) .

Let Xˉi\bar X_i be an estimator of E[X∣Y=i]\mathbf{E}[X \mid Y = i]: the average of nin_i observations of XX generated conditional on Y=iY = i (from “stratum” ii). Then the stratified sampling estimator is

θ^=∑i=1kXˉi P(Y=i).\hat\theta = \sum_{i=1}^k \bar X_i\, P(Y = i) .

Why it helps: the strata are sampled independently, so

Var⁡(θ^)=∑i=1kP(Y=i)2 Var⁡(X∣Y=i)ni.\operatorname{Var}(\hat\theta) = \sum_{i=1}^k P(Y = i)^2\,\frac{\operatorname{Var}(X \mid Y = i)}{n_i} .

With NN runs in total and proportional allocation, ni=N P(Y=i)n_i = N\,P(Y = i), this is 1N∑iP(Y=i)Var⁡(X∣Y=i)=1NE[Var⁡(X∣Y)]≤1NVar⁡(X)\frac{1}{N}\sum_i P(Y = i)\operatorname{Var}(X \mid Y = i) = \frac{1}{N}\mathbf{E}[\operatorname{Var}(X \mid Y)] \le \frac{1}{N}\operatorname{Var}(X), the crude Monte Carlo variance with NN runs. Stratification removes the part 1NVar⁡(E[X∣Y])\frac{1}{N}\operatorname{Var}(\mathbf{E}[X \mid Y]) of the variance, i.e. the randomness of how many observations happen to fall in each stratum (law of total variance, §7.6).

Ex: Estimate E[g(U)]=∫01g(x) dx\mathbf{E}[g(U)] = \int_0^1 g(x)\, dx, U∼U(0,1)U \sim U(0, 1). Two approaches, each with 2n2n evaluations of gg.

Crude Monte Carlo based on a sample of 2n2n runs:

g^=12n∑i=12ng(Ui),\hat g = \frac{1}{2n}\sum_{i=1}^{2n} g(U_i),

Var⁡(g^)=14n2∑i=12nVar⁡(g(Ui))=12nVar⁡(g(U))=12n[∫01g2(x) dx−(∫01g(x) dx)2].\operatorname{Var}(\hat g) = \frac{1}{4n^2}\sum_{i=1}^{2n}\operatorname{Var}(g(U_i)) = \frac{1}{2n}\operatorname{Var}(g(U)) = \frac{1}{2n}\left[\int_0^1 g^2(x)\, dx - \left(\int_0^1 g(x)\, dx\right)^2\right].

Stratified sampling with

Y={0,U∈(0,12),1,U∈(12,1),P(Y=0)=P(Y=1)=12.Y = \begin{cases} 0, & U \in (0, \frac{1}{2}), \\ 1, & U \in (\frac{1}{2}, 1), \end{cases} \qquad P(Y = 0) = P(Y = 1) = \tfrac{1}{2} .

E[g(U)]=E[g(U)∣U∈(0,12)]⋅12+E[g(U)∣U∈(12,1)]⋅12=E[g(Uˉ)]⋅12+E[g(U~)]⋅12,Uˉ∼U(0,12), U~∼U(12,1)=E[g(12U)]⋅12+E[g(12+12U)]⋅12,\begin{aligned} \mathbf{E}[g(U)] &= \mathbf{E}\big[g(U) \mid U \in (0, \tfrac{1}{2})\big]\cdot\tfrac{1}{2} + \mathbf{E}\big[g(U) \mid U \in (\tfrac{1}{2}, 1)\big]\cdot\tfrac{1}{2} \\ &= \mathbf{E}[g(\bar U)]\cdot\tfrac{1}{2} + \mathbf{E}[g(\tilde U)]\cdot\tfrac{1}{2}, \qquad \bar U \sim U(0, \tfrac{1}{2}),\ \tilde U \sim U(\tfrac{1}{2}, 1) \\ &= \mathbf{E}\big[g(\tfrac{1}{2}U)\big]\cdot\tfrac{1}{2} + \mathbf{E}\big[g(\tfrac{1}{2} + \tfrac{1}{2}U)\big]\cdot\tfrac{1}{2}, \end{aligned}

since 12U∼U(0,12)\frac{1}{2}U \sim U(0, \frac{1}{2}) and 12+12U∼U(12,1)\frac{1}{2} + \frac{1}{2}U \sim U(\frac{1}{2}, 1). Estimator (nn points in each half):

g~=12⋅1n∑i=1ng(12Ui)+12⋅1n∑i=n+12ng(12+12Ui),\tilde g = \frac{1}{2}\cdot\frac{1}{n}\sum_{i=1}^{n} g\big(\tfrac{1}{2}U_i\big) + \frac{1}{2}\cdot\frac{1}{n}\sum_{i=n+1}^{2n} g\big(\tfrac{1}{2} + \tfrac{1}{2}U_i\big),

Var⁡(g~)=Var⁡(g^)−12n[∫01/2g(x) dx−∫1/21g(x) dx]2≤Var⁡(g^).\operatorname{Var}(\tilde g) = \operatorname{Var}(\hat g) - \frac{1}{2n}\left[\int_0^{1/2} g(x)\, dx - \int_{1/2}^1 g(x)\, dx\right]^2 \le \operatorname{Var}(\hat g) .

Derivation: let A=∫01/2gA = \int_0^{1/2} g, B=∫1/21gB = \int_{1/2}^1 g, A2=∫01/2g2A_2 = \int_0^{1/2} g^2, B2=∫1/21g2B_2 = \int_{1/2}^1 g^2. The density of 12U\frac{1}{2}U is 2 on (0,12)(0, \frac{1}{2}), so E[g(12U)]=2A\mathbf{E}[g(\frac{1}{2}U)] = 2A, E[g2(12U)]=2A2\mathbf{E}[g^2(\frac{1}{2}U)] = 2A_2, and Var⁡(g(12U))=2A2−4A2\operatorname{Var}(g(\frac{1}{2}U)) = 2A_2 - 4A^2; likewise for the other half. Then

Var⁡(g~)=14n(2A2−4A2)+14n(2B2−4B2)=12n[A2+B2−2A2−2B2],\operatorname{Var}(\tilde g) = \frac{1}{4n}\big(2A_2 - 4A^2\big) + \frac{1}{4n}\big(2B_2 - 4B^2\big) = \frac{1}{2n}\big[A_2 + B_2 - 2A^2 - 2B^2\big],

Var⁡(g^)=12n[A2+B2−(A+B)2],\operatorname{Var}(\hat g) = \frac{1}{2n}\big[A_2 + B_2 - (A + B)^2\big],

Var⁡(g^)−Var⁡(g~)=12n[2A2+2B2−(A+B)2]=12n(A−B)2≥0.\operatorname{Var}(\hat g) - \operatorname{Var}(\tilde g) = \frac{1}{2n}\big[2A^2 + 2B^2 - (A + B)^2\big] = \frac{1}{2n}(A - B)^2 \ge 0 .

The reduction is large when the two halves have very different integrals. Crude Monte Carlo may by chance put more points in one half; stratification puts exactly nn in each:

crude: 2n = 10 points0½13 in (0, ½), 7 in (½, 1): by chancestratified: n = 5 + 50½1exactly half in each half

E.g. g(x)=exg(x) = e^x: A=e1/2−1=0.649A = e^{1/2} - 1 = 0.649, B=e−e1/2=1.070B = e - e^{1/2} = 1.070, (A−B)2=0.177(A - B)^2 = 0.177, and Var⁡(g(U))=0.242\operatorname{Var}(g(U)) = 0.242. So Var⁡(g^)=0.242/2n\operatorname{Var}(\hat g) = 0.242/2n and Var⁡(g~)=(0.242−0.177)/2n=0.065/2n\operatorname{Var}(\tilde g) = (0.242 - 0.177)/2n = 0.065/2n: a 73% reduction.

7.6. Conditioning

Goal: estimate μ=E[X]\mu = \mathbf{E}[X], using an intermediate r.v. ZZ satisfying: if the value of ZZ is known, then E[X∣Z=z]\mathbf{E}[X \mid Z = z] is a known, deterministic function of zz (it can be computed exactly, without simulation).

μ=E[X]=E[E[X∣Z]].\mu = \mathbf{E}[X] = \mathbf{E}\big[\mathbf{E}[X \mid Z]\big] .

The conditioning estimator is

μ^=E[X∣Z=z],\hat\mu = \mathbf{E}[X \mid Z = z],

evaluated at the observed value zz of ZZ (i.e. the random variable E[X∣Z]\mathbf{E}[X \mid Z]): instead of using the observed XX, observe ZZ and use the exact conditional mean.

Ex: a single-server queue (FCFS) with exponential service times. μ=E[Di]\mu = \mathbf{E}[D_i], DiD_i = delay of the ii-th customer. ZZ = number of customers in the system seen by the ii-th arrival. Then

E[Di∣Z=z]=z E[S].\mathbf{E}[D_i \mid Z = z] = z\,\mathbf{E}[S] .

Why: customer ii has to wait for the zz customers ahead of him. The z−1z - 1 customers waiting in the queue each need a full service time (mean E[S]\mathbf{E}[S]). The customer in service needs its remaining service time, which by the memoryless property of the exponential distribution also has mean E[S]\mathbf{E}[S], no matter how long the service has already lasted. In total, z E[S]z\,\mathbf{E}[S]. (With non-exponential service times the remaining service time would depend on the elapsed service time, and the formula would fail.) So the conditioning estimator of E[Di]\mathbf{E}[D_i] is Z E[S]Z\,\mathbf{E}[S], and for the average delay of the first nn customers it is 1n∑i=1nZi E[S]\frac{1}{n}\sum_{i=1}^n Z_i\,\mathbf{E}[S].

Law of total variance:

Var⁡(X)=E[Var⁡(X∣Z)]+Var⁡(E[X∣Z])\operatorname{Var}(X) = \mathbf{E}\big[\operatorname{Var}(X \mid Z)\big] + \operatorname{Var}\big(\mathbf{E}[X \mid Z]\big)

(total variance = average variance within each value of ZZ + variance of the conditional means). Hence

Var⁡(E[X∣Z])=Var⁡(X)−E[Var⁡(X∣Z)]≤Var⁡(X),\operatorname{Var}\big(\mathbf{E}[X \mid Z]\big) = \operatorname{Var}(X) - \mathbf{E}\big[\operatorname{Var}(X \mid Z)\big] \le \operatorname{Var}(X),

since E[Var⁡(X∣Z)]≥0\mathbf{E}[\operatorname{Var}(X \mid Z)] \ge 0. So the conditioning estimator μ^=E[X∣Z]\hat\mu = \mathbf{E}[X \mid Z] is unbiased (because μ=E[X]=E[E[X∣Z]]\mu = \mathbf{E}[X] = \mathbf{E}[\mathbf{E}[X \mid Z]]) and its variance is never larger than that of XX. The randomness it removes is E[Var⁡(X∣Z)]\mathbf{E}[\operatorname{Var}(X \mid Z)]: here, the randomness of the actual service times of the customers ahead, which is averaged out exactly instead of being simulated.

E.g. M/M/1 queue (λ=1\lambda = 1, E[S]=0.9\mathbf{E}[S] = 0.9, empty start), XX = average delay of the first 100 customers (20,000 runs): both estimators have mean 4.13, but Var⁡(X)=8.48\operatorname{Var}(X) = 8.48 while Var⁡(1100∑i0.9 Zi)=6.56\operatorname{Var}\big(\frac{1}{100}\sum_i 0.9\,Z_i\big) = 6.56, a 23% reduction.

7.7. Importance Sampling

Goal: estimate

μ=E[X]=E[h(Y)]=∫−∞∞h(y)f(y) dy=∫−∞∞h(y) f(y)g(y) g(y) dy,\mu = \mathbf{E}[X] = \mathbf{E}[h(Y)] = \int_{-\infty}^{\infty} h(y) f(y)\, dy = \int_{-\infty}^{\infty} h(y)\,\frac{f(y)}{g(y)}\, g(y)\, dy,

where YY has density ff, and gg is another density that we choose (and can sample from). We need ff to be absolutely continuous with respect to gg (the board said “gg with respect to ff”, but the “i.e.” part is the right condition), i.e. whenever g(y)=0g(y) = 0, f(y)=0f(y) = 0. Equivalently:

support of f⊆support of g,\text{support of } f \subseteq \text{support of } g,

where the support of ff is the set of values of yy s.t. f(y)>0f(y) > 0.
Why this is needed: we divide by g(y)g(y), and any region where f(y)>0f(y) > 0 but g(y)=0g(y) = 0 would never be sampled from gg, so that part of the integral would be missed. (Strictly, it is enough where h(y)≠0h(y) \ne 0.)

Write Ef\mathbf{E}_f for an expectation when YY has density ff, and Eg\mathbf{E}_g when YY has density gg. Then

μ=E[X]=Eg ⁣[h(Y) f(Y)g(Y)].\mu = \mathbf{E}[X] = \mathbf{E}_g\!\left[h(Y)\,\frac{f(Y)}{g(Y)}\right].

Generate Y1,Y2,…,Yn∼gY_1, Y_2, \dots, Y_n \sim g (i.i.d.) and use

μ^(n)=1n∑i=1nh(Yi){f(Yi)g(Yi)}⏟likelihood ratio.\hat\mu(n) = \frac{1}{n}\sum_{i=1}^n h(Y_i)\underbrace{\left\{\frac{f(Y_i)}{g(Y_i)}\right\}}_{\text{likelihood ratio}} .

This one is unbiased, Eg[μ^(n)]=μ\mathbf{E}_g[\hat\mu(n)] = \mu, just like the crude estimator 1n∑i=1nh(Yi)\frac{1}{n}\sum_{i=1}^n h(Y_i) with Yi∼fY_i \sim f. The likelihood ratio is a weight that corrects for sampling from the “wrong” density: values that gg produces more often than ff would (g>fg > f) get weight <1< 1, values that gg produces less often get weight >1> 1.

Variance:

Var⁡(μ^(n))=1n(Eg ⁣[h2(Y)f2(Y)g2(Y)]−{Eg ⁣[h(Y)f(Y)g(Y)]}2⏟= μ2)=1n Eg ⁣[h2(Y)f2(Y)g2(Y)]−1nμ2=1n∫h2(y)f2(y)g2(y) g(y) dy−1nμ2=1n∫h2(y)f2(y)g(y) dy−1nμ2(one g(y) cancels)=1n∫h2(y) f(y)g(y) f(y) dy−1nμ2=1n Ef ⁣[h2(Y)f(Y)g(Y)]−1nμ2.\begin{aligned} \operatorname{Var}(\hat\mu(n)) &= \frac{1}{n}\left(\mathbf{E}_g\!\left[\frac{h^2(Y)f^2(Y)}{g^2(Y)}\right] - \underbrace{\left\{\mathbf{E}_g\!\left[\frac{h(Y)f(Y)}{g(Y)}\right]\right\}^2}_{=\ \mu^2}\right) \\ &= \frac{1}{n}\,\mathbf{E}_g\!\left[\frac{h^2(Y)f^2(Y)}{g^2(Y)}\right] - \frac{1}{n}\mu^2 \\ &= \frac{1}{n}\int \frac{h^2(y)f^2(y)}{g^2(y)}\, g(y)\, dy - \frac{1}{n}\mu^2 \\ &= \frac{1}{n}\int \frac{h^2(y)f^2(y)}{g(y)}\, dy - \frac{1}{n}\mu^2 \qquad (\text{one } g(y) \text{ cancels}) \\ &= \frac{1}{n}\int h^2(y)\,\frac{f(y)}{g(y)}\, f(y)\, dy - \frac{1}{n}\mu^2 \\ &= \frac{1}{n}\,\mathbf{E}_f\!\left[\frac{h^2(Y)f(Y)}{g(Y)}\right] - \frac{1}{n}\mu^2 . \end{aligned}

“Y∼fY \sim f? Wait, but YY is already ∼g\sim g?” In the simulation, the YiY_i’s are always generated from gg; nothing changes there. The subscript of E\mathbf{E} only says which density the integral is taken against: Eg[k(Y)]=∫k(y)g(y) dy\mathbf{E}_g[k(Y)] = \int k(y) g(y)\, dy and Ef[k(Y)]=∫k(y)f(y) dy\mathbf{E}_f[k(Y)] = \int k(y) f(y)\, dy, and the YY inside is just a name for the integration variable. The last step does not change the number: it regroups the same integral ∫h2f2/g dy\int h^2 f^2/g\, dy, using f(y)f(y) instead of g(y)g(y) as the density, so that it can be compared with crude Monte Carlo:

crude (Yi∼f):  Var⁡=1n Ef[h2(Y)]−1nμ2,\text{crude } (Y_i \sim f):\ \ \operatorname{Var} = \frac{1}{n}\,\mathbf{E}_f\big[h^2(Y)\big] - \frac{1}{n}\mu^2,

importance sampling (Yi∼g):  Var⁡=1n Ef ⁣[h2(Y) f(Y)g(Y)]−1nμ2.\text{importance sampling } (Y_i \sim g):\ \ \operatorname{Var} = \frac{1}{n}\,\mathbf{E}_f\!\left[h^2(Y)\,\frac{f(Y)}{g(Y)}\right] - \frac{1}{n}\mu^2 .

The μ2\mu^2 terms are the same, so importance sampling wins when f(y)/g(y)<1f(y)/g(y) < 1 mostly where h2(y)f(y)h^2(y) f(y) is large: gg should generate the “important” values (those that contribute most to the integral) more often than ff does. Hence the name.

Choosing gg: only the first term depends on gg, so we want

min⁡g Ef ⁣[h2(Y)f(Y)g(Y)].\min_g\ \mathbf{E}_f\!\left[\frac{h^2(Y)f(Y)}{g(Y)}\right].

  • The best possible choice (for h≥0h \ge 0) is g∗(y)=h(y)f(y)/μg^*(y) = h(y)f(y)/\mu: then h(y)f(y)/g∗(y)=μh(y)f(y)/g^*(y) = \mu for every yy and the variance is 0. It cannot be used, because it requires the unknown μ\mu, but it is the guideline: choose gg roughly proportional to h⋅fh \cdot f.
  • In practice, gg is chosen from a family and its parameter is optimized. E.g. if ff is Exp(λ)\text{Exp}(\lambda), take g=Exp(λ′)g = \text{Exp}(\lambda') and choose the rate λ′\lambda'. (The board called this rate μ\mu; I renamed it to avoid a clash with μ=E[X]\mu = \mathbf{E}[X].)

Ex (worked out for this family): estimate p=P(Y>a)p = P(Y > a) for Y∼Exp(λ)Y \sim \text{Exp}(\lambda), so h(y)=I{y>a}h(y) = I\{y > a\} (the indicator II is explained in the next example). For λ′<2λ\lambda' < 2\lambda,

Ef ⁣[h2(Y)f(Y)g(Y)]=∫a∞(λe−λy)2λ′e−λ′y dy=λ2 e−(2λ−λ′)aλ′(2λ−λ′),\mathbf{E}_f\!\left[\frac{h^2(Y)f(Y)}{g(Y)}\right] = \int_a^\infty \frac{(\lambda e^{-\lambda y})^2}{\lambda' e^{-\lambda' y}}\, dy = \frac{\lambda^2\, e^{-(2\lambda - \lambda')a}}{\lambda'(2\lambda - \lambda')},

and setting the derivative of its logarithm to 0 gives

λ′∗=λ+1a−λ2+1a2  ≈  1afor large a,\lambda'^* = \lambda + \frac{1}{a} - \sqrt{\lambda^2 + \frac{1}{a^2}} \;\approx\; \frac{1}{a} \quad \text{for large } a,

i.e. the best gg has mean ≈a\approx a: it puts the samples around the threshold. With λ=1\lambda = 1 and a=20a = 20, p=e−20=2.06×10−9p = e^{-20} = 2.06 \times 10^{-9}. One crude sample has standard deviation p(1−p)≈22,000×p\sqrt{p(1-p)} \approx 22{,}000 \times p. One importance sample with λ′∗=0.049\lambda'^* = 0.049 has standard deviation 5.2×p5.2 \times p.

Ex: X∼N(0,1)X \sim N(0, 1). Estimate p=P(10<X<11)p = P(10 < X < 11) by simulation. The true value is

p=P(10<X<11)=7.6×10−24(board: ≈10−24, the right order of magnitude).p = P(10 < X < 11) = 7.6 \times 10^{-24} \quad (\text{board: } \approx 10^{-24}, \text{ the right order of magnitude}).

Monte Carlo simulation: generate X1,…,Xn∼N(0,1)X_1, \dots, X_n \sim N(0, 1). Write the probability as an expectation:

P(10<X<11)=E[I{10<X<11}].P(10 < X < 11) = \mathbf{E}\big[I\{10 < X < 11\}\big] .

What is II? The indicator function of an event: I{A}=1I\{A\} = 1 if AA happens and 00 otherwise. So I{10<X<11}I\{10 < X < 11\} is a Bernoulli r.v.: it equals 1 with probability pp and 0 with probability 1−p1 - p. Hence E[I]=1⋅p+0⋅(1−p)=p\mathbf{E}[I] = 1 \cdot p + 0 \cdot (1 - p) = p and Var⁡(I)=p(1−p)\operatorname{Var}(I) = p(1 - p). This turns any probability into an expectation, with h(y)=I{10<y<11}h(y) = I\{10 < y < 11\}. The estimator

μ~(n)=1n∑i=1nI{10<Xi<11}\tilde\mu(n) = \frac{1}{n}\sum_{i=1}^n I\{10 < X_i < 11\}

is the fraction of the XiX_i’s that fall in (10,11)(10, 11), and

Var⁡(μ~(n))=p(1−p)n≈10−24n.\operatorname{Var}(\tilde\mu(n)) = \frac{p(1 - p)}{n} \approx \frac{10^{-24}}{n} .

A 95% CI for μ=p\mu = p is

[μ~(n)−1.9610−24n,    μ~(n)+1.9610−24n].\left[\tilde\mu(n) - 1.96\sqrt{\frac{10^{-24}}{n}},\;\; \tilde\mu(n) + 1.96\sqrt{\frac{10^{-24}}{n}}\right].

  • n=106n = 10^6: half-width =1.9610−24/106=1.96×10−15= 1.96\sqrt{10^{-24}/10^{6}} = 1.96 \times 10^{-15}, about 10910^9 times larger than pp.
  • n=1010n = 10^{10}: half-width =1.9610−24/1010=1.9610−34=1.96×10−17= 1.96\sqrt{10^{-24}/10^{10}} = 1.96\sqrt{10^{-34}} = 1.96 \times 10^{-17}. This is the 10−1710^{-17} on the board: the half-width is still about ten million times larger than the true mean.

“Variance is too large compared with the true probability?” Yes, exactly. The variance 10−3410^{-34} is tiny in absolute terms, but what matters is the error relative to the tiny pp:

standard deviation of μ~(n)p=p(1−p)/np≈1np.\frac{\text{standard deviation of } \tilde\mu(n)}{p} = \frac{\sqrt{p(1-p)/n}}{p} \approx \frac{1}{\sqrt{np}} .

For the half-width to be even as small as pp itself (a 100% error!), we need 1.96/np≤11.96/\sqrt{np} \le 1, i.e. n≥1.962/p≈5×1023n \ge 1.96^2/p \approx 5 \times 10^{23} runs. Intuitively: with n=1010n = 10^{10} runs, the expected number of XiX_i’s that land in (10,11)(10, 11) is np≈10−13np \approx 10^{-13}. In practice no XiX_i ever lands there, so μ~(n)=0\tilde\mu(n) = 0, the sample variance is 0 too, and the computed CI is [0,0][0, 0]: a meaningless answer. Crude Monte Carlo cannot see an event that it never generates.

Importance sampling. Sample from g=N(10,1)g = N(10, 1) instead, i.e. move the normal density to the interval:

-405101115f= N(0, 1)original densityg= N(10, 1)sampling density(10, 11)f(y) < 10⁻²² here, sosamples from f neverland in (10, 11)y

Write ϕ\phi for the N(0,1)N(0, 1) density (=f= f) and ϕ10\phi_{10} for the N(10,1)N(10, 1) density (=g= g); “N(0,1)/N(10,1)N(0,1)/N(10,1)” on the board means the ratio ϕ(x)/ϕ10(x)\phi(x)/\phi_{10}(x) of the two densities. Then

μ=P(10<X<11)=E[I{10<X<11}]=∫−∞∞I{10<x<11} ϕ(x)ϕ10(x) ϕ10(x)⏟g(x) dx=Eg ⁣[I{10<X<11} ϕ(X)ϕ10(X)]=Eg ⁣[I{10<X<11} 12π e−X2/212π e−(X−10)2/2]=Eg ⁣[I{10<X<11} e−X2/2+(X−10)2/2]=Eg ⁣[I{10<X<11} e−10X+50].\begin{aligned} \mu = P(10 < X < 11) &= \mathbf{E}\big[I\{10 < X < 11\}\big] \\ &= \int_{-\infty}^{\infty} I\{10 < x < 11\}\,\frac{\phi(x)}{\phi_{10}(x)}\,\underbrace{\phi_{10}(x)}_{g(x)}\, dx \\ &= \mathbf{E}_g\!\left[I\{10 < X < 11\}\,\frac{\phi(X)}{\phi_{10}(X)}\right] \\ &= \mathbf{E}_g\!\left[I\{10 < X < 11\}\,\frac{\frac{1}{\sqrt{2\pi}}\,e^{-X^2/2}}{\frac{1}{\sqrt{2\pi}}\,e^{-(X-10)^2/2}}\right] \\ &= \mathbf{E}_g\!\left[I\{10 < X < 11\}\, e^{-X^2/2 + (X-10)^2/2}\right] \\ &= \mathbf{E}_g\!\left[I\{10 < X < 11\}\, e^{-10X + 50}\right]. \end{aligned}

The 12π\frac{1}{\sqrt{2\pi}}'s cancel out, and −x22+(x−10)22=−x22+x2−20x+1002=−10x+50-\frac{x^2}{2} + \frac{(x - 10)^2}{2} = -\frac{x^2}{2} + \frac{x^2 - 20x + 100}{2} = -10x + 50.

Generate X1,…,Xn∼N(10,1)X_1, \dots, X_n \sim N(10, 1). (The board says N(0,1)N(0, 1), but the samples must come from g=N(10,1)g = N(10, 1): that is the whole point.) The estimator is

μ^(n)=1n∑i=1nI{10<Xi<11} e−10Xi+50\hat\mu(n) = \frac{1}{n}\sum_{i=1}^n I\{10 < X_i < 11\}\, e^{-10X_i + 50}

(written μ~(n)\tilde\mu(n) on the board; I use μ^(n)\hat\mu(n), as in the general formula above, to keep it apart from the crude estimator μ~(n)\tilde\mu(n)).

Variance:

Var⁡(μ^(n))=1n(Eg[I2{10<X<11} e−20X+100]−p2)=1n(Eg[I{10<X<11} e−20X+100]−p2)≤1n e−100−1n p2=1n(10−44−10−48)≈1n 10−44.\begin{aligned} \operatorname{Var}(\hat\mu(n)) &= \frac{1}{n}\Big(\mathbf{E}_g\big[I^2\{10 < X < 11\}\, e^{-20X + 100}\big] - p^2\Big) \\ &= \frac{1}{n}\Big(\mathbf{E}_g\big[I\{10 < X < 11\}\, e^{-20X + 100}\big] - p^2\Big) \\ &\le \frac{1}{n}\, e^{-100} - \frac{1}{n}\, p^2 = \frac{1}{n}\big(10^{-44} - 10^{-48}\big) \approx \frac{1}{n}\, 10^{-44} . \end{aligned}

Step by step:

  1. This is the general formula 1n(Eg[(weighted observation)2]−μ2)\frac{1}{n}\big(\mathbf{E}_g[(\text{weighted observation})^2] - \mu^2\big), and the square of the weight is (e−10x+50)2=e−20x+100(e^{-10x + 50})^2 = e^{-20x + 100}.
  2. I2=II^2 = I, because 02=00^2 = 0 and 12=11^2 = 1.
  3. The bound: the indicator is nonzero only when 10<X<1110 < X < 11, and there −20X+100<−200+100=−100-20X + 100 < -200 + 100 = -100. So I{10<X<11} e−20X+100≤e−100I\{10 < X < 11\}\, e^{-20X + 100} \le e^{-100} always, and its expectation is ≤e−100=3.7×10−44≈10−44\le e^{-100} = 3.7 \times 10^{-44} \approx 10^{-44}.
  4. p2≈(10−24)2=10−48p^2 \approx (10^{-24})^2 = 10^{-48} (with the exact pp, 5.8×10−475.8 \times 10^{-47}), negligible.

Compare with crude Monte Carlo:

Var⁡(μ~(n))≈10−24nvs.Var⁡(μ^(n))≲10−44n:\operatorname{Var}(\tilde\mu(n)) \approx \frac{10^{-24}}{n} \qquad\text{vs.}\qquad \operatorname{Var}(\hat\mu(n)) \lesssim \frac{10^{-44}}{n} :

the variance is about 102010^{20} times smaller, for the same nn. In relative terms, the standard deviation of one sample is now at most 10−44/10−24=100\sqrt{10^{-44}}/10^{-24} = 100 times pp (bound with p≈10−24p \approx 10^{-24}), instead of 1/p≈10121/\sqrt p \approx 10^{12} times pp. E.g. with n=106n = 10^6 the 95% half-width is at most 1.9610−44/106≈2×10−251.96\sqrt{10^{-44}/10^{6}} \approx 2 \times 10^{-25}, a fraction of pp, while crude Monte Carlo gave 2×10−152 \times 10^{-15} with the same nn.

The bound is conservative. The exact per-sample variance is 6.8×10−466.8 \times 10^{-46} (computed numerically), i.e. a standard deviation of 3.4×p3.4 \times p. E.g. with n=104n = 10^4: 3421 of the XiX_i’s land in (10,11)(10, 11), μ^=7.44×10−24\hat\mu = 7.44 \times 10^{-24}, half-width 5.0×10−255.0 \times 10^{-25}, so the 95% CI is [6.94, 7.95]×10−24[6.94,\ 7.95] \times 10^{-24}, which covers p=7.62×10−24p = 7.62 \times 10^{-24}. Over 2000 repetitions, this CI covered pp in 94.3% of cases.

Why it works: under gg, about 34% of the XiX_i’s land in (10,11)(10, 11) (P(10<X<11)=Φ(1)−Φ(0)=0.341P(10 < X < 11) = \Phi(1) - \Phi(0) = 0.341 for X∼N(10,1)X \sim N(10, 1)), instead of np≈10−13np \approx 10^{-13} expected hits with crude sampling. Each hit is then multiplied by the likelihood ratio e−10x+50e^{-10x + 50}, between e−60=8.8×10−27e^{-60} = 8.8 \times 10^{-27} (at x=11x = 11) and e−50=1.9×10−22e^{-50} = 1.9 \times 10^{-22} (at x=10x = 10), which scales the frequent hits back down to the true, tiny probability.

Choice of the shift: among g=N(θ,1)g = N(\theta, 1), the variance is smallest at θ≈10.05\theta \approx 10.05 (3.43×p3.43 \times p), so θ=10\theta = 10 is essentially optimal (θ=10.5\theta = 10.5 gives 3.6×p3.6 \times p, θ=11\theta = 11 gives 4.4×p4.4 \times p). The reason: ff falls so fast that almost all of the probability of (10,11)(10, 11) sits just above 10, i.e. h⋅fh \cdot f is concentrated near 10, and the guideline “gg roughly proportional to h⋅fh \cdot f” says to put the samples there.


7. Variance Reduction Techniques
http://example.com/2026/09/08/2026-09-08-variance-reduction-techniques/
Author
Wind_like
Posted on
September 8, 2026
Licensed under