Off-Policy Learning & Importance Sampling
Learning about one policy from another.
A hospital wants to know how a new treatment policy would have fared, using only the records of the old one. A robot wants to learn from yesterday's logs. Off-policy learning answers questions about one policy with data from another, and its central tool, importance sampling, is unbiased, simple and, over long horizons, catastrophically noisy.
What you’ll learn
- Derive ordinary, weighted and per-decision importance-sampling estimators, and quantify how their variance grows with the horizon.
- Compare off-policy return operators (importance sampling, Q(), tree backup, Retrace) by variance, safety and efficiency, and prove Retrace's contraction property.
- Derive the doubly robust estimator, prove its unbiasedness and decompose its variance.
- Explain why offline RL fails through distribution shift, and how policy constraints and conservative Q-learning counter it.
Importance sampling for returns
Data come from a behavior policy ; the questions concern a target policy . Off-policy estimation needs coverage, . The probabilities of a trajectory under the two policies share every transition factor, so their ratio involves only the policies, and reweighting returns by it corrects the mismatch:
One state, two actions. Right ends the episode with reward 0. Left returns to the state with probability 0.9 and ends with reward with probability 0.1. The target policy always goes left, the behavior picks each action with probability , and :
Only all-left episodes have a nonzero weight. One that ends after steps has weight , return 1 and probability under . The second moment of the estimate is
The mean is right, , but the variance is infinite, and ordinary importance-sampling estimates keep jumping even after millions of episodes. Weighted importance sampling returns exactly 1 as soon as a single all-left episode has been seen.
Part of the variance is avoidable. The reward cannot depend on actions taken after time , and each later ratio has conditional mean 1, . Dropping those ratios gives the per-decision estimator (Precup, Sutton & Singh, 2000), still unbiased:
The variance still compounds. Per step, , so over a horizon the second moment of the full weight can grow like : the curse of horizon (Liu et al., 2018).
A greedy target policy is evaluated with data from an ε-greedy behavior (, 4 actions), which takes the greedy action with probability 0.925. In a 100-step episode in which the behavior happened to act greedily every time, how large is the importance weight ?
Show the answer
2430
. Such episodes occur with probability , and every other episode has weight 0. The mean weight is exactly 1, but the estimate rests on a few rare, huge terms: the variance of the weight is about 2,400.
Off-policy control: from one step to Retrace
One-step Q-learning needs no correction at all. Its update conditions on the pair that was tried, and its target maximizes over the next action instead of sampling it, so the behavior policy decides only which pairs get updated. The widget makes the point as starkly as possible: a behavior that is completely random.
Multi-step targets lose this immunity: a two-step target uses the behavior's next action , which the target policy might never take. Every multi-step correction fits one template (Munos et al., 2016), a discounted sum of TD errors weighted by products of trace coefficients :
| Method | Variance | Safe for any ? | Efficient near on-policy? | |
|---|---|---|---|---|
| Importance sampling | high: products of ratios | yes | yes | |
| (Harutyunyan et al., 2016) | low | no: only if and are close | yes | |
| Tree backup (Precup et al., 2000) | low | yes | no: cuts traces even on-policy | |
| Retrace (Munos et al., 2016) | low: | yes | yes |
Let . By the Bellman equation the rewards cancel against , and regrouping the sum by time (the terms cancel) leaves, with the empty product equal to 1:
Average over : the bracket becomes , whose coefficients are nonnegative exactly when . So is a nonnegative combination of values of , with total weight
Hence everywhere. Longer traces make the modulus smaller and propagation faster; larger would make some coefficients negative and break the argument, which is what truncation at , or at 1 for low variance, prevents.
Distributed agents such as IMPALA (Espeholt et al., 2018) use V-trace, a state-value relative of Retrace with truncation levels . It converges to the value of a policy between and , the one proportional to , trading a little bias for bounded variance when the actors' policies lag behind the learner's.
Off-policy evaluation: direct, importance-weighted, doubly robust
Off-policy evaluation (OPE) estimates from logged data without ever running , as when a recommender system or a clinic must judge a new policy before deploying it. Start with a single decision, a contextual bandit: contexts , logged actions , rewards with mean and variance , and a learned reward model .
| Estimator | One-sample formula | Bias | Variance |
|---|---|---|---|
| Direct method | that of the model | low | |
| Importance sampling (inverse propensity) | with | none, if is known | high when can be large |
| Doubly robust (Dudík, Langford & Li, 2011) | none, if is known, for any | importance sampling's, with replaced by the residual |
Condition on and average over and the reward noise. The correction term reweights residuals by :
Adding the direct term cancels exactly, whatever its errors:
The law of total variance splits the error into context sampling (unavoidable), action sampling (importance sampling's dominant term, which shrinks when ) and reward noise amplified by :
"Doubly robust" also covers estimated propensities: if is replaced by an estimate , the estimator stays consistent when either or is correct.
For sequential decisions, Jiang and Li (2016) apply the same correction at every step, backward from the horizon, with an estimated and :
It is unbiased whenever per-decision importance sampling is, and its variance is the per-decision variance with each reward replaced by a TD-like residual. They also prove a Cramér–Rao-type lower bound on the variance of any unbiased estimator for tree-structured MDPs, which doubly robust approaches when is accurate. Weighted variants (Thomas & Brunskill, 2016) accept a little bias for much lower variance, and marginalized estimators that reweight state distributions instead of action sequences (Liu et al., 2018; Nachum et al., 2019) avoid the exponential dependence on the horizon, at the price of learning density ratios.
Offline RL: when you cannot collect more data
In offline RL the dataset , collected by some behavior policy , is all there is (Levine et al., 2020). Run Q-learning on it, and the max in the target queries actions that the data rarely or never contain. Their values are pure extrapolation by the network, as likely to be optimistic as pessimistic, and the max selects the optimistic ones. Online, acting on them would produce corrective data; offline, nothing ever does. This extrapolation error (Fujimoto, Meger & Precup, 2019) is a distribution shift between the actions the learned policy picks and the actions the data support.
Two families respond. Policy constraints keep the learned policy near the data: BCQ (Fujimoto et al., 2019) considers only actions that a generative model of finds likely, and TD3+BC (Fujimoto & Gu, 2021) adds a behavior-cloning term to the actor's loss. Pessimism lowers the values of unsupported actions instead. Conservative Q-learning (CQL; Kumar, Zhou, Tucker & Levine, 2020) adds a regularizer that pushes down on actions the policy would choose and up on actions in the data:
Replace the soft maximum by for a fixed distribution , the policy being evaluated. For each state–action pair the objective is quadratic in , with data weight . Set its derivative to zero:
So each iteration subtracts a penalty that is large exactly where puts mass that the data lack:
Averaged over , the penalty is a chi-squared divergence, never negative. The value of is therefore pushed below the plain backup, and with a large enough the fixed point lower-bounds the true value despite sampling error (Kumar et al., 2020).
Theory supports pessimism: offline algorithms that subtract an uncertainty penalty compete with any policy whose state–action distribution the data cover well (single-policy concentrability), without needing coverage of every policy (Jin, Yang & Wang, 2021; Rashidinejad et al., 2021). Implicit Q-learning (Kostrikov, Nair & Levine, 2022) sidesteps unseen actions altogether by fitting an upper expectile of the values of actions in the data.
Online Q-learning with function approximation also overestimates some actions, yet the errors tend to get corrected. Why do they persist offline?
- Offline datasets are too small to train a network
- Offline training uses a different loss function
- Online, the agent eventually tries the overvalued actions and observes their real returns; offline, no new data about those actions ever arrives
- Offline RL cannot use target networks
Show the answer
C. Online, the agent eventually tries the overvalued actions and observes their real returns; offline, no new data about those actions ever arrives
Acting greedily on an overestimate is a form of exploration: the resulting transitions pull the estimate back down. A fixed dataset never contains those transitions, so the max keeps selecting the same extrapolated values, and bootstrapping spreads them. That is why offline methods constrain the policy or penalize unsupported values.
Key takeaways
- Importance sampling reweights returns by : unbiased, with a second moment that can grow like ; weighted IS trades bias for variance, and per-decision IS drops ratios that cannot matter.
- One-step Q-learning needs no correction: even uniformly random behavior teaches it the optimal greedy policy. Multi-step targets need traces.
- Off-policy operators differ in their traces: importance sampling has high variance, is unsafe for distant policies, tree backup is inefficient, and Retrace () is a -contraction to for any behavior.
- Doubly robust estimates add an importance-weighted residual to a model-based estimate: unbiased for any reward model when the propensities are known, and lower in variance when the model is good.
- Offline RL fails through extrapolation error on unsupported actions; constraints (BCQ, TD3+BC) keep the policy near the data, and pessimism (CQL) lowers unsupported values, an approach that is provably efficient under single-policy coverage.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Per-decision importance sampling weights the reward by instead of the full . Why does it stay unbiased?
- Because the dropped ratios are close to 1 in practice
- Because it is a weighted (self-normalized) estimator
- Because does not depend on later actions and each later ratio has conditional mean 1
- It does not; it trades bias for variance
Show the answer
C. Because does not depend on later actions and each later ratio has conditional mean 1
By iterated expectations, , and the inner expectation is 1. The dropped factors contribute only variance.
Retrace uses . What does the truncation at 1 buy?
- Bounded traces, hence low variance, while the operator still contracts to for any behavior
- Unbiasedness, which plain importance sampling lacks
- Faster convergence than any untruncated trace
- It removes the need to know
Show the answer
A. Bounded traces, hence low variance, while the operator still contracts to for any behavior
Products of truncated ratios never exceed 1, so the variance stays controlled. Because , the contraction proof still applies: is the fixed point for any ("safe"), and near on-policy the traces are not cut ("efficient").
A doubly robust estimate uses a reward model that is badly wrong, but the logged propensities are exact. The estimate is:
- Biased by the model's error
- Undefined
- Exactly as good as the direct method
- Unbiased, though its variance may be larger than with a good model
Show the answer
D. Unbiased, though its variance may be larger than with a good model
The correction term has conditional mean , which cancels the model's contribution exactly. A good model shrinks the residuals and hence the variance; a bad one leaves a high-variance but still unbiased estimator.
CQL pushes down on actions the learned policy favors and up on actions in the dataset. Which failure is it designed to prevent?
- Slow propagation of rewards over long horizons
- Overestimated values of actions the data never contain, which the max in the target then selects
- Underexploration of the environment
- Instability caused by correlated minibatches
Show the answer
B. Overestimated values of actions the data never contain, which the max in the target then selects
Offline, unsupported actions have extrapolated values that no new data will correct, and maximization picks the optimistic ones. CQL's penalty makes the learned values conservative where the data are thin, so the policy prefers actions the data support.
Further reading
- D. Precup, R. S. Sutton & S. Singh, "Eligibility traces for off-policy policy evaluation," ICML (2000).
- R. Munos, T. Stepleton, A. Harutyunyan & M. G. Bellemare, "Safe and efficient off-policy reinforcement learning," NeurIPS (2016).
- N. Jiang & L. Li, "Doubly robust off-policy value evaluation for reinforcement learning," ICML (2016).
- A. Kumar, A. Zhou, G. Tucker & S. Levine, "Conservative Q-learning for offline reinforcement learning," NeurIPS (2020).
- S. Levine, A. Kumar, G. Tucker & J. Fu, "Offline reinforcement learning: Tutorial, review, and perspectives on open problems," arXiv:2005.01643 (2020).