Chapter 07 · Part 2: Learning values
Values as regression, and the winner's curse
Fitting a value function is regression on targets you build yourself; TD's fixed point, LSTD as instrumental variables, and why a max over noisy estimates is biased upward.
Chapter 5 ended with a table of values: one number per state. That is the right object when there are seven states and the wrong one when there are , or when the state is a vector of pixels. We then need the estimate to be a function with far fewer parameters than states, fitted from data. That is a regression problem, and a statistician already has opinions about regression: what the estimand is, what the estimating equation is, when the estimator is consistent, and what goes wrong when a predictor is correlated with the error.
The twist is that in RL the response variable is not given. For Monte Carlo it is the observed return. For TD it is , which depends on the very function being fitted. Most of the chapter is about what that does to the estimator. The last third is about a second, independent trap: when the targets contain a over estimated values, selecting the best-looking option and then reading off its estimate gives a biased number.
What is gradient Monte Carlo estimating?
Fix a policy and write for the on-policy weighting: the long-run (or expected-visit) fraction of time steps spent in state . Let be a feature vector and . The natural loss is the mean squared value error
We cannot evaluate it because is unknown, but we observe with (chapter 1). So regress on by least squares. The population normal equations are
where has rows and . The second equality is the tower property: . Nothing in this is special to RL. is the -weighted orthogonal projection of the true value function onto the span of the features, and least squares converges to it whether or not the linear model is correct. The online version, gradient Monte Carlo, is stochastic gradient descent on the squared error with the return as target:
Two things to notice. First, the "data" are not independent: successive states in one episode are correlated, and the weighting is determined by the policy. Least squares still targets , but standard errors from an iid regression formula would be wrong; the episode, not the step, is the independent unit. Second, the targets are unbiased for and typically have large variance, for the reasons given in chapter 4.
What does semi-gradient TD converge to?
Replace the return by the TD target, as in chapter 5:
This is called semi-gradient because is a squared error whose gradient would also differentiate through the target ; the update ignores that part and treats the target as a constant. It is a Robbins–Monro iteration, so its limit is the root of the mean update. Write
The mean update is , so the fixed point solves :
Compare the two estimating equations. Least squares sets . TD sets . Both are orthogonality conditions between the features and a residual; they differ in which residual. In matrix form, with the transition matrix under and the expected reward vector, the TD condition reads , that is
is the Bellman operator; projects. The fixed point is the projected Bellman equation. When the features can represent exactly, is a fixed point and both estimators agree. When they cannot, they generally differ, and that is the situation the experiment below is built to show.
How far can TD be from the best linear fit? Tsitsiklis and Van Roy (1997) proved that, on-policy, , and with linearly independent features, TD(0) converges with probability one and
The proof is short enough to give. is a -contraction in , because makes by Jensen's inequality (the paper states this for the stationary distribution of an ergodic chain; the same argument works for expected-visit weights in an episodic problem). is non-expansive. Since we have , so
Pythagoras ( is orthogonal to the feature span) gives , and rearranging yields the bound. Two honest remarks. The factor is a worst case and blows up as . And the bound is about bias in the limit; it says nothing about variance. The case for TD over Monte Carlo is the one from chapter 5: a biased target with much lower variance can win in mean squared error at realistic sample sizes. It is not a theorem that it wins, and the experiment below contains a case where it does not.
Why is LSTD an instrumental-variables estimator?
Semi-gradient TD finds the root of by stochastic approximation, with all the step-size tuning that implies. But we can estimate and by sample averages and solve the linear system once. With data , , put and :
This is least-squares temporal-difference learning (Bradtke and Barto, 1996; the extension and a careful account are in Boyan, 2002). It needs no step size, converges to by the law of large numbers, and costs per step with a rank-one update or to solve once.
Now the statistical reading. Suppose the features are rich enough that . Define the Bellman residual noise
By the Bellman equation (the law of total expectation from chapter 1), . Rearranging gives a linear regression model for the reward:
So is the coefficient of a linear model with response , regressor , and error . Why not run OLS? Because the regressor contains , and is not mean zero given : a lucky reward makes large and also tells us something about where we landed. Hence in general, OLS is inconsistent, and the usual name for this is endogeneity (an errors-in-variables problem: the "true" regressor is the expected next-state feature, we see a noisy draw of it).
The textbook repair is an instrument: a variable that is uncorrelated with the error but correlated with the regressor. The features of the current state qualify on both counts:
- Exogeneity: .
- Relevance: is nonsingular on-policy.
The just-identified IV estimator solves , which is . That is exactly .
When the features are not rich enough, the regression model above is misspecified and there is no true . IV still has an estimand: the root of the moment condition , which is . So the same equation has two readings: the stationary point of TD learning, and a method-of-moments estimate whose instruments are the features. The two readings explain why LSTD and TD agree, and why neither agrees with least squares on returns.
How does regression become control? Fitted Q-iteration
So far we evaluated one policy. To find a good policy from a fixed batch of transitions , fit action values with a regression and then act greedily. Fitted Q-iteration (Ernst, Geurts and Wehenkel, 2005, with extremely randomised trees as the regressor) repeats:
- Start from .
- Build responses .
- Fit over a function class.
Neural fitted Q-iteration (Riedmiller, 2005) is the same loop with a multilayer perceptron as the regressor. Each round is a regression with a generated response. The squared loss is the right one because , where is the Bellman optimality operator; regression on therefore targets projected onto the function class. The iteration is "project, apply the operator, project, ..." and inherits the projection error at every step, so errors can compound across rounds.
But look at step 2. The response contains a maximum over estimated values. Even if were unbiased for at every point, would not be unbiased for .
What is the winner's curse?
Let be estimates with . The maximum is a convex function, so Jensen's inequality gives
with equality only when the same arm wins in every sample. The max of unbiased estimates is biased upward. In decision analysis this is the optimizer's curse: choose the option with the best estimated value, and its realised value is expected to fall short of the estimate, even though no estimate was biased (Smith and Winkler, 2006). In RL it was identified for Q-learning with function approximation by Thrun and Schwartz (1993), who observed that generalisation errors in are compounded by the max operator. And because Bellman backups feed the biased max back in as the next target, the bias is not a one-off: it propagates through bootstrapping.
A worked example by hand
Take two arms with true means and two unbiased estimates that are iid . Use the identity :
, and for we have . With that is , so
while . If each estimate is a mean of draws with standard deviation , replace by : the overestimate is . For large with all means equal, . So the bias grows with the noise, shrinks like , and grows (slowly) with the number of options.
Double estimators as sample splitting
The bias arises because the same noise is used twice: once to pick the winner and once to score it. The cure is the one statisticians use for post-selection inference: split the sample. Take two independent estimates and of each arm. Select with one, evaluate with the other:
Given (a function of only), is unbiased for , so . The sign of the bias has flipped: the double estimator is biased downward, by an amount that depends on how often selection picks a suboptimal arm. Double Q-learning (van Hasselt, 2010) maintains two tables , and, on each transition, flips a coin to update one of them using the other to evaluate:
If you have used cross-fitting for double machine learning, this is the same idea: the nuisance function that picks is trained on a different fold from the one that scores. Double DQN (van Hasselt, Guez and Silver, 2016) carries the idea to deep networks without a second network: the online network selects and the slowly moving target network evaluates it. The two networks are correlated, so this is a partial split rather than an independent one, and it reduces the overestimation rather than removing it.
Experiment
ch07_regression.py runs both parts below. Everything is NumPy.
(a) Overestimation as the number of arms and the noise vary
Each of arms has draws from . The single estimator is the largest sample mean. The double estimator selects the arm with the largest mean of the first 10 draws and reports that arm's mean over the other 10. We average over 20,000 replications and print the bias relative to , first with all true means zero, then with means spread evenly over .
(a) Bias of E[estimate of max_a mu_a] - max_a mu_a, n=20 draws per arm
all true means equal (0)
sigma K | single double
0.5 2 | +0.063 -0.001
0.5 10 | +0.172 -0.000
0.5 100 | +0.280 +0.000
1.0 2 | +0.125 -0.002
1.0 10 | +0.344 -0.001
1.0 100 | +0.560 -0.002
2.0 2 | +0.246 -0.007
2.0 10 | +0.689 +0.007
2.0 100 | +1.120 +0.003
means spread over [0, 1]
sigma K | single double
0.5 2 | -0.000 +0.001
0.5 10 | +0.026 -0.062
0.5 100 | +0.140 -0.067
1.0 2 | -0.001 -0.012
1.0 10 | +0.111 -0.144
1.0 100 | +0.347 -0.129
2.0 2 | +0.021 -0.128
2.0 10 | +0.364 -0.258
2.0 100 | +0.818 -0.228Check the hand calculation: for , , , the formula gives , and the simulation prints . The bias of the single estimator is proportional to (it doubles from to ) and grows with , roughly like : at it goes for . When all means are equal the double estimator is unbiased to within simulation error, because whichever arm is chosen has true mean zero. When the means are spread, selection is informative, the single estimator's bias shrinks (the winner is usually the right arm), but the double estimator now underestimates, by up to here: it sometimes picks a worse arm and pays for it, and the held-out half cannot compensate. Neither estimator is uniformly better; the double estimator trades a bias of known sign for one of the opposite sign, and which has the smaller magnitude depends on the gaps and the noise.
(b) LSTD, gradient MC and semi-gradient TD with coarse features
The environment is a random walk on 7 states, started in the middle. Each step moves left or right with probability ; leaving on the right pays reward , leaving on the left pays , and . The features are state aggregation: three indicator features, one for each of the groups , and . So is a step function, and is not representable. The script computes and exactly from and , then runs 200 independent datasets of 100 episodes each through five estimators:
- gradient MC and semi-gradient TD, both online with step size per episode;
- batch least squares on returns ("MC least squares"), the estimator that gradient MC is trying to compute;
- LSTD;
- the naive OLS of on , to show the endogeneity problem.
(b) Random walk, 7 states, gamma=0.9, reward 1 on right exit,
3 aggregated features (states {0,1},{2,3,4},{5,6}).
true v : 0.026 0.057 0.101 0.168 0.271 0.435 0.696
on-policy d : 0.062 0.125 0.188 0.250 0.188 0.125 0.062
MC limit Xw: 0.047 0.047 0.179 0.179 0.179 0.522 0.522
TD fixed Xw: 0.100 0.100 0.184 0.184 0.184 0.403 0.403
weights, MC limit : [0.0466 0.1787 0.522 ]
weights, TD fixed : [0.1002 0.1837 0.4032]
||Pi v - v||_D = 0.0751 (best possible, = MC limit)
||X w_TD - v||_D = 0.0940
TVR bound (1/sqrt(1-g^2)) = 0.1723
100 episodes per run, 200 runs. Error is ||X w - v||_D.
method | mean err | dist to MC limit | dist to TD fixed pt | sd of err
grad MC (SGD) | 0.1136 | 0.0402 | 0.1589 | 0.0314
semi-grad TD (SGD) | 0.0948 | 0.1113 | 0.0195 | 0.0096
MC least squares | 0.0793 | 0.0040 | 0.1334 | 0.0043
LSTD | 0.0962 | 0.1299 | 0.0006 | 0.0070
naive OLS of R on z | 0.2424 | 0.3830 | 0.3617 | 0.0303
LSTD on 20000 episodes: weights [0.0982 0.1805 0.3967] (TD fixed point [0.1002 0.1837 0.4032])What the numbers show.
- The two population solutions differ. The MC limit has weights and the TD fixed point . TD is pulled toward a more even profile: its estimate for the left group is more than twice the MC value, and for the right group is lower. The TD fixed point is slightly worse in the -norm ( against the best-possible ) and inside the Tsitsiklis–Van Roy bound (). The bound is loose here, as expected for a worst-case result.
- Each estimator converges to its own target. In the "dist to" columns, MC least squares sits from the MC limit and from the TD fixed point; LSTD sits from the TD fixed point and from the MC limit. Semi-gradient TD (SGD) is from the TD fixed point, the remainder being step-size noise. Gradient MC (SGD) is closer to the MC limit () than to the TD point, but it is the noisiest method ( standard deviation of the error) at this step schedule.
- LSTD equals the TD fixed point. With 20,000 episodes its weights are against .
- TD is not automatically better here. Among the online methods, semi-gradient TD beats gradient MC on both mean error and spread ( against ; against ). In the batch comparison, MC least squares has lower error and spread than LSTD ( against ). With , binary rewards and episodes of about a dozen steps, returns are not very noisy, so there is little variance for TD to save and its projection bias costs more than it gains. The bias–variance advantage of chapter 5 is a statement about regimes, and this is one where it does not appear for the batch estimators. Longer horizons, noisier rewards, or richer features change it.
- Endogeneity matters. The naive OLS of on has mean error , three times that of LSTD, and is from the TD fixed point in weight space. It uses exactly the same data and the same regressors; the only difference is that LSTD uses as an instrument.
Deadly triad, in one paragraph
Function approximation (here, a linear model), bootstrapping (targets that depend on ), and off-policy data (the sampling distribution is not the target policy's) are each harmless alone. Together, with a semi-gradient update, the mean update no longer comes from a positive-definite and the iteration can diverge, even with linear features and exact expected updates. Sutton and Barto name this the deadly triad (2018, section 11.3); van Hasselt et al. (2018) study empirically how it shows up in deep Q-learning, including the role of overestimation. In the language of this chapter: with off-policy data the sampling weights are not the stationary weights of , the contraction argument fails, and may not be a contraction. Chapter 6's importance ratios fix the bias of the targets but not this.
Exercises
- Pen and paper. Show that for three iid estimates . (Hint: the density of the maximum is ; integrate by parts.) Compare with the value and with .
- Pen and paper. Show that but . Build a two-step example in which this is nonzero, so that OLS of on is inconsistent.
- Change the code and predict. In
experiment_a, double from 20 to 80. Predict the single estimator's bias at , with equal means, then run it. - Change the code and predict. Set
GAMMA = 0.99and rerun part (b). Which moves more, the distance between the MC limit and the TD fixed point, or the Tsitsiklis–Van Roy bound? What happens to the relative variance of gradient MC and TD? - Change the code and predict. Give the random walk one-hot features (replace
GROUPSwithrange(7)andXwith columns). What do you expect the distance between and to be, and why?
Sources
- Sutton, R. S. and Barto, A. G. (2018). Reinforcement Learning: An Introduction, 2nd ed. MIT Press. Chapters 9–11 (approximation, the deadly triad); section 6.7 (maximisation bias and double learning).
- Tsitsiklis, J. N. and Van Roy, B. (1997). An analysis of temporal-difference learning with function approximation. IEEE Transactions on Automatic Control 42(5), 674–690.
- Bradtke, S. J. and Barto, A. G. (1996). Linear least-squares algorithms for temporal difference learning. Machine Learning 22, 33–57.
- Boyan, J. A. (2002). Technical update: least-squares temporal difference learning. Machine Learning 49, 233–246.
- Ernst, D., Geurts, P. and Wehenkel, L. (2005). Tree-based batch mode reinforcement learning. Journal of Machine Learning Research 6, 503–556.
- Riedmiller, M. (2005). Neural fitted Q iteration: first experiences with a data efficient neural reinforcement learning method. ECML 2005, LNCS 3720, 317–328.
- Thrun, S. and Schwartz, A. (1993). Issues in using function approximation for reinforcement learning. Proceedings of the 4th Connectionist Models Summer School.
- Smith, J. E. and Winkler, R. L. (2006). The optimizer's curse: skepticism and postdecision surprise in decision analysis. Management Science 52(3), 311–322.
- van Hasselt, H. (2010). Double Q-learning. Advances in Neural Information Processing Systems 23.
- van Hasselt, H., Guez, A. and Silver, D. (2016). Deep reinforcement learning with double Q-learning. AAAI 2016 (arXiv:1509.06461).
- van Hasselt, H., Doron, Y., Strub, F., Hessel, M., Sonnerat, N. and Modayil, J. (2018). Deep reinforcement learning and the deadly triad. arXiv:1812.02648.