Chapter 9

On-policy Prediction with Approximation

A table cannot generalize. Every method of Part I, up to the Dyna and tree-search planners of Chapter 8, stores one number per state (or per state–action pair), and learning about one state teaches nothing about any other. In a game of backgammon, a camera image or a robot’s sensor readings, almost every state the agent meets is new, so this is no longer a question of memory alone: a table would never be filled. In this chapter we replace the table by a parameterized function v^(s,𝐰)≈vπ(s)\hat v(s, \mathbf{w}) \approx v_\pi(s) with a weight vector 𝐰∈ℝd\mathbf{w} \in \mathbb{R}^d, d≪∣𝒮∣d \ll |\mathcal{S}|, and ask what happens to the prediction problem of Chapter 6 when changing one weight changes the estimated value of many states at once.Richard S. Sutton and Andrew G. Barto. Reinforcement Learning: An Introduction. Adaptive Computation and Machine Learning. MIT Press, Cambridge, MA, second edition, 2018. ISBN 978-0-262-03924-6. URL http://incompleteideas.net/book/the-book-2nd.html The answer has three parts: an objective that says which errors matter, the semi-gradient methods that bootstrap towards it, and, for linear functions, a complete theory of where semi-gradient TD converges and how far that point can be from the best possible one.

The policy π\pi is fixed throughout; control is the subject of Chapter 10. One consequence of approximation is worth stating at once. If v^\hat v cannot depend on some aspect of the state, the agent behaves as if that aspect were unobservable; all results of this chapter therefore hold unchanged for partially observable problems, in which the “state” is whatever the features see. What approximation cannot do is add memory of past observations to the state.

9.1Value Prediction as Supervised Learning

Every prediction method of Part I can be written as a sequence of individual updates s↦us \mapsto u: the estimated value of state ss is moved towards an update target uu. Monte Carlo uses St↦GtS_t \mapsto G_t, TD(0) uses St↦Rt+1+γv^(St+1,𝐰t)S_t \mapsto R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}_t), nn-step TD uses St↦Gt:t+nS_t \mapsto G_{t:t+n}, and the DP backup uses s↦𝔼π[Rt+1+γv^(St+1,𝐰t)∣St=s]s \mapsto \mathbb{E}_\pi[R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}_t) \mid S_t = s]. In the tabular case an update touched one entry; now we hand the pair (s,u)(s, u) to a learning method as a training example of the input–output behavior we want, and let it decide how to change 𝐰\mathbf{w}. Value prediction becomes regression, and in principle any method of supervised learning may be used.

Two features of reinforcement learning restrict the choice. First, data arrive one at a time while the agent acts, so the learner must be incremental and online; methods that need a fixed training set and many passes over it fit poorly. Second, the targets are nonstationary. In control the policy, and with it vπv_\pi, changes as learning proceeds; and even for a fixed policy every bootstrapped target contains the current estimate v^(⋅,𝐰t)\hat v(\cdot, \mathbf{w}_t), so the regression problem moves as we solve it. Supervised learning with moving targets is the theme of the chapter.

9.2The Prediction Objective

With fewer weights than states we cannot make every value right: improving one state makes others worse. We must say which states we care about, through a distribution μ(s)≥0\mu(s) \ge 0, ∑sμ(s)=1\sum_s \mu(s) = 1, and measure the mean squared value error

VE̅(𝐰)≐∑s∈𝒮μ(s)[vπ(s)−v^(s,𝐰)]2. \overline{\mathrm{VE}}(\mathbf{w}) \doteq \sum_{s \in \mathcal{S}} \mu(s)\bigl[v_\pi(s) - \hat v(s, \mathbf{w})\bigr]^2 . (9.1)

Its square root is a typical size of the error in value units. The natural weighting, and the only one studied in this chapter, is the fraction of time spent in each state while following π\pi: the on-policy distribution. In a continuing task it is the stationary distribution of the Markov chain that π\pi induces, μ⊤=μ⊤𝐏\mu^\top = \mu^\top\mathbf{P} with 𝐏ss′=∑aπ(a∣s) p(s′∣s,a)\mathbf{P}_{s s'} = \sum_a \pi(a \mid s)\,p(s' \mid s, a) (see Section A.3). In an episodic task, let h(s)h(s) be the probability that an episode starts in ss and η(s)\eta(s) the expected number of visits to ss in one episode. Time is spent in ss either by starting there or by arriving from a state s¯\bar s in which time was spent:

η(s)=h(s)+∑s¯η(s¯)∑aπ(a∣s¯) p(s∣s¯,a),μ(s)=η(s)∑s′η(s′). \eta(s) = h(s) + \sum_{\bar s} \eta(\bar s)\sum_a \pi(a \mid \bar s)\,p(s \mid \bar s, a), \qquad \mu(s) = \frac{\eta(s)}{\sum_{s'} \eta(s')} . (9.2)

In matrix form η⊤=h⊤(𝐈−𝐏)−1\eta^\top = h^\top(\mathbf{I} - \mathbf{P})^{-1}, where 𝐏\mathbf{P} is now substochastic (rows of states that may terminate sum to less than one). Discounting can be folded in as a probability 1−γ1 - \gamma of terminating at each step, by writing γ𝐏\gamma\mathbf{P} in place of 𝐏\mathbf{P}.

A weight vector 𝐰∗\mathbf{w}^* is a global optimum if VE̅(𝐰∗)≤VE̅(𝐰)\overline{\mathrm{VE}}(\mathbf{w}^*) \le \overline{\mathrm{VE}}(\mathbf{w}) for all 𝐰\mathbf{w}, a local one if this holds in a neighborhood of 𝐰∗\mathbf{w}^*. For linear v^\hat v the two coincide; for neural networks a local optimum is usually the best that can be promised.

It is not obvious that VE̅\overline{\mathrm{VE}} is the right objective: we learn values in order to choose better actions, and the value function that ranks actions best need not have the smallest squared error. Other objectives, built on the Bellman error, appear in Chapter 11. For on-policy prediction VE̅\overline{\mathrm{VE}} is clear and measurable, and it will turn out that the bootstrapping methods we use do not even minimize it; they come provably close.

9.3Stochastic-Gradient and Semi-Gradient Methods

Assume for a moment that each training example carries the true value, St↦vπ(St)S_t \mapsto v_\pi(S_t), with St∼μS_t \sim \mu. Stochastic gradient descent (SGD) moves the weights a small step against the gradient of the squared error on that one example:

𝐰t+1≐𝐰t−12α∇[vπ(St)−v^(St,𝐰t)]2=𝐰t+α[vπ(St)−v^(St,𝐰t)]∇v^(St,𝐰t), \begin{aligned} \mathbf{w}_{t+1} &\doteq \mathbf{w}_t - \tfrac{1}{2}\alpha\nabla\bigl[v_\pi(S_t) - \hat v(S_t, \mathbf{w}_t)\bigr]^2 \\ &= \mathbf{w}_t + \alpha\bigl[v_\pi(S_t) - \hat v(S_t, \mathbf{w}_t)\bigr]\nabla\hat v(S_t, \mathbf{w}_t), \end{aligned}

where ∇f(𝐰)=(∂f/∂w1,…,∂f/∂wd)⊤\nabla f(\mathbf{w}) = (\partial f/\partial w_1, \dots, \partial f/\partial w_d)^\top. Its expectation over St∼μS_t \sim \mu is exactly −α2∇VE̅(𝐰t)-\frac{\alpha}{2}\nabla\overline{\mathrm{VE}}(\mathbf{w}_t): the per-example step is an unbiased sample of the full gradient step. The step is small on purpose: correcting each example completely would undo the balance between states that an approximation must strike.

The true value is unknown, so we substitute a target UtU_t:

𝐰t+1≐𝐰t+α[Ut−v^(St,𝐰t)]∇v^(St,𝐰t). \mathbf{w}_{t+1} \doteq \mathbf{w}_t + \alpha\bigl[U_t - \hat v(S_t, \mathbf{w}_t)\bigr]\nabla\hat v(S_t, \mathbf{w}_t). (9.3)

The derivation above needed only 𝔼[Ut∣St=s]=vπ(s)\mathbb{E}[U_t \mid S_t = s] = v_\pi(s) and that UtU_t does not depend on 𝐰t\mathbf{w}_t. The Monte Carlo return satisfies both.

Proposition 9.1 (Gradient Monte Carlo Descends the Value Error). Let Ut=GtU_t = G_t in (9.3), with the weights held fixed during each episode and the accumulated update applied at its end. The expected update of one episode is

𝔼[∑t=0T−1(Gt−v^(St,𝐰))∇v^(St,𝐰)]=−12(∑sη(s))∇VE̅(𝐰), \mathbb{E}\Bigl[\sum_{t=0}^{T-1}\bigl(G_t - \hat v(S_t, \mathbf{w})\bigr)\nabla\hat v(S_t, \mathbf{w})\Bigr] = -\frac{1}{2}\Bigl(\sum_{s}\eta(s)\Bigr)\nabla\overline{\mathrm{VE}}(\mathbf{w}),

with μ\mu the episodic on-policy distribution (9.2). Consequently, under the step-size conditions of Theorem A.7 and standard regularity (bounded rewards, smooth v^\hat v, bounded iterates), gradient Monte Carlo converges with probability one to a stationary point, in practice a local minimum, of VE̅\overline{\mathrm{VE}}.

Proof. Write the sum over time as a sum over states visited: ∑tf(St)=∑s∑t𝟙{St=s}f(s)\sum_t f(S_t) = \sum_s \sum_t \mathbb{1}\{S_t = s\}f(s). By the Markov property, given St=sS_t = s the return GtG_t has mean vπ(s)v_\pi(s) whatever happened before tt, so

𝔼[∑t𝟙{St=s}(Gt−v^(s,𝐰))]=η(s)(vπ(s)−v^(s,𝐰)). \mathbb{E}\Bigl[\sum_t \mathbb{1}\{S_t = s\}\bigl(G_t - \hat v(s, \mathbf{w})\bigr)\Bigr] = \eta(s)\bigl(v_\pi(s) - \hat v(s, \mathbf{w})\bigr).

Multiplying by ∇v^(s,𝐰)\nabla\hat v(s, \mathbf{w}) and summing over ss gives ∑sη(s)(vπ(s)−v^(s,𝐰))∇v^(s,𝐰)=−12(∑s′η(s′))∇VE̅(𝐰)\sum_s \eta(s)(v_\pi(s) - \hat v(s, \mathbf{w}))\nabla\hat v(s, \mathbf{w}) = -\frac{1}{2}(\sum_{s'}\eta(s'))\nabla\overline{\mathrm{VE}}(\mathbf{w}). The update is therefore a noisy gradient step whose noise has conditional mean zero given the past episodes, which is the setting of the Robbins–Monro theorem for stochastic gradient descent. □

Algorithm 9.2 (Gradient Monte Carlo Prediction). Input: the policy π\pi; a differentiable v^:𝒮×ℝd→ℝ\hat v : \mathcal{S} \times \mathbb{R}^d \to \mathbb{R}. Parameter: step size α>0\alpha > 0. Initialize: 𝐰∈ℝd\mathbf{w} \in \mathbb{R}^d arbitrarily (e.g. 𝐰=𝟎\mathbf{w} = \mathbf{0}).

  1. Loop for each episode:
    1. Generate S0,A0,R1,S1,…,RT,STS_0, A_0, R_1, S_1, \dots, R_T, S_T following π\pi.
    2. For t=0,1,…,T−1t = 0, 1, \dots, T-1: 𝐰←𝐰+α [Gt−v^(St,𝐰)] ∇v^(St,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\,[G_t - \hat v(S_t, \mathbf{w})]\,\nabla\hat v(S_t, \mathbf{w}).

9.3.1Why “Semi”

Now let the target bootstrap, Ut=Rt+1+γv^(St+1,𝐰t)U_t = R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}_t). The target depends on 𝐰t\mathbf{w}_t, so (9.3) is no longer the gradient of the squared error on the example. Differentiating 12[Rt+1+γv^(St+1,𝐰)−v^(St,𝐰)]2\frac{1}{2}[R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}) - \hat v(S_t, \mathbf{w})]^2 honestly would give

−δt[∇v^(St,𝐰)−γ∇v^(St+1,𝐰)], -\delta_t\bigl[\nabla\hat v(S_t, \mathbf{w}) - \gamma\nabla\hat v(S_{t+1}, \mathbf{w})\bigr],

with δt\delta_t the TD error (6.2) computed with v^\hat v. The update (9.3) keeps only the first term: it accounts for the effect of 𝐰\mathbf{w} on the estimate but ignores its effect on the target. It includes only part of a gradient, and we call such methods semi-gradient methods.The name is new in the second edition of Sutton and Barto. The observation that bootstrapping methods are not gradient descent goes back at least to Barnard (1993).Etienne Barnard. Temporal-Difference Methods and Markov Models. IEEE Transactions on Systems, Man, and Cybernetics, 23(2):357–365, 1993 Worse, the expected semi-gradient TD update is in general not the gradient of any function of 𝐰\mathbf{w}, as we will see for linear v^\hat v in Section 9.4: the matrix that drives it is not symmetric. So the convergence theory of SGD does not apply, and a different argument is needed. What semi-gradient methods offer in exchange is what we valued in Part I: lower variance and faster learning, and the ability to learn online and in continuing tasks without waiting for returns.

Algorithm 9.3 (Semi-gradient TD(0) Prediction). Input: the policy π\pi; a differentiable v^:𝒮+×ℝd→ℝ\hat v : \mathcal{S}^+ \times \mathbb{R}^d \to \mathbb{R} with v^(terminal,⋅)=0\hat v(\text{terminal}, \cdot) = 0. Parameter: step size α>0\alpha > 0. Initialize: 𝐰\mathbf{w} arbitrarily.

  1. Loop for each episode: initialize SS; then repeat until SS is terminal:
    1. Choose A∼π(⋅∣S)A \sim \pi(\cdot \mid S), take it, observe R,S′R, S'.
    2. 𝐰←𝐰+α [R+γv^(S′,𝐰)−v^(S,𝐰)] ∇v^(S,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\,[R + \gamma\hat v(S', \mathbf{w}) - \hat v(S, \mathbf{w})]\,\nabla\hat v(S, \mathbf{w}).
    3. S←S′S \leftarrow S'.

This is tabular TD(0), Algorithm 6.1, with the table entry replaced by v^\hat v and the unit change of one entry replaced by the gradient. The simplest generalizing approximator shows the connection most plainly. In state aggregation the states are partitioned into groups, each group has one weight, and v^(s,𝐰)\hat v(s, \mathbf{w}) is the weight of ss’s group. The gradient is the indicator of that group, so an update changes exactly one weight, which is shared by all the states in the group.

9.4Linear Methods

The most important special case, and the one with a complete theory, represents each state by a feature vector 𝐱(s)=(x1(s),…,xd(s))⊤\mathbf{x}(s) = (x_1(s), \dots, x_d(s))^\top and sets

v^(s,𝐰)≐𝐰⊤𝐱(s)=∑i=1dwixi(s),∇v^(s,𝐰)=𝐱(s). \hat v(s, \mathbf{w}) \doteq \mathbf{w}^\top\mathbf{x}(s) = \sum_{i=1}^d w_i x_i(s), \qquad \nabla\hat v(s, \mathbf{w}) = \mathbf{x}(s).

The functions xi:𝒮→ℝx_i : \mathcal{S} \to \mathbb{R} are the basis functions of the approximation. Tabular methods are the special case of one-hot features, 𝐱(s)=𝐞s\mathbf{x}(s) = \mathbf{e}_s (Exercise 9.10), and state aggregation is the case of one-hot group indicators. VE̅\overline{\mathrm{VE}} is a convex quadratic in 𝐰\mathbf{w}, so its local minima are global, and gradient Monte Carlo finds the global minimum.

9.4.1The TD Fixed Point

Consider a continuing task and write 𝐱t=𝐱(St)\mathbf{x}_t = \mathbf{x}(S_t). Linear semi-gradient TD(0) reads

𝐰t+1=𝐰t+α(Rt+1+γ𝐰t⊤𝐱t+1−𝐰t⊤𝐱t)𝐱t=𝐰t+α(Rt+1𝐱t−𝐱t(𝐱t−γ𝐱t+1)⊤𝐰t). \begin{aligned} \mathbf{w}_{t+1} &= \mathbf{w}_t + \alpha\bigl(R_{t+1} + \gamma\mathbf{w}_t^\top\mathbf{x}_{t+1} - \mathbf{w}_t^\top\mathbf{x}_t\bigr)\mathbf{x}_t \\ &= \mathbf{w}_t + \alpha\bigl(R_{t+1}\mathbf{x}_t - \mathbf{x}_t(\mathbf{x}_t - \gamma\mathbf{x}_{t+1})^\top\mathbf{w}_t\bigr). \end{aligned}

The update is linear in 𝐰t\mathbf{w}_t. Once the chain is in its stationary regime, St∼μS_t \sim \mu, the expected next weight is

𝔼[𝐰t+1∣𝐰t]=𝐰t+α(𝐛−𝐀𝐰t),𝐀≐𝔼[𝐱t(𝐱t−γ𝐱t+1)⊤],𝐛≐𝔼[Rt+1𝐱t]. \begin{aligned} \mathbb{E}[\mathbf{w}_{t+1} \mid \mathbf{w}_t] &= \mathbf{w}_t + \alpha(\mathbf{b} - \mathbf{A}\mathbf{w}_t), \\ \mathbf{A} &\doteq \mathbb{E}\bigl[\mathbf{x}_t(\mathbf{x}_t - \gamma\mathbf{x}_{t+1})^\top\bigr], \\ \mathbf{b} &\doteq \mathbb{E}[R_{t+1}\mathbf{x}_t]. \end{aligned} (9.4)

If the iteration converges, it can only converge to the point where the expected update vanishes, the TD fixed point

𝐰TD≐𝐀−1𝐛. \mathbf{w}_{\mathrm{TD}} \doteq \mathbf{A}^{-1}\mathbf{b}. (9.5)

To study 𝐀\mathbf{A}, collect the features as the rows of the ∣𝒮∣×d|\mathcal{S}| \times d matrix 𝐗\mathbf{X}, put μ\mu on the diagonal of 𝐃\mathbf{D}, and let 𝐏\mathbf{P} be the state-transition matrix under π\pi and 𝐫π(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a) r\mathbf{r}_\pi(s) = \sum_a \pi(a \mid s)\sum_{s', r} p(s', r \mid s, a)\,r the expected reward. Conditioning on St=sS_t = s and then on St+1=s′S_{t+1} = s',

𝐀=∑sμ(s)∑s′𝐏ss′ 𝐱(s)(𝐱(s)−γ𝐱(s′))⊤=𝐗⊤𝐃(𝐈−γ𝐏)𝐗,𝐛=𝐗⊤𝐃 𝐫π. \begin{aligned} \mathbf{A} &= \sum_s \mu(s)\sum_{s'}\mathbf{P}_{s s'}\,\mathbf{x}(s)\bigl(\mathbf{x}(s) - \gamma\mathbf{x}(s')\bigr)^\top \\ &= \mathbf{X}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P})\mathbf{X}, \\ \mathbf{b} &= \mathbf{X}^\top\mathbf{D}\,\mathbf{r}_\pi . \end{aligned} (9.6)

Everything about linear TD is in the key matrix 𝐃(𝐈−γ𝐏)\mathbf{D}(\mathbf{I} - \gamma\mathbf{P}). A real (not necessarily symmetric) matrix 𝐌\mathbf{M} is called positive definite if 𝐲⊤𝐌𝐲>0\mathbf{y}^\top\mathbf{M}\mathbf{y} > 0 for every 𝐲≠𝟎\mathbf{y} \neq \mathbf{0}; since 𝐲⊤𝐌𝐲=12𝐲⊤(𝐌+𝐌⊤)𝐲\mathbf{y}^\top\mathbf{M}\mathbf{y} = \frac{1}{2}\mathbf{y}^\top(\mathbf{M} + \mathbf{M}^\top)\mathbf{y}, this holds exactly when the symmetric part 𝐌+𝐌⊤\mathbf{M} + \mathbf{M}^\top is positive definite in the usual sense.

Theorem 9.4 (Stability of On-policy Linear TD(0)). Let γ<1\gamma < 1, let μ\mu be the stationary distribution of 𝐏\mathbf{P} with μ(s)>0\mu(s) > 0 for all ss, and let 𝐗\mathbf{X} have linearly independent columns. Then the key matrix 𝐃(𝐈−γ𝐏)\mathbf{D}(\mathbf{I} - \gamma\mathbf{P}) and 𝐀=𝐗⊤𝐃(𝐈−γ𝐏)𝐗\mathbf{A} = \mathbf{X}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P})\mathbf{X} are positive definite. Hence 𝐀\mathbf{A} is invertible, every eigenvalue of 𝐀\mathbf{A} has positive real part, and the expected iteration 𝐰↦𝐰+α(𝐛−𝐀𝐰)\mathbf{w} \mapsto \mathbf{w} + \alpha(\mathbf{b} - \mathbf{A}\mathbf{w}) converges to 𝐰TD\mathbf{w}_{\mathrm{TD}} for every sufficiently small α>0\alpha > 0.

Proof. The key matrix. Let 𝐌=𝐃(𝐈−γ𝐏)\mathbf{M} = \mathbf{D}(\mathbf{I} - \gamma\mathbf{P}) and 𝐒=𝐌+𝐌⊤\mathbf{S} = \mathbf{M} + \mathbf{M}^\top. The diagonal entries of 𝐒\mathbf{S} are 2μ(s)(1−γ𝐏ss)>02\mu(s)(1 - \gamma\mathbf{P}_{ss}) > 0 and the off-diagonal entries −γ(μ(s)𝐏ss′+μ(s′)𝐏s′s)-\gamma(\mu(s)\mathbf{P}_{s s'} + \mu(s')\mathbf{P}_{s' s}) are ≤0\le 0. A symmetric matrix whose diagonal entries are positive and exceed the sum of the absolute values of the other entries in their row is positive definite: by Gershgorin’s theorem each eigenvalue lies in a disc centered at a diagonal entry with radius smaller than that entry. Because the off-diagonal entries are nonpositive, this strict diagonal dominance says precisely that every row sum of 𝐒\mathbf{S} is positive, and the row sums of 𝐒\mathbf{S} are the row sums of 𝐌\mathbf{M} plus its column sums. The row sums are μ(s)(1−γ∑s′𝐏ss′)=(1−γ)μ(s)\mu(s)(1 - \gamma\sum_{s'}\mathbf{P}_{s s'}) = (1 - \gamma)\mu(s). The column sums form the row vector

𝟏⊤𝐃(𝐈−γ𝐏)=μ⊤−γμ⊤𝐏=μ⊤−γμ⊤=(1−γ)μ⊤, \mathbf{1}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P}) = \mu^\top - \gamma\mu^\top\mathbf{P} = \mu^\top - \gamma\mu^\top = (1 - \gamma)\mu^\top,

where stationarity μ⊤𝐏=μ⊤\mu^\top\mathbf{P} = \mu^\top is used exactly once. Each row of 𝐒\mathbf{S} sums to 2(1−γ)μ(s)>02(1 - \gamma)\mu(s) > 0, so 𝐒\mathbf{S}, and with it 𝐌\mathbf{M}, is positive definite.

The matrix 𝐀\mathbf{A}. For 𝐲≠𝟎\mathbf{y} \neq \mathbf{0}, 𝐗𝐲≠𝟎\mathbf{X}\mathbf{y} \neq \mathbf{0} by independence of the columns, so 𝐲⊤𝐀𝐲=(𝐗𝐲)⊤𝐌(𝐗𝐲)>0\mathbf{y}^\top\mathbf{A}\mathbf{y} = (\mathbf{X}\mathbf{y})^\top\mathbf{M}(\mathbf{X}\mathbf{y}) > 0.

Eigenvalues and the iteration. If 𝐀𝐯=λ𝐯\mathbf{A}\mathbf{v} = \lambda\mathbf{v} with complex 𝐯=𝐲+i𝐳≠𝟎\mathbf{v} = \mathbf{y} + i\mathbf{z} \neq \mathbf{0}, then Re⁡(𝐯¯⊤𝐀𝐯)=𝐲⊤𝐀𝐲+𝐳⊤𝐀𝐳>0\operatorname{Re}(\bar{\mathbf{v}}^\top\mathbf{A}\mathbf{v}) = \mathbf{y}^\top\mathbf{A}\mathbf{y} + \mathbf{z}^\top\mathbf{A}\mathbf{z} > 0 while 𝐯¯⊤𝐀𝐯=λ∥𝐯∥2\bar{\mathbf{v}}^\top\mathbf{A}\mathbf{v} = \lambda\lVert\mathbf{v}\rVert^2; so Re⁡λ>0\operatorname{Re}\lambda > 0, and in particular λ≠0\lambda \neq 0. The error 𝐞k=𝐰k−𝐰TD\mathbf{e}_k = \mathbf{w}_k - \mathbf{w}_{\mathrm{TD}} of the expected iteration obeys 𝐞k+1=(𝐈−α𝐀)𝐞k\mathbf{e}_{k+1} = (\mathbf{I} - \alpha\mathbf{A})\mathbf{e}_k, and the eigenvalues 1−αλ1 - \alpha\lambda of 𝐈−α𝐀\mathbf{I} - \alpha\mathbf{A} satisfy ∣1−αλ∣2=1−2αRe⁡λ+α2∣λ∣2<1|1 - \alpha\lambda|^2 = 1 - 2\alpha\operatorname{Re}\lambda + \alpha^2|\lambda|^2 < 1 as soon as α<2Re⁡λ/∣λ∣2\alpha < 2\operatorname{Re}\lambda/|\lambda|^2 for all λ\lambda. Then 𝐞k→𝟎\mathbf{e}_k \to \mathbf{0}. □

The proof never asked 𝐀\mathbf{A} to be symmetric, and it is not. That settles the remark of Section 9.3.1: the expected update field 𝐛−𝐀𝐰\mathbf{b} - \mathbf{A}\mathbf{w} has Jacobian −𝐀-\mathbf{A}, and a vector field is the gradient of a function only if its Jacobian is symmetric. Linear TD follows no objective’s gradient, yet the field still points inwards, which is all stability needs. Two remarks on the hypotheses. Stationarity of μ\mu entered only through the column sums; if states are updated with some other distribution, the column sums can be negative and TD can diverge, the phenomenon studied in Section 11.3. And the theorem is about the expected update; with probability one convergence of the stochastic iterates requires in addition decreasing step sizes and mixing of the chain, and was proved by Tsitsiklis and Van Roy.John N. Tsitsiklis and Benjamin Van Roy. An Analysis of Temporal-Difference Learning with Function Approximation. IEEE Transactions on Automatic Control, 42(5):674–690, 1997. doi: 10.1109/9.580874

9.4.2How Good Is the TD Fixed Point?

Gradient Monte Carlo finds min⁡𝐰VE̅\min_{\mathbf{w}}\overline{\mathrm{VE}}; TD finds 𝐰TD\mathbf{w}_{\mathrm{TD}}. To compare them, it helps to look at functions on 𝒮\mathcal{S} as vectors in ℝ∣𝒮∣\mathbb{R}^{|\mathcal{S}|} with the inner product ⟨𝐮,𝐯⟩μ=𝐮⊤𝐃𝐯\langle\mathbf{u}, \mathbf{v}\rangle_\mu = \mathbf{u}^\top\mathbf{D}\mathbf{v} and norm ∥𝐯∥μ2=∑sμ(s)v(s)2\lVert\mathbf{v}\rVert_\mu^2 = \sum_s \mu(s)v(s)^2, so that VE̅(𝐰)=∥𝐯π−𝐗𝐰∥μ2\overline{\mathrm{VE}}(\mathbf{w}) = \lVert\mathbf{v}_\pi - \mathbf{X}\mathbf{w}\rVert_\mu^2. The representable functions form the subspace {𝐗𝐰}\{\mathbf{X}\mathbf{w}\}, and the closest one to any 𝐯\mathbf{v} is its orthogonal projection

Π𝐯≐𝐗 𝐰𝐯,𝐰𝐯=arg⁡min⁡𝐰∥𝐯−𝐗𝐰∥μ2,Π=𝐗(𝐗⊤𝐃𝐗)−1𝐗⊤𝐃. \Pi\mathbf{v} \doteq \mathbf{X}\,\mathbf{w}_{\mathbf{v}}, \quad \mathbf{w}_{\mathbf{v}} = \arg\min_{\mathbf{w}}\lVert\mathbf{v} - \mathbf{X}\mathbf{w}\rVert_\mu^2, \qquad \Pi = \mathbf{X}(\mathbf{X}^\top\mathbf{D}\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{D}. (9.7)

So min⁡𝐰VE̅(𝐰)=∥𝐯π−Π𝐯π∥μ2\min_{\mathbf{w}}\overline{\mathrm{VE}}(\mathbf{w}) = \lVert\mathbf{v}_\pi - \Pi\mathbf{v}_\pi\rVert_\mu^2, the best that any method can do with these features. The Bellman operator Tπ𝐯=𝐫π+γ𝐏𝐯T_\pi\mathbf{v} = \mathbf{r}_\pi + \gamma\mathbf{P}\mathbf{v} has 𝐯π\mathbf{v}_\pi as its fixed point; TD has its own:

Proposition 9.5 (TD Solves the Projected Bellman Equation). 𝐀𝐰=𝐛\mathbf{A}\mathbf{w} = \mathbf{b} if and only if 𝐗𝐰=ΠTπ𝐗𝐰\mathbf{X}\mathbf{w} = \Pi T_\pi\mathbf{X}\mathbf{w}.

Proof. 𝐛−𝐀𝐰=𝐗⊤𝐃(𝐫π+γ𝐏𝐗𝐰−𝐗𝐰)=𝐗⊤𝐃(Tπ𝐗𝐰−𝐗𝐰)\mathbf{b} - \mathbf{A}\mathbf{w} = \mathbf{X}^\top\mathbf{D}(\mathbf{r}_\pi + \gamma\mathbf{P}\mathbf{X}\mathbf{w} - \mathbf{X}\mathbf{w}) = \mathbf{X}^\top\mathbf{D}(T_\pi\mathbf{X}\mathbf{w} - \mathbf{X}\mathbf{w}). This vanishes iff the residual Tπ𝐗𝐰−𝐗𝐰T_\pi\mathbf{X}\mathbf{w} - \mathbf{X}\mathbf{w} is μ\mu-orthogonal to every column of 𝐗\mathbf{X}, that is, iff 𝐗𝐰\mathbf{X}\mathbf{w} is the projection of Tπ𝐗𝐰T_\pi\mathbf{X}\mathbf{w}. □

TD does not look for the representable function nearest to 𝐯π\mathbf{v}_\pi; it looks for the representable function that reproduces itself when backed up once and projected back. Two inequalities show that this is a well-posed and nearly optimal request.

Lemma 9.6 (Nonexpansiveness in the μ-norm). If μ\mu is stationary for 𝐏\mathbf{P}, then ∥𝐏𝐲∥μ≤∥𝐲∥μ\lVert\mathbf{P}\mathbf{y}\rVert_\mu \le \lVert\mathbf{y}\rVert_\mu for every 𝐲\mathbf{y}, and ∥Π𝐲∥μ≤∥𝐲∥μ\lVert\Pi\mathbf{y}\rVert_\mu \le \lVert\mathbf{y}\rVert_\mu. Consequently ΠTπ\Pi T_\pi is a γ\gamma-contraction in ∥⋅∥μ\lVert\cdot\rVert_\mu.

Proof. By Jensen’s inequality (∑s′𝐏ss′y(s′))2≤∑s′𝐏ss′y(s′)2\bigl(\sum_{s'}\mathbf{P}_{s s'}y(s')\bigr)^2 \le \sum_{s'}\mathbf{P}_{s s'}y(s')^2; multiplying by μ(s)\mu(s), summing over ss and using ∑sμ(s)𝐏ss′=μ(s′)\sum_s \mu(s)\mathbf{P}_{s s'} = \mu(s') gives ∥𝐏𝐲∥μ2≤∑s′μ(s′)y(s′)2\lVert\mathbf{P}\mathbf{y}\rVert_\mu^2 \le \sum_{s'}\mu(s')y(s')^2. The gap is exactly ∑sμ(s)∑s′𝐏ss′(y(s′)−(𝐏𝐲)(s))2\sum_s\mu(s)\sum_{s'}\mathbf{P}_{s s'}\bigl(y(s') - (\mathbf{P}\mathbf{y})(s)\bigr)^2, a weighted variance. An orthogonal projection never increases a norm: ∥𝐲∥μ2=∥Π𝐲∥μ2+∥𝐲−Π𝐲∥μ2\lVert\mathbf{y}\rVert_\mu^2 = \lVert\Pi\mathbf{y}\rVert_\mu^2 + \lVert\mathbf{y} - \Pi\mathbf{y}\rVert_\mu^2. Finally ∥ΠTπ𝐮−ΠTπ𝐯∥μ≤∥Tπ𝐮−Tπ𝐯∥μ=γ∥𝐏(𝐮−𝐯)∥μ≤γ∥𝐮−𝐯∥μ\lVert\Pi T_\pi\mathbf{u} - \Pi T_\pi\mathbf{v}\rVert_\mu \le \lVert T_\pi\mathbf{u} - T_\pi\mathbf{v}\rVert_\mu = \gamma\lVert\mathbf{P}(\mathbf{u} - \mathbf{v})\rVert_\mu \le \gamma\lVert\mathbf{u} - \mathbf{v}\rVert_\mu. □

By Theorem A.3 the projected Bellman operator has a unique fixed point in the subspace, which by Proposition 9.5 is 𝐗𝐰TD\mathbf{X}\mathbf{w}_{\mathrm{TD}}: a second proof that the TD fixed point exists, and one that explains why the on-policy distribution matters. In any other norm 𝐏\mathbf{P} can expand, and ΠTπ\Pi T_\pi need not contract.

Theorem 9.7 (Error Bound at the TD Fixed Point). Under the hypotheses of Theorem 9.4,

VE̅(𝐰TD)≤11−γ2min⁡𝐰VE̅(𝐰)≤11−γmin⁡𝐰VE̅(𝐰). \overline{\mathrm{VE}}(\mathbf{w}_{\mathrm{TD}}) \le \frac{1}{1 - \gamma^2}\min_{\mathbf{w}}\overline{\mathrm{VE}}(\mathbf{w}) \le \frac{1}{1 - \gamma}\min_{\mathbf{w}}\overline{\mathrm{VE}}(\mathbf{w}). (9.8)

Proof. Write 𝐯=𝐯π\mathbf{v} = \mathbf{v}_\pi and 𝐯^=𝐗𝐰TD\hat{\mathbf{v}} = \mathbf{X}\mathbf{w}_{\mathrm{TD}}. The vector 𝐯^−Π𝐯\hat{\mathbf{v}} - \Pi\mathbf{v} lies in the subspace and Π𝐯−𝐯\Pi\mathbf{v} - \mathbf{v} is orthogonal to it, so by Pythagoras

∥𝐯^−𝐯∥μ2=∥Π𝐯−𝐯∥μ2+∥𝐯^−Π𝐯∥μ2. \lVert\hat{\mathbf{v}} - \mathbf{v}\rVert_\mu^2 = \lVert\Pi\mathbf{v} - \mathbf{v}\rVert_\mu^2 + \lVert\hat{\mathbf{v}} - \Pi\mathbf{v}\rVert_\mu^2 .

For the second term use the two fixed points, 𝐯^=ΠTπ𝐯^\hat{\mathbf{v}} = \Pi T_\pi\hat{\mathbf{v}} and 𝐯=Tπ𝐯\mathbf{v} = T_\pi\mathbf{v}, and the lemma: ∥𝐯^−Π𝐯∥μ=∥ΠTπ𝐯^−ΠTπ𝐯∥μ≤γ∥𝐯^−𝐯∥μ\lVert\hat{\mathbf{v}} - \Pi\mathbf{v}\rVert_\mu = \lVert\Pi T_\pi\hat{\mathbf{v}} - \Pi T_\pi\mathbf{v}\rVert_\mu \le \gamma\lVert\hat{\mathbf{v}} - \mathbf{v}\rVert_\mu. Hence (1−γ2)∥𝐯^−𝐯∥μ2≤∥Π𝐯−𝐯∥μ2=min⁡𝐰VE̅(𝐰)(1 - \gamma^2)\lVert\hat{\mathbf{v}} - \mathbf{v}\rVert_\mu^2 \le \lVert\Pi\mathbf{v} - \mathbf{v}\rVert_\mu^2 = \min_{\mathbf{w}}\overline{\mathrm{VE}}(\mathbf{w}). The second inequality is 1−γ2=(1−γ)(1+γ)≥1−γ1 - \gamma^2 = (1 - \gamma)(1 + \gamma) \ge 1 - \gamma. □

Sutton and Barto state the weaker factor 11−γ\frac{1}{1-\gamma}; the Pythagorean step gives 11−γ2\frac{1}{1-\gamma^2} for free.Tsitsiklis and Van Roy (1997) proved, for linear TD(λ\lambda), the bound ∥𝐯^−𝐯π∥μ≤1−λγ1−γ∥Π𝐯π−𝐯π∥μ\lVert\hat{\mathbf{v}} - \mathbf{v}_\pi\rVert_\mu \le \frac{1 - \lambda\gamma}{1 - \gamma}\lVert\Pi\mathbf{v}_\pi - \mathbf{v}_\pi\rVert_\mu on the root error. The Pythagorean refinement used here, giving the factor 1/1−γ21/\sqrt{1 - \gamma^2} on the root error for λ=0\lambda = 0, is standard in the later literature on projected equations (Bertsekas’s books, for instance). Either way the factor blows up as γ→1\gamma \to 1, and this is the asymptotic price of bootstrapping: the representation errors made at successor states are backed up and accumulate. Monte Carlo pays no such price but learns slowly; as in Part I the right compromise is usually in between. The same argument applies to every on-policy bootstrapping method that solves a projected Bellman equation: linear semi-gradient DP with updates distributed according to μ\mu converges to the same point, nn-step methods replace γ\gamma by γn\gamma^n (Exercise 9.13), and semi-gradient Sarsa with a fixed policy has the analogous bound for action values.

9.4.3In Wolfram Language

All of this can be computed exactly on a three-state chain. From state 1 the agent moves to state 2 with probability pp and otherwise stays; from state 2 it moves to 1 or 3 with equal probability; state 3 returns to state 1 and pays a reward of 1. Two features, 𝐱(1)=(1,0)⊤\mathbf{x}(1) = (1, 0)^\top, 𝐱(2)=(1,1)⊤\mathbf{x}(2) = (1, 1)^\top, 𝐱(3)=(0,1)⊤\mathbf{x}(3) = (0, 1)^\top, cannot represent every value function. We first find the on-policy distribution, then build 𝐀\mathbf{A} from its definition as an expectation and check it against 𝐗⊤𝐃(𝐈−γ𝐏)𝐗\mathbf{X}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P})\mathbf{X}.

The stationary distribution μ\mu spans the left null space of 𝐏−𝐈\mathbf{P} - \mathbf{I}; normalize it to sum to one.
const P = await wl`{{1 - p, p, 0}, {1/2, 0, 1/2}, {1, 0, 0}}`
const mu = await show`With[{n = First[NullSpace[Transpose[${P}] - IdentityMatrix[3]]]}, Simplify[n/Total[n]]]`
μ={22+3p,2p2+3p,p2+3p} \mu = \left\{\frac{2}{2+3 p},\frac{2 p}{2+3 p},\frac{p}{2+3 p}\right\} 𝐀\mathbf{A} computed as ∑sμ(s)∑s′𝐏ss′𝐱(s)(𝐱(s)−γ𝐱(s′))⊤\sum_s\mu(s)\sum_{s'}\mathbf{P}_{ss'}\mathbf{x}(s)(\mathbf{x}(s) - \gamma\mathbf{x}(s'))^\top; the block returns it only if it equals 𝐗⊤𝐃(𝐈−γ𝐏)𝐗\mathbf{X}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P})\mathbf{X}. Note 𝐀12≠𝐀21\mathbf{A}_{12} \ne \mathbf{A}_{21}: no objective has this field as its gradient.
const X = await wl`{{1, 0}, {1, 1}, {0, 1}}`
const rpi = await wl`{0, 0, 1}`
const Dmu = await wl`DiagonalMatrix[${mu}]`
const A = await show`With[{byExpectation = Sum[${mu}[[s]] ${P}[[s, s2]] Outer[Times, ${X}[[s]], ${X}[[s]] - \[Gamma] ${X}[[s2]]], {s, 3}, {s2, 3}]},
  If[Simplify[byExpectation - Transpose[${X}] . ${Dmu} . (IdentityMatrix[3] - \[Gamma] ${P}) . ${X}] === {{0, 0}, {0, 0}}, Simplify[byExpectation], $Failed]]`
𝐀=(2+2p−2γ−pγ2+3pp(2−3γ)2+3p−2p(−1+γ)2+3p−p(−3+γ)2+3p) \mathbf{A} = \left( \begin{array}{cc} \frac{2+2 p-2 \gamma -p \gamma }{2+3 p} & \frac{p (2-3 \gamma )}{2+3 p} \\ -\frac{2 p (-1+\gamma )}{2+3 p} & -\frac{p (-3+\gamma )}{2+3 p} \\ \end{array} \right) The TD fixed point 𝐰TD=𝐀−1𝐛\mathbf{w}_{\mathrm{TD}} = \mathbf{A}^{-1}\mathbf{b} with 𝐛=𝐗⊤𝐃𝐫π\mathbf{b} = \mathbf{X}^\top\mathbf{D}\mathbf{r}_\pi, as a function of γ\gamma and pp, one component per row.
const wTD = await wl`Simplify[Inverse[${A}] . (Transpose[${X}] . ${Dmu} . ${rpi})]`
await show`Column[${wTD}]`
𝐰TD=(p(2−3γ)−2(3−4γ+γ2)+p(−2−5γ+5γ2)2+2p−2γ−pγ6+2p−8γ+5pγ+2γ2−5pγ2) \mathbf{w}_{\mathrm{TD}} = \left( \begin{array}{l} \frac{p (2-3 \gamma )}{-2 \left(3-4 \gamma +\gamma ^2\right)+p \left(-2-5 \gamma +5 \gamma ^2\right)} \\ \frac{2+2 p-2 \gamma -p \gamma }{6+2 p-8 \gamma +5 p \gamma +2 \gamma ^2-5 p \gamma ^2} \\ \end{array} \right)

The stability argument rests on the row and column sums of the key matrix, both (1−γ)μ(1 - \gamma)\mu, and on the positive definiteness of its symmetric part, which we confirm for all 0<γ<10 < \gamma < 1 and 0<p<10 < p < 1 through the leading principal minors (Sylvester’s criterion).

Column sums (first row of the output) and row sums (second row) of 𝐃(𝐈−γ𝐏)\mathbf{D}(\mathbf{I} - \gamma\mathbf{P}): both equal (1−γ)μ(1 - \gamma)\mu.
const M = await wl`${Dmu} . (IdentityMatrix[3] - \[Gamma] ${P})`
await show`Simplify[{Total[${M}], Total[${M}, {2}]}]`
(𝟏⊤𝐌(𝐌𝟏)⊤)=(2−2γ2+3p−2p(−1+γ)2+3pp−pγ2+3p2−2γ2+3p−2p(−1+γ)2+3pp−pγ2+3p) \begin{pmatrix} \mathbf{1}^\top\mathbf{M} \\ (\mathbf{M}\mathbf{1})^\top \end{pmatrix} = \left( \begin{array}{ccc} \frac{2-2 \gamma }{2+3 p} & -\frac{2 p (-1+\gamma )}{2+3 p} & \frac{p-p \gamma }{2+3 p} \\ \frac{2-2 \gamma }{2+3 p} & -\frac{2 p (-1+\gamma )}{2+3 p} & \frac{p-p \gamma }{2+3 p} \\ \end{array} \right) A quantifier elimination over the whole parameter square: the three leading minors of 𝐌+𝐌⊤\mathbf{M} + \mathbf{M}^\top are positive.
await show`With[{S = ${M} + Transpose[${M}]}, Resolve[ForAll[{\[Gamma], p}, 0 < \[Gamma] < 1 && 0 < p < 1, And @@ Table[Det[S[[1 ;; k, 1 ;; k]]] > 0, {k, 3}]], Reals]]`
[∀ γ,p∈(0,1): det⁡(𝐌+𝐌⊤)1:k, 1:k>0, k=1,2,3]≡True \Bigl[\forall\,\gamma, p \in (0, 1):\ \det\bigl(\mathbf{M} + \mathbf{M}^\top\bigr)_{1:k,\,1:k} > 0,\ k = 1, 2, 3\Bigr] \equiv \text{True}

The contraction lemma hides an identity: the loss of μ\mu-norm under 𝐏\mathbf{P} is a variance. With a symbolic vector 𝐲\mathbf{y} we check that ∥𝐲∥μ2−∥𝐏𝐲∥μ2−∑sμ(s)∑s′𝐏ss′(y(s′)−(𝐏𝐲)(s))2\lVert\mathbf{y}\rVert_\mu^2 - \lVert\mathbf{P}\mathbf{y}\rVert_\mu^2 - \sum_s\mu(s)\sum_{s'}\mathbf{P}_{ss'}(y(s') - (\mathbf{P}\mathbf{y})(s))^2 vanishes identically, and that 𝐗𝐰TD\mathbf{X}\mathbf{w}_{\mathrm{TD}} is the fixed point of the projected Bellman operator.

The gap ∥𝐲∥μ2−∥𝐏𝐲∥μ2\lVert\mathbf{y}\rVert_\mu^2 - \lVert\mathbf{P}\mathbf{y}\rVert_\mu^2 equals a μ\mu-weighted conditional variance, hence is ≥0\ge 0; this uses μ⊤𝐏=μ⊤\mu^\top\mathbf{P} = \mu^\top.
await show`With[{y = {Subscript[y, 1], Subscript[y, 2], Subscript[y, 3]}}, Simplify[y . ${Dmu} . y - (${P} . y) . ${Dmu} . (${P} . y) - Sum[${mu}[[s]] ${P}[[s, s2]] (y[[s2]] - (${P} . y)[[s]])^2, {s, 3}, {s2, 3}]]]`
∥𝐲∥μ2−∥𝐏𝐲∥μ2−∑sμ(s)∑s′𝐏ss′(y(s′)−(𝐏𝐲)(s))2=0 \lVert\mathbf{y}\rVert_\mu^2 - \lVert\mathbf{P}\mathbf{y}\rVert_\mu^2 - \sum_s \mu(s)\sum_{s'}\mathbf{P}_{ss'}\bigl(y(s') - (\mathbf{P}\mathbf{y})(s)\bigr)^2 = 0 Π=𝐗(𝐗⊤𝐃𝐗)−1𝐗⊤𝐃\Pi = \mathbf{X}(\mathbf{X}^\top\mathbf{D}\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{D} is idempotent, and 𝐗𝐰TD−Π(𝐫π+γ𝐏𝐗𝐰TD)=𝟎\mathbf{X}\mathbf{w}_{\mathrm{TD}} - \Pi(\mathbf{r}_\pi + \gamma\mathbf{P}\mathbf{X}\mathbf{w}_{\mathrm{TD}}) = \mathbf{0}.
const Proj = await wl`Simplify[${X} . Inverse[Transpose[${X}] . ${Dmu} . ${X}] . Transpose[${X}] . ${Dmu}]`
await show`Simplify[{${Proj} . ${Proj} - ${Proj}, ${X} . ${wTD} - ${Proj} . (${rpi} + \[Gamma] ${P} . ${X} . ${wTD})}]`
(Π2−Π, 𝐗𝐰TD−ΠTπ𝐗𝐰TD)={(000000000),{0,0,0}} \bigl(\Pi^2 - \Pi,\ \mathbf{X}\mathbf{w}_{\mathrm{TD}} - \Pi T_\pi\mathbf{X}\mathbf{w}_{\mathrm{TD}}\bigr) = \left\{\left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),\{0,0,0\}\right\}

Finally the bound. We compute 𝐯π=(𝐈−γ𝐏)−1𝐫π\mathbf{v}_\pi = (\mathbf{I} - \gamma\mathbf{P})^{-1}\mathbf{r}_\pi, the best weights (𝐗⊤𝐃𝐗)−1𝐗⊤𝐃𝐯π(\mathbf{X}^\top\mathbf{D}\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{D}\mathbf{v}_\pi, and the ratio VE̅(𝐰TD)/min⁡𝐰VE̅\overline{\mathrm{VE}}(\mathbf{w}_{\mathrm{TD}})/\min_{\mathbf{w}}\overline{\mathrm{VE}} for p=12p = \frac{1}{2}.

The ratio is 11 at γ=0\gamma = 0, where the TD target is the reward itself, and grows with γ\gamma.
const vpi = await wl`Simplify[Inverse[IdentityMatrix[3] - \[Gamma] ${P}] . ${rpi}]`
const ve = await wl`Function[w, Simplify[(${vpi} - ${X} . w) . ${Dmu} . (${vpi} - ${X} . w)]]`
const wMin = await wl`Simplify[Inverse[Transpose[${X}] . ${Dmu} . ${X}] . Transpose[${X}] . ${Dmu} . ${vpi}]`
const ratio = await show`Factor[(${ve}[${wTD}]/${ve}[${wMin}]) /. p -> 1/2]`
VE̅(𝐰TD)min⁡𝐰VE̅(𝐰)∣p=1/2=7(56−88γ+51γ2−22γ3+17γ4)2(−14+11γ+γ2)2 \left.\frac{\overline{\mathrm{VE}}(\mathbf{w}_{\mathrm{TD}})}{\min_{\mathbf{w}}\overline{\mathrm{VE}}(\mathbf{w})}\right|_{p = 1/2} = \frac{7 \left(56-88 \gamma +51 \gamma ^2-22 \gamma ^3+17 \gamma ^4\right)}{2 \left(-14+11 \gamma +\gamma ^2\right)^2} The bound 1/(1−γ2)1/(1 - \gamma^2) holds on all of (0,1)(0, 1), and at γ=0.9\gamma = 0.9 the ratio 4.284.28 is not far below it (5.265.26), while 1/(1−γ)=101/(1-\gamma) = 10.
await show`{Resolve[ForAll[\[Gamma], 0 < \[Gamma] < 1, ${ratio} <= 1/(1 - \[Gamma]^2)], Reals], N[{${ratio}, 1/(1 - \[Gamma]^2), 1/(1 - \[Gamma])} /. \[Gamma] -> 9/10, 3]}`
([ratio≤11−γ2 ∀γ∈(0,1)], (ratio,11−γ2,11−γ)γ=0.9)={True,{4.28,5.26,10.0}} \begin{aligned} &\Bigl(\bigl[\text{ratio} \le \tfrac{1}{1 - \gamma^2}\ \forall\gamma \in (0, 1)\bigr],\ \bigl(\text{ratio}, \tfrac{1}{1 - \gamma^2}, \tfrac{1}{1 - \gamma}\bigr)_{\gamma = 0.9}\Bigr) \\ &\quad = \{\text{True},\{4.28,5.26,10.0\}\} \end{aligned}

9.4.4State Aggregation on a Long Random Walk

The same linear algebra applies to episodic tasks, with γ=1\gamma = 1, the substochastic 𝐏\mathbf{P} and the episodic μ\mu of (9.2). Sutton and Barto’s running example is a random walk on states 1,…,10001, \dots, 1000 that starts in state 500 and jumps uniformly to one of the 100 states on either side; a jump past either end terminates with reward −1-1 on the left and +1+1 on the right. Aggregating the states into ten groups of 100 gives ten weights. Rather than simulate, we solve for η\eta (an episode lasts 83.283.2 steps on average; the start state gets μ=0.0137\mu = 0.0137 of the time, its neighbors about 0.00170.0017 each and the end states 0.000150.00015), 𝐯π\mathbf{v}_\pi, the minimizer of VE̅\overline{\mathrm{VE}} (the μ\mu-weighted average of vπv_\pi within each group) and 𝐰TD=𝐀−1𝐛\mathbf{w}_{\mathrm{TD}} = \mathbf{A}^{-1}\mathbf{b} exactly (Figure 9.1).

Figure 9.1: The 1000-state random walk with ten aggregated groups, computed exactly rather than by sampling. Gray: the weights that minimize VE̅\overline{\mathrm{VE}}, to which gradient Monte Carlo converges. Blue: the TD fixed point 𝐀−1𝐛\mathbf{A}^{-1}\mathbf{b}, to which semi-gradient TD(0) converges. Bootstrapping from the neighboring groups pulls each TD value towards its neighbors’, flattening the staircase: the root VE̅\overline{\mathrm{VE}} is 0.0540.054 for Monte Carlo and 0.1170.117 for TD, a ratio of 4.64.6 in VE̅\overline{\mathrm{VE}}.True value of the 1000-state random walk, an increasing S-shaped curve from about -0.92 to 0.92, with two ten-step staircases: the Monte Carlo fixed point follows the curve closely, while the TD fixed point is flatter, above the curve on the left and below it on the right

The TD staircase is visibly shrunk towards zero. Each group’s TD value is built from the values of the groups it jumps into, which are themselves averages, so representation error propagates inwards from both ends. Within each Monte Carlo group, by contrast, the weight is pulled towards the states that μ\mu weights most, which near the ends are the inner ones.

9.4.5n-step Semi-gradient TD

The nn-step return of (7.1), with v^\hat v in place of VV,

Gt:t+n≐Rt+1+γRt+2+⋯+γn−1Rt+n+γnv^(St+n,𝐰t+n−1), G_{t:t+n} \doteq R_{t+1} + \gamma R_{t+2} + \dots + \gamma^{n-1}R_{t+n} + \gamma^n\hat v(S_{t+n}, \mathbf{w}_{t+n-1}), (9.9)

used as UtU_t in (9.3), interpolates between TD(0) (n=1n = 1) and gradient Monte Carlo (n≥T−tn \ge T - t). With state aggregation on the random walk, the error curves as a function of α\alpha and nn look strikingly like the tabular ones of Chapter 7, with intermediate nn best.

Algorithm 9.8 (n-step Semi-gradient TD Prediction). Input: π\pi; differentiable v^\hat v with v^(terminal,⋅)=0\hat v(\text{terminal}, \cdot) = 0. Parameters: α>0\alpha > 0, integer n≥1n \ge 1. Initialize: 𝐰\mathbf{w} arbitrarily. Indices of stored St,RtS_t, R_t may be taken mod n+1n + 1.

  1. Loop for each episode: store S0S_0; T←∞T \leftarrow \infty.
  2. For t=0,1,2,…t = 0, 1, 2, \dots:
    1. If t<Tt < T: act with π(⋅∣St)\pi(\cdot \mid S_t), store Rt+1,St+1R_{t+1}, S_{t+1}; if St+1S_{t+1} is terminal, T←t+1T \leftarrow t + 1.
    2. τ←t−n+1\tau \leftarrow t - n + 1 (the time being updated). If τ≥0\tau \ge 0:
      1. G←∑i=τ+1min⁡(τ+n,T)γi−τ−1RiG \leftarrow \sum_{i=\tau+1}^{\min(\tau+n, T)}\gamma^{i-\tau-1}R_i; if τ+n<T\tau + n < T, G←G+γnv^(Sτ+n,𝐰)G \leftarrow G + \gamma^n\hat v(S_{\tau+n}, \mathbf{w}).
      2. 𝐰←𝐰+α [G−v^(Sτ,𝐰)] ∇v^(Sτ,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\,[G - \hat v(S_\tau, \mathbf{w})]\,\nabla\hat v(S_\tau, \mathbf{w}).
    3. Stop when τ=T−1\tau = T - 1.

9.5Feature Construction for Linear Methods

The theory above holds for any features; how well a linear method works depends almost entirely on which features it is given. Features are the main channel for prior knowledge: they should vary along the directions in which values vary, and coincide along the directions in which generalization is safe. Their great limitation is that a linear function cannot express interactions by itself. In pole balancing a high angular velocity is good when the pole is falling back to vertical and bad when it is falling away; a value linear in separate angle and velocity features cannot say this, so we need features of combinations.

9.5.1Polynomials and the Fourier Basis

If the state is a vector of kk numbers s=(s1,…,sk)s = (s_1, \dots, s_k), the order-nn polynomial basis consists of the monomials xi(s)=∏j=1ksjci,jx_i(s) = \prod_{j=1}^k s_j^{c_{i,j}} with exponents ci,j∈{0,…,n}c_{i,j} \in \{0, \dots, n\}; the constant feature allows affine functions and the mixed monomials express interactions. There are (n+1)k(n+1)^k of them, exponential in kk, so in practice a subset is chosen. Polynomials generalize globally and poorly in online learning, and are not recommended.

The Fourier basis of Konidaris, Osentoski and Thomas does better.George Konidaris, Sarah Osentoski, and Philip Thomas. Value Function Approximation in Reinforcement Learning Using the Fourier Basis. In Proceedings of the Twenty-Fifth AAAI Conference on Artificial Intelligence (AAAI-11), pages 380–385, 2011 On a bounded interval rescaled to [0,1][0, 1], extend a function evenly to [−1,1][-1, 1] and then periodically: the extension is continuous at the boundary, so its Fourier series contains cosines only and converges well. The order-nn cosine basis in kk dimensions is

xi(s)=cos⁡(π 𝐜i⊤s),s∈[0,1]k,𝐜i∈{0,…,n}k, x_i(s) = \cos\bigl(\pi\,\mathbf{c}^{i\top}s\bigr), \qquad s \in [0, 1]^k, \quad \mathbf{c}^i \in \{0, \dots, n\}^k, (9.10)

again (n+1)k(n+1)^k features. A zero in 𝐜i\mathbf{c}^i makes the feature constant along that coordinate; nonzero entries in several coordinates encode an interaction, with frequencies cjic^i_j and a direction given by their ratios. Sine features would be odd, and an odd function is generally discontinuous at the origin, which is why cosines are preferred. Konidaris et al. suggest the per-feature step size αi=α/∥𝐜i∥\alpha_i = \alpha/\lVert\mathbf{c}^i\rVert (with αi=α\alpha_i = \alpha for 𝐜i=𝟎\mathbf{c}^i = \mathbf{0}), so that high frequencies learn more slowly. The cosine features are orthogonal on [0,1][0, 1], so with uniform μ\mu the least-squares weights decouple into Fourier coefficients:

∫01cos⁡(iπs)cos⁡(jπs) ds\int_0^1\cos(i\pi s)\cos(j\pi s)\,ds for i,j=0,…,3i, j = 0, \dots, 3: the Gram matrix of the 1-D basis is diagonal.
await show`Table[Integrate[Cos[i Pi s] Cos[j Pi s], {s, 0, 1}], {i, 0, 3}, {j, 0, 3}]`
(∫01cos⁡(iπs)cos⁡(jπs) ds)i,j=03=(1000012000012000012) \Bigl(\int_0^1 \cos(i\pi s)\cos(j\pi s)\,ds\Bigr)_{i, j = 0}^{3} = \left( \begin{array}{cccc} 1 & 0 & 0 & 0 \\ 0 & \frac{1}{2} & 0 & 0 \\ 0 & 0 & \frac{1}{2} & 0 \\ 0 & 0 & 0 & \frac{1}{2} \\ \end{array} \right)

Being nonzero almost everywhere, Fourier features describe global properties of a state; they struggle with discontinuities (ringing) and with sharply local structure, which is what the next family is for.

9.5.2Coarse Coding and Tile Coding

Cover a continuous state space with overlapping regions, say circles, and give each region a binary feature that is 11 when the state lies inside it. This is coarse coding. Training at one state changes the weights of all regions containing it, so the value changes at every state sharing a region with it, in proportion to the number of shared regions. The size and shape of the regions determine generalization (wide regions generalize far; elongated ones generalize along their long axis), but the finest discrimination finally achievable, the acuity, is governed by the number of features. Wide receptive fields do not doom the approximation to be coarse.

Tile coding is coarse coding organized for speed.James S. Albus. A Theory of Cerebellar Function. Mathematical Biosciences, 10(1–2):25–61, 1971. doi: 10.1016/0025-5564(71)90051-4 Each tiling is a partition of the state space into tiles (a grid, say), and several tilings are used, each offset from the others by a fraction of the tile width. A state activates exactly one tile in each tiling, so with mm tilings exactly mm features are 11 and the rest 00. Computing v^\hat v amounts to summing mm weights found by index arithmetic; and since 𝐱⊤𝐱=m\mathbf{x}^\top\mathbf{x} = m for every state, the step size has a simple meaning: α=1/m\alpha = 1/m moves the trained state’s value all the way to the target, and α=1/(10m)\alpha = 1/(10m) one tenth of the way.

The generalization from a trained state ss to another state s′s' is proportional to the number of tiles they share. In one dimension with mm tilings of width ww offset by w/mw/m, two points at distance Δ\Delta share, on average over the position of ss, mmax⁡(0,1−Δ/w)m\max(0, 1 - \Delta/w) tiles: a triangular kernel, and exactly m−jm - j tiles, wherever ss lies, when Δ=jw/m\Delta = jw/m with j≤mj \le m: the interval between the two points contains jj tile boundaries, each from a different tiling.

With m=4m = 4 tilings of unit width: first, the tiles shared by s=1/8s = 1/8 and s+Δs + \Delta for Δ=0,14,…,54\Delta = 0, \frac{1}{4}, \dots, \frac{5}{4}; second, the exact average over s∈[0,1]s \in [0, 1] for Δ=0,0.1,0.3,0.5,0.9,1.2\Delta = 0, 0.1, 0.3, 0.5, 0.9, 1.2, which is 4(1−Δ)+4(1 - \Delta)^+.
const shared = await wl`Function[{s, d, m}, Sum[Boole[Floor[s + k/m] == Floor[s + d + k/m]], {k, 0, m - 1}]]`
await show`{Table[${shared}[1/8, d, 4], {d, 0, 5/4, 1/4}], Table[Integrate[${shared}[s, d, 4], {s, 0, 1}], {d, {0, 1/10, 3/10, 1/2, 9/10, 6/5}}]}`
(#shared(18,18+Δ)Δ=0,14,…,54∫01#shared(s,s+Δ) ds Δ=0,0.1,0.3,0.5,0.9,1.2)=(43210041851452250) \begin{pmatrix} \#\text{shared}(\tfrac{1}{8}, \tfrac{1}{8} + \Delta)_{\Delta = 0, \frac{1}{4}, \dots, \frac{5}{4}} \\ \int_0^1 \#\text{shared}(s, s + \Delta)\,ds_{\,\Delta = 0, 0.1, 0.3, 0.5, 0.9, 1.2} \end{pmatrix} = \left( \begin{array}{cccccc} 4 & 3 & 2 & 1 & 0 & 0 \\ 4 & \frac{18}{5} & \frac{14}{5} & 2 & \frac{2}{5} & 0 \\ \end{array} \right)

In several dimensions the offsets shape the kernel. Offsetting every tiling by the same displacement along each axis, the vector (1,1)(1, 1) in units of w/mw/m, produces generalization patterns elongated along the diagonal; Miller and Glanz recommend the displacement vector of the first odd integers (1,3,5,…,2k−1)(1, 3, 5, \dots, 2k-1) with mm a power of two at least 4k4k, which gives nearly isotropic patterns. Tiles need not be squares: stripes promote generalization along one dimension (ignoring others), log-spaced stripes give finer resolution where needed, and mixing stripe tilings with conjunctive ones lets a learner both generalize along each dimension and represent specific conjunctions. Finally hashing collapses a huge tiling pseudo-randomly onto a much smaller table: each table entry then stands for many scattered tiles, but since high resolution matters in only a small part of the state space, memory can track the needs of the task instead of growing exponentially with kk.

9.5.3Radial Basis Functions

A radial basis function feature responds by degree, xi(s)=exp⁡(−∥s−ci∥2/(2σi2))x_i(s) = \exp\bigl(-\lVert s - c_i\rVert^2/(2\sigma_i^2)\bigr), with center cic_i and width σi\sigma_i. RBFs are the graded version of coarse coding and give smooth, differentiable approximations, which rarely matters in practice and costs computation; in more than two dimensions tile coding usually works at least as well. Learning the centers and widths too turns an RBF network into a nonlinear approximator.

9.5.4Choosing the Step Size

The classical conditions of Theorem A.7 guarantee convergence but learn too slowly, and αt=1/t\alpha_t = 1/t suits neither bootstrapping nor nonstationarity. In the tabular case α=1/τ\alpha = 1/\tau means that a state’s estimate approaches the recent average of its targets after about τ\tau visits. For linear methods, the same intuition gives a rule of thumb. One update at ss with target UU changes the value of ss itself by

v^(s,𝐰t+1)−v^(s,𝐰t)=α[U−v^(s,𝐰t)]𝐱(s)⊤𝐱(s), \hat v(s, \mathbf{w}_{t+1}) - \hat v(s, \mathbf{w}_t) = \alpha\bigl[U - \hat v(s, \mathbf{w}_t)\bigr]\mathbf{x}(s)^\top\mathbf{x}(s),

so the fraction of the error removed is α 𝐱⊤𝐱\alpha\,\mathbf{x}^\top\mathbf{x}. To learn in about τ\tau presentations of similar feature vectors, set

α≐(τ 𝔼[𝐱⊤𝐱])−1, \alpha \doteq \bigl(\tau\,\mathbb{E}[\mathbf{x}^\top\mathbf{x}]\bigr)^{-1}, (9.11)

with the expectation over the feature vectors met in training. The rule is exact when 𝐱⊤𝐱\mathbf{x}^\top\mathbf{x} is constant, as in tile coding.

9.6Nonlinear Function Approximation: Neural Networks

An artificial neural network computes v^(s,𝐰)\hat v(s, \mathbf{w}) through layers of units, each applying a nonlinearity (logistic, tanh, rectifier) to a weighted sum of the previous layer’s outputs; 𝐰\mathbf{w} collects all the connection weights. One hidden layer of enough units can approximate any continuous function on a compact set,George Cybenko. Approximation by Superpositions of a Sigmoidal Function. Mathematics of Control, Signals and Systems, 2(4):303–314, 1989. doi: 10.1007/BF02551274 but deep networks, with many layers composing simple features into abstract ones, are far more economical for the functions that matter, and their hidden layers learn the features that linear methods must be given.

Nothing in (9.3) needs to change: backpropagation computes ∇v^(St,𝐰)\nabla\hat v(S_t, \mathbf{w}), and semi-gradient TD, gradient Monte Carlo and nn-step methods run as before. What changes is the theory. Gradient Monte Carlo still descends VE̅\overline{\mathrm{VE}} towards a local minimum, but semi-gradient TD with a nonlinear v^\hat v has no general convergence guarantee, and classic counterexamples diverge. The practical toolkit of deep learning (careful initialization, rectifiers, batch normalization, residual connections, dropout against overfitting, convolutional layers that share weights across positions in an image) is what made the large applications of Chapter 15 possible. Deep Q-networks, which combine semi-gradient Q-learning with a convolutional network, a replay memory and a periodically frozen target network, are off-policy learners and are described in Section 15.5; why their training tricks are needed is the subject of Chapter 11.

9.7Least-Squares TD

Semi-gradient TD reaches 𝐰TD=𝐀−1𝐛\mathbf{w}_{\mathrm{TD}} = \mathbf{A}^{-1}\mathbf{b} by small steps, discarding each transition after one use. Why not estimate 𝐀\mathbf{A} and 𝐛\mathbf{b} from all the data and solve? This is least-squares TD (LSTD) of Bradtke and Barto:Steven J. Bradtke and Andrew G. Barto. Linear Least-Squares Algorithms for Temporal Difference Learning. Machine Learning, 22:33–57, 1996. doi: 10.1007/BF00114723

𝐀̂t≐∑k=0t−1𝐱k(𝐱k−γ𝐱k+1)⊤+ε𝐈,𝐛̂t≐∑k=0t−1Rk+1𝐱k,𝐰t≐𝐀̂t−1𝐛̂t. \begin{aligned} \widehat{\mathbf{A}}_t &\doteq \sum_{k=0}^{t-1}\mathbf{x}_k(\mathbf{x}_k - \gamma\mathbf{x}_{k+1})^\top + \varepsilon\mathbf{I}, \\ \widehat{\mathbf{b}}_t &\doteq \sum_{k=0}^{t-1}R_{k+1}\mathbf{x}_k, \qquad \mathbf{w}_t \doteq \widehat{\mathbf{A}}_t^{-1}\widehat{\mathbf{b}}_t . \end{aligned} (9.12)

Both sums estimate tt times their expectations (9.4), and the factors tt cancel; the small ε𝐈\varepsilon\mathbf{I} keeps 𝐀̂t\widehat{\mathbf{A}}_t invertible. Maintaining 𝐀̂t\widehat{\mathbf{A}}_t costs O(d2)O(d^2) per step, but inverting it afresh would cost O(d3)O(d^3). Because each step adds a rank-one matrix, the inverse can be updated directly by the Sherman–Morrison formulaJack Sherman and Winifred J. Morrison. Adjustment of an Inverse Matrix Corresponding to a Change in One Element of a Given Matrix. The Annals of Mathematical Statistics, 21(1):124–127, Institute of Mathematical Statistics, 1950. doi: 10.1214/aoms/1177729893

(𝐁+𝐮𝐯⊤)−1=𝐁−1−𝐁−1𝐮 𝐯⊤𝐁−11+𝐯⊤𝐁−1𝐮, (\mathbf{B} + \mathbf{u}\mathbf{v}^\top)^{-1} = \mathbf{B}^{-1} - \frac{\mathbf{B}^{-1}\mathbf{u}\,\mathbf{v}^\top\mathbf{B}^{-1}}{1 + \mathbf{v}^\top\mathbf{B}^{-1}\mathbf{u}}, (9.13)

which needs only matrix–vector products. Proof: multiply the right-hand side by 𝐁+𝐮𝐯⊤\mathbf{B} + \mathbf{u}\mathbf{v}^\top; the scalar 𝐯⊤𝐁−1𝐮\mathbf{v}^\top\mathbf{B}^{-1}\mathbf{u} factors out and everything except 𝐈\mathbf{I} cancels. The kernel confirms it on a general 2×22 \times 2 matrix:

The difference between the two sides of the Sherman–Morrison formula, for symbolic 𝐁\mathbf{B}, 𝐮\mathbf{u}, 𝐯\mathbf{v}.
await show`Simplify[With[{B = {{Subscript[b, 11], Subscript[b, 12]}, {Subscript[b, 21], Subscript[b, 22]}}, u = {Subscript[u, 1], Subscript[u, 2]}, v = {Subscript[v, 1], Subscript[v, 2]}},
  Inverse[B + Outer[Times, u, v]] - (Inverse[B] - Inverse[B] . Outer[Times, u, v] . Inverse[B]/(1 + v . Inverse[B] . u))]]`
(𝐁+𝐮𝐯⊤)−1−(𝐁−1−𝐁−1𝐮𝐯⊤𝐁−11+𝐯⊤𝐁−1𝐮)=(0000) (\mathbf{B} + \mathbf{u}\mathbf{v}^\top)^{-1} - \Bigl(\mathbf{B}^{-1} - \frac{\mathbf{B}^{-1}\mathbf{u}\mathbf{v}^\top\mathbf{B}^{-1}}{1 + \mathbf{v}^\top\mathbf{B}^{-1}\mathbf{u}}\Bigr) = \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right)

Algorithm 9.9 (LSTD, O(d²) Version). Input: features 𝐱:𝒮+→ℝd\mathbf{x} : \mathcal{S}^+ \to \mathbb{R}^d with 𝐱(terminal)=𝟎\mathbf{x}(\text{terminal}) = \mathbf{0}. Parameter: small ε>0\varepsilon > 0. Initialize: 𝐀−1̂←ε−1𝐈\widehat{\mathbf{A}^{-1}} \leftarrow \varepsilon^{-1}\mathbf{I}, 𝐛̂←𝟎\widehat{\mathbf{b}} \leftarrow \mathbf{0}.

  1. Loop for each episode: initialize SS, 𝐱←𝐱(S)\mathbf{x} \leftarrow \mathbf{x}(S); repeat until S′S' is terminal:
    1. Choose and take A∼π(⋅∣S)A \sim \pi(\cdot \mid S); observe R,S′R, S'; 𝐱′←𝐱(S′)\mathbf{x}' \leftarrow \mathbf{x}(S').
    2. 𝐯←𝐀−1̂⊤(𝐱−γ𝐱′)\mathbf{v} \leftarrow \widehat{\mathbf{A}^{-1}}{}^\top(\mathbf{x} - \gamma\mathbf{x}').
    3. 𝐀−1̂←𝐀−1̂−(𝐀−1̂𝐱)𝐯⊤/(1+𝐯⊤𝐱)\widehat{\mathbf{A}^{-1}} \leftarrow \widehat{\mathbf{A}^{-1}} - (\widehat{\mathbf{A}^{-1}}\mathbf{x})\mathbf{v}^\top/(1 + \mathbf{v}^\top\mathbf{x}).
    4. 𝐛̂←𝐛̂+R 𝐱\widehat{\mathbf{b}} \leftarrow \widehat{\mathbf{b}} + R\,\mathbf{x}; 𝐰←𝐀−1̂𝐛̂\mathbf{w} \leftarrow \widehat{\mathbf{A}^{-1}}\widehat{\mathbf{b}}.
    5. S←S′S \leftarrow S', 𝐱←𝐱′\mathbf{x} \leftarrow \mathbf{x}'.

The recursion is exact, not an approximation of (9.12). We run it on a short trajectory of the three-state chain, with exact rationals, and compare it with the direct solution:

Fold the Sherman–Morrison step of Algorithm 9.9 over eight transitions with γ=910\gamma = \frac{9}{10}, ε=1100\varepsilon = \frac{1}{100}; the output is 𝐰8\mathbf{w}_8 and its difference from 𝐀̂8−1𝐛̂8\widehat{\mathbf{A}}_8^{-1}\widehat{\mathbf{b}}_8.
const traj = await wl`{1, 2, 3, 1, 1, 2, 1, 2, 3}`
await show`With[{g = 9/10, eps = 1/100, xs = ${X}[[${traj}]], rs = ${rpi}[[Most[${traj}]]]},
  With[{fold = Fold[Function[{acc, k}, With[{Ai = acc[[1]], x = xs[[k]], v = Transpose[acc[[1]]] . (xs[[k]] - g xs[[k + 1]])},
      {Ai - Outer[Times, Ai . x, v]/(1 + v . x), acc[[2]] + rs[[k]] x}]], {IdentityMatrix[2]/eps, {0, 0}}, Range[8]],
    direct = Inverse[eps IdentityMatrix[2] + Sum[Outer[Times, xs[[k]], xs[[k]] - g xs[[k + 1]]], {k, 8}]] . Sum[rs[[k]] xs[[k]], {k, 8}]},
   {fold[[1]] . fold[[2]], fold[[1]] . fold[[2]] - direct}]]`
(𝐰8⊤ (Sherman–Morrison)𝐰8⊤−(𝐀̂8−1𝐛̂8)⊤)=(1500073471251007347100) \begin{pmatrix} \mathbf{w}_8^\top \text{ (Sherman–Morrison)} \\ \mathbf{w}_8^\top - (\widehat{\mathbf{A}}_8^{-1}\widehat{\mathbf{b}}_8)^\top \end{pmatrix} = \left( \begin{array}{cc} \frac{15000}{73471} & \frac{25100}{73471} \\ 0 & 0 \\ \end{array} \right)

LSTD is the most data-efficient form of linear TD(0), and it has no step size; but it has ε\varepsilon instead (too small and the early inverses swing wildly, too large and learning slows), its cost is O(d2)O(d^2) against O(d)O(d), and having no step size it never forgets, which is a defect once the policy changes, as in control.LSTD was extended to control as least-squares policy iteration by Lagoudakis and Parr (2003), which evaluates each policy by LSTD on action values.Michail G. Lagoudakis and Ronald Parr. Least-Squares Policy Iteration. Journal of Machine Learning Research, 4:1107–1149, 2003

9.8Memory-based and Kernel-based Approximation

Parametric methods compress experience into 𝐰\mathbf{w} and discard the examples. Memory-based methods do the opposite: they store the examples s′↦gs' \mapsto g and, when a query state needs a value, compute one from the stored examples near it. Nearest-neighbor returns the target of the closest stored state; weighted averaging combines several neighbors with weights decreasing in distance; locally weighted regression fits a small parametric surface around the query and discards it after answering. Such nonparametric methods are not confined to a fixed functional form, so their accuracy keeps improving with data; they concentrate effort on the states that trajectories actually visit, which is all that matters in many problems; and their memory grows with the number of examples, not exponentially with the dimension. The price is query time, mitigated by data structures such as kk-d trees.

The function that weights stored examples is a kernel k(s,s′)k(s, s'), the strength of generalization from s′s' to ss, and kernel regression sets

v^(s,𝒟)=∑s′∈𝒟k(s,s′) g(s′), \hat v(s, \mathcal{D}) = \sum_{s' \in \mathcal{D}} k(s, s')\,g(s'), (9.14)

with 𝒟\mathcal{D} the stored examples and g(s′)g(s') their targets. The link with linear methods is immediate. One linear update at s′s' changes the value at every ss by

v^(s,𝐰t+1)−v^(s,𝐰t)=α[Ut−v^(s′,𝐰t)] 𝐱(s)⊤𝐱(s′), \hat v(s, \mathbf{w}_{t+1}) - \hat v(s, \mathbf{w}_t) = \alpha\bigl[U_t - \hat v(s', \mathbf{w}_t)\bigr]\,\mathbf{x}(s)^\top\mathbf{x}(s'),

so a linear method generalizes according to the kernel k(s,s′)=𝐱(s)⊤𝐱(s′)k(s, s') = \mathbf{x}(s)^\top\mathbf{x}(s'); the shared-tile counts of Section 9.5.2 are exactly this kernel for tile coding. Conversely, any linear regression can be rewritten as kernel regression with this inner-product kernel, and for many feature maps the kernel has a closed form that never builds the (possibly huge) feature vectors: the kernel trick.

9.9Interest and Emphasis

On-policy methods weight every visited state equally. Sometimes we care about some states more: the start state of a discounted episodic task, say, or the greedy action rather than the poor ones. Let the interest It≥0I_t \ge 0 express how much we care about valuing StS_t accurately (set causally, possibly from the trajectory so far), and let μ\mu in (9.1) be the on-policy distribution weighted by interest. The emphasis MtM_t multiplies the update,

𝐰t+n≐𝐰t+n−1+αMt[Gt:t+n−v^(St,𝐰t+n−1)]×∇v^(St,𝐰t+n−1),Mt=It+γnMt−n, \begin{aligned} \mathbf{w}_{t+n} &\doteq \mathbf{w}_{t+n-1} + \alpha M_t\bigl[G_{t:t+n} - \hat v(S_t, \mathbf{w}_{t+n-1})\bigr] \\ &\qquad \times \nabla\hat v(S_t, \mathbf{w}_{t+n-1}), \\ M_t &= I_t + \gamma^n M_{t-n}, \end{aligned} (9.15)

with Mt=0M_t = 0 for t<0t < 0; in the Monte Carlo case Mt=ItM_t = I_t. The recursion is the point: a state of no interest still receives emphasis if an interesting state bootstraps from it nn steps earlier, because the accuracy of the interesting state depends on it. Exercise 9.12 works through Sutton and Barto’s four-state example. Emphasis returns in off-policy learning, where emphatic TD uses it to restore stability (Chapter 11).

9.10Beyond the Book: Finite-Time Theory, Deep TD and Learned Representations

9.10.1How Fast Does Linear TD Converge?

The results above are asymptotic. Bhandari, Russo and Singal gave a short finite-time analysis of linear TD(0) that mirrors textbook SGD proofs.Jalaj Bhandari, Daniel Russo, and Raghav Singal. A Finite Time Analysis of Temporal Difference Learning with Linear Function Approximation. Operations Research, 69(3):950–973, 2021. doi: 10.1287/opre.2020.2024. Extended abstract in Proceedings of the 31st Conference on Learning Theory (COLT 2018), PMLR 75; arXiv:1806.02450; and R. Srikant and Lei Ying. Finite-Time Error Bounds for Linear Stochastic Approximation and TD Learning. In Proceedings of the Thirty-Second Conference on Learning Theory, volume 99 of Proceedings of Machine Learning Research, pages 2803–2830. PMLR, 2019Beyond Sutton and Barto (2018). Bhandari, Russo and Singal’s analysis appeared on arXiv in June 2018, as a COLT 2018 extended abstract, and in full in Operations Research 69(3), 2021. Srikant and Ying (COLT 2019) soon obtained constant-step-size bounds under Markovian sampling without the projection step, by Lyapunov drift arguments. The key is an inequality we have essentially proved: with g¯(𝐰)=𝐛−𝐀𝐰\bar g(\mathbf{w}) = \mathbf{b} - \mathbf{A}\mathbf{w} the expected TD direction and features scaled so that ∥𝐱(s)∥≤1\lVert\mathbf{x}(s)\rVert \le 1,

(𝐰TD−𝐰)⊤g¯(𝐰)≥(1−γ)∥𝐗𝐰TD−𝐗𝐰∥μ2,∥g¯(𝐰)∥≤2∥𝐗𝐰TD−𝐗𝐰∥μ. \begin{aligned} (\mathbf{w}_{\mathrm{TD}} - \mathbf{w})^\top\bar g(\mathbf{w}) &\ge (1 - \gamma)\lVert\mathbf{X}\mathbf{w}_{\mathrm{TD}} - \mathbf{X}\mathbf{w}\rVert_\mu^2, \\ \lVert\bar g(\mathbf{w})\rVert &\le 2\lVert\mathbf{X}\mathbf{w}_{\mathrm{TD}} - \mathbf{X}\mathbf{w}\rVert_\mu . \end{aligned} (9.16)

The first is Lemma 9.6 in disguise: with 𝐲=𝐗(𝐰TD−𝐰)\mathbf{y} = \mathbf{X}(\mathbf{w}_{\mathrm{TD}} - \mathbf{w}), the left side is 𝐲⊤𝐃(𝐈−γ𝐏)𝐲≥∥𝐲∥μ2−γ∥𝐲∥μ∥𝐏𝐲∥μ≥(1−γ)∥𝐲∥μ2\mathbf{y}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P})\mathbf{y} \ge \lVert\mathbf{y}\rVert_\mu^2 - \gamma\lVert\mathbf{y}\rVert_\mu\lVert\mathbf{P}\mathbf{y}\rVert_\mu \ge (1 - \gamma)\lVert\mathbf{y}\rVert_\mu^2, by Cauchy–Schwarz. This is a quantitative version of Theorem 9.4: the symmetric part of 𝐀\mathbf{A} dominates (1−γ)𝐗⊤𝐃𝐗(1 - \gamma)\mathbf{X}^\top\mathbf{D}\mathbf{X}. Together the two inequalities say that the TD direction behaves like the gradient of a strongly convex function, even though it is no gradient. From them follow, with i.i.d. sampling of St∼μS_t \sim \mu and ω\omega the smallest eigenvalue of 𝐗⊤𝐃𝐗\mathbf{X}^\top\mathbf{D}\mathbf{X}: an O(1/T)O(1/\sqrt{T}) bound on the value error of the averaged iterate with step size 1/T1/\sqrt{T}, independent of ω\omega; geometric decay, at rate e−α(1−γ)ωTe^{-\alpha(1-\gamma)\omega T}, down to a noise floor proportional to α\alpha with a small constant step; and 𝔼∥𝐰T−𝐰TD∥2=O(1/((1−γ)2ω2T))\mathbb{E}\lVert\mathbf{w}_T - \mathbf{w}_{\mathrm{TD}}\rVert^2 = O\bigl(1/((1-\gamma)^2\omega^2 T)\bigr) with steps decaying like 1/t1/t. For Markovian data analogous bounds hold, inflated by the mixing time of the chain, for a variant that projects the iterates onto a ball containing 𝐰TD\mathbf{w}_{\mathrm{TD}}.

9.10.2Deep TD and the Tangent Kernel

Linearize a network around its current weights: v^(s,𝐰+Δ)≈v^(s,𝐰)+∇v^(s,𝐰)⊤Δ\hat v(s, \mathbf{w} + \Delta) \approx \hat v(s, \mathbf{w}) + \nabla\hat v(s, \mathbf{w})^\top\Delta. Locally, semi-gradient TD on the network is linear TD with the features 𝐱(s)=∇v^(s,𝐰)\mathbf{x}(s) = \nabla\hat v(s, \mathbf{w}), and by Section 9.8 it generalizes through the kernel k(s,s′)=∇v^(s,𝐰)⊤∇v^(s′,𝐰)k(s, s') = \nabla\hat v(s, \mathbf{w})^\top\nabla\hat v(s', \mathbf{w}), the neural tangent kernel.Arthur Jacot, Franck Gabriel, and Clément Hongler. Neural Tangent Kernel: Convergence and Generalization in Neural Networks. In Advances in Neural Information Processing Systems 31 (NeurIPS 2018), pages 8571–8580, 2018 The stability question of Theorem 9.4 therefore becomes a question about this kernel: when it generalizes too strongly between a state and its successor, a TD update can raise the target as much as the estimate and the iteration runs away.Beyond Sutton and Barto (2018). Achiam, Knight and Abbeel (arXiv, 2019) analyzed divergence of deep Q-learning through this first-order expansion and proposed PreQN, which preconditions the TD errors with the inverse Gram matrix of gradients. Cai, Yang, Lee and Wang (NeurIPS 2019) proved that for sufficiently overparameterized two-layer networks, which stay close to their linearization, neural TD converges at a sublinear rate to the global optimum of the projected Bellman error. Achiam et al. used this expansion to explain divergence in deep Q-learning,Joshua Achiam, Ethan Knight, and Pieter Abbeel. Towards Characterizing Divergence in Deep Q-Learning, 2019 and Cai et al. proved that sufficiently wide networks, which barely leave their linearization, inherit convergence to a projected-Bellman fixed point.Qi Cai, Zhuoran Yang, Jason D. Lee, and Zhaoran Wang. Neural Temporal-Difference Learning Converges to Global Optima. In Advances in Neural Information Processing Systems 32 (NeurIPS 2019), pages 11315–11326, 2019 For narrow networks trained far from initialization the linear picture breaks down, and there is still no general theory.

9.10.3Learning the Representation

A network learns its own features, but the value signal is a single scalar per step, often sparse, and a poor teacher for a large representation. Jaderberg et al.’s UNREAL agent trains the shared layers on several auxiliary tasks at once: predicting immediate rewards, controlling changes in the pixels of the input, and replaying the value loss, with the auxiliary predictions thrown away and only the representation kept.Max Jaderberg, Volodymyr Mnih, Wojciech Marian Czarnecki, Tom Schaul, Joel Z. Leibo, David Silver, and Koray Kavukcuoglu. Reinforcement Learning with Unsupervised Auxiliary Tasks. In International Conference on Learning Representations (ICLR), 2017Beyond Sutton and Barto (2018). Jaderberg et al. (ICLR 2017) reported mean expert-human-normalized performance of 880% on Atari and a tenfold mean speed-up in learning on 3-D Labyrinth tasks from auxiliary tasks alone. The general lesson, that predictions about many signals shape features good for value, is also the motivation of Sutton’s general value functions, discussed in Chapter 16. In the language of this chapter, the auxiliary tasks choose the feature map 𝐱(s)\mathbf{x}(s) on which the final, nearly linear, value layer runs.

Everything in this chapter evaluated a fixed policy. Chapter 10 lets the policy change: semi-gradient methods carry over to action values almost verbatim, but continuing control without discounting needs a new objective, the average reward.

9.11Exercises

Exercise 9.10. Show that tabular TD(0) is linear semi-gradient TD(0) with one-hot features 𝐱(s)=𝐞s∈ℝ∣𝒮∣\mathbf{x}(s) = \mathbf{e}_s \in \mathbb{R}^{|\mathcal{S}|}. What are 𝐀\mathbf{A}, 𝐛\mathbf{b} and 𝐰TD\mathbf{w}_{\mathrm{TD}}, and what does Theorem 9.7 say in this case?

Solution

With 𝐱(s)=𝐞s\mathbf{x}(s) = \mathbf{e}_s the update (9.3) changes only component StS_t of 𝐰\mathbf{w}, by αδt\alpha\delta_t: this is tabular TD(0) with V(s)=wsV(s) = w_s. Now 𝐗=𝐈\mathbf{X} = \mathbf{I}, so 𝐀=𝐃(𝐈−γ𝐏)\mathbf{A} = \mathbf{D}(\mathbf{I} - \gamma\mathbf{P}) and 𝐛=𝐃𝐫π\mathbf{b} = \mathbf{D}\mathbf{r}_\pi, and 𝐰TD=(𝐈−γ𝐏)−1𝐃−1𝐃𝐫π=𝐯π\mathbf{w}_{\mathrm{TD}} = (\mathbf{I} - \gamma\mathbf{P})^{-1}\mathbf{D}^{-1}\mathbf{D}\mathbf{r}_\pi = \mathbf{v}_\pi. Since Π=𝐈\Pi = \mathbf{I}, the minimum of VE̅\overline{\mathrm{VE}} is 00, and the bound says VE̅(𝐰TD)=0\overline{\mathrm{VE}}(\mathbf{w}_{\mathrm{TD}}) = 0: TD’s fixed point is exact. On the chain of Section 9.4.3:

With 𝐗=𝐈\mathbf{X} = \mathbf{I}, the TD fixed point minus 𝐯π\mathbf{v}_\pi is zero for all γ\gamma and pp.
await show`Simplify[Inverse[${Dmu} . (IdentityMatrix[3] - \[Gamma] ${P})] . (${Dmu} . ${rpi}) - ${vpi}]`
(𝐃(𝐈−γ𝐏))−1𝐃𝐫π−𝐯π={0,0,0} \bigl(\mathbf{D}(\mathbf{I} - \gamma\mathbf{P})\bigr)^{-1}\mathbf{D}\mathbf{r}_\pi - \mathbf{v}_\pi = \{0,0,0\}

Exercise 9.11. A seven-dimensional state is tile coded with eight stripe tilings of each dimension separately (5656 tilings) and two conjunctive tilings of each of the (72)=21\binom{7}{2} = 21 pairs of dimensions (4242 tilings). You want learning to approach its asymptote after about ten presentations of the same feature vector. Which step size should you use?

Solution

Each tiling contributes exactly one active binary feature, so 𝐱⊤𝐱=56+42=98\mathbf{x}^\top\mathbf{x} = 56 + 42 = 98 for every state. By (9.11) with τ=10\tau = 10, α=1/(10⋅98)=1/980≈0.001\alpha = 1/(10 \cdot 98) = 1/980 \approx 0.001. Each update then removes one tenth of the error at the trained state, and less at states sharing only some of its tiles.

Exercise 9.12. Episodes of a four-state chain start in the leftmost state and move one state right on each step, with reward +1+1, until termination after the fourth state; γ=1\gamma = 1, so the true values are 4,3,2,14, 3, 2, 1. The approximation is v^=w1\hat v = w_1 for the first two states and v^=w2\hat v = w_2 for the last two. Only the first state is of interest: I=1,0,0,0I = 1, 0, 0, 0. Find the limits of gradient Monte Carlo and of two-step semi-gradient TD, each without and with emphasis.

Solution

Each state is visited once per episode, so the expected update is the sum over states of M (U−v^)∇v^M\,(U - \hat v)\nabla\hat v, and the limit is where it vanishes. Monte Carlo, no emphasis: w1w_1 averages the returns 44 and 33, w2w_2 averages 22 and 11: 𝐰=(3.5,1.5)\mathbf{w} = (3.5, 1.5). Monte Carlo with emphasis: Mt=ItM_t = I_t, only the first state updates, w1=4w_1 = 4. Two-step TD, no emphasis: states 1 and 2 have targets 2+v^(St+2)=2+w22 + \hat v(S_{t+2}) = 2 + w_2; state 3 has target 22 and state 4 target 11. With emphasis: M0=1M_0 = 1, M1=0M_1 = 0, M2=I2+M0=1M_2 = I_2 + M_0 = 1, M3=I3+M1=0M_3 = I_3 + M_1 = 0: state 3 is emphasized because state 1 bootstraps from it.

Zero of the expected two-step update, without and with emphasis M=(1,0,1,0)M = (1, 0, 1, 0).
await show`With[{feat = {{1, 0}, {1, 0}, {0, 1}, {0, 1}}, w = {Subscript[w, 1], Subscript[w, 2]}},
  With[{targets = {2 + w[[2]], 2 + w[[2]], 2, 1}},
   Table[Solve[Thread[Sum[em[[s]] (targets[[s]] - feat[[s]] . w) feat[[s]], {s, 4}] == 0], w], {em, {{1, 1, 1, 1}, {1, 0, 1, 0}}}]]]`
(𝐰no emphasis, 𝐰emphasis)={{{w1→72,w2→32}},{{w1→4,w2→2}}} \bigl(\mathbf{w}_{\text{no emphasis}},\ \mathbf{w}_{\text{emphasis}}\bigr) = \left\{\left\{\left\{w_1\to \frac{7}{2},w_2\to \frac{3}{2}\right\}\right\},\left\{\left\{w_1\to 4,w_2\to 2\right\}\right\}\right\}

Without emphasis two-step TD also converges to (3.5,1.5)(3.5, 1.5); with emphasis it converges to (4,2)(4, 2), exact for the first state and for the state it bootstraps from, while states 2 and 4 are never updated.

Exercise 9.13. Show that the expected update of linear nn-step semi-gradient TD (continuing case, St∼μS_t \sim \mu) vanishes exactly when 𝐗𝐰=ΠTπn𝐗𝐰\mathbf{X}\mathbf{w} = \Pi T_\pi^n\mathbf{X}\mathbf{w}, with Tπn𝐯=∑k<nγk𝐏k𝐫π+γn𝐏n𝐯T_\pi^n\mathbf{v} = \sum_{k<n}\gamma^k\mathbf{P}^k\mathbf{r}_\pi + \gamma^n\mathbf{P}^n\mathbf{v}, and deduce VE̅≤(1−γ2n)−1min⁡𝐰VE̅\overline{\mathrm{VE}} \le (1 - \gamma^{2n})^{-1}\min_{\mathbf{w}}\overline{\mathrm{VE}} at the fixed point.

Solution

Conditioning on St=sS_t = s, the expected nn-step return with fixed 𝐰\mathbf{w} is (Tπn𝐗𝐰)(s)(T_\pi^n\mathbf{X}\mathbf{w})(s), so the expected update is α𝐗⊤𝐃(Tπn𝐗𝐰−𝐗𝐰)\alpha\mathbf{X}^\top\mathbf{D}(T_\pi^n\mathbf{X}\mathbf{w} - \mathbf{X}\mathbf{w}), which vanishes iff 𝐗𝐰\mathbf{X}\mathbf{w} is the μ\mu-projection of Tπn𝐗𝐰T_\pi^n\mathbf{X}\mathbf{w}, as in Proposition 9.5. By Lemma 9.6, ∥Tπn𝐮−Tπn𝐯∥μ=γn∥𝐏n(𝐮−𝐯)∥μ≤γn∥𝐮−𝐯∥μ\lVert T_\pi^n\mathbf{u} - T_\pi^n\mathbf{v}\rVert_\mu = \gamma^n\lVert\mathbf{P}^n(\mathbf{u} - \mathbf{v})\rVert_\mu \le \gamma^n\lVert\mathbf{u} - \mathbf{v}\rVert_\mu, and 𝐯π=Tπn𝐯π\mathbf{v}_\pi = T_\pi^n\mathbf{v}_\pi. The proof of Theorem 9.7 goes through with γn\gamma^n in place of γ\gamma. As n→∞n \to \infty the factor tends to one, the Monte Carlo limit.