Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

Off-Policy Monte Carlo & Importance Sampling

Ordinary and weighted importance sampling, and why their variance can explode.

Importance sampling turns data from one policy into an unbiased estimate for another, and the price can be infinite. In a one-state example, whether the variance is finite or infinite hinges on one probability crossing 0.9. This lesson maps that boundary, shows why weighted importance sampling survives on the wrong side of it, removes the variance that per-decision and discounting-aware weighting can remove, and proves that what remains can grow exponentially with the horizon for any unbiased estimator, unless the problem has Markov structure to exploit.

What you’ll learn

  • Prove that ordinary importance sampling is unbiased, compute its second moment, and locate the boundary between finite and infinite variance in a one-state example.
  • Prove that weighted importance sampling is bounded and consistent, and explain why its error vanishes even when the weights have infinite variance.
  • Derive per-decision and discounting-aware importance sampling, and identify which part of the variance each one removes.
  • Quantify the growth of Eb[ρ2]\E_b[\rho^2] with the horizon through the effective sample size, and state the Cramér–Rao lower bound that no unbiased estimator escapes without Markov structure.

Unbiased, and possibly infinite variance

Episodes τ1,…,τn\tau_1, \dots, \tau_n are drawn i.i.d. from a behavior policy bb, starting in ss, and the ordinary importance sampling estimate of Vπ(s)V^\pi(s) is V^OIS=1n∑iρ(τi)G(τi)\hat V_{\mathrm{OIS}} = \frac1n\sum_i\rho(\tau_i)G(\tau_i), with ρ=ρ0:T−1\rho = \rho_{0:T-1}. Under coverage it is unbiased (Monte Carlo Methods), and its variance is (Eb[ρ2G2]−Vπ(s)2)/n\big(\E_b[\rho^2G^2] - V^\pi(s)^2\big)/n. A second change of measure shows what drives the second moment:

Eb[ρ2G2]=∑τPr⁡b(τ) Pr⁡π(τ)2Pr⁡b(τ)2 G(τ)2=Eπ[ρ G2].\E_b\big[\rho^2G^2\big] = \sum_\tau \Pr_b(\tau)\,\frac{\Pr_\pi(\tau)^2}{\Pr_b(\tau)^2}\,G(\tau)^2 = \E_\pi\big[\rho\,G^2\big].

It is the target policy's mean squared return, weighted by the ratio itself: large wherever π\pi goes and bb rarely does. With episodes of unbounded length, that weight can grow without limit.

DerivationA threshold between finite and infinite variance
  1. π(left)=1,b(left)=β,Vπ(s)=1\pi(\text{left}) = 1,\qquad b(\text{left}) = \beta,\qquad V^\pi(s) = 1

    A single state with two actions and no discounting (γ=1\gamma = 1). Choosing right terminates at once with reward 0. Choosing left pays 0 and keeps the agent where it is with probability 0.9; with probability 0.1 the episode ends with reward +1+1 instead. The target policy always chooses left, so it collects the +1+1 sooner or later: Vπ(s)=1V^\pi(s) = 1. The behavior chooses left with probability β\beta.

  2. Eb[(ρG)2]=∑k≥1βk 0.9k−1 0.1⋅β−2k=0.10.9∑k≥1(0.9β)k\E_b\big[(\rho G)^2\big] = \sum_{k \ge 1}\beta^k\,0.9^{k-1}\,0.1\cdot\beta^{-2k} = \frac{0.1}{0.9}\sum_{k \ge 1}\Big(\frac{0.9}{\beta}\Big)^k

    Any episode containing a right has weight 0. An all-left episode of kk steps has return 1, weight β−k\beta^{-k}, and probability βk 0.9k−1 0.1\beta^k\,0.9^{k-1}\,0.1 under bb. So

  3. Var⁡b(ρG)={0.1β−0.9−1,β>0.9∞,β≤0.9\Var_b(\rho G) = \begin{cases}\dfrac{0.1}{\beta - 0.9} - 1, & \beta > 0.9\\[2mm] \infty, & \beta \le 0.9\end{cases}

    A geometric series: finite exactly when 0.9/β<10.9/\beta < 1. Each extra step multiplies the summand by the probability of continuing (0.9) times the expected squared ratio of that step under bb (β⋅β−2=1/β\beta \cdot \beta^{-2} = 1/\beta), and the variance is finite only if that growth factor is below 1:

  4. At β=1\beta = 1 (on-policy) the variance is 0, as it should be. Sutton and Barto's Example 5.5 uses β=12\beta = \tfrac12, deep in the infinite region: there the ordinary estimates still jump around after millions of episodes, even though their mean is exactly right.

Predict before you look

In this example the behavior goes left with probability β=0.95\beta = 0.95. What is the variance of a single episode's estimate ρG\rho G?

Show the answer

1

Eb[(ρG)2]=0.1/(0.95−0.9)=2\E_b[(\rho G)^2] = 0.1/(0.95 - 0.9) = 2 and Eb[ρG]=1\E_b[\rho G] = 1, so the variance is 2−1=12 - 1 = 1. At β=0.91\beta = 0.91 it is already 9, and below 0.90.9 it is infinite. A behavior policy that takes the target's action 95% of the time sounds close to on-policy; the long episodes are what make it costly.

Infinite variance does not break the law of large numbers: Eb[ρG]=1\E_b[\rho G] = 1 is finite, so V^OIS→1\hat V_{\mathrm{OIS}} \to 1 almost surely. What breaks is the central limit theorem and any error bar built on it. The estimate converges more slowly than 1/n1/\sqrt n, its sample variance is meaningless, and a single long episode can move it by an arbitrary amount.

Weighted importance sampling: biased, bounded, consistent

Proof. (1) The estimate is a convex combination of observed returns. (2) The strong law of large numbers needs only first moments: 1n∑iρiGi→Eb[ρG]=Vπ(s)\frac1n\sum_i\rho_iG_i \to \E_b[\rho G] = V^\pi(s), which is finite because ∣ρG∣≤Gmax⁡ρ\abs{\rho G} \le G_{\max}\rho, and 1n∑iρi→Eb[ρ]=1\frac1n\sum_i\rho_i \to \E_b[\rho] = 1. The ratio converges almost surely, and an almost surely convergent sequence bounded by Gmax⁡G_{\max} converges in mean square by dominated convergence. (3) Linearize the ratio around its limit: V^WIS−Vπ≈1n∑iρi(Gi−Vπ)\hat V_{\mathrm{WIS}} - V^\pi \approx \frac1n\sum_i\rho_i(G_i - V^\pi), a mean of i.i.d. terms with mean zero and second moment Eb[ρ2(G−Vπ)2]\E_b[\rho^2(G - V^\pi)^2] (the delta method). ■\blacksquare

Compare the asymptotic variances: ordinary IS has Eb[ρ2G2]−V2\E_b[\rho^2G^2] - V^2, weighted IS Eb[ρ2(G−V)2]\E_b[\rho^2(G - V)^2]. Weighted IS subtracts the value inside the weighted square, so a large weight on a return close to VV costs nothing. In the threshold example every positive-weight episode has G=1=VπG = 1 = V^\pi: weighted IS is exactly right as soon as one all-left episode appears, for every β\beta, and its asymptotic variance is 0. The comparison is not always this lopsided (a strong negative correlation between weights and returns can favor ordinary IS), but in practice weighted IS is the default (Sutton & Barto, §5.5).

Check your understanding

Why does weighted importance sampling converge in mean square even when Eb[ρ2]=∞\E_b[\rho^2] = \infty?

  1. Because self-normalization makes the weights' variance finite
  2. Because its numerator and denominator converge by the strong law, which needs only first moments, and a bounded, almost surely convergent estimate converges in mean square
  3. Because it is unbiased, so its variance must shrink
  4. It does not: infinite-variance weights make every estimator fail
Show the answer

B. Because its numerator and denominator converge by the strong law, which needs only first moments, and a bounded, almost surely convergent estimate converges in mean square

Both averages have finite means (Eb[ρ]=1\E_b[\rho] = 1, ∣Eb[ρG]∣≤Gmax⁡\abs{\E_b[\rho G]} \le G_{\max}), so the strong law applies, and the ratio is trapped in [−Gmax⁡,Gmax⁡][-G_{\max}, G_{\max}], so dominated convergence finishes the job. Nothing is said about the rate, which can be slower than 1/n1/n when Eb[ρ2]=∞\E_b[\rho^2] = \infty.

Removing avoidable variance: per-decision and discounting-aware weights

Ordinary IS multiplies the whole return by the whole product of ratios. But a reward cannot depend on actions taken after it, and a ratio whose action has not yet influenced anything averages to 1. Both observations remove variance without adding bias.

DerivationPer-decision importance sampling (Precup, Sutton & Singh, 2000)
  1. ρt:T−1 Gt=∑k=tT−1γk−t ρt:T−1 Rk+1\rho_{t:T-1}\,G_t = \sum_{k=t}^{T-1}\gamma^{k-t}\,\rho_{t:T-1}\,R_{k+1}

    Write the weighted return reward by reward:

  2. Eb[ρt:T−1Rk+1]=Eb[ρt:kRk+1 Eb[ρk+1:T−1∣Sk+1,…]]=Eb[ρt:kRk+1]\E_b\big[\rho_{t:T-1}R_{k+1}\big] = \E_b\Big[\rho_{t:k}R_{k+1}\,\E_b\big[\rho_{k+1:T-1} \mid S_{k+1}, \dots\big]\Big] = \E_b\big[\rho_{t:k}R_{k+1}\big]

    Each later ratio has conditional mean one, Eb[π(Aj∣Sj)/b(Aj∣Sj)∣Sj]=∑aπ(a∣Sj)=1\E_b\big[\pi(A_j \mid S_j)/b(A_j \mid S_j) \mid S_j\big] = \sum_a\pi(a \mid S_j) = 1, so by the tower property the ratios after time kk average away given everything up to Rk+1R_{k+1}:

  3. G~t=∑k=tT−1γk−t ρt:k Rk+1\tilde G_t = \sum_{k=t}^{T-1}\gamma^{k-t}\,\rho_{t:k}\,R_{k+1}

    Dropping them gives the per-decision return, unbiased like ordinary IS, in which each reward carries only the ratios of the actions that preceded it:

It helps exactly when rewards arrive before the end. If the only reward is the last one, as in the threshold example, the per-decision return is the ordinary weighted return and nothing is gained.

Discounting-aware importance sampling (Sutton & Barto, §5.8) attacks the case of a terminal reward when γ<1\gamma < 1. Read γ\gamma as a probability of continuing: then GtG_t is an average of undiscounted flat partial returns Gˉt:h=Rt+1+⋯+Rh\bar G_{t:h} = R_{t+1} + \cdots + R_h, each truncated at a random horizon,

Gt=(1−γ)∑h=t+1T−1γh−t−1 Gˉt:h+γT−t−1 Gˉt:T,G_t = (1-\gamma)\sum_{h=t+1}^{T-1}\gamma^{h-t-1}\,\bar G_{t:h} + \gamma^{T-t-1}\,\bar G_{t:T},

which you can check by collecting the coefficient of each RjR_j: (1−γ)∑h≥jγh−t−1+γT−t−1=γj−t−1(1-\gamma)\sum_{h \ge j}\gamma^{h-t-1} + \gamma^{T-t-1} = \gamma^{j-t-1}. A partial return truncated at hh depends only on actions before hh, so it needs only ρt:h−1\rho_{t:h-1}. Weighting each term by its own shorter product gives an unbiased estimator in which long products appear only with the small weight γh−t−1\gamma^{h-t-1}. With γ=0\gamma = 0, just one ratio is needed, however long the episode.

EstimatorRatios multiplying reward Rk+1R_{k+1}BiasHelps most when
Ordinary ISρt:T−1\rho_{t:T-1}, all of themnoneexact unbiasedness matters, e.g. when many independent estimates will be averaged
Weighted ISρt:T−1\rho_{t:T-1}, then self-normalizedO(1/n)O(1/n), consistenta few huge weights on typical returns
Per-decision ISρt:k\rho_{t:k}, those before Rk+1R_{k+1}nonerewards spread over the episode
Discounting-aware ISρt:h−1\rho_{t:h-1} for each truncation hhnoneγ<1\gamma < 1 and late rewards
The weighted versions of the last two normalize each term's weights separately; they are consistent and often steadier, but their bias is harder to analyze.
Check your understanding

Every episode pays a single reward at the very end, and γ=1\gamma = 1. Compared with ordinary importance sampling, what do the per-decision and discounting-aware estimators achieve?

  1. Both remove most of the variance
  2. Per-decision IS helps but discounting-aware IS does not
  3. Discounting-aware IS helps but per-decision IS does not
  4. Nothing: both reduce exactly to ordinary IS
Show the answer

D. Nothing: both reduce exactly to ordinary IS

The single reward is preceded by every action, so it carries the full product either way, and with γ=1\gamma = 1 the only flat partial return with nonzero content is the full one. To do better you need self-normalization (weighted IS), a better behavior policy, or structure: a model or Markov state distributions.

The curse of horizon, and a lower bound

How fast do the weights grow? Suppose that in every state the two policies are the same distributions over actions, π(⋅)\pi(\cdot) and b(⋅)b(\cdot). The per-step ratios are then i.i.d. with Eb[ρt]=1\E_b[\rho_t] = 1 and second moment ∑aπ(a)2/b(a)=1+χ2(π ∥ b)\sum_a\pi(a)^2/b(a) = 1 + \chi^2(\pi \,\|\, b), so for a horizon HH

Eb[ρ0:H−12]=(1+χ2(π ∥ b))H.\E_b\big[\rho_{0:H-1}^2\big] = \big(1 + \chi^2(\pi \,\|\, b)\big)^H.

The mean weight stays 1 while its second moment grows exponentially: the mass of the estimate concentrates on fewer and fewer episodes. The effective sample size of nn weighted episodes, neff=(∑iwi)2/∑iwi2n_{\mathrm{eff}} = (\sum_i w_i)^2/\sum_i w_i^2, makes this concrete: for large nn it is about n (Ebw)2/Eb[w2]=n/Eb[w2]n\,(\E_b w)^2/\E_b[w^2] = n/\E_b[w^2].

Predict before you look

At every step the target takes action 1 with probability 0.9 and the behavior with probability 0.5 (two actions). You log 1,000 episodes of length H=10H = 10. About how large is the effective sample size?

Show the answer

7

Per step, ∑aπ(a)2/b(a)=0.81/0.5+0.01/0.5=1.64\sum_a\pi(a)^2/b(a) = 0.81/0.5 + 0.01/0.5 = 1.64, so Eb[ρ2]=1.6410≈141\E_b[\rho^2] = 1.64^{10} \approx 141 and neff≈1000/141≈7n_{\mathrm{eff}} \approx 1000/141 \approx 7. At H=20H = 20 the formula gives 1000/1.6420≈0.051000/1.64^{20} \approx 0.05, which means it has broken down: the sample effective size can never fall below 1, and a typical dataset of a thousand episodes is dominated by its single largest weight. The behavior here is not exotic, only uniform.

Is the explosion a defect of importance sampling, or of the problem? Without further structure, of the problem.

The products of squared ratios inside the bound are the same quantities that grew like (1+χ2)t(1 + \chi^2)^t above, so in histories without Markov structure the exponential growth is intrinsic: no clever unbiased estimator removes it. The way out is structure. If the process is Markov, different histories that reach the same state can share information, and the trajectory ratio can be replaced by a ratio of state (or state–action) distributions, dπ(s)/db(s)d^\pi(s)/d^b(s), which need not grow with the horizon. Jiang and Li show that merging histories into states (their DAG MDPs) never increases the bound, and marginalized importance sampling (Liu, Li, Tang & Zhou, 2018) builds estimators on this idea; see Breaking the Curse of Horizon in the topic Learning from Demonstrations & Data. The price is that those ratios are unknown and must be estimated, which brings back bias.

Key takeaways

  • Ordinary IS is unbiased with second moment Eπ[ρ G2]\E_\pi[\rho\,G^2]; with unbounded episode lengths it can be infinite. In the one-state example the variance is 0.1/(β−0.9)−10.1/(\beta - 0.9) - 1 for β>0.9\beta > 0.9 and infinite otherwise.
  • Weighted IS is bounded by the returns, consistent using first moments only, and converges in mean square even when Eb[ρ2]=∞\E_b[\rho^2] = \infty; its asymptotic variance Eb[ρ2(G−V)2]\E_b[\rho^2(G - V)^2] is often far below ordinary IS's.
  • Per-decision IS gives each reward only the ratios before it; discounting-aware IS gives each flat partial return only the ratios before its truncation. Both are unbiased, and both reduce to ordinary IS for a single terminal reward with γ=1\gamma = 1.
  • With i.i.d. per-step mismatch, Eb[ρ2]=(1+χ2)H\E_b[\rho^2] = (1 + \chi^2)^H and the effective sample size is about n/(1+χ2)Hn/(1+\chi^2)^H; a Cramér–Rao bound shows the growth is intrinsic without Markov structure, which marginalized methods exploit.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In the one-state example (left continues with probability 0.9, the target always goes left), the behavior goes left with probability β=0.8\beta = 0.8. What can you say about ordinary importance sampling?

  1. It is biased, because the behavior rarely matches the target
  2. Its variance is 0.1/(0.8−0.9)−1<00.1/(0.8 - 0.9) - 1 < 0, so the formula breaks
  3. Its variance is finite but large
  4. It is unbiased and still converges almost surely, but its variance is infinite
Show the answer

D. It is unbiased and still converges almost surely, but its variance is infinite

The growth factor per step is 0.9/β=1.125>10.9/\beta = 1.125 > 1, so Eb[(ρG)2]=∞\E_b[(\rho G)^2] = \infty. Unbiasedness needs only coverage, and the strong law needs only a finite mean, so the estimate still converges, but without a central limit theorem or meaningful error bars.

Question 2

Which expression is the asymptotic variance (times nn) of weighted importance sampling when Eb[ρ2]<∞\E_b[\rho^2] < \infty?

  1. Eb[ρ2G2]−V2\E_b[\rho^2G^2] - V^2
  2. Eb[ρ2(G−V)2]\E_b[\rho^2(G - V)^2]
  3. Var⁡b(G)\Var_b(G)
  4. Eb[ρ2] Var⁡π(G)\E_b[\rho^2]\,\Var_\pi(G)
Show the answer

B. Eb[ρ2(G−V)2]\E_b[\rho^2(G - V)^2]

Linearizing the ratio gives V^WIS−V≈1n∑iρi(Gi−V)\hat V_{\mathrm{WIS}} - V \approx \frac1n\sum_i\rho_i(G_i - V). The value is subtracted inside the weighted square, which is why large weights on returns near VV cost weighted IS nothing.

Question 3

Why may per-decision importance sampling drop the ratios of actions taken after reward Rk+1R_{k+1} without introducing bias?

  1. Because those ratios are close to 1 when the policies are similar
  2. Because Rk+1R_{k+1} is already known when those actions are chosen, and each later ratio has conditional expectation 1 under the behavior policy
  3. Because the discount factor makes late ratios negligible
  4. Because weighted normalization cancels them
Show the answer

B. Because Rk+1R_{k+1} is already known when those actions are chosen, and each later ratio has conditional expectation 1 under the behavior policy

Eb[π(Aj∣Sj)/b(Aj∣Sj)∣Sj]=∑aπ(a∣Sj)=1\E_b[\pi(A_j \mid S_j)/b(A_j \mid S_j) \mid S_j] = \sum_a\pi(a \mid S_j) = 1, so by the tower property the product of later ratios averages to 1 given everything up to Rk+1R_{k+1}. The identity is exact for any pair of policies with coverage; no approximation or discounting is involved.

Question 4

What does Jiang and Li's Cramér–Rao bound imply about off-policy evaluation in tree MDPs, where the state is the whole history?

  1. Weighted importance sampling always attains the bound
  2. The exponential growth of variance with the horizon is a flaw of importance sampling that a better unbiased estimator could avoid
  3. Every unbiased estimator's variance includes sums of squared cumulative ratios, so the growth with the horizon is intrinsic; escaping it needs structure such as Markov states
  4. Off-policy evaluation is impossible beyond a fixed horizon
Show the answer

C. Every unbiased estimator's variance includes sums of squared cumulative ratios, so the growth with the horizon is intrinsic; escaping it needs structure such as Markov states

The bound holds for every unbiased estimator and contains Eb[ρ1:t−12Var⁡t(⋅)]\E_b[\rho_{1:t-1}^2\Var_t(\cdot)] terms, which can grow exponentially in tt. Merging histories into Markov states never increases the bound and can shrink it drastically, which is what marginalized importance sampling exploits.

Further reading

  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, §§5.5–5.9 (MIT Press, 2nd ed., 2018).
  • D. Precup, R. S. Sutton & S. Singh, "Eligibility traces for off-policy policy evaluation," Proceedings of the 17th International Conference on Machine Learning (ICML), 759–766 (2000).
  • N. Jiang & L. Li, "Doubly robust off-policy value evaluation for reinforcement learning," Proceedings of the 33rd International Conference on Machine Learning (ICML), 652–661 (2016).
  • Q. Liu, L. Li, Z. Tang & D. Zhou, "Breaking the curse of horizon: infinite-horizon off-policy estimation," Advances in Neural Information Processing Systems 31 (2018).