Simple Algorithm
Reinforcement Learning · Bootstrapping · 22 min

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 J(π)=ES0∼μ[Vπ(S0)]J(\pi) = \E_{S_0\sim\mu}[V^\pi(S_0)] of a target policy π\pi from nn trajectories logged by a behavior policy bb (written πβ\pi_\beta 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 xx, a logged action a∼b(⋅∣x)a\sim b(\cdot\mid x), a reward rr. 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 π\pi would have taken that action:

J^IPS=1n∑i=1nπ(ai∣xi)b(ai∣xi) ri.\hat J_{\text{IPS}} = \frac1n\sum_{i=1}^n\frac{\pi(a_i\mid x_i)}{b(a_i\mid x_i)}\,r_i.

It is unbiased when the behavior's probabilities (its propensities) are logged correctly and b(a∣x)>0b(a\mid x) > 0 wherever π(a∣x)>0\pi(a\mid x) > 0: Ea∼b[π(a∣x)b(a∣x)r]=Ea∼π[r]\E_{a\sim b}\big[\tfrac{\pi(a\mid x)}{b(a\mid x)}r\big] = \E_{a\sim\pi}[r]. 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 π\pi inside it. The model can describe the dynamics, P^\hat P and r^\hat r, which you then simulate under π\pi; 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.

python
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 starts

Each regression uses only logged pairs, but its targets average over π\pi's actions at the next state, so the regressor is queried where π\pi and bb 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 HH 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.

Predict before you look

The behavior picks each action with probability 12\tfrac12; the target picks action 1 with probability 0.8 (mismatch 0.6). Repeats happen with probability 12\tfrac12 under the behavior and 0.82+0.22=0.680.8^2 + 0.2^2 = 0.68 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 H=10H = 10?

Show the answer

0.81

From step 2 on, the target's expected bonus per step is 0.5×0.68=0.340.5\times0.68 = 0.34, but the model credits only 0.5×0.5=0.250.5\times0.5 = 0.25. Nine such steps miss 9×0.09=0.819\times0.09 = 0.81. In general the bias is −β(H−1)m2/2-\beta(H-1)m^2/2 for bonus β\beta and mismatch mm, since p2+(1−p)2−12=m2/2p^2 + (1-p)^2 - \tfrac12 = m^2/2 when p=(1+m)/2p = (1+m)/2. 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 ρt=π(At∣St)/b(At∣St)\rho_t = \pi(A_t\mid S_t)/b(A_t\mid S_t), with cumulative weight wt=ρ0ρ1⋯ρtw_t = \rho_0\rho_1\cdots\rho_t; 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 HH-step task:

J^IS=1n∑i=1nwH−1(i)G(i),J^PDIS=1n∑i=1n∑t=0H−1γt wt(i)Rt+1(i),J^WIS=∑iwH−1(i)G(i)∑iwH−1(i)\hat J_{\text{IS}} = \frac1n\sum_{i=1}^n w^{(i)}_{H-1}G^{(i)},\qquad \hat J_{\text{PDIS}} = \frac1n\sum_{i=1}^n\sum_{t=0}^{H-1}\gamma^t\,w^{(i)}_t R^{(i)}_{t+1},\qquad \hat J_{\text{WIS}} = \frac{\sum_i w^{(i)}_{H-1}G^{(i)}}{\sum_i w^{(i)}_{H-1}}
Trajectory-wise importance sampling weights the whole return by the whole product; per-decision IS weights each reward only by the ratios of the actions before it; weighted IS normalizes by the total weight. The first two are unbiased; the third is biased toward the behavior's value for small nn, consistent, and much steadier. The proofs, and a threshold between finite and infinite variance, are in Off-Policy Monte Carlo & Importance Sampling.

The price is variance. If the two policies use the same action distributions π(⋅)\pi(\cdot) and b(⋅)b(\cdot) in every state, the ratios are i.i.d. with mean 1 and Eb[wH−12]=(∑aπ(a)2/b(a))H\E_b[w_{H-1}^2] = \big(\sum_a\pi(a)^2/b(a)\big)^H. In the widget's task the target picks action 1 with probability p=(1+m)/2p = (1+m)/2 against the behavior's 12\tfrac12, which makes this (1+m2)H(1 + m^2)^H: 1.54 at the default H=5H = 5, m=0.3m = 0.3; 13.3 at H=30H = 30; and about 10910^9 at H=30H = 30 with a deterministic target (m=1m = 1).

Check your understanding

In the widget's task a reward arrives at every step. Why does per-decision importance sampling spread less than trajectory-wise importance sampling?

  1. Because it is biased toward the behavior policy's value
  2. Because it normalizes by the total weight
  3. Because each reward is multiplied only by the ratios of the actions before it, so early rewards carry short products
  4. 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 Q^\hat Q inside importance sampling as a control variate. With V^(s)=∑aπ(a∣s)Q^(s,a)\hat V(s) = \sum_a\pi(a\mid s)\hat Q(s,a) and w−1=1w_{-1} = 1:

J^DR=1n∑i=1n∑t=0H−1γt(wt(i)(Rt+1(i)−Q^(St(i),At(i)))+wt−1(i) V^(St(i)))\hat J_{\text{DR}} = \frac1n\sum_{i=1}^n\sum_{t=0}^{H-1}\gamma^t\Big(w^{(i)}_t\big(R^{(i)}_{t+1} - \hat Q(S^{(i)}_t, A^{(i)}_t)\big) + w^{(i)}_{t-1}\,\hat V\big(S^{(i)}_t\big)\Big)
DerivationTwo ways to read the same estimator
  1. J^DR=J^PDIS+1n∑i∑tγt wt−1(i)(V^(St(i))−ρt(i) Q^(St(i),At(i)))\hat J_{\text{DR}} = \hat J_{\text{PDIS}} + \frac1n\sum_{i}\sum_{t}\gamma^t\,w^{(i)}_{t-1}\Big(\hat V\big(S^{(i)}_t\big) - \rho^{(i)}_t\,\hat Q\big(S^{(i)}_t, A^{(i)}_t\big)\Big)

    Importance sampling plus zero-mean corrections. Regroup by weight. The rewards form per-decision IS, and the model terms come in pairs:

  2. Eb[ρt Q^(St,At) ∣ St]=∑ab(a∣St) π(a∣St)b(a∣St) Q^(St,a)=V^(St)\E_b\big[\rho_t\,\hat Q(S_t,A_t)\,\big|\,S_t\big] = \sum_a b(a\mid S_t)\,\frac{\pi(a\mid S_t)}{b(a\mid S_t)}\,\hat Q(S_t,a) = \hat V(S_t)

    Given the history up to StS_t, the weight wt−1w_{t-1} is fixed and At∼b(⋅∣St)A_t\sim b(\cdot\mid S_t), so each pair has conditional mean zero. By the tower property E[J^DR]=E[J^PDIS]=J(π)\E[\hat J_{\text{DR}}] = \E[\hat J_{\text{PDIS}}] = J(\pi), whatever Q^\hat Q is:

  3. J^DR=1n∑i(V^(S0(i))+∑tγt wt(i) δ^t(i)),δ^t=Rt+1+γV^(St+1)−Q^(St,At)\hat J_{\text{DR}} = \frac1n\sum_i\Big(\hat V\big(S^{(i)}_0\big) + \sum_t\gamma^t\,w^{(i)}_t\,\hat\delta^{(i)}_t\Big),\qquad \hat\delta_t = R_{t+1} + \gamma\hat V(S_{t+1}) - \hat Q(S_t,A_t)

    The model's value plus weighted TD errors. Regroup by time instead, with V^(SH)=0\hat V(S_H) = 0: 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:

  4. If Q^=Qπ\hat Q = Q^\pi, every δ^t\hat\delta_t has conditional mean zero given (St,At)(S_t, A_t), and the large weights multiply only the irreducible noise of a single step. If Q^=0\hat Q = 0, the estimator is exactly per-decision IS. And if the propensities themselves are estimated, the estimator remains consistent when either the propensity model or Q^\hat Q is correct, which is where the name comes from.

python
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 episodes

The same model as the direct method, used differently: DM reports V^(S0)\hat V(S_0); DR reports V^(S0)\hat V(S_0) plus importance-weighted corrections that cancel the model's bias in expectation.

Check your understanding

What does the doubly robust estimator become when the model is Q^≡0\hat Q\equiv0?

  1. The direct method
  2. Weighted importance sampling
  3. Zero
  4. Per-decision importance sampling
Show the answer

D. Per-decision importance sampling

With Q^=0\hat Q = 0 and V^=0\hat V = 0, only the terms γtwtRt+1\gamma^tw_tR_{t+1} 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

EstimatorBiasVarianceNeeds
Direct method (FQE, or a dynamics model)from model error; does not shrink with datalowa model class that fits QπQ^\pi
Trajectory-wise ISnonegrows like (1+χ2)H(1+\chi^2)^Hcorrect propensities
Per-decision ISnonelower when rewards come earlycorrect propensities
Weighted IS (and per-decision)O(1/n)O(1/n), toward the behavior's valuebounded by the range of returnscorrect propensities
Doubly robustnone, with correct propensitiesimportance-sampling-like, reduced where Q^≈Qπ\hat Q\approx Q^\piboth

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 (max⁡π/b)H(\max\pi/b)^H, 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 (1+χ2)H(1+\chi^2)^H 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 Q^\hat Q, and low-variance when Q^≈Qπ\hat Q\approx Q^\pi.
  • 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.

Question 1

Inverse propensity scoring weights each logged reward by π(a∣x)/b(a∣x)\pi(a\mid x)/b(a\mid x). Which assumption is essential for it to be unbiased?

  1. The target policy is deterministic
  2. Overlap, b(a∣x)>0b(a\mid x) > 0 whenever π(a∣x)>0\pi(a\mid x) > 0, with the logged propensities correct
  3. Rewards are bounded in [0,1][0,1]
  4. The behavior policy is uniformly random
Show the answer

B. Overlap, b(a∣x)>0b(a\mid x) > 0 whenever π(a∣x)>0\pi(a\mid x) > 0, with the logged propensities correct

Ea∼b[π(a∣x)b(a∣x)r]=∑aπ(a∣x)E[r∣x,a]\E_{a\sim b}\big[\tfrac{\pi(a\mid x)}{b(a\mid x)}r\big] = \sum_a\pi(a\mid x)\E[r\mid x,a] requires every action π\pi takes to have positive, correctly recorded probability under bb. Determinism, bounded rewards and uniform logging are all optional.

Question 2

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?

  1. It shrinks like 1/n1/\sqrt n
  2. It shrinks like 1/n1/n
  3. It grows
  4. 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 H=5H = 5 and mismatch 0.3, DM's bias is −0.10-0.10, −0.10-0.10 and −0.09-0.09 at 10, 100 and 1,000 episodes.

Question 3

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?

  1. Trajectory-wise importance sampling, since it is unbiased
  2. Weighted importance sampling, since it is bounded
  3. The direct method: almost no logged episode follows the target for 30 steps, so nearly every weight is zero
  4. 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 2−30≈10−92^{-30}\approx10^{-9}, 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.

Question 4

Why is the doubly robust estimator unbiased for any model Q^\hat Q, given correct propensities?

  1. Its model terms enter in pairs wt−1(V^(St)−ρtQ^(St,At))w_{t-1}\big(\hat V(S_t) - \rho_t\hat Q(S_t,A_t)\big), each with conditional mean zero, because Eb[ρtQ^(St,At)∣St]=V^(St)\E_b[\rho_t\hat Q(S_t,A_t)\mid S_t] = \hat V(S_t)
  2. Because it averages the direct method and importance sampling, whose biases cancel
  3. Because model errors are symmetric around zero
  4. Because it normalizes the importance weights
Show the answer

A. Its model terms enter in pairs wt−1(V^(St)−ρtQ^(St,At))w_{t-1}\big(\hat V(S_t) - \rho_t\hat Q(S_t,A_t)\big), each with conditional mean zero, because Eb[ρtQ^(St,At)∣St]=V^(St)\E_b[\rho_t\hat Q(S_t,A_t)\mid S_t] = \hat V(S_t)

Remove the zero-mean pairs and what remains is per-decision importance sampling, which is unbiased. The model affects only the variance.

Question 5

Voloshin et al. (2021) studied how OPE errors grew as the horizon and the policy mismatch increased. What did they find?

  1. The horizon was more damaging: errors grew about a hundredfold, while mismatch mattered little
  2. The mismatch was more damaging: a comparable increase degraded accuracy roughly a hundredfold, against roughly tenfold for the horizon
  3. Neither mattered for FQE
  4. 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 (max⁡π/b)H(\max\pi/b)^H, the larger per-step divergence hurt about ten times more than the longer horizon. That matches the variance formula: the per-step factor 1+χ2(π ∥ b)1 + \chi^2(\pi\,\|\,b) is raised to the power HH, so the mismatch sets the base of the exponential.

Go deeper

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).