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 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 are drawn i.i.d. from a behavior policy , starting in , and the ordinary importance sampling estimate of is , with . Under coverage it is unbiased (Monte Carlo Methods), and its variance is . A second change of measure shows what drives the second moment:
It is the target policy's mean squared return, weighted by the ratio itself: large wherever goes and rarely does. With episodes of unbounded length, that weight can grow without limit.
A single state with two actions and no discounting (). 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 instead. The target policy always chooses left, so it collects the sooner or later: . The behavior chooses left with probability .
Any episode containing a right has weight 0. An all-left episode of steps has return 1, weight , and probability under . So
A geometric series: finite exactly when . Each extra step multiplies the summand by the probability of continuing (0.9) times the expected squared ratio of that step under (), and the variance is finite only if that growth factor is below 1:
At (on-policy) the variance is 0, as it should be. Sutton and Barto's Example 5.5 uses , deep in the infinite region: there the ordinary estimates still jump around after millions of episodes, even though their mean is exactly right.
In this example the behavior goes left with probability . What is the variance of a single episode's estimate ?
Show the answer
1
and , so the variance is . At it is already 9, and below 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: is finite, so almost surely. What breaks is the central limit theorem and any error bar built on it. The estimate converges more slowly than , 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: , which is finite because , and . The ratio converges almost surely, and an almost surely convergent sequence bounded by converges in mean square by dominated convergence. (3) Linearize the ratio around its limit: , a mean of i.i.d. terms with mean zero and second moment (the delta method).
Compare the asymptotic variances: ordinary IS has , weighted IS . Weighted IS subtracts the value inside the weighted square, so a large weight on a return close to costs nothing. In the threshold example every positive-weight episode has : weighted IS is exactly right as soon as one all-left episode appears, for every , 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).
Why does weighted importance sampling converge in mean square even when ?
- Because self-normalization makes the weights' variance finite
- 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
- Because it is unbiased, so its variance must shrink
- 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 (, ), so the strong law applies, and the ratio is trapped in , so dominated convergence finishes the job. Nothing is said about the rate, which can be slower than when .
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.
Write the weighted return reward by reward:
Each later ratio has conditional mean one, , so by the tower property the ratios after time average away given everything up to :
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 . Read as a probability of continuing: then is an average of undiscounted flat partial returns , each truncated at a random horizon,
which you can check by collecting the coefficient of each : . A partial return truncated at depends only on actions before , so it needs only . Weighting each term by its own shorter product gives an unbiased estimator in which long products appear only with the small weight . With , just one ratio is needed, however long the episode.
| Estimator | Ratios multiplying reward | Bias | Helps most when |
|---|---|---|---|
| Ordinary IS | , all of them | none | exact unbiasedness matters, e.g. when many independent estimates will be averaged |
| Weighted IS | , then self-normalized | , consistent | a few huge weights on typical returns |
| Per-decision IS | , those before | none | rewards spread over the episode |
| Discounting-aware IS | for each truncation | none | and late rewards |
Every episode pays a single reward at the very end, and . Compared with ordinary importance sampling, what do the per-decision and discounting-aware estimators achieve?
- Both remove most of the variance
- Per-decision IS helps but discounting-aware IS does not
- Discounting-aware IS helps but per-decision IS does not
- 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 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, and . The per-step ratios are then i.i.d. with and second moment , so for a horizon
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 weighted episodes, , makes this concrete: for large it is about .
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 . About how large is the effective sample size?
Show the answer
7
Per step, , so and . At the formula gives , 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 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, , 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 ; with unbounded episode lengths it can be infinite. In the one-state example the variance is for and infinite otherwise.
- Weighted IS is bounded by the returns, consistent using first moments only, and converges in mean square even when ; its asymptotic variance 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 .
- With i.i.d. per-step mismatch, and the effective sample size is about ; 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.
In the one-state example (left continues with probability 0.9, the target always goes left), the behavior goes left with probability . What can you say about ordinary importance sampling?
- It is biased, because the behavior rarely matches the target
- Its variance is , so the formula breaks
- Its variance is finite but large
- 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 , so . 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.
Which expression is the asymptotic variance (times ) of weighted importance sampling when ?
Show the answer
B.
Linearizing the ratio gives . The value is subtracted inside the weighted square, which is why large weights on returns near cost weighted IS nothing.
Why may per-decision importance sampling drop the ratios of actions taken after reward without introducing bias?
- Because those ratios are close to 1 when the policies are similar
- Because is already known when those actions are chosen, and each later ratio has conditional expectation 1 under the behavior policy
- Because the discount factor makes late ratios negligible
- Because weighted normalization cancels them
Show the answer
B. Because is already known when those actions are chosen, and each later ratio has conditional expectation 1 under the behavior policy
, so by the tower property the product of later ratios averages to 1 given everything up to . The identity is exact for any pair of policies with coverage; no approximation or discounting is involved.
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?
- Weighted importance sampling always attains the bound
- The exponential growth of variance with the horizon is a flaw of importance sampling that a better unbiased estimator could avoid
- 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
- 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 terms, which can grow exponentially in . 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).