12.2 — TD(λ): The Backward View and Eligibility Traces¶
Chapter 12: Eligibility Traces · Book sections: §12.2–§12.3 Previous: 12.1 — The λ-return · Next: 12.3 — Online λ-return & True Online TD(λ)
🌱 The Big Picture¶
TD(λ) is one of the most elegant algorithms in RL. It approximates the forward-view λ-return algorithm using:
- one extra vector (the eligibility trace \(\mathbf{z}\)),
- one scalar TD error per step,
- updates that flow backward to recently visited states — online, every step, same cost as TD(0)-with-a-trace.
Computational beauty: the forward view needs the future; TD(λ) needs only the present TD error plus a fading memory of the past. Same math, reversed bookkeeping.
🧮 The algorithm¶
The eligibility trace \(\mathbf{z}_t \in \mathbb{R}^d\) (same shape as \(\mathbf{w}\)) accumulates and fades:
"Every step: decay the whole trace by γλ, then bump up the components corresponding to the state just visited." The trace records which weights recently contributed to valuations — which are eligible for credit or blame.
Then the TD error
updates all eligible weights at once:
Semi-gradient TD(λ):
z = 0
for each step:
take action (per π), observe R, S′
z ← γλ z + ∇v̂(S,w)
δ = R + γ v̂(S′,w) − v̂(S,w)
w ← w + α δ z
S ← S′
Intuition: the bell and the echo 🔔¶
When a TD error occurs ("surprise! this turned out better/worse than expected"), it shouldn't only update the current state — earlier states led here and deserve a share of the credit/blame, with more recent states deserving more. The trace is exactly that recency-weighted attribution, decaying by γλ per step. (In the tabular case the trace value of a state is large if visited recently/often — credit assignment by recency and frequency.)
- λ = 0: trace = just the current gradient → exactly TD(0).
- λ = 1, γ = 1: nothing decays → the sum of updates over an episode equals a Monte Carlo update — "TD(1) is MC", but implementable online and incrementally (and on continuing tasks!).
🔬 Forward ⟷ backward equivalence¶
For offline updating (accumulate increments, apply at episode end), TD(λ)'s total update exactly equals the λ-return algorithm's. Online (updating each step), the equivalence is approximate — close for small α, and fixed entirely by True Online TD(λ) (next note).
On the 19-state random walk, TD(λ) performs almost identically to the offline λ-return algorithm — slightly worse at high α/λ extremes.
Convergence ✅¶
Linear TD(λ) converges on-policy, with an error bound that interpolates beautifully:
λ → 1 makes the bound approach 1 (MC's asymptotic optimality); λ = 0 gives the familiar \(\frac{1}{1-\gamma}\) TD bound. (In practice λ = 1 is rarely best — variance!)
🎯 Key Takeaways¶
- Eligibility trace: \(\mathbf{z} \leftarrow \gamma\lambda\mathbf{z} + \nabla\hat v\) — a fading memory of which weights deserve credit.
- TD(λ) update: \(\mathbf{w} \mathrel{+}= \alpha\,\delta_t\,\mathbf{z}_t\) — one error broadcast to all recently active weights.
- Backward view ≈ forward view (exact when offline); λ=0 → TD(0); λ=1 → online MC.
- Costs one extra vector; works on continuing tasks; fully online. The classic "single most elegant trick in RL."
➡️ Next: 12.3 — Online λ-return & True Online TD(λ) — fixing the small online discrepancy, with dutch traces.