Off-Policy Evaluation
Estimating a new policy’s value from old logs.
Before a hospital adopts a new treatment policy, or a website a new recommender, someone has to ask how well it will do. Running it to find out may be dangerous, slow or expensive. Off-policy evaluation answers from logs of the old policy, in three ways: model the world and simulate the new policy (the direct method), reweight the logged outcomes (importance sampling), or combine the two (doubly robust). This lesson builds each one, measures their bias and variance in a live experiment, and reviews what large benchmarks found.
What you’ll learn
- State the off-policy evaluation problem and the assumptions it needs: overlap, known or estimable logging probabilities, and no unobserved confounding.
- Implement fitted Q-evaluation, and explain where the direct method's bias comes from and why more data does not remove it.
- Derive inverse propensity scoring for one decision and its sequential extensions, and predict how their variance grows with the horizon and the policy mismatch.
- Derive the doubly robust estimator as importance sampling plus a zero-mean control variate, and prove that it is unbiased for any model.
- Choose among estimators using the horizon, the mismatch, the data size and the findings of empirical benchmarks.
Evaluating a policy without running it
Off-policy evaluation (OPE) estimates the value of a target policy from trajectories logged by a behavior policy (written in the offline RL lessons). It is how an offline RL practitioner chooses hyperparameters without online trials, how a recommender team screens candidates before an A/B test, and how clinical researchers ask what a treatment strategy would have achieved for past patients.
Start with one decision, a contextual bandit: a context , a logged action , a reward . Inverse propensity scoring (IPS), the survey-sampling estimator of Horvitz and Thompson (1952) in new clothes, reweights each logged reward by how much more often would have taken that action:
It is unbiased when the behavior's probabilities (its propensities) are logged correctly and wherever : . Li, Chu, Langford and Wang (2011) evaluated news-recommendation algorithms from logs of uniformly randomized traffic at Yahoo! with a closely related replay method, which keeps only the logged events where the evaluated policy would have chosen the logged article, and checked the offline estimates against online measurements.
The direct method: fitted Q-evaluation
The direct method fits a model and evaluates inside it. The model can describe the dynamics, and , which you then simulate under ; or it can describe the action values directly. Fitted Q-evaluation (FQE; Le, Voloshin & Yue, 2019) is the second kind: fitted Q-iteration with the target policy's average in place of the maximum.
import numpy as np
def fitted_q_evaluation(s, a, r, s_next, done, pi, fit_q, gamma=0.99, iters=100):
"""Logged arrays s, a, r, s_next, done. pi(states) -> (n, A) probabilities of the
target policy. fit_q(s, a, y) -> function q(states) -> (n, A) action values."""
q = lambda states: np.zeros(pi(states).shape) # Q_0 = 0
for _ in range(iters):
v_next = np.sum(pi(s_next) * q(s_next), axis=1) # E_{a' ~ pi} Q(s', a')
y = r + gamma * (1.0 - done) * v_next # target for pi: no max
q = fit_q(s, a, y) # regression on logged pairs
return q
def estimate_value(q, pi, s0):
return np.mean(np.sum(pi(s0) * q(s0), axis=1)) # average V(s0) over startsEach regression uses only logged pairs, but its targets average over 's actions at the next state, so the regressor is queried where and disagree: FQE extrapolates exactly there. No importance weights appear, so its variance is low; its error is bias, from the function class and from extrapolation. Le et al. bound it by a concentrability coefficient times how far the class is from being closed under the backup (Importance Sampling & Doubly Robust Estimators).
A model's bias does not shrink with more data if the model cannot represent the truth. The widgets below use a task built to show this. Episodes have steps with two actions; action 1 pays more at two steps out of three and less at every third; and repeating the previous action earns a bonus of 0.5. The direct method's model predicts each step's reward from the step and the action only, so it cannot see the bonus.
The behavior picks each action with probability ; the target picks action 1 with probability 0.8 (mismatch 0.6). Repeats happen with probability under the behavior and under the target. The model's per-step rewards are averages over the behavior's data, so from step 2 on they include the bonus at the behavior's repeat rate. By how much does the direct method underestimate the target's value at ?
Show the answer
0.81
From step 2 on, the target's expected bonus per step is , but the model credits only . Nine such steps miss . In general the bias is for bonus and mismatch , since when . More data does not help: the model is wrong, not noisy.
Reweighting over many steps: importance sampling
For trajectories the IPS weight becomes a product of per-step ratios , with cumulative weight ; the unknown transition probabilities cancel from the ratio of trajectory probabilities, so no model is needed (Monte Carlo Methods). Three members of the family, for an -step task:
The price is variance. If the two policies use the same action distributions and in every state, the ratios are i.i.d. with mean 1 and . In the widget's task the target picks action 1 with probability against the behavior's , which makes this : 1.54 at the default , ; 13.3 at ; and about at with a deterministic target ().
In the widget's task a reward arrives at every step. Why does per-decision importance sampling spread less than trajectory-wise importance sampling?
- Because it is biased toward the behavior policy's value
- Because it normalizes by the total weight
- Because each reward is multiplied only by the ratios of the actions before it, so early rewards carry short products
- Because it ignores the mismatch between the policies
Show the answer
C. Because each reward is multiplied only by the ratios of the actions before it, so early rewards carry short products
A reward cannot depend on later actions, and later ratios have conditional mean 1, so dropping them keeps the estimator unbiased and removes their variance. The gain is largest when rewards come early; with a single reward at the end, the two estimators coincide.
Doubly robust: a model as a control variate
The direct method has low variance and a bias you cannot see; importance sampling has no bias and a variance you can. The doubly robust estimator (Dudík, Langford & Li, 2011, for one decision; Jiang & Li, 2016, for sequences) uses a model inside importance sampling as a control variate. With and :
Importance sampling plus zero-mean corrections. Regroup by weight. The rewards form per-decision IS, and the model terms come in pairs:
Given the history up to , the weight is fixed and , so each pair has conditional mean zero. By the tower property , whatever is:
The model's value plus weighted TD errors. Regroup by time instead, with : the estimate starts from the model's own answer and corrects it with the model's temporal-difference errors along the logged trajectory, each weighted by the importance ratios so far:
If , every has conditional mean zero given , and the large weights multiply only the irreducible noise of a single step. If , the estimator is exactly per-decision IS. And if the propensities themselves are estimated, the estimator remains consistent when either the propensity model or is correct, which is where the name comes from.
def doubly_robust_episode(rewards, rhos, q_taken, v_hat, gamma=1.0):
"""One logged episode. rewards[t] = R_{t+1}; rhos[t] = pi(A_t|S_t) / b(A_t|S_t);
q_taken[t] = Q_hat(S_t, A_t); v_hat[t] = sum over a of pi(a|S_t) Q_hat(S_t, a)."""
w_prev, total = 1.0, 0.0
for t in range(len(rewards)):
w = w_prev * rhos[t]
total += gamma**t * (w * (rewards[t] - q_taken[t]) + w_prev * v_hat[t])
w_prev = w
return total # the estimate is the average of this over logged episodesThe same model as the direct method, used differently: DM reports ; DR reports plus importance-weighted corrections that cancel the model's bias in expectation.
What does the doubly robust estimator become when the model is ?
- The direct method
- Weighted importance sampling
- Zero
- Per-decision importance sampling
Show the answer
D. Per-decision importance sampling
With and , only the terms survive: per-decision IS. A good model can only help relative to that baseline; a bad one can add variance, but never bias, when the propensities are right.
Which estimator? What the benchmarks found
| Estimator | Bias | Variance | Needs |
|---|---|---|---|
| Direct method (FQE, or a dynamics model) | from model error; does not shrink with data | low | a model class that fits |
| Trajectory-wise IS | none | grows like | correct propensities |
| Per-decision IS | none | lower when rewards come early | correct propensities |
| Weighted IS (and per-decision) | , toward the behavior's value | bounded by the range of returns | correct propensities |
| Doubly robust | none, with correct propensities | importance-sampling-like, reduced where | both |
Two large empirical studies compared these estimators. Voloshin, Le, Jiang and Yue (2021) found no clear-cut winner. Direct methods, FQE among the best of them, were surprisingly competitive with hybrids; weighted importance sampling was more accurate than its unbiased counterpart; hybrids such as MAGIC built on FQE did best when the model was badly misspecified, at the cost of extra hyperparameters; and in two scenarios built to have the same total mismatch , a larger per-step divergence between the policies degraded accuracy about a hundredfold where a longer horizon degraded it about tenfold. Fu et al. (2021) built a benchmark for deep OPE on high-dimensional control (DOPE): FQE and model-based estimates were the most reliable on one suite; on D4RL tasks FQE had the lowest absolute error while importance sampling and DR ranked policies better; and no method performed consistently well across tasks.
As of the mid-2020s, fitted Q-evaluation is the default baseline to beat, weighted estimators are preferred to unweighted ones, and estimates for policies far from the behavior should be trusted least.
Key takeaways
- Off-policy evaluation estimates a target policy's value from another policy's logs. It needs overlap, correct propensities and no unobserved confounding, and no amount of data repairs a violation of these.
- The direct method (FQE or a dynamics model) has low variance and a bias from model error that does not shrink with data.
- Importance sampling (IPS for one step; trajectory, per-decision and weighted IS for many) needs no model, but its variance grows like with the horizon.
- Doubly robust equals per-decision IS plus zero-mean model corrections, or equally the model's value plus importance-weighted TD errors of the model: unbiased for any , and low-variance when .
- Benchmarks found no universal winner. FQE is a strong default, weighted estimators beat unweighted ones, and policy mismatch hurts more than horizon length.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Inverse propensity scoring weights each logged reward by . Which assumption is essential for it to be unbiased?
- The target policy is deterministic
- Overlap, whenever , with the logged propensities correct
- Rewards are bounded in
- The behavior policy is uniformly random
Show the answer
B. Overlap, whenever , with the logged propensities correct
requires every action takes to have positive, correctly recorded probability under . Determinism, bounded rewards and uniform logging are all optional.
The direct method's model ignores a feature of the reward that the target policy exploits more than the behavior. What happens to its error as the number of logged episodes grows?
- It shrinks like
- It shrinks like
- It grows
- Its variance shrinks, but its bias stays, so the error levels off at the bias
Show the answer
D. Its variance shrinks, but its bias stays, so the error levels off at the bias
More data pins the misspecified model down more precisely, around the wrong answer. In the widget at and mismatch 0.3, DM's bias is , and at 10, 100 and 1,000 episodes.
The target is deterministic, the behavior uniform over two actions, episodes last 30 steps, and you have 1,000 logged episodes. Which estimator is likely to have the lowest error?
- Trajectory-wise importance sampling, since it is unbiased
- Weighted importance sampling, since it is bounded
- The direct method: almost no logged episode follows the target for 30 steps, so nearly every weight is zero
- Per-decision importance sampling, since it drops irrelevant ratios
Show the answer
C. The direct method: almost no logged episode follows the target for 30 steps, so nearly every weight is zero
An episode follows the target for all 30 steps with probability , so IS and WIS see only zero weights and return 0 in practice. In the widget at these settings the biased direct method has the lowest RMSE (7.25), ahead of doubly robust (9.1) and far ahead of the importance-sampling estimators.
Why is the doubly robust estimator unbiased for any model , given correct propensities?
- Its model terms enter in pairs , each with conditional mean zero, because
- Because it averages the direct method and importance sampling, whose biases cancel
- Because model errors are symmetric around zero
- Because it normalizes the importance weights
Show the answer
A. Its model terms enter in pairs , each with conditional mean zero, because
Remove the zero-mean pairs and what remains is per-decision importance sampling, which is unbiased. The model affects only the variance.
Voloshin et al. (2021) studied how OPE errors grew as the horizon and the policy mismatch increased. What did they find?
- The horizon was more damaging: errors grew about a hundredfold, while mismatch mattered little
- The mismatch was more damaging: a comparable increase degraded accuracy roughly a hundredfold, against roughly tenfold for the horizon
- Neither mattered for FQE
- Both were equally damaging for every estimator
Show the answer
B. The mismatch was more damaging: a comparable increase degraded accuracy roughly a hundredfold, against roughly tenfold for the horizon
In two scenarios built to have the same total mismatch , the larger per-step divergence hurt about ten times more than the longer horizon. That matches the variance formula: the per-step factor is raised to the power , so the mismatch sets the base of the exponential.
Go deeper
- Importance Sampling & Doubly Robust Estimators · Per-decision, weighted and doubly robust estimators, and their bias–variance trade-offs.
- Breaking the Curse of Horizon · Marginalized importance sampling and the DICE family.
- High-Confidence Evaluation & Safe Improvement · Confidence bounds on a policy’s value, and deploying only what is provably better.
Further reading
- M. Dudík, J. Langford & L. Li, "Doubly robust policy evaluation and learning," Proceedings of the 28th International Conference on Machine Learning (ICML) (2011).
- 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).
- H. M. Le, C. Voloshin & Y. Yue, "Batch policy learning under constraints," Proceedings of the 36th International Conference on Machine Learning (ICML) (2019).
- C. Voloshin, H. M. Le, N. Jiang & Y. Yue, "Empirical study of off-policy policy evaluation for reinforcement learning," NeurIPS Datasets and Benchmarks Track (2021).
- J. Fu, M. Norouzi, O. Nachum, G. Tucker, Z. Wang, A. Novikov, M. Yang, M. R. Zhang, Y. Chen, A. Kumar, C. Paduraru, S. Levine & T. L. Paine, "Benchmarks for deep off-policy evaluation," International Conference on Learning Representations (ICLR) (2021).