RLS RL, Read as Statistics

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 vπv_\pi from episodes generated by π\pi 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, bb. Off-policy evaluation asks: from data drawn under bb, estimate J(π)=Eπ[G]J(\pi) = \mathbb{E}_\pi[G]. 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 τ=(S1,A1,R1,…,SH,AH,RH)\tau = (S_1, A_1, R_1, \dots, S_H, A_H, R_H) of fixed length HH (the horizon), with the reward written RtR_t for the one received after AtA_t; γ=1\gamma=1 so G=∑t=1HRtG=\sum_{t=1}^H R_t (the notation page's Rt+1R_{t+1} indexing just shifts the subscript). The importance ratio at step tt is ρt=π(At∣St)/b(At∣St)\rho_t = \pi(A_t\mid S_t)/b(A_t\mid S_t) and the cumulative ratio is ρ1:t=∏k=1tρk\rho_{1:t}=\prod_{k=1}^t\rho_k. The target value is θ=J(π)=Eπ[G]\theta = J(\pi)=\mathbb{E}_\pi[G].

Why does reweighting give an unbiased estimate?

The probability of a trajectory under policy μ\mu factorises as

pμ(τ)=p(S1)∏t=1Hμ(At∣St) p(St+1,Rt∣St,At),p_\mu(\tau) = p(S_1)\prod_{t=1}^{H}\mu(A_t\mid S_t)\,p(S_{t+1}, R_t\mid S_t, A_t),

and the initial-state and environment factors are the same for π\pi and bb. Hence the likelihood ratio of a whole trajectory is a product of policy ratios only; the unknown dynamics cancel:

pπ(τ)pb(τ)=∏t=1Hπ(At∣St)b(At∣St)=ρ1:H.\frac{p_\pi(\tau)}{p_b(\tau)} = \prod_{t=1}^H \frac{\pi(A_t\mid S_t)}{b(A_t\mid S_t)} = \rho_{1:H}.

This cancellation is why OPE is possible without a model. The coverage (support) condition is b(a∣s)>0b(a\mid s) > 0 whenever π(a∣s)>0\pi(a\mid s)>0. Under it,

Eb[ρ1:H G]=∑τpb(τ)pπ(τ)pb(τ)G(τ)=Eπ[G],Eb[ρ1:H]=1.\mathbb{E}_b\big[\rho_{1:H}\,G\big] = \sum_\tau p_b(\tau)\frac{p_\pi(\tau)}{p_b(\tau)}G(\tau) = \mathbb{E}_\pi[G], \qquad \mathbb{E}_b[\rho_{1:H}] = 1.

Without coverage, trajectories that π\pi 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 nn logged trajectories is

θ^IS=1n∑i=1nwiGi,wi=ρ1:H(i).\hat\theta_{\mathrm{IS}} = \frac1n\sum_{i=1}^n w_i G_i, \qquad w_i = \rho_{1:H}^{(i)}.

It is an unbiased sample mean of iid terms wiGiw_iG_i, so the CLT and every other first-course tool applies. Whether those tools are any use depends on Var⁡(wG)\operatorname{Var}(wG), to which we turn after a small case.

A worked example by hand

One state, two actions a1,a2a_1,a_2, horizon H=1H=1. The logged policy is b=(0.5,0.5)b=(0.5, 0.5) and the target is π=(0.8,0.2)\pi=(0.8, 0.2), so the ratios are ρ(a1)=1.6\rho(a_1) = 1.6, ρ(a2)=0.4\rho(a_2)=0.4. Five logged samples (a,r)(a, r):

(a1,1), (a1,1), (a1,0), (a2,1), (a2,0).(a_1, 1),\ (a_1, 1),\ (a_1, 0),\ (a_2, 1),\ (a_2, 0).

  • IS. 15(1.6⋅1+1.6⋅1+1.6⋅0+0.4⋅1+0.4⋅0)=3.6/5=0.72\tfrac15(1.6\cdot1 + 1.6\cdot 1 + 1.6\cdot 0 + 0.4\cdot 1 + 0.4\cdot 0) = 3.6/5 = 0.72.
  • Weighted IS. Divide by the sum of weights rather than nn: ∑w=3(1.6)+2(0.4)=5.6\sum w = 3(1.6)+2(0.4) = 5.6, so 3.6/5.6=0.64293.6/5.6 = 0.6429.
  • Why the two differ. The sample happened to contain a1a_1 in 60%60\% of draws rather than 50%50\%, so the weights sum to 5.65.6 rather than 55. IS ignores that fluctuation. WIS corrects for it by normalising, at the cost of dividing by a random quantity.
  • Effective sample size. (∑w)2/∑w2=5.62/(3⋅2.56+2⋅0.16)=31.36/8=3.92(\sum w)^2/\sum w^2 = 5.6^2/(3\cdot 2.56 + 2\cdot 0.16) = 31.36/8 = 3.92 out of 55.
  • Doubly robust. Suppose a model says both actions have mean reward q^=0.5\hat q=0.5, so V^=∑aπ(a)q^(a)=0.5\hat V=\sum_a \pi(a)\hat q(a)= 0.5. Then θ^DR=V^+1n∑iwi(ri−q^(ai))=0.5+15(1.6(0.5)+1.6(0.5)+1.6(−0.5)+0.4(0.5)+0.4(−0.5))=0.5+0.8/5=0.66\hat\theta_{\mathrm{DR}} = \hat V + \frac1n\sum_i w_i(r_i - \hat q(a_i)) = 0.5 + \tfrac15\big(1.6(0.5)+1.6(0.5)+1.6(-0.5)+0.4(0.5)+0.4(-0.5)\big) = 0.5 + 0.8/5=0.66.
  • One-step variance. Eb[ρ2]=0.5(1.6)2+0.5(0.4)2=1.36\mathbb{E}_b[\rho^2] = 0.5(1.6)^2+0.5(0.4)^2 = 1.36, so Var⁡b(ρ)=0.36\operatorname{Var}_b(\rho)=0.36.

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 HH random factors. Suppose, as in the experiment below, that Eb[ρt2∣St,history]=c\mathbb{E}_b[\rho_t^2\mid S_t, \text{history}]=c does not depend on the state. By the tower property,

Eb[ρ1:H2]=cH,Var⁡b(ρ1:H)=cH−1,c=∑aπ(a∣s)2b(a∣s)=1+χ2(π∥b).\mathbb{E}_b[\rho_{1:H}^2] = c^H, \qquad \operatorname{Var}_b(\rho_{1:H}) = c^H - 1, \qquad c = \sum_a\frac{\pi(a\mid s)^2}{b(a\mid s)} = 1+\chi^2(\pi\Vert b).

The last equality identifies c−1c-1 as the chi-squared divergence between the two action distributions. So E[ρ1:H]=1\mathbb{E}[\rho_{1:H}]=1 at every HH while the variance is cH−1c^H-1: the weights are a martingale with mean one and a spread that compounds multiplicatively. In the example above c=1.36c = 1.36, so at H=20H=20 the variance is 1.3620−1≈4681.36^{20}-1\approx 468 and the standard deviation about 2222, for an estimator whose target is a number near 1212.

An extreme case makes the mechanism plain. Let π\pi be deterministic and bb uniform over two actions. Then ρt\rho_t is 22 if bb chose π\pi's action and 00 otherwise, and ρ1:H\rho_{1:H} is 2H2^H with probability 2−H2^{-H} and 00 otherwise. Mean 11; variance 2H−12^H-1. For H=20H=20, one trajectory in about a million carries all the weight, and a dataset of 500500 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 nn:

θ^WIS=∑iwiGi∑iwi.\hat\theta_{\mathrm{WIS}} = \frac{\sum_{i}w_iG_i}{\sum_i w_i}.

It is a ratio of two sample means, Nˉ/Dˉ\bar N/\bar D with E[D]=E[w]=1\mathbb{E}[D]=\mathbb{E}[w]=1 and E[N]=θ\mathbb{E}[N]=\theta. Three properties follow from ratio-estimator theory:

  1. Consistent. By the law of large numbers, Nˉ/Dˉ→θ/1\bar N/\bar D\to\theta/1.
  2. Biased, with bias of order 1/n1/n. The delta method gives

E[θ^WIS]−θ≈1n(θVar⁡(w)−Cov⁡(w,wG)).\mathbb{E}[\hat\theta_{\mathrm{WIS}}] - \theta \approx \frac1n\Big(\theta\operatorname{Var}(w) - \operatorname{Cov}(w, wG)\Big).

The bias is small relative to the standard deviation when the weights are mild, and the bias formula itself blows up with Var⁡(w)\operatorname{Var}(w) when they are not. 3. Bounded by the data. θ^WIS\hat\theta_{\mathrm{WIS}} is a weighted average of the observed GiG_i, so it always lies between min⁡Gi\min G_i and max⁡Gi\max G_i. It cannot report a value of 10510^5 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 RtR_t cannot depend on actions taken after time tt, so the later ratios ρt+1:H\rho_{t+1:H} should not be needed to correct RtR_t. Write G=∑tRtG=\sum_t R_t and examine one term. Conditional on the history through (St,At,Rt,St+1)(S_t,A_t,R_t,S_{t+1}), the future ratio satisfies Eb[ρt+1:H∣history through St+1]=1\mathbb{E}_b[\rho_{t+1:H}\mid \text{history through } S_{t+1}]=1, so

Eb[ρ1:HRt]=Eb[ρ1:tRt Eb[ρt+1:H∣⋅]]=Eb[ρ1:tRt].\mathbb{E}_b[\rho_{1:H}R_t] = \mathbb{E}_b\big[\rho_{1:t}R_t\,\mathbb{E}_b[\rho_{t+1:H}\mid \cdot]\big] = \mathbb{E}_b[\rho_{1:t}R_t].

The per-decision IS (PDIS) estimator therefore uses

θ^PDIS=1n∑i=1n∑t=1Hρ1:t(i)Rt(i),\hat\theta_{\mathrm{PDIS}} = \frac1n\sum_{i=1}^n\sum_{t=1}^H \rho_{1:t}^{(i)}R_t^{(i)},

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, ρ1:HRH\rho_{1:H}R_H, is as heavy-tailed as before, and only terms with a small tt benefit.

How many samples does the weighting leave? Effective sample size

If YiY_i were iid with variance σ2\sigma^2, the weighted mean ∑iw~iYi\sum_i \tilde w_iY_i with normalised weights w~i=wi/∑jwj\tilde w_i = w_i/\sum_j w_j has variance σ2∑iw~i2=σ2 ∑wi2/(∑wi)2\sigma^2\sum_i\tilde w_i^2 = \sigma^2\,\sum w_i^2/(\sum w_i)^2. Matching this to σ2/neff\sigma^2/n_{\mathrm{eff}} defines

neff=(∑iwi)2∑iwi2.n_{\mathrm{eff}} = \frac{\left(\sum_i w_i\right)^2}{\sum_i w_i^2}.

It equals nn when all weights are equal and 11 when one weight carries everything. This is the importance-sampling effective sample size due to Kong (1992). In expectation neff≈n/Eb[w2]=n/(1+Var⁡(w))n_{\mathrm{eff}}\approx n/\mathbb{E}_b[w^2] = n/(1+\operatorname{Var}(w)), so with the weights above it shrinks like n/cHn/c^H.

Two warnings. The formula uses only the weights, not the returns GG, and the derivation assumes the values YiY_i 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 neffn_{\mathrm{eff}} 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 q^(s,a)\hat q(s,a) of the action value (from a regression, a simulator, or anything else) and V^(s)=∑aπ(a∣s)q^(s,a)\hat V(s)=\sum_a\pi(a\mid s)\hat q(s,a). Start with one step, H=1H=1 (a contextual bandit), where Dudík, Langford and Li (2011) introduced the estimator:

θ^DR=1n∑i=1n[V^(Si)+ρi(Ri−q^(Si,Ai))].\hat\theta_{\mathrm{DR}} = \frac1n\sum_{i=1}^n\Big[\hat V(S_i) + \rho_i\big(R_i - \hat q(S_i, A_i)\big)\Big].

Rewrite it as IS minus a correction: θ^DR=1n∑i[ρiRi−(ρiq^(Si,Ai)−V^(Si))]\hat\theta_{\mathrm{DR}}=\frac1n\sum_i\big[\rho_iR_i - \big(\rho_i\hat q(S_i,A_i) - \hat V(S_i)\big)\big]. The term in parentheses has mean zero under bb because Eb[ρ q^(S,A)∣S]=∑aπ(a∣S)q^(S,a)=V^(S)\mathbb{E}_b[\rho\,\hat q(S,A)\mid S]=\sum_a \pi(a\mid S)\hat q(S,a)=\hat V(S).

The name comes from two ways of being right. If the weights ρ\rho are correct (here, bb is known) the estimator is unbiased for any q^\hat q; if q^\hat q is correct, the correction has mean zero and the estimate is accurate even when bb is misestimated. For logged data where bb 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 q^h\hat q_h and V^h\hat V_h the model values with hh steps remaining, the estimator for horizon HH is the average over trajectories of

∑t=1H[ρ1:t(Rt−q^H−t+1(St,At))+ρ1:t−1 V^H−t+1(St)].\sum_{t=1}^{H}\Big[\rho_{1:t}\big(R_t - \hat q_{H-t+1}(S_t,A_t)\big) + \rho_{1:t-1}\,\hat V_{H-t+1}(S_t)\Big].

Each step has the PDIS term ρ1:tRt\rho_{1:t}R_t minus a mean-zero control ρ1:tq^−ρ1:t−1V^\rho_{1:t}\hat q - \rho_{1:t-1}\hat V. 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 bb is uniform. The target policy π\pi plays action 11 with probability 0.80.8 in every state. Action 11 moves the state up with probability 0.70.7 and earns more reward. The true value J(π)J(\pi) for each horizon is computed exactly by dynamic programming. For each H=1,…,20H=1,\dots,20, we generate 400 independent datasets of 500 trajectories and apply five estimators to the first HH 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 ±0.10\pm0.10 to ±0.15\pm0.15 and transitions blurred 40%40\% 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.

Output
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.78

Variance. The standard deviation of ordinary IS grows from 0.0320.032 at H=1H=1 to 23.623.6 at H=20H=20, a factor of about 730730, for a target that grows only from 0.560.56 to 12.312.3. PDIS is lower at every horizon (3.73.7 at H=20H=20) but still grows exponentially. WIS and DR have standard deviations below 11 at H=20H=20.

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 H=20H=20 the IS entry, 1.181.18, equals 23.6/40023.6/\sqrt{400}, so it is indistinguishable from zero (and is not evidence that IS is biased). WIS, in contrast, shows real bias that grows with HH, to −0.17-0.17 at H=20H=20 (about 3.53.5 standard errors from zero): the expected cost of normalising. DM's bias is large and grows roughly linearly in HH, to −0.97-0.97, because its reward errors add up over steps; it has no variance because it uses no data.

RMSE. At H=1H=1 the imperfect model is already good enough that DM has the lowest RMSE (0.0170.017): with one step, there is little for the model to get wrong. From H=2H=2, DR is the best or tied-best of the data-using estimators (0.0510.051 vs WIS 0.0610.061 at H=3H=3; 0.1970.197 vs 0.3170.317 at H=10H=10), as the control variate removes the part of the noise the model explains. At H=20H=20 IS has RMSE 23.623.6, PDIS 3.73.7, and WIS, DM, DR are close at 0.990.99, 0.970.97, 0.940.94. By then, with 500500 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 neffn_{\mathrm{eff}} falls from 368368 at H=1H=1 to 3333 at H=10H=10 and 1111 at H=20H=20, out of 500500. The prediction n/cHn/c^H matches at small HH (367.6367.6 vs 367.7367.7) but is too pessimistic at large HH (1.11.1 vs 10.810.8 at H=20H=20). The theoretical Var⁡(w)=cH−1\operatorname{Var}(w)=c^H-1 is 468468 at H=20H=20, while the average sample variance is 11191119: 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

  1. Hand check. In the worked example, compute Eb[ρ r]\mathbb{E}_b[\rho\, r] for b=(0.5,0.5)b=(0.5,0.5), π=(0.8,0.2)\pi=(0.8,0.2) and true means q(a1)=0.6q(a_1)=0.6, q(a2)=0.4q(a_2)=0.4, and verify that it equals Eπ[r]\mathbb{E}_\pi[r]. Then compute Var⁡b(ρr)\operatorname{Var}_b(\rho r) given Bernoulli rewards.
  2. Coverage. Let b=(1,0)b=(1,0) and π=(0.8,0.2)\pi=(0.8,0.2). What do IS and WIS converge to as n→∞n\to\infty? Compare each limit with 0.8q(a1)+0.2q(a2)0.8q(a_1)+0.2q(a_2), where q(a)q(a) is the mean reward of action aa.
  3. Variance growth. For π=(0.9,0.1)\pi=(0.9, 0.1) and b=(0.5,0.5)b=(0.5,0.5) compute c=∑aπ(a)2/b(a)c=\sum_a\pi(a)^2/b(a) and the horizon at which Var⁡(ρ1:H)\operatorname{Var}(\rho_{1:H}) first exceeds 10001000. Then edit PI in the script to this policy and compare with the observed Var⁡(w)\operatorname{Var}(w).
  4. Change the code and predict. Replace R_HAT by -R_TRUE (a model that is wrong in sign) and predict whether DR's RMSE at H=10H=10 beats PDIS. Then run it. Next, set D, n = 100, 5000 and predict which estimators' RMSE falls by roughly 10\sqrt{10} and which do not.
  5. Optimal coefficient. For the one-step DR estimator with control variate Z=ρq^Z=\rho\hat q, write the variance-minimising coefficient β∗\beta^* in place of 11. When is β∗=1\beta^*=1?

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