Chapter 12

Eligibility Traces

An eligibility trace is the mechanism that turns the one-step methods of the last three chapters into multi-step ones without waiting for the future: a short-term memory vector 𝐳t∈ℝd\mathbf{z}_t \in \mathbb{R}^d that runs alongside the long-term weight vector 𝐰t\mathbf{w}_t. When a component of 𝐰\mathbf{w} helps to produce an estimate, the matching component of 𝐳\mathbf{z} is bumped up and then fades; if a TD error arrives before it has faded, that component learns. The nn-step methods of Chapter 7 already interpolate between one-step TD and Monte Carlo, but they must store the last nn feature vectors and they learn nn steps late. A trace needs one vector and learns at once. The chapter develops one idea in several layers. First we define an ideal forward view, the λ\lambda-return, which looks ahead from each state to a geometric mixture of all nn-step returns. Then we find backward views, algorithms that look back from the current TD error along the trace, and prove that they implement the forward view, approximately for TD(λ\lambda) and exactly for true online TD(λ\lambda). Finally we generalize the forward view, to variable bootstrapping and discounting and to off-policy data, and read off the corresponding traces.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

12.1The λ\lambda-return

Write the nn-step return of (7.1) for a general approximator, bootstrapping from the most recent weights,

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}),

with Gt:t+n=GtG_{t:t+n} = G_t when t+n≥Tt + n \ge T. Each Gt:t+nG_{t:t+n} is a valid target, and so is any average of them with nonnegative weights summing to one, since averaging preserves the error-reduction property that makes nn-step targets work. Such a target is a compound update. The λ\lambda-return gives weight proportional to λn−1\lambda^{n-1}, λ∈[0,1]\lambda \in [0, 1], to the nn-step return:

Gtλ≐(1−λ)∑n=1∞λn−1Gt:t+n. G_t^\lambda \doteq (1 - \lambda)\sum_{n=1}^\infty \lambda^{n-1}G_{t:t+n}. (12.1)

In an episode ending at TT every return with n≥T−tn \ge T - t equals GtG_t, and their weights add to (1−λ)∑n≥T−tλn−1=λT−t−1(1 - \lambda)\sum_{n \ge T-t}\lambda^{n-1} = \lambda^{T-t-1}, so

Gtλ=(1−λ)∑n=1T−t−1λn−1Gt:t+n+λT−t−1Gt. G_t^\lambda = (1 - \lambda)\sum_{n=1}^{T-t-1}\lambda^{n-1}G_{t:t+n} + \lambda^{T-t-1}G_t. (12.2)

At λ=0\lambda = 0 only the one-step return survives and the target is TD(0)’s; at λ=1\lambda = 1 only GtG_t survives and the target is Monte Carlo’s. The weight on Gt:t+nG_{t:t+n} halves every τλ=ln⁡2/ln⁡(1/λ)\tau_\lambda = \ln 2/\ln(1/\lambda) steps (Exercise 12.9). The offline λ\lambda-return algorithm waits for the end of an episode and then makes the semi-gradient updates

𝐰t+1=𝐰t+α[Gtλ−v^(St,𝐰t)]∇v^(St,𝐰t),t=0,…,T−1. \mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\bigl[G_t^\lambda - \hat v(S_t, \mathbf{w}_t)\bigr]\nabla\hat v(S_t, \mathbf{w}_t), \qquad t = 0, \dots, T - 1. (12.3)

On the 19-state random walk of Chapter 7 it performs about as well as the best nn-step method, with the best results, again, at intermediate λ\lambda.

Two identities turn this forward view into something computable. They hold for a fixed weight vector, written Vk≐v^(Sk,𝐰)V_k \doteq \hat v(S_k, \mathbf{w}) with VT=0V_T = 0, and δk=Rk+1+γVk+1−Vk\delta_k = R_{k+1} + \gamma V_{k+1} - V_k the TD error (6.2).

Proposition 12.1 (Recursion and TD-error form of the λ\lambda-return). For a fixed value function,

Gtλ=Rt+1+γ[(1−λ)Vt+1+λGt+1λ], G_t^\lambda = R_{t+1} + \gamma\bigl[(1 - \lambda)V_{t+1} + \lambda G_{t+1}^\lambda\bigr], (12.4)Gtλ−Vt=∑k=tT−1(γλ)k−tδk, G_t^\lambda - V_t = \sum_{k=t}^{T-1}(\gamma\lambda)^{k-t}\delta_k, (12.5)

with the convention GTλ=0G_T^\lambda = 0.

Proof. Each nn-step return splits as Gt:t+n=Rt+1+γGt+1:t+1+n−1G_{t:t+n} = R_{t+1} + \gamma G_{t+1:t+1+n-1} for n≥2n \ge 2, while Gt:t+1=Rt+1+γVt+1G_{t:t+1} = R_{t+1} + \gamma V_{t+1}. Substituting in (12.1),

Gtλ=(1−λ)(Rt+1+γVt+1)+(1−λ)∑n=2∞λn−1(Rt+1+γGt+1:t+n)=Rt+1+γ(1−λ)Vt+1+γλ (1−λ)∑m=1∞λm−1Gt+1:t+1+m, \begin{aligned} G_t^\lambda &= (1 - \lambda)\bigl(R_{t+1} + \gamma V_{t+1}\bigr) + (1 - \lambda)\sum_{n=2}^\infty\lambda^{n-1}\bigl(R_{t+1} + \gamma G_{t+1:t+n}\bigr) \\ &= R_{t+1} + \gamma(1 - \lambda)V_{t+1} \\ &\quad + \gamma\lambda\,(1 - \lambda)\sum_{m=1}^\infty\lambda^{m-1}G_{t+1:t+1+m}, \end{aligned}

using (1−λ)∑n≥1λn−1=1(1 - \lambda)\sum_{n \ge 1}\lambda^{n-1} = 1; the last sum is Gt+1λG_{t+1}^\lambda. For (12.5), subtract VtV_t from the recursion and add and subtract γλVt+1\gamma\lambda V_{t+1}: Gtλ−Vt=δt+γλ (Gt+1λ−Vt+1)G_t^\lambda - V_t = \delta_t + \gamma\lambda\,(G_{t+1}^\lambda - V_{t+1}). Unrolling to TT, where GTλ−VT=0G_T^\lambda - V_T = 0, gives the sum. □

The recursion is the λ\lambda-analogue of Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} ((3.5)): at the next state the target bootstraps with probability 1−λ1 - \lambda and continues with probability λ\lambda. The TD-error form says that the λ\lambda-return error is a sum of future one-step errors, each discounted by γλ\gamma\lambda per step of delay. That sum is what a trace will deliver.

12.1.1In Wolfram Language

We check the definitions on an episode of length T=4T = 4 with symbolic rewards RkR_k and values VkV_k.

The weights of the episodic λ\lambda-return (12.2) sum to one for any horizon m=T−tm = T - t.
await show`Simplify[(1 - \[Lambda]) Sum[\[Lambda]^(n - 1), {n, 1, m - 1}] + \[Lambda]^(m - 1)]`
(1−λ)∑n=1m−1λn−1+λm−1=1 (1 - \lambda)\sum_{n=1}^{m-1}\lambda^{n-1} + \lambda^{m-1} = 1 nn-step returns and the λ\lambda-return as pure functions; for t=2t = 2 only the one-step return and the full return remain.
const gn = await wl`Function[{t, n}, Sum[\[Gamma]^(k - 1) Subscript[R, t + k], {k, 1, Min[n, 4 - t]}] + If[t + n < 4, \[Gamma]^n Subscript[V, t + n], 0]]`
const glam = await wl`Function[{t}, (1 - \[Lambda]) Sum[\[Lambda]^(n - 1) ${gn}[t, n], {n, 1, 3 - t}] + \[Lambda]^(3 - t) ${gn}[t, 4 - t]]`
await show`Collect[Expand[${glam}[2]], \[Lambda]]`
G2λ=R3+γV3+λ(γR4−γV3) G_2^\lambda = R_3+\gamma V_3+\lambda \left(\gamma R_4-\gamma V_3\right) The recursion (12.4): the difference of the two sides vanishes at every t=0,…,3t = 0, \dots, 3.
await show`Table[Simplify[${glam}[t] - (Subscript[R, t + 1] + \[Gamma] ((1 - \[Lambda]) If[t + 1 < 4, Subscript[V, t + 1], 0] + \[Lambda] If[t + 1 < 4, ${glam}[t + 1], 0]))], {t, 0, 3}]`
{0,0,0,0} \{0,0,0,0\} The TD-error form (12.5), again at every tt.
const tdErr = await wl`Function[{k}, Subscript[R, k + 1] + \[Gamma] If[k + 1 < 4, Subscript[V, k + 1], 0] - Subscript[V, k]]`
await show`Table[Simplify[${glam}[t] - Subscript[V, t] - Sum[(\[Gamma] \[Lambda])^(k - t) ${tdErr}[k], {k, t, 3}]], {t, 0, 3}]`
{0,0,0,0} \{0,0,0,0\} The two ends of the family: at λ=0\lambda = 0 the TD(0) target, at λ=1\lambda = 1 the Monte Carlo return.
await show`{${glam}[0] /. \[Lambda] -> 0, Expand[${glam}[0] /. \[Lambda] -> 1]}`
(G0λ∣λ=0, G0λ∣λ=1)={R1+γV1,R1+γR2+γ2R3+γ3R4} \bigl(G_0^\lambda\big|_{\lambda=0},\ G_0^\lambda\big|_{\lambda=1}\bigr) = \left\{R_1+\gamma V_1,R_1+\gamma R_2+\gamma ^2 R_3+\gamma ^3 R_4\right\}

12.2TD(λ\lambda)

**TD(λ\lambda)** replaces the forward view by a backward one. It keeps an eligibility trace, reset at the start of each episode, incremented by the value gradient and decaying by γλ\gamma\lambda,

𝐳−1=𝟎,𝐳t=γλ 𝐳t−1+∇v^(St,𝐰t), \mathbf{z}_{-1} = \mathbf{0}, \qquad \mathbf{z}_t = \gamma\lambda\,\mathbf{z}_{t-1} + \nabla\hat v(S_t, \mathbf{w}_t), (12.6)

and moves the weights at every step along the trace, in proportion to the current TD error, 𝐰t+1=𝐰t+αδt𝐳t\mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\delta_t\mathbf{z}_t. This is the accumulating trace; with linear features 𝐳t\mathbf{z}_t is simply a decaying sum of past feature vectors.

Algorithm 12.2 (Semi-gradient TD(λ\lambda)). Input: the policy π\pi to evaluate; a differentiable v^(s,𝐰)\hat v(s, \mathbf{w}) with v^(terminal,⋅)=0\hat v(\text{terminal}, \cdot) = 0. Parameters: step size α>0\alpha > 0, trace decay λ∈[0,1]\lambda \in [0, 1]. Initialize: 𝐰∈ℝd\mathbf{w} \in \mathbb{R}^d arbitrarily.

Loop for each episode:

  1. Initialize SS; 𝐳←𝟎\mathbf{z} \leftarrow \mathbf{0}.
  2. Loop for each step until S′S' is terminal:
    • choose A∼π(⋅∣S)A \sim \pi(\cdot \mid S), take it, observe R,S′R, S';
    • 𝐳←γλ 𝐳+∇v^(S,𝐰)\mathbf{z} \leftarrow \gamma\lambda\,\mathbf{z} + \nabla\hat v(S, \mathbf{w});
    • δ←R+γv^(S′,𝐰)−v^(S,𝐰)\delta \leftarrow R + \gamma\hat v(S', \mathbf{w}) - \hat v(S, \mathbf{w});
    • 𝐰←𝐰+α δ 𝐳\mathbf{w} \leftarrow \mathbf{w} + \alpha\,\delta\,\mathbf{z}; S←S′S \leftarrow S'.

At λ=0\lambda = 0 the trace is the current gradient and TD(λ\lambda) is semi-gradient TD(0), hence the name. At λ=1\lambda = 1 the credit given to a state kk steps back falls by γk\gamma^k, exactly the discount that the reward Rt+1R_{t+1} inside δt\delta_t deserves in that state’s return; TD(1) is an incremental, online implementation of Monte Carlo that works for continuing tasks too and learns from an episode while it is still running.

12.2.1The Forward and the Backward View Agree Offline

The link between the two views is an exchange of the order of summation.

Theorem 12.3 (Offline equivalence of TD(λ\lambda) and the λ\lambda-return algorithm). Suppose that during an episode the weights are held fixed at 𝐰\mathbf{w}, the TD(λ\lambda) increments αδt𝐳t\alpha\delta_t\mathbf{z}_t being accumulated and applied only at the end. Then the total TD(λ\lambda) update equals the total update of the offline λ\lambda-return algorithm computed with the same fixed 𝐰\mathbf{w}:

∑t=0T−1α δt 𝐳t=∑t=0T−1α[Gtλ−v^(St,𝐰)]∇v^(St,𝐰). \sum_{t=0}^{T-1}\alpha\,\delta_t\,\mathbf{z}_t = \sum_{t=0}^{T-1}\alpha\bigl[G_t^\lambda - \hat v(S_t, \mathbf{w})\bigr]\nabla\hat v(S_t, \mathbf{w}).

Proof. With 𝐰\mathbf{w} fixed, 𝐳t=∑k=0t(γλ)t−k∇v^(Sk,𝐰)\mathbf{z}_t = \sum_{k=0}^{t}(\gamma\lambda)^{t-k}\nabla\hat v(S_k, \mathbf{w}). Hence

∑t=0T−1δt𝐳t=∑t=0T−1∑k=0t(γλ)t−kδt ∇v^(Sk,𝐰)=∑k=0T−1∇v^(Sk,𝐰)∑t=kT−1(γλ)t−kδt=∑k=0T−1[Gkλ−v^(Sk,𝐰)]∇v^(Sk,𝐰), \begin{aligned} \sum_{t=0}^{T-1}\delta_t\mathbf{z}_t &= \sum_{t=0}^{T-1}\sum_{k=0}^{t}(\gamma\lambda)^{t-k}\delta_t\,\nabla\hat v(S_k, \mathbf{w}) \\ &= \sum_{k=0}^{T-1}\nabla\hat v(S_k, \mathbf{w})\sum_{t=k}^{T-1}(\gamma\lambda)^{t-k}\delta_t \\ &= \sum_{k=0}^{T-1}\bigl[G_k^\lambda - \hat v(S_k, \mathbf{w})\bigr]\nabla\hat v(S_k, \mathbf{w}), \end{aligned}

where the last step is (12.5). □

Online, the weights do change within the episode, and the equivalence becomes approximate, with an error of order α2\alpha^2 per episode. For small step sizes TD(λ\lambda) and the offline λ\lambda-return algorithm are practically indistinguishable; for large ones TD(λ\lambda) is markedly worse and can become unstable, because a state’s value is adjusted repeatedly with stale TD errors. Section 12.4 removes this defect.

Offline equivalence on a three-step episode with arbitrary linear features 𝐱t∈ℝ2\mathbf{x}_t \in \mathbb{R}^2 and fixed weights: backward minus forward total update.
const xsT = await wl`Table[If[t < 3, {Subscript[x, t, 1], Subscript[x, t, 2]}, {0, 0}], {t, 0, 3}]`
const wFix = await wl`{Subscript[w, 1], Subscript[w, 2]}`
await show`Module[{v, d, z, gl, back, fwd}, v = Function[t, ${wFix} . ${xsT}[[t + 1]]]; d = Function[t, Subscript[R, t + 1] + \[Gamma] v[t + 1] - v[t]]; z = Function[t, Sum[(\[Gamma] \[Lambda])^(t - k) ${xsT}[[k + 1]], {k, 0, t}]]; gl = Function[t, (1 - \[Lambda]) Sum[\[Lambda]^(n - 1) (Sum[\[Gamma]^(j - 1) Subscript[R, t + j], {j, 1, n}] + \[Gamma]^n v[t + n]), {n, 1, 2 - t}] + \[Lambda]^(2 - t) Sum[\[Gamma]^(j - 1) Subscript[R, t + j], {j, 1, 3 - t}]]; back = Sum[\[Alpha] d[t] z[t], {t, 0, 2}]; fwd = Sum[\[Alpha] (gl[t] - v[t]) ${xsT}[[t + 1]], {t, 0, 2}]; Simplify[back - fwd]]`
{0,0} \{0,0\}

12.2.2What TD(λ\lambda) Converges To

On-policy linear TD(λ\lambda) converges with probability one under the usual step-size conditions, to a weight vector 𝐰λ\mathbf{w}_\lambda that depends on λ\lambda. It is the fixed point of a projected operator. Define the TD(λ\lambda) operator as the λ\lambda-mixture of iterated Bellman operators,

𝐓(λ)𝐯≐(1−λ)∑n=0∞λn𝐁πn+1𝐯,𝐓(λ)𝐮−𝐓(λ)𝐯=𝐏(λ)(𝐮−𝐯),𝐏(λ)=(1−λ)∑n=0∞λnγn+1𝐏πn+1. \begin{aligned} \mathbf{T}^{(\lambda)}\mathbf{v} &\doteq (1 - \lambda)\sum_{n=0}^\infty\lambda^n\mathbf{B}_\pi^{n+1}\mathbf{v}, \\ \mathbf{T}^{(\lambda)}\mathbf{u} - \mathbf{T}^{(\lambda)}\mathbf{v} &= \mathbf{P}^{(\lambda)}(\mathbf{u} - \mathbf{v}), \\ \mathbf{P}^{(\lambda)} &= (1 - \lambda)\sum_{n=0}^\infty\lambda^n\gamma^{n+1}\mathbf{P}_\pi^{n+1}. \end{aligned}

Then 𝐗𝐰λ=Π𝐓(λ)𝐗𝐰λ\mathbf{X}\mathbf{w}_\lambda = \Pi\mathbf{T}^{(\lambda)}\mathbf{X}\mathbf{w}_\lambda, with Π\Pi the μ\mu-projection of Section 11.4.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

Theorem 12.4 (Error bound for linear TD(λ\lambda)). On-policy, with μ\mu the stationary distribution of π\pi and γ<1\gamma < 1, let κ=γ(1−λ)/(1−γλ)\kappa = \gamma(1 - \lambda)/(1 - \gamma\lambda). Then

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

Proof. Stationarity gives ∥𝐏π𝐯∥μ≤∥𝐯∥μ\lVert\mathbf{P}_\pi\mathbf{v}\rVert_\mu \le \lVert\mathbf{v}\rVert_\mu (Jensen’s inequality row by row, then μ⊤𝐏π=μ⊤\mu^\top\mathbf{P}_\pi = \mu^\top), so ∥𝐏(λ)𝐯∥μ≤(1−λ)∑nλnγn+1∥𝐯∥μ=κ∥𝐯∥μ\lVert\mathbf{P}^{(\lambda)}\mathbf{v}\rVert_\mu \le (1 - \lambda)\sum_n\lambda^n\gamma^{n+1}\lVert\mathbf{v}\rVert_\mu = \kappa\lVert\mathbf{v}\rVert_\mu: 𝐓(λ)\mathbf{T}^{(\lambda)} is a κ\kappa-contraction with fixed point vπv_\pi, and so is Π𝐓(λ)\Pi\mathbf{T}^{(\lambda)}, since Π\Pi is non-expansive. Write 𝐯λ=𝐗𝐰λ\mathbf{v}_\lambda = \mathbf{X}\mathbf{w}_\lambda. The vector 𝐯λ−Πvπ\mathbf{v}_\lambda - \Pi v_\pi lies in the subspace and Πvπ−vπ\Pi v_\pi - v_\pi is μ\mu-orthogonal to it, so by Pythagoras

∥𝐯λ−vπ∥μ2=∥Π𝐓(λ)𝐯λ−Π𝐓(λ)vπ∥μ2+∥Πvπ−vπ∥μ2≤κ2∥𝐯λ−vπ∥μ2+∥Πvπ−vπ∥μ2. \begin{aligned} \lVert\mathbf{v}_\lambda - v_\pi\rVert_\mu^2 &= \lVert\Pi\mathbf{T}^{(\lambda)}\mathbf{v}_\lambda - \Pi\mathbf{T}^{(\lambda)}v_\pi\rVert_\mu^2 + \lVert\Pi v_\pi - v_\pi\rVert_\mu^2 \\ &\le \kappa^2\lVert\mathbf{v}_\lambda - v_\pi\rVert_\mu^2 + \lVert\Pi v_\pi - v_\pi\rVert_\mu^2 . \end{aligned}

Rearranging gives the first inequality, since ∥Πvπ−vπ∥μ2=min⁡𝐰VE̅(𝐰)\lVert\Pi v_\pi - v_\pi\rVert_\mu^2 = \min_\mathbf{w}\overline{\mathrm{VE}}(\mathbf{w}). The second follows from 1/(1−κ2)≤1/(1−κ)1/(1 - \kappa^2) \le 1/(1 - \kappa) and 1−κ=(1−γ)/(1−γλ)1 - \kappa = (1 - \gamma)/(1 - \gamma\lambda). □

At λ=0\lambda = 0 this is the bound of Theorem 9.7; as λ→1\lambda \to 1 it tends to the Monte Carlo optimum. Yet in practice λ=1\lambda = 1 is rarely the best choice: the bound is about the limit, while the speed of learning, which bootstrapping improves, usually matters more.

The contraction factor of 𝐓(λ)\mathbf{T}^{(\lambda)} along an eigenvalue pp of 𝐏π\mathbf{P}_\pi: summing the series gives γ(1−λ)p/(1−γλp)\gamma(1 - \lambda)p/(1 - \gamma\lambda p), and κ\kappa at p=1p = 1.
const serP = await show`Assuming[0 < \[Gamma] < 1 && 0 <= \[Lambda] <= 1 && 0 < p <= 1, Simplify[Sum[(1 - \[Lambda]) \[Lambda]^n \[Gamma]^(n + 1) p^(n + 1), {n, 0, Infinity}]]]`
(1−λ)∑n≥0λnγn+1pn+1=pγ(−1+λ)−1+pγλ (1-\lambda)\sum_{n \ge 0}\lambda^n\gamma^{n+1}p^{n+1} = \frac{p \gamma (-1+\lambda )}{-1+p \gamma \lambda } The gap between the book’s factor (1−γλ)/(1−γ)(1 - \gamma\lambda)/(1 - \gamma) and the sharper 1/(1−κ2)1/(1 - \kappa^2) is a nonnegative rational function.
const kap = await wl`${serP} /. p -> 1`
await show`Factor[(1 - \[Gamma] \[Lambda])/(1 - \[Gamma]) - 1/(1 - ${kap}^2)]`
1−γλ1−γ−11−κ2=γ(−1+λ)(−1+γλ)(−1+γ)(−1−γ+2γλ) \frac{1-\gamma\lambda}{1-\gamma} - \frac{1}{1-\kappa^2} = \frac{\gamma (-1+\lambda ) (-1+\gamma \lambda )}{(-1+\gamma ) (-1-\gamma +2 \gamma \lambda )}

12.3Truncated λ\lambda-returns and Redoing Updates

The offline λ\lambda-return is known only at the end of the episode, and never in a continuing task. Its dependence on distant rewards fades like (γλ)n(\gamma\lambda)^n, so it is natural to truncate it at a horizon hh, giving the residual weight to the longest available nn-step return:

Gt:hλ≐(1−λ)∑n=1h−t−1λn−1Gt:t+n+λh−t−1Gt:h,0≤t<h≤T. G_{t:h}^\lambda \doteq (1 - \lambda)\sum_{n=1}^{h-t-1}\lambda^{n-1}G_{t:t+n} + \lambda^{h-t-1}G_{t:h}, \qquad 0 \le t < h \le T. (12.7)

**Truncated TD(λ\lambda)** updates 𝐰\mathbf{w} toward Gt:t+nλG_{t:t+n}^\lambda with an nn-step delay, like the methods of Chapter 7, but with all kk-step returns, k≤nk \le n, mixed geometrically. It is cheap because the truncated return is again a sum of TD errors, now with each TD error using the weights current at its time (Exercise 12.12).

Truncation forces a compromise: a long horizon approximates the λ\lambda-return well, a short one lets updates influence behavior sooner. The online λ\lambda-return algorithm avoids it by brute force. Whenever the horizon advances from h−1h - 1 to hh, it redoes every update of the episode so far, from the initial weights, with the new, better targets:

𝐰t+1h=𝐰th+α[Gt:hλ−v^(St,𝐰th)]∇v^(St,𝐰th), \mathbf{w}_{t+1}^h = \mathbf{w}_t^h + \alpha\bigl[G_{t:h}^\lambda - \hat v(S_t, \mathbf{w}_t^h)\bigr]\nabla\hat v(S_t, \mathbf{w}_t^h), (12.8)

for 0≤t<h0 \le t < h, starting each row from 𝐰0h=𝐰0\mathbf{w}_0^h = \mathbf{w}_0; its output is the diagonal 𝐰h≐𝐰hh\mathbf{w}_h \doteq \mathbf{w}_h^h, which is also the weight vector the truncated returns bootstrap from. It is fully online, it uses only data available at time hh, and at the end of the episode it is at least as good as the offline algorithm, because its bootstrapping weights have received more informative updates. Its only defect is cost: O(h)O(h) updates at step hh.

12.4True Online TD(λ\lambda)

For linear approximation, v^(s,𝐰)=𝐰⊤𝐱(s)\hat v(s, \mathbf{w}) = \mathbf{w}^\top\mathbf{x}(s), the diagonal of the online λ\lambda-return algorithm can be computed exactly with O(d)O(d) work per step. The result is **true online TD(λ\lambda)**,Harm van Seijen and Richard S. Sutton. True Online TD(λ\lambda). In Proceedings of the 31st International Conference on Machine Learning (ICML 2014), volume 32 of Proceedings of Machine Learning Research, pages 692–700, 2014; and Harm van Seijen, A. Rupam Mahmood, Patrick M. Pilarski, Marlos C. Machado, and Richard S. Sutton. True Online Temporal-Difference Learning. Journal of Machine Learning Research, 17(145):1–40, 2016

𝐰t+1=𝐰t+αδt𝐳t+α(𝐰t⊤𝐱t−𝐰t−1⊤𝐱t)(𝐳t−𝐱t),𝐳t=γλ 𝐳t−1+(1−αγλ 𝐳t−1⊤𝐱t)𝐱t, \begin{aligned} \mathbf{w}_{t+1} &= \mathbf{w}_t + \alpha\delta_t\mathbf{z}_t + \alpha\bigl(\mathbf{w}_t^\top\mathbf{x}_t - \mathbf{w}_{t-1}^\top\mathbf{x}_t\bigr)(\mathbf{z}_t - \mathbf{x}_t), \\ \mathbf{z}_t &= \gamma\lambda\,\mathbf{z}_{t-1} + \bigl(1 - \alpha\gamma\lambda\,\mathbf{z}_{t-1}^\top\mathbf{x}_t\bigr)\mathbf{x}_t, \end{aligned} (12.9)

with δt\delta_t as in TD(λ\lambda). The trace in (12.9) is the dutch trace. Compared with the accumulating trace, the increment 𝐱t\mathbf{x}_t is reduced along the direction in which the trace already points, 𝐳t=(𝐈−α 𝐱t𝐱t⊤)γλ𝐳t−1+𝐱t\mathbf{z}_t = (\mathbf{I} - \alpha\,\mathbf{x}_t\mathbf{x}_t^\top)\gamma\lambda\mathbf{z}_{t-1} + \mathbf{x}_t: the matrix 𝐈−α𝐱t𝐱t⊤\mathbf{I} - \alpha\mathbf{x}_t\mathbf{x}_t^\top is exactly what one LMS step on 𝐱t\mathbf{x}_t does to the weights, so the trace already knows that the earlier contributions will be partly overwritten. The older replacing trace, defined for binary features by zi,t=1z_{i,t} = 1 if xi,t=1x_{i,t} = 1 and zi,t=γλzi,t−1z_{i,t} = \gamma\lambda z_{i,t-1} otherwise, is a crude version of the same idea (Exercise 12.10).

Algorithm 12.5 (True online TD(λ\lambda)). Input: π\pi; features 𝐱(⋅)\mathbf{x}(\cdot) with 𝐱(terminal)=𝟎\mathbf{x}(\text{terminal}) = \mathbf{0}. Parameters: α>0\alpha > 0, λ∈[0,1]\lambda \in [0, 1]. Initialize: 𝐰∈ℝd\mathbf{w} \in \mathbb{R}^d.

Loop for each episode:

  1. Initialize SS, 𝐱←𝐱(S)\mathbf{x} \leftarrow \mathbf{x}(S), 𝐳←𝟎\mathbf{z} \leftarrow \mathbf{0}, Vold←0V_{\text{old}} \leftarrow 0.
  2. Loop for each step until 𝐱′=𝟎\mathbf{x}' = \mathbf{0}:
    • choose A∼π(⋅∣S)A \sim \pi(\cdot \mid S), observe RR and 𝐱′=𝐱(S′)\mathbf{x}' = \mathbf{x}(S');
    • V←𝐰⊤𝐱V \leftarrow \mathbf{w}^\top\mathbf{x}, V′←𝐰⊤𝐱′V' \leftarrow \mathbf{w}^\top\mathbf{x}', δ←R+γV′−V\delta \leftarrow R + \gamma V' - V;
    • 𝐳←γλ 𝐳+(1−αγλ 𝐳⊤𝐱) 𝐱\mathbf{z} \leftarrow \gamma\lambda\,\mathbf{z} + (1 - \alpha\gamma\lambda\,\mathbf{z}^\top\mathbf{x})\,\mathbf{x};
    • 𝐰←𝐰+α(δ+V−Vold) 𝐳−α(V−Vold) 𝐱\mathbf{w} \leftarrow \mathbf{w} + \alpha(\delta + V - V_{\text{old}})\,\mathbf{z} - \alpha(V - V_{\text{old}})\,\mathbf{x};
    • Vold←V′V_{\text{old}} \leftarrow V', 𝐱←𝐱′\mathbf{x} \leftarrow \mathbf{x}'.

The update of 𝐰\mathbf{w} in the box is (12.9) rearranged: VoldV_{\text{old}} is 𝐰t−1⊤𝐱t\mathbf{w}_{t-1}^\top\mathbf{x}_t, saved at the previous step. Memory is that of TD(λ\lambda), and the cost per step grows by one inner product.

Theorem 12.6 (Equivalence of true online TD(λ\lambda) and the online λ\lambda-return algorithm). For linear function approximation, the weight vectors 𝐰t\mathbf{w}_t produced by Algorithm 12.5 coincide, for every t≤Tt \le T, with the diagonal 𝐰tt\mathbf{w}_t^t of the online λ\lambda-return algorithm (12.8).

The general proof (van Seijen et al., appendix) finds a recursion for 𝐰t+1t+1−𝐰tt\mathbf{w}_{t+1}^{t+1} - \mathbf{w}_t^t by comparing two consecutive rows of the triangle {𝐰kh}\{\mathbf{w}_k^h\} and expressing their difference through a trace. Rather than reproduce it, we verify the theorem symbolically. The script below runs the online λ\lambda-return algorithm literally, redoing all updates at each horizon and bootstrapping from the diagonal, and runs true online TD(λ\lambda) on the same three-step episode with arbitrary features 𝐱0,𝐱1,𝐱2∈ℝ2\mathbf{x}_0, \mathbf{x}_1, \mathbf{x}_2 \in \mathbb{R}^2, rewards, step size and initial weights.

The online λ\lambda-return algorithm, literally: for each horizon hh, redo all updates from 𝐰0\mathbf{w}_0 with targets Gt:hλG^\lambda_{t:h} that bootstrap from the diagonal weights; return the diagonal. The display checks the first row: at horizon 11 the algorithm makes exactly one TD(0) step.
const xsO = await wl`{{Subscript[x, 0, 1], Subscript[x, 0, 2]}, {Subscript[x, 1, 1], Subscript[x, 1, 2]}, {Subscript[x, 2, 1], Subscript[x, 2, 2]}, {0, 0}}`
const w0O = await wl`{Subscript[w, 1], Subscript[w, 2]}`
const onlineDiag = await wl`Fold[Function[{diag, h}, With[{gn = Function[{t, n}, Sum[\[Gamma]^(j - 1) Subscript[R, t + j], {j, 1, n}] + If[t + n < 3, \[Gamma]^n diag[[t + n]] . ${xsO}[[t + n + 1]], 0]]}, With[{gl = Function[{t}, (1 - \[Lambda]) Sum[\[Lambda]^(n - 1) gn[t, n], {n, 1, h - t - 1}] + \[Lambda]^(h - t - 1) gn[t, h - t]]}, Append[diag, Expand[Fold[Function[{w, t}, w + \[Alpha] (gl[t] - w . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]], ${w0O}, Range[0, h - 1]]]]]]], {${w0O}}, {1, 2, 3}]`
await show`Simplify[${onlineDiag}[[2]] - (${w0O} + \[Alpha] (Subscript[R, 1] + \[Gamma] ${w0O} . ${xsO}[[2]] - ${w0O} . ${xsO}[[1]]) ${xsO}[[1]])]`
{0,0} \{0,0\} True online TD(λ\lambda) of Algorithm 12.5 on the same episode, and the difference of the two weight sequences: identically zero.
const trueOnline = await wl`FoldList[Function[{st, t}, Module[{w = st[[1]], z = st[[2]], vo = st[[3]], v, v1, d, zn}, v = w . ${xsO}[[t + 1]]; v1 = w . ${xsO}[[t + 2]]; d = Subscript[R, t + 1] + \[Gamma] v1 - v; zn = \[Gamma] \[Lambda] z + (1 - \[Alpha] \[Gamma] \[Lambda] z . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]; {Expand[w + \[Alpha] (d + v - vo) zn - \[Alpha] (v - vo) ${xsO}[[t + 1]]], zn, v1}]], {${w0O}, {0, 0}, 0}, {0, 1, 2}][[All, 1]]`
await show`Expand[${trueOnline} - ${onlineDiag}]`
(00000000) \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ 0 & 0 \\ 0 & 0 \\ \end{array} \right)

12.4.1Dutch Traces in Monte Carlo Learning

Dutch traces are not specific to TD learning: they appear as soon as one implements a forward view efficiently, even with no bootstrapping at all.Hado van Hasselt and Richard S. Sutton. Learning to Predict Independent of Span, 2015 Take linear gradient Monte Carlo (the LMS rule) on an episode whose only reward GG arrives at the end, with γ=1\gamma = 1:

𝐰t+1=𝐰t+α(G−𝐰t⊤𝐱t)𝐱t,0≤t<T. \mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\bigl(G - \mathbf{w}_t^\top\mathbf{x}_t\bigr)\mathbf{x}_t, \qquad 0 \le t < T.

Nothing can be done before GG is known, and the obvious implementation stores all TT feature vectors and spends O(Td)O(Td) at the end. Writing the update as 𝐰t+1=𝐅t𝐰t+αG𝐱t\mathbf{w}_{t+1} = \mathbf{F}_t\mathbf{w}_t + \alpha G\mathbf{x}_t with the fading matrix 𝐅t=𝐈−α𝐱t𝐱t⊤\mathbf{F}_t = \mathbf{I} - \alpha\mathbf{x}_t\mathbf{x}_t^\top reveals a better way.

Proposition 12.7 (Dutch traces for Monte Carlo). Let 𝐚−1=𝐰0\mathbf{a}_{-1} = \mathbf{w}_0, 𝐳−1=𝟎\mathbf{z}_{-1} = \mathbf{0} and, for 0≤t<T0 \le t < T,

𝐚t=𝐚t−1−α 𝐱t𝐱t⊤𝐚t−1,𝐳t=𝐳t−1+(1−α 𝐳t−1⊤𝐱t)𝐱t. \mathbf{a}_t = \mathbf{a}_{t-1} - \alpha\,\mathbf{x}_t\mathbf{x}_t^\top\mathbf{a}_{t-1}, \qquad \mathbf{z}_t = \mathbf{z}_{t-1} + \bigl(1 - \alpha\,\mathbf{z}_{t-1}^\top\mathbf{x}_t\bigr)\mathbf{x}_t. (12.10)

Then the final LMS weights are 𝐰T=𝐚T−1+αG 𝐳T−1\mathbf{w}_T = \mathbf{a}_{T-1} + \alpha G\,\mathbf{z}_{T-1}, computed with O(d)O(d) work and memory per step.

Proof. Unrolling 𝐰t+1=𝐅t𝐰t+αG𝐱t\mathbf{w}_{t+1} = \mathbf{F}_t\mathbf{w}_t + \alpha G\mathbf{x}_t gives 𝐰T=𝐅T−1⋯𝐅0𝐰0+αG∑k=0T−1𝐅T−1⋯𝐅k+1𝐱k\mathbf{w}_T = \mathbf{F}_{T-1}\cdots\mathbf{F}_0\mathbf{w}_0 + \alpha G\sum_{k=0}^{T-1}\mathbf{F}_{T-1}\cdots\mathbf{F}_{k+1}\mathbf{x}_k. The first term is 𝐚T−1\mathbf{a}_{T-1}, because 𝐚t=𝐅t𝐚t−1\mathbf{a}_t = \mathbf{F}_t\mathbf{a}_{t-1}. Call the sum 𝐳T−1\mathbf{z}_{T-1}; splitting off its last term, 𝐳t=𝐅t𝐳t−1+𝐱t=𝐳t−1−α𝐱t(𝐱t⊤𝐳t−1)+𝐱t\mathbf{z}_t = \mathbf{F}_t\mathbf{z}_{t-1} + \mathbf{x}_t = \mathbf{z}_{t-1} - \alpha\mathbf{x}_t(\mathbf{x}_t^\top\mathbf{z}_{t-1}) + \mathbf{x}_t, which is (12.10). □

This 𝐳t\mathbf{z}_t is the dutch trace (12.9) with γλ=1\gamma\lambda = 1.Sutton and Barto define 𝐚t≐𝐅t𝐅t−1⋯𝐅0𝐰0\mathbf{a}_t \doteq \mathbf{F}_t\mathbf{F}_{t-1}\cdots\mathbf{F}_0\mathbf{w}_0 but initialize 𝐚0=𝐰0\mathbf{a}_0 = \mathbf{w}_0 and apply the recursion 𝐚t=𝐅t𝐚t−1\mathbf{a}_t = \mathbf{F}_t\mathbf{a}_{t-1} only for 1≤t<T1 \le t < T. That produces 𝐅T−1⋯𝐅1𝐰0\mathbf{F}_{T-1}\cdots\mathbf{F}_1\mathbf{w}_0, without the factor 𝐅0\mathbf{F}_0. Their own definition gives 𝐚0=𝐅0𝐰0=𝐰0−α𝐱0𝐱0⊤𝐰0\mathbf{a}_0 = \mathbf{F}_0\mathbf{w}_0 = \mathbf{w}_0 - \alpha\mathbf{x}_0\mathbf{x}_0^\top\mathbf{w}_0, or equivalently the start 𝐚−1=𝐰0\mathbf{a}_{-1} = \mathbf{w}_0 used in the proposition. The two coincide when 𝐱0⊤𝐰0=0\mathbf{x}_0^\top\mathbf{w}_0 = 0, in particular for the usual 𝐰0=𝟎\mathbf{w}_0 = \mathbf{0}; the second check below gives the error otherwise. Eligibility traces, it seems, are what any efficient implementation of a long-horizon prediction needs, TD or not.

LMS run to the end of a three-step episode versus the dutch-trace recursion (12.10), with arbitrary 𝐱t∈ℝ2\mathbf{x}_t \in \mathbb{R}^2: the difference vanishes.
const lms = await wl`Fold[Function[{w, t}, w + \[Alpha] (G - w . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]], ${w0O}, {0, 1, 2}]`
const dutchMC = await wl`Fold[Function[{st, t}, {st[[1]] - \[Alpha] ${xsO}[[t + 1]] (${xsO}[[t + 1]] . st[[1]]), st[[2]] + (1 - \[Alpha] st[[2]] . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]}], {${w0O}, {0, 0}}, {0, 1, 2}]`
await show`Expand[${lms} - (${dutchMC}[[1]] + \[Alpha] G ${dutchMC}[[2]])]`
{0,0} \{0,0\} The same comparison with the start 𝐚0=𝐰0\mathbf{a}_0 = \mathbf{w}_0 and the recursion run only for t=1,2t = 1, 2: the result misses 𝐅2𝐅1(𝐅0−𝐈)𝐰0\mathbf{F}_2\mathbf{F}_1(\mathbf{F}_0 - \mathbf{I})\mathbf{w}_0, whose first-order term in α\alpha is −α𝐱0(𝐱0⊤𝐰0)-\alpha\mathbf{x}_0(\mathbf{x}_0^\top\mathbf{w}_0), shown here.
const aBook = await wl`Fold[Function[{a, t}, a - \[Alpha] ${xsO}[[t + 1]] (${xsO}[[t + 1]] . a)], ${w0O}, {1, 2}]`
await show`Map[Factor, Normal[Series[${lms} - (${aBook} + \[Alpha] G ${dutchMC}[[2]]), {\[Alpha], 0, 1}]]]`
[𝐰3−(𝐚2book+αG𝐳2)]O(α)={−αx0,1(w1x0,1+w2x0,2),−αx0,2(w1x0,1+w2x0,2)} \begin{aligned} &\Bigl[\mathbf{w}_3 - \bigl(\mathbf{a}^{\text{book}}_2 + \alpha G\mathbf{z}_2\bigr)\Bigr]_{O(\alpha)} \\ &\quad = \left\{-\alpha x_{0,1} \left(w_1 x_{0,1}+w_2 x_{0,2}\right),-\alpha x_{0,2} \left(w_1 x_{0,1}+w_2 x_{0,2}\right)\right\} \end{aligned}

12.5Sarsa(λ\lambda)

For action values everything carries over with q^(s,a,𝐰)\hat q(s, a, \mathbf{w}) in place of v^(s,𝐰)\hat v(s, \mathbf{w}), the action-value nn-step returns Gt:t+n=Rt+1+⋯+γnq^(St+n,At+n,𝐰t+n−1)G_{t:t+n} = R_{t+1} + \dots + \gamma^n\hat q(S_{t+n}, A_{t+n}, \mathbf{w}_{t+n-1}), and state–action features 𝐱t=𝐱(St,At)\mathbf{x}_t = \mathbf{x}(S_t, A_t). **Sarsa(λ\lambda)** is TD(λ\lambda) with

δt=Rt+1+γq^(St+1,At+1,𝐰t)−q^(St,At,𝐰t),𝐳t=γλ 𝐳t−1+∇q^(St,At,𝐰t). \begin{aligned} \delta_t &= R_{t+1} + \gamma\hat q(S_{t+1}, A_{t+1}, \mathbf{w}_t) - \hat q(S_t, A_t, \mathbf{w}_t), \\ \mathbf{z}_t &= \gamma\lambda\,\mathbf{z}_{t-1} + \nabla\hat q(S_t, A_t, \mathbf{w}_t). \end{aligned}

The advantage over one-step and nn-step control is easy to see in a gridworld where the only reward is at the goal. When it is reached, one-step Sarsa raises the value of the last action only, and nn-step Sarsa the last nn equally; Sarsa(λ\lambda) raises every action of the episode, fading with distance from the goal, which is usually the right credit assignment. With tile coding the features are binary and the loops over active features make each step cheap; with the true online construction one obtains the following algorithm, which on Mountain Car outperforms Sarsa(λ\lambda) with accumulating and with replacing traces.

Algorithm 12.8 (True online Sarsa(λ\lambda)). Input: features 𝐱(s,a)\mathbf{x}(s, a) with 𝐱(terminal,⋅)=𝟎\mathbf{x}(\text{terminal}, \cdot) = \mathbf{0}; a policy π\pi, or ε\varepsilon-greedy selection with respect to q^\hat q for control. Parameters: α>0\alpha > 0, λ∈[0,1]\lambda \in [0, 1]. Initialize: 𝐰∈ℝd\mathbf{w} \in \mathbb{R}^d.

Loop for each episode:

  1. Initialize SS; choose AA from SS; 𝐱←𝐱(S,A)\mathbf{x} \leftarrow \mathbf{x}(S, A), 𝐳←𝟎\mathbf{z} \leftarrow \mathbf{0}, Qold←0Q_{\text{old}} \leftarrow 0.
  2. Loop for each step until S′S' is terminal:
    • take AA, observe R,S′R, S'; choose A′A' from S′S'; 𝐱′←𝐱(S′,A′)\mathbf{x}' \leftarrow \mathbf{x}(S', A');
    • Q←𝐰⊤𝐱Q \leftarrow \mathbf{w}^\top\mathbf{x}, Q′←𝐰⊤𝐱′Q' \leftarrow \mathbf{w}^\top\mathbf{x}', δ←R+γQ′−Q\delta \leftarrow R + \gamma Q' - Q;
    • 𝐳←γλ 𝐳+(1−αγλ 𝐳⊤𝐱) 𝐱\mathbf{z} \leftarrow \gamma\lambda\,\mathbf{z} + (1 - \alpha\gamma\lambda\,\mathbf{z}^\top\mathbf{x})\,\mathbf{x};
    • 𝐰←𝐰+α(δ+Q−Qold) 𝐳−α(Q−Qold) 𝐱\mathbf{w} \leftarrow \mathbf{w} + \alpha(\delta + Q - Q_{\text{old}})\,\mathbf{z} - \alpha(Q - Q_{\text{old}})\,\mathbf{x};
    • Qold←Q′Q_{\text{old}} \leftarrow Q', 𝐱←𝐱′\mathbf{x} \leftarrow \mathbf{x}', A←A′A \leftarrow A'.

12.6Variable λ\lambda and γ\gamma

The last generalization of the forward view lets bootstrapping and discounting vary from step to step: λt≐λ(St,At)\lambda_t \doteq \lambda(S_t, A_t) and γt≐γ(St)\gamma_t \doteq \gamma(S_t). The function γ:𝒮→[0,1]\gamma : \mathcal{S} \to [0, 1] is a termination function, and it changes the problem, since the return becomes

Gt≐Rt+1+γt+1Gt+1=∑k=t∞(∏i=t+1kγi)Rk+1. G_t \doteq R_{t+1} + \gamma_{t+1}G_{t+1} = \sum_{k=t}^\infty\Bigl(\prod_{i=t+1}^{k}\gamma_i\Bigr)R_{k+1}. (12.11)

A terminal state is now just a state with γ(s)=0\gamma(s) = 0 that transitions to the start distribution, so episodic and discounted continuing tasks become one stream of experience; pseudo-termination, predicting a quantity without altering the flow of the process, is included. Varying λ\lambda, on the other hand, changes only the solution method: how much to trust the estimate at each state. The state-based λ\lambda-return generalizes (12.4) to

Gtλs≐Rt+1+γt+1((1−λt+1)v^(St+1,𝐰t)+λt+1Gt+1λs), G_t^{\lambda s} \doteq R_{t+1} + \gamma_{t+1}\bigl((1 - \lambda_{t+1})\hat v(S_{t+1}, \mathbf{w}_t) + \lambda_{t+1}G_{t+1}^{\lambda s}\bigr), (12.12)

and the action-based one either bootstraps from q^(St+1,At+1,𝐰t)\hat q(S_{t+1}, A_{t+1}, \mathbf{w}_t) (Sarsa form) or from V¯t(St+1)≐∑aπ(a∣St+1)q^(St+1,a,𝐰t)\bar V_t(S_{t+1}) \doteq \sum_a\pi(a \mid S_{t+1})\hat q(S_{t+1}, a, \mathbf{w}_t) (Expected Sarsa form). Watkins already noted that cutting a return at a chosen time amounts to setting λ=0\lambda = 0 there.

12.7Off-policy Traces with Control Variates

For full λ\lambda-returns there is no practical way to apply importance sampling to the whole return; instead the ratio is applied per decision, with the control variate of Chapter 7:

Gtλs≐ρt(Rt+1+γt+1((1−λt+1)v^(St+1,𝐰t)+λt+1Gt+1λs))+(1−ρt)v^(St,𝐰t). \begin{aligned} G_t^{\lambda s} \doteq{} &\rho_t\Bigl(R_{t+1} + \gamma_{t+1}\bigl((1 - \lambda_{t+1})\hat v(S_{t+1}, \mathbf{w}_t) + \lambda_{t+1}G_{t+1}^{\lambda s}\bigr)\Bigr) \\ &+ (1 - \rho_t)\hat v(S_t, \mathbf{w}_t). \end{aligned} (12.13)

The added term (1−ρt)v^(St)(1 - \rho_t)\hat v(S_t) has expectation zero under bb, because 𝔼b[ρt∣St]=1\mathbb{E}_b[\rho_t \mid S_t] = 1; it changes nothing on average but cancels much of the variance when ρt\rho_t is far from one: if ρt=0\rho_t = 0 the target is the current estimate and no update occurs. For a fixed value function, with δts=Rt+1+γt+1v^(St+1)−v^(St)\delta_t^s = R_{t+1} + \gamma_{t+1}\hat v(S_{t+1}) - \hat v(S_t), the same telescoping as in Proposition 12.1 gives

Gtλs=v^(St)+ρt∑k=t∞δks∏i=t+1kγiλiρi, G_t^{\lambda s} = \hat v(S_t) + \rho_t\sum_{k=t}^\infty\delta_k^s\prod_{i=t+1}^{k}\gamma_i\lambda_i\rho_i, (12.14)

approximately when the weights change. Summing the forward-view updates over time and exchanging the sums, exactly as in Theorem 12.3,

∑tρt∇v^(St)∑k≥tδks∏i=t+1kγiλiρi=∑kδks∑t≤kρt∇v^(St)∏i=t+1kγiλiρi⏟𝐳k, \sum_t\rho_t\nabla\hat v(S_t)\sum_{k \ge t}\delta_k^s\prod_{i=t+1}^k\gamma_i\lambda_i\rho_i = \sum_k\delta_k^s\underbrace{\sum_{t \le k}\rho_t\nabla\hat v(S_t)\prod_{i=t+1}^k\gamma_i\lambda_i\rho_i}_{\mathbf{z}_k},

and the bracketed sum obeys a one-step recursion, the general accumulating trace

𝐳t=ρt(γtλt𝐳t−1+∇v^(St,𝐰t)). \mathbf{z}_t = \rho_t\bigl(\gamma_t\lambda_t\mathbf{z}_{t-1} + \nabla\hat v(S_t, \mathbf{w}_t)\bigr). (12.15)

With 𝐰t+1=𝐰t+αδts𝐳t\mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\delta_t^s\mathbf{z}_t this is TD(λ\lambda) for on- or off-policy data; with ρt≡1\rho_t \equiv 1 it is Algorithm 12.2. For action values, starting from the Expected Sarsa form, Gtλa=Rt+1+γt+1(V¯t(St+1)+λt+1ρt+1[Gt+1λa−q^(St+1,At+1,𝐰t)])G_t^{\lambda a} = R_{t+1} + \gamma_{t+1}\bigl(\bar V_t(S_{t+1}) + \lambda_{t+1}\rho_{t+1}[G_{t+1}^{\lambda a} - \hat q(S_{t+1}, A_{t+1}, \mathbf{w}_t)]\bigr), the same steps give the TD error δta=Rt+1+γt+1V¯t(St+1)−q^(St,At,𝐰t)\delta_t^a = R_{t+1} + \gamma_{t+1}\bar V_t(S_{t+1}) - \hat q(S_t, A_t, \mathbf{w}_t) and the trace

𝐳t=γtλtρt 𝐳t−1+∇q^(St,At,𝐰t), \mathbf{z}_t = \gamma_t\lambda_t\rho_t\,\mathbf{z}_{t-1} + \nabla\hat q(S_t, A_t, \mathbf{w}_t), (12.16)

an off-policy Expected Sarsa(λ\lambda). Two caveats. At λ=1\lambda = 1 these methods are related to Monte Carlo only in expectation, not episode by episode: they make irrevocable updates along the way, whereas an importance-sampled Monte Carlo update would be canceled entirely by a later zero ratio. Provisional-weight methods, PTD(λ\lambda) and PQ(λ\lambda), restore an exact equivalence. And for λ<1\lambda < 1 all of them bootstrap off-policy with function approximation: the deadly triad of Section 11.3 applies. Off-policy traces correct the targets, not the distribution of updates.

The expansion (12.14) is exact for fixed values: an episode of length 33 with arbitrary ratios ρk\rho_k and constant γ,λ\gamma, \lambda (γ3=0\gamma_3 = 0 at termination).
const gOff = await wl`Function[{t}, Fold[Function[{g, k}, Subscript[\[Rho], k] (Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] ((1 - \[Lambda]) Subscript[V, k + 1] + \[Lambda] g), 0]) + (1 - Subscript[\[Rho], k]) Subscript[V, k]], 0, Range[2, t, -1]]]`
const dOff = await wl`Function[{k}, Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] Subscript[V, k + 1], 0] - Subscript[V, k]]`
await show`Table[Simplify[${gOff}[t] - (Subscript[V, t] + Subscript[\[Rho], t] Sum[${dOff}[k] Product[\[Gamma] \[Lambda] Subscript[\[Rho], i], {i, t + 1, k}], {k, t, 2}])], {t, 0, 2}]`
{0,0,0} \{0,0,0\} The control variate costs nothing in expectation: with two actions, ρ=π/b\rho = \pi/b, action-conditional returns g1,g2g_1, g_2 and any baseline VV, the behavior expectation of the one-step target is the target-policy expectation.
await show`Simplify[Sum[{Subscript[b, 1], 1 - Subscript[b, 1]}[[a]] (({Subscript[\[Pi], 1], 1 - Subscript[\[Pi], 1]}[[a]]/{Subscript[b, 1], 1 - Subscript[b, 1]}[[a]]) {Subscript[g, 1], Subscript[g, 2]}[[a]] + (1 - {Subscript[\[Pi], 1], 1 - Subscript[\[Pi], 1]}[[a]]/{Subscript[b, 1], 1 - Subscript[b, 1]}[[a]]) V), {a, 1, 2}]]`
−g2(−1+π1)+g1π1 -g_2 \left(-1+\pi _1\right)+g_1 \pi _1

12.8Watkins’s Q(λ\lambda) and Tree-Backup(λ\lambda)

**Watkins’s Q(λ\lambda)** extends Q-learning by decaying the traces normally while greedy actions are taken and cutting them to zero at the first exploratory action, after which the greedy target policy’s return no longer applies. **Tree-Backup(λ\lambda)**, TB(λ\lambda), is the trace version of the tree-backup algorithm of Chapter 7 and, like it, uses no importance sampling. Expanding the Expected Sarsa form of the λ\lambda-return with the tree-backup target,

Gtλa=Rt+1+γt+1(V¯t(St+1)+λt+1π(At+1∣St+1)[Gt+1λa−q^(St+1,At+1,𝐰t)]), \begin{aligned} G_t^{\lambda a} = R_{t+1} + \gamma_{t+1}\Bigl(&\bar V_t(S_{t+1}) \\ &+ \lambda_{t+1}\pi(A_{t+1} \mid S_{t+1})\bigl[G_{t+1}^{\lambda a} - \hat q(S_{t+1}, A_{t+1}, \mathbf{w}_t)\bigr]\Bigr), \end{aligned}

the derivation of the previous section goes through with ρ\rho replaced by π(A∣S)\pi(A \mid S):

𝐳t=γtλt π(At∣St) 𝐳t−1+∇q^(St,At,𝐰t). \mathbf{z}_t = \gamma_t\lambda_t\,\pi(A_t \mid S_t)\,\mathbf{z}_{t-1} + \nabla\hat q(S_t, A_t, \mathbf{w}_t). (12.17)

For a greedy target policy π(At∣St)\pi(A_t \mid S_t) is 11 for a greedy action and 00 otherwise, so (12.17) is Watkins’s trace cutting; TB(λ\lambda) is its natural generalization to stochastic target policies, and arguably the true successor of Q-learning. Both are semi-gradient methods and both can diverge with function approximation.

12.9Stable Off-policy Methods with Traces

The Gradient-TD and Emphatic-TD ideas of Chapter 11 extend to traces, giving linear off-policy methods with convergence guarantees. With δts\delta_t^s, ρt\rho_t and the trace (12.15) (linear case, ∇v^=𝐱t\nabla\hat v = \mathbf{x}_t), and a secondary vector 𝐯\mathbf{v} with step size β\beta:

  • **GTD(λ\lambda)**, the trace version of TDC: 𝐰t+1=𝐰t+αδts𝐳t−αγt+1(1−λt+1)(𝐳t⊤𝐯t)𝐱t+1\mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\delta_t^s\mathbf{z}_t - \alpha\gamma_{t+1}(1 - \lambda_{t+1})(\mathbf{z}_t^\top\mathbf{v}_t)\mathbf{x}_{t+1} and 𝐯t+1=𝐯t+βδts𝐳t−β(𝐯t⊤𝐱t)𝐱t\mathbf{v}_{t+1} = \mathbf{v}_t + \beta\delta_t^s\mathbf{z}_t - \beta(\mathbf{v}_t^\top\mathbf{x}_t)\mathbf{x}_t. At λ=1\lambda = 1 the correction vanishes, as it should: without bootstrapping there is nothing to correct.
  • GQ(λ\lambda), the action-value analogue, uses δta\delta_t^a, the trace (12.16) and the target-policy average feature 𝐱¯t+1=∑aπ(a∣St+1)𝐱(St+1,a)\bar{\mathbf{x}}_{t+1} = \sum_a\pi(a \mid S_{t+1})\mathbf{x}(S_{t+1}, a) in the correction term; with a target policy that is greedy-ish, it is a stable control method.
  • HTD(λ\lambda) adds a second, behavior-policy trace 𝐳tb=γtλt𝐳t−1b+𝐱t\mathbf{z}_t^b = \gamma_t\lambda_t\mathbf{z}_{t-1}^b + \mathbf{x}_t and corrects with (𝐳t−𝐳tb)⊤𝐯t(\mathbf{z}_t - \mathbf{z}_t^b)^\top\mathbf{v}_t; when b=πb = \pi the two traces coincide and HTD(λ\lambda) is TD(λ\lambda), keeping its speed and single step size in the on-policy case.
  • **Emphatic TD(λ\lambda)** uses the followon trace Ft=ρt−1γtFt−1+ItF_t = \rho_{t-1}\gamma_tF_{t-1} + I_t, the emphasis Mt=λtIt+(1−λt)FtM_t = \lambda_tI_t + (1 - \lambda_t)F_t and the trace 𝐳t=ρt(γtλt𝐳t−1+Mt𝐱t)\mathbf{z}_t = \rho_t(\gamma_t\lambda_t\mathbf{z}_{t-1} + M_t\mathbf{x}_t) in the ordinary update 𝐰t+1=𝐰t+αδt𝐳t\mathbf{w}_{t+1} = \mathbf{w}_t + \alpha\delta_t\mathbf{z}_t. It is stable for any state-dependent λ\lambda, which is not true even of on-policy TD(λ\lambda), at the price of high variance.

12.10Choosing λ\lambda

Traces cost little. In the tabular case only the few recently visited states have non-negligible traces, so an implementation can update only those and pay a small multiple of the one-step cost; with function approximation the overhead is roughly a doubling of memory and computation. Empirically, on tasks with many steps per episode, or many steps within the discount horizon, an intermediate λ\lambda is clearly better than λ=0\lambda = 0, while λ\lambda close to 11 degrades sharply: traces should move TD toward Monte Carlo, not all the way. They are the first line of defense against long-delayed rewards and against partially non-Markov states, where pure bootstrapping is misled by a poor state representation. When data are cheap, as with a fast simulator, the per-sample gain of traces may not pay for their cost, and one-step methods on more data can win.

12.11Beyond the Book: Off-policy Returns at Scale

The off-policy traces of Section 12.7 multiply ratios ρi\rho_i that can be large, and the tree-backup traces multiply probabilities π(Ai∣Si)\pi(A_i \mid S_i) that cut learning short even on-policy. The modern answer is to truncate the ratios.Beyond Sutton and Barto (2018). Retrace (Munos et al., 2016) appears in the book only as a reference in its variance discussion, and V-trace (Espeholt et al., 2018) postdates it. The JMLR analysis of true online TD (van Seijen et al., 2016) is the source of Theorem 12.6.

Retrace(λ\lambda). Munos et al. write many return-based algorithms as one operator with “traces” cs≥0c_s \ge 0,Rémi Munos, Tom Stepleton, Anna Harutyunyan, and Marc G. Bellemare. Safe and Efficient Off-Policy Reinforcement Learning. In Advances in Neural Information Processing Systems 29 (NIPS 2016), volume 29, pages 1054–1062. Curran Associates, Inc., 2016. URL https://proceedings.neurips.cc/paper/2016/hash/c3992e9a68c5ae12bd18488bc579b30d-Abstract.html

ℛQ(x,a)=Q(x,a)+𝔼b[∑t≥0γt(∏s=1tcs)×(rt+γ 𝔼πQ(xt+1,⋅)−Q(xt,at))], \begin{aligned} \mathcal{R}Q(x, a) = Q(x, a) + \mathbb{E}_b\Bigl[&\sum_{t \ge 0}\gamma^t\Bigl(\prod_{s=1}^{t}c_s\Bigr) \\ &\times\bigl(r_t + \gamma\,\mathbb{E}_\pi Q(x_{t+1}, \cdot) - Q(x_t, a_t)\bigr)\Bigr], \end{aligned}

with cs=ρsc_s = \rho_s for importance sampling, cs=λπ(as∣xs)c_s = \lambda\pi(a_s \mid x_s) for TB(λ\lambda) and cs=λc_s = \lambda for the uncorrected Qπ^\pi(λ\lambda). **Retrace(λ\lambda)** takes cs=λmin⁡(1,π(as∣xs)/b(as∣xs))c_s = \lambda\min\bigl(1, \pi(a_s \mid x_s)/b(a_s \mid x_s)\bigr). They show that any traces with 0≤cs≤π/b0 \le c_s \le \pi/b give a γ\gamma-contraction around qπq_\pi for arbitrary bb (safety), Retrace’s traces are never cut on-policy (efficiency) and never exceed one (low variance), and in control, with increasingly greedy π\pi, the sampled algorithm converges to q∗q_* without the GLIE assumption, which yields as a corollary the first convergence proof of Watkins’s Q(λ\lambda), open since 1989.

V-trace. In the distributed IMPALA architecture, actors run slightly stale policies bb and the learner corrects with the V-trace targetLasse Espeholt, Hubert Soyer, Rémi Munos, Karen Simonyan, Volodymyr Mnih, Tom Ward, Yotam Doron, Vlad Firoiu, Tim Harley, Iain Dunning, Shane Legg, and Koray Kavukcuoglu. IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures. In Proceedings of the 35th International Conference on Machine Learning (ICML 2018), volume 80 of Proceedings of Machine Learning Research, pages 1407–1416, 2018

vs=V(xs)+∑t=ss+n−1γt−s(∏i=st−1ci)ρt(rt+γV(xt+1)−V(xt)),ρt=min⁡(ρ¯,π(at∣xt)b(at∣xt)),ci=λmin⁡(c¯,π(ai∣xi)b(ai∣xi)), \begin{aligned} v_s &= V(x_s) + \sum_{t=s}^{s+n-1}\gamma^{t-s}\Bigl(\prod_{i=s}^{t-1}c_i\Bigr)\rho_t\bigl(r_t + \gamma V(x_{t+1}) - V(x_t)\bigr), \\ \rho_t &= \min\Bigl(\bar\rho, \tfrac{\pi(a_t \mid x_t)}{b(a_t \mid x_t)}\Bigr), \qquad c_i = \lambda\min\Bigl(\bar c, \tfrac{\pi(a_i \mid x_i)}{b(a_i \mid x_i)}\Bigr), \end{aligned}

which is (12.14) with two separately clipped ratios. The truncation level c¯\bar c of the trace only affects speed (variance); ρ¯\bar\rho sets the fixed point, which is the value of the policy πρ¯(a∣x)∝min⁡(ρ¯ b(a∣x),π(a∣x))\pi_{\bar\rho}(a \mid x) \propto \min\bigl(\bar\rho\,b(a \mid x), \pi(a \mid x)\bigr), between bb and π\pi. On-policy, with c¯≥1\bar c \ge 1, V-trace is the ordinary nn-step (or TD(λ\lambda)) target.

Generalized advantage estimation. The advantage estimator GAE of policy-gradient methods, ∑l≥0(γλ)lδt+l\sum_{l \ge 0}(\gamma\lambda)^l\delta_{t+l}, is by (12.5) exactly the λ\lambda-return error Gtλ−V(St)G_t^\lambda - V(S_t) used as an advantage, computed backward over a batch with the recursion A^t=δt+γλA^t+1\hat A_t = \delta_t + \gamma\lambda\hat A_{t+1}, a trace in all but name; it is treated in Section 13.8.3.

True online learning. Van Seijen et al. gave the full proof of Theorem 12.6, showed how to derive true online versions from modified forward views, and found true online TD(λ\lambda) and Sarsa(λ\lambda) faster than their classic versions in many domains and never worse.Harm van Seijen, A. Rupam Mahmood, Patrick M. Pilarski, Marlos C. Machado, and Richard S. Sutton. True Online Temporal-Difference Learning. Journal of Machine Learning Research, 17(145):1–40, 2016

Every method so far, with or without traces, has learned values and derived its policy from them. Chapter 13 learns the policy directly; value functions and the traces of this chapter return there as the critic of actor–critic methods.

12.12Exercises

Exercise 12.9. The weights of the λ\lambda-return decay by the factor λ\lambda per step. Find the half-life τλ\tau_\lambda, the number of steps after which the weight has halved, and evaluate it for λ=0.9\lambda = 0.9 and λ=0.99\lambda = 0.99. What is the half-life of the trace 𝐳t\mathbf{z}_t in TD(λ\lambda)?

Solution

Solve λτ=12\lambda^{\tau} = \frac{1}{2}, so τλ=ln⁡2/ln⁡(1/λ)\tau_\lambda = \ln 2/\ln(1/\lambda).

The half-life and its value at λ=0.9\lambda = 0.9 and 0.990.99.
const halfLife = await wl`Log[2]/Log[1/\[Lambda]]`
await show`{Simplify[(\[Lambda]^${halfLife}), 0 < \[Lambda] < 1], N[${halfLife} /. \[Lambda] -> {9/10, 99/100}]}`
{12,{6.57881,68.9676}} \left\{\frac{1}{2},\{6.57881,68.9676\}\right\}

So τ0.9≈6.6\tau_{0.9} \approx 6.6 and τ0.99≈69\tau_{0.99} \approx 69 steps. The trace decays by γλ\gamma\lambda per step, so its half-life is ln⁡2/ln⁡(1/(γλ))\ln 2/\ln(1/(\gamma\lambda)), shorter than τλ\tau_\lambda when γ<1\gamma < 1: discounting and bootstrapping shorten the memory together.

Exercise 12.10. Take tabular (one-hot) features, 𝐱t=𝐞St\mathbf{x}_t = \mathbf{e}_{S_t}. Show that the dutch trace (12.9) updates the visited component as zs←(1−α)γλzs+1z_{s} \leftarrow (1 - \alpha)\gamma\lambda z_{s} + 1 and decays all others by γλ\gamma\lambda. Conclude that for α=1\alpha = 1 it is the replacing trace, and that for small α\alpha it is close to the accumulating trace.

SolutionOne dutch-trace step with 𝐱=𝐞1\mathbf{x} = \mathbf{e}_1 in three dimensions, then at α=1\alpha = 1.
const dutchStep = await wl`Function[{z, x}, \[Gamma] \[Lambda] z + (1 - \[Alpha] \[Gamma] \[Lambda] z . x) x]`
await show`{Collect[${dutchStep}[{Subscript[z, 1], Subscript[z, 2], Subscript[z, 3]}, {1, 0, 0}], Subscript[z, 1], Factor], ${dutchStep}[{Subscript[z, 1], Subscript[z, 2], Subscript[z, 3]}, {1, 0, 0}] /. \[Alpha] -> 1}`
(1−(−1+α)γλz1γλz2γλz31γλz2γλz3) \left( \begin{array}{ccc} 1-(-1+\alpha ) \gamma \lambda z_1 & \gamma \lambda z_2 & \gamma \lambda z_3 \\ 1 & \gamma \lambda z_2 & \gamma \lambda z_3 \\ \end{array} \right)

Only the visited component is affected by the correction: z1←γλz1+1−αγλz1=(1−α)γλz1+1z_1 \leftarrow \gamma\lambda z_1 + 1 - \alpha\gamma\lambda z_1 = (1 - \alpha)\gamma\lambda z_1 + 1. With α=1\alpha = 1 it is reset to 11, the replacing trace; as α→0\alpha \to 0 it becomes γλz1+1\gamma\lambda z_1 + 1, the accumulating trace. The dutch trace interpolates, with the step size deciding how much of the old credit the new update overwrites; in the tabular case with α=1\alpha = 1 a visited state’s value is overwritten completely, so its old eligibility is rightly forgotten.

Exercise 12.11. Prove that, for fixed action values Qk≐q^(Sk,Ak,𝐰)Q_k \doteq \hat q(S_k, A_k, \mathbf{w}), the off-policy Expected Sarsa λ\lambda-return of Section 12.7 satisfies Gtλa=Qt+∑k≥tδka∏i=t+1kγiλiρiG_t^{\lambda a} = Q_t + \sum_{k \ge t}\delta_k^a\prod_{i=t+1}^k\gamma_i\lambda_i\rho_i, and use it to derive the trace (12.16).

Solution

Subtract QtQ_t from the recursion: Gtλa−Qt=(Rt+1+γt+1V¯(St+1)−Qt)+γt+1λt+1ρt+1(Gt+1λa−Qt+1)=δta+γt+1λt+1ρt+1(Gt+1λa−Qt+1)G_t^{\lambda a} - Q_t = \bigl(R_{t+1} + \gamma_{t+1}\bar V(S_{t+1}) - Q_t\bigr) + \gamma_{t+1}\lambda_{t+1}\rho_{t+1}(G_{t+1}^{\lambda a} - Q_{t+1}) = \delta_t^a + \gamma_{t+1}\lambda_{t+1}\rho_{t+1}(G_{t+1}^{\lambda a} - Q_{t+1}), and unroll. A symbolic check for an episode of length 33 with expected next values V¯k\bar V_k:

The action-value expansion is exact for fixed values; the episode terminates after the third reward.
const gQ = await wl`Function[{t}, Fold[Function[{g, k}, Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] (Subscript[Vbar, k + 1] + \[Lambda] Subscript[\[Rho], k + 1] (g - Subscript[Q, k + 1])), 0]], 0, Range[2, t, -1]]]`
const dQ = await wl`Function[{k}, Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] Subscript[Vbar, k + 1], 0] - Subscript[Q, k]]`
await show`Table[Simplify[${gQ}[t] - (Subscript[Q, t] + Sum[${dQ}[k] Product[\[Gamma] \[Lambda] Subscript[\[Rho], i], {i, t + 1, k}], {k, t, 2}])], {t, 0, 2}]`
{0,0,0} \{0,0,0\}

The forward update α(Gtλa−Qt)∇q^(St,At)\alpha(G_t^{\lambda a} - Q_t)\nabla\hat q(S_t, A_t) summed over tt becomes, after exchanging the sums, α∑kδka𝐳k\alpha\sum_k\delta_k^a\mathbf{z}_k with 𝐳k=∑t≤k∇q^(St,At)∏i=t+1kγiλiρi\mathbf{z}_k = \sum_{t \le k}\nabla\hat q(S_t, A_t)\prod_{i=t+1}^k\gamma_i\lambda_i\rho_i, and splitting off the term t=kt = k gives 𝐳k=γkλkρk𝐳k−1+∇q^(Sk,Ak)\mathbf{z}_k = \gamma_k\lambda_k\rho_k\mathbf{z}_{k-1} + \nabla\hat q(S_k, A_k). Unlike the state-value trace (12.15), the ratio does not multiply the new gradient: the action AkA_k whose value is updated needs no correction, as in one-step Expected Sarsa.

Exercise 12.12. Let δt′≐Rt+1+γv^(St+1,𝐰t)−v^(St,𝐰t−1)\delta_t' \doteq R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}_t) - \hat v(S_t, \mathbf{w}_{t-1}), a TD error whose two values come from consecutive weight vectors. Show that the truncated λ\lambda-return (12.7), with nn-step returns bootstrapping from 𝐰t+n−1\mathbf{w}_{t+n-1}, satisfies Gt:t+kλ=v^(St,𝐰t−1)+∑i=tt+k−1(γλ)i−tδi′G_{t:t+k}^\lambda = \hat v(S_t, \mathbf{w}_{t-1}) + \sum_{i=t}^{t+k-1}(\gamma\lambda)^{i-t}\delta_i', even though the weights change. Why does this make truncated TD(λ\lambda) cheap?

Solution

Write Vi,j≐v^(Si,𝐰j)V_{i,j} \doteq \hat v(S_i, \mathbf{w}_j); only the values Vi,i−1V_{i,i-1}, each state valued by the weights in force when it was reached, appear in the nn-step returns, and the sum telescopes in them. For t=0t = 0, k=3k = 3:

Truncated λ\lambda-return with three components, against the sum of the “shifted” TD errors δi′\delta'_i.
const gTr = await wl`Function[{n}, Sum[\[Gamma]^(j - 1) Subscript[R, j], {j, 1, n}] + \[Gamma]^n Subscript[V, n, n - 1]]`
const dTr = await wl`Function[{i}, Subscript[R, i + 1] + \[Gamma] Subscript[V, i + 1, i] - Subscript[V, i, i - 1]]`
await show`Simplify[(1 - \[Lambda]) (${gTr}[1] + \[Lambda] ${gTr}[2]) + \[Lambda]^2 ${gTr}[3] - (Subscript[V, 0, -1] + Sum[(\[Gamma] \[Lambda])^i ${dTr}[i], {i, 0, 2}])]`
0 0

In general, Gt:t+kλ−Vt,t−1=δt′+γλ (Gt+1:t+kλ−Vt+1,t)G^\lambda_{t:t+k} - V_{t,t-1} = \delta_t' + \gamma\lambda\,(G^\lambda_{t+1:t+k} - V_{t+1,t}), by the argument of Proposition 12.1 applied to the truncated return. Each δi′\delta_i' is computed once, when Ri+1R_{i+1} and Si+1S_{i+1} arrive, and the target of the update at time t+kt + k is a running geometric sum of them: O(1)O(1) work per step beyond the value computations, regardless of kk.