Chapter 06 · Part 2: Learning values
Off-policy evaluation as importance sampling
Evaluating a new policy from data logged by another is importance sampling, and its variance grows exponentially with the horizon unless you reweight smartly or add a control variate.
Chapter 4 estimated from episodes generated by itself. Often that is impossible: the policy we want to assess has not been deployed, and deploying it to find out is expensive or dangerous (a clinical protocol, a pricing rule, a recommender). What we have instead is a log of trajectories generated by the policy that was running, . Off-policy evaluation asks: from data drawn under , estimate . Statistically, this is the problem of estimating a mean under one distribution from a sample of another. Importance sampling is the standard answer, and everything difficult about it is already visible in the textbook version.
Notation. A trajectory is of fixed length (the horizon), with the reward written for the one received after ; so (the notation page's indexing just shifts the subscript). The importance ratio at step is and the cumulative ratio is . The target value is .
Why does reweighting give an unbiased estimate?
The probability of a trajectory under policy factorises as
and the initial-state and environment factors are the same for and . Hence the likelihood ratio of a whole trajectory is a product of policy ratios only; the unknown dynamics cancel:
This cancellation is why OPE is possible without a model. The coverage (support) condition is whenever . Under it,
Without coverage, trajectories that would produce are never observed, no reweighting can recover them, and the estimator is biased toward the covered part (generally downward for nonnegative rewards). The ordinary importance-sampling (IS) estimator from logged trajectories is
It is an unbiased sample mean of iid terms , so the CLT and every other first-course tool applies. Whether those tools are any use depends on , to which we turn after a small case.
A worked example by hand
One state, two actions , horizon . The logged policy is and the target is , so the ratios are , . Five logged samples :
- IS. .
- Weighted IS. Divide by the sum of weights rather than : , so .
- Why the two differ. The sample happened to contain in of draws rather than , so the weights sum to rather than . IS ignores that fluctuation. WIS corrects for it by normalising, at the cost of dividing by a random quantity.
- Effective sample size. out of .
- Doubly robust. Suppose a model says both actions have mean reward , so . Then .
- One-step variance. , so .
The script prints the same five numbers. None of them is "the answer" because we do not know the truth; with a larger sample all four estimators agree.
Why does the variance grow exponentially with the horizon?
The cumulative ratio is a product of random factors. Suppose, as in the experiment below, that does not depend on the state. By the tower property,
The last equality identifies as the chi-squared divergence between the two action distributions. So at every while the variance is : the weights are a martingale with mean one and a spread that compounds multiplicatively. In the example above , so at the variance is and the standard deviation about , for an estimator whose target is a number near .
An extreme case makes the mechanism plain. Let be deterministic and uniform over two actions. Then is if chose 's action and otherwise, and is with probability and otherwise. Mean ; variance . For , one trajectory in about a million carries all the weight, and a dataset of trajectories almost surely contains none of them. The estimator is unbiased and its realised value is zero.
What does self-normalising buy?
The weighted (self-normalised) IS estimator divides by the sum of the weights instead of :
It is a ratio of two sample means, with and . Three properties follow from ratio-estimator theory:
- Consistent. By the law of large numbers, .
- Biased, with bias of order . The delta method gives
The bias is small relative to the standard deviation when the weights are mild, and the bias formula itself blows up with when they are not. 3. Bounded by the data. is a weighted average of the observed , so it always lies between and . It cannot report a value of because one weight was huge: a huge weight enters numerator and denominator alike, and the estimate then tends to that single trajectory's return.
That last point is both its strength (stable) and its weakness: when one weight dominates, WIS is simply the return of one trajectory, silently. Weighted IS was already known in the Monte Carlo literature; Kong (1992) studied standardised weights, and Precup, Sutton and Singh (2000) used both versions in the reinforcement-learning setting.
Which weights can be dropped? Per-decision importance sampling
IS multiplies the whole return by the whole ratio. But reward cannot depend on actions taken after time , so the later ratios should not be needed to correct . Write and examine one term. Conditional on the history through , the future ratio satisfies , so
The per-decision IS (PDIS) estimator therefore uses
which is unbiased and, because it removes a mean-one factor of noise from each term, typically has lower variance than IS; this is the construction of Precup, Sutton and Singh (2000). The statistical point is the same as in chapter 5: replace a random factor by its known conditional expectation. It does not remove the exponential growth. The last term, , is as heavy-tailed as before, and only terms with a small benefit.
How many samples does the weighting leave? Effective sample size
If were iid with variance , the weighted mean with normalised weights has variance . Matching this to defines
It equals when all weights are equal and when one weight carries everything. This is the importance-sampling effective sample size due to Kong (1992). In expectation , so with the weights above it shrinks like .
Two warnings. The formula uses only the weights, not the returns , and the derivation assumes the values do not depend on the weights; it is a rule of thumb for how much the weights hurt, not a confidence statement. Elvira, Martino and Robert (2022) discuss how many approximations sit behind it. And when the weights are heavy-tailed, the realised can be misleadingly optimistic in a finite sample because the largest weights have not yet been seen, as the experiment shows.
How can a model help without introducing bias? Doubly robust estimation
Suppose we have a model of the action value (from a regression, a simulator, or anything else) and . Start with one step, (a contextual bandit), where Dudík, Langford and Li (2011) introduced the estimator:
Rewrite it as IS minus a correction: . The term in parentheses has mean zero under because .
The name comes from two ways of being right. If the weights are correct (here, is known) the estimator is unbiased for any ; if is correct, the correction has mean zero and the estimate is accurate even when is misestimated. For logged data where must itself be estimated, that second property is the one that matters.
For sequences, Jiang and Li (2016) apply the same idea at every step through a backward recursion. With and the model values with steps remaining, the estimator for horizon is the average over trajectories of
Each step has the PDIS term minus a mean-zero control . It is unbiased when the model is fixed independently of the logged data. Thomas and Brunskill (2016) extend DR with a weighted version and a way (the MAGIC estimator) of blending the model-based and importance-sampling estimates to minimise MSE; the idea is optional for this course but is the natural next step.
What do the estimators do as the horizon grows? An experiment
ch06_ope.py uses a 3-state, 2-action MDP with Bernoulli rewards. The behaviour policy is uniform. The target policy plays action with probability in every state. Action moves the state up with probability and earns more reward. The true value for each horizon is computed exactly by dynamic programming. For each , we generate 400 independent datasets of 500 trajectories and apply five estimators to the first steps. The doubly robust estimator and the "direct method" (DM; the model's own estimate, no data) both use the same deliberately imperfect model: reward means shifted by to and transitions blurred toward uniform. The model is fixed in advance, which keeps DR unbiased. We report bias (mean over datasets minus truth), standard deviation, and RMSE across datasets, plus the effective sample size of the weights.
Hand example: b = (0.5, 0.5), pi = (0.8, 0.2), 5 samples
ratios rho : [1.6 1.6 1.6 0.4 0.4]
IS : 0.7200
WIS : 0.6429
DR (qhat=0.5,0.5): 0.6600
ESS : 3.9200
E_b[rho^2] : 1.3600
Setup: 3-state MDP, 2 actions, b uniform, pi plays action 1 w.p. 0.8
400 datasets x 500 trajectories; per-step E_b[rho^2] = 1.360
Bias
H truth IS WIS PDIS DM DR
1 0.5600 -0.0004 -0.0003 -0.0004 -0.0167 -0.0002
2 1.1520 0.0059 0.0034 0.0022 -0.0461 0.0010
3 1.7600 0.0013 0.0028 0.0028 -0.0865 0.0026
5 2.9948 -0.0052 0.0043 0.0012 -0.1825 0.0042
8 4.8603 -0.0097 -0.0164 0.0019 -0.3385 0.0018
10 6.1056 -0.0178 -0.0296 0.0007 -0.4441 -0.0003
15 9.2195 0.0379 -0.0716 0.0671 -0.7087 0.0557
20 12.3335 1.1777 -0.1730 0.1028 -0.9734 0.0375
Std dev
H truth IS WIS PDIS DM DR
1 0.5600 0.0323 0.0262 0.0323 0.0000 0.0262
2 1.1520 0.0724 0.0449 0.0608 0.0000 0.0409
3 1.7600 0.1292 0.0613 0.0939 0.0000 0.0510
5 2.9948 0.3006 0.1092 0.1831 0.0000 0.0839
8 4.8603 0.8112 0.2232 0.3880 0.0000 0.1407
10 6.1056 1.3811 0.3151 0.5979 0.0000 0.1965
15 9.2195 4.8422 0.6802 1.5291 0.0000 0.4680
20 12.3335 23.5940 0.9764 3.6603 0.0000 0.9353
RMSE
H truth IS WIS PDIS DM DR
1 0.5600 0.0323 0.0262 0.0323 0.0167 0.0262
2 1.1520 0.0727 0.0450 0.0608 0.0461 0.0409
3 1.7600 0.1292 0.0614 0.0940 0.0865 0.0510
5 2.9948 0.3006 0.1093 0.1831 0.1825 0.0840
8 4.8603 0.8112 0.2238 0.3880 0.3385 0.1408
10 6.1056 1.3812 0.3165 0.5979 0.4441 0.1965
15 9.2195 4.8424 0.6840 1.5306 0.7087 0.4713
20 12.3335 23.6234 0.9916 3.6617 0.9734 0.9360
Monte Carlo standard error of each Bias entry = Std dev / sqrt(400)
Effective sample size of the IS weights (n = 500)
H mean ESS n/E[w^2] Var(w) thy Var(w) obs
1 367.7 367.6 0.36 0.36
2 270.6 270.3 0.85 0.85
3 199.3 198.8 1.52 1.51
5 108.7 107.5 3.65 3.63
8 48.4 42.7 10.70 10.71
10 32.7 23.1 20.65 20.76
15 16.1 5.0 99.71 99.21
20 10.8 1.1 467.57 1118.78Variance. The standard deviation of ordinary IS grows from at to at , a factor of about , for a target that grows only from to . PDIS is lower at every horizon ( at ) but still grows exponentially. WIS and DR have standard deviations below at .
Bias. IS, PDIS and DR have no bias in theory. The bias column shows noise of the size predicted by the standard error line: at the IS entry, , equals , so it is indistinguishable from zero (and is not evidence that IS is biased). WIS, in contrast, shows real bias that grows with , to at (about standard errors from zero): the expected cost of normalising. DM's bias is large and grows roughly linearly in , to , because its reward errors add up over steps; it has no variance because it uses no data.
RMSE. At the imperfect model is already good enough that DM has the lowest RMSE (): with one step, there is little for the model to get wrong. From , DR is the best or tied-best of the data-using estimators ( vs WIS at ; vs at ), as the control variate removes the part of the noise the model explains. At IS has RMSE , PDIS , and WIS, DM, DR are close at , , . By then, with trajectories, no estimator that depends on the weights alone is accurate: WIS is stable because it has retreated to a few trajectories, and DR is accurate because the model carries most of the load.
Effective sample size. Mean falls from at to at and at , out of . The prediction matches at small ( vs ) but is too pessimistic at large ( vs at ). The theoretical is at , while the average sample variance is : the variance of a heavy-tailed quantity is itself estimated badly. The ESS is best read as a warning light, not a precise count.
Exercises
- Hand check. In the worked example, compute for , and true means , , and verify that it equals . Then compute given Bernoulli rewards.
- Coverage. Let and . What do IS and WIS converge to as ? Compare each limit with , where is the mean reward of action .
- Variance growth. For and compute and the horizon at which first exceeds . Then edit
PIin the script to this policy and compare with the observed . - Change the code and predict. Replace
R_HATby-R_TRUE(a model that is wrong in sign) and predict whether DR's RMSE at beats PDIS. Then run it. Next, setD, n = 100, 5000and predict which estimators' RMSE falls by roughly and which do not. - Optimal coefficient. For the one-step DR estimator with control variate , write the variance-minimising coefficient in place of . When is ?
Chapter 7 turns from evaluating one policy to approximating its value function by regression, which brings the bias of bootstrapping and the variance of the data into one fit.
Sources
- Precup, D., Sutton, R. S. and Singh, S. (2000). Eligibility traces for off-policy policy evaluation. Proceedings of the 17th International Conference on Machine Learning (ICML), 759–766.
- Kong, A. (1992). A note on importance sampling using standardized weights. Technical Report 348, Department of Statistics, University of Chicago.
- Elvira, V., Martino, L. and Robert, C. P. (2022). Rethinking the effective sample size. International Statistical Review (arXiv 2018).
- Dudík, M., Langford, J. and Li, L. (2011). Doubly robust policy evaluation and learning. ICML 2011.
- Jiang, N. and Li, L. (2016). Doubly robust off-policy value evaluation for reinforcement learning. ICML 2016.
- Thomas, P. S. and Brunskill, E. (2016). Data-efficient off-policy policy evaluation for reinforcement learning. arXiv:1604.00923 (the MAGIC estimator).
- Sutton, R. S. and Barto, A. G. (2018). Reinforcement Learning: An Introduction, 2nd edition, MIT Press. Sections 5.5 and 5.9 (off-policy Monte Carlo prediction by importance sampling, including weighted and per-decision versions).