Pearl
Lab

Chapter 04 · Foundations

Temporal-Difference Learning

Abstract. Temporal-difference (TD) learning estimates value functions from sampled experience while bootstrapping — updating a guess using another guess — instead of waiting for a full Monte Carlo return or summing over a known model. This chapter derives TD(0) from the Bellman expectation equation, interprets the TD error \(\delta_t\), contrasts on-policy SARSA with off-policy Q-learning, discusses exploration, sketches n-step returns and TD(\(\lambda\)), and flags the deadly triad that appears once function approximation enters. The laboratory runs SARSA and Q-learning side by side on a cliff-style grid so the on-/off-policy split is visible in behavior, not only in formulas.

1. Overview

Dynamic Programming needed \(P\) and a full sweep of \(S\). Real agents have neither. Temporal-difference learning is the practical middle path: interact, observe \((s,a,r,s')\), and apply a sample Bellman backup.

Almost every value-based deep RL method — DQN, Rainbow, the critic in SAC — is a neural approximation of ideas in this chapter. Actor–critic methods reuse the same \(\delta_t\) as an advantage signal. If DP was “Bellman with a model,” TD is “Bellman with a trajectory.”

2. The spectrum: DP, Monte Carlo, TD

All three methods target the same fixed point \(V^{\pi}\) (or \(Q^{\pi}\)/\(Q^*\)). They differ in how they form the target in the incremental update

(1) \[ \text{estimate} \;\leftarrow\; \text{estimate} + \alpha\, \big[\underbrace{\text{target}}_{\text{what we aim at}} - \text{estimate}\big]. \]
DP
Target \(=\mathbb{E}_\pi[r+\gamma V(s')]\). Full backup, needs \(P\). No sampling.
MC
Target \(=G_t\). Unbiased sample of return. Wait until episode ends. High variance.
TD
Target \(=r_{t+1}+\gamma V(s_{t+1})\). One real step + bootstrap. Online, low variance, biased until \(V\) is correct.

Monte Carlo is deferred to a short companion note in your roadmap; here we need only enough MC to place TD on the bias–variance dial.

Bootstrapping

An update bootstraps when its target depends on the current value estimate rather than only on observed rewards. DP and TD bootstrap; pure Monte Carlo does not. Bootstrapping enables learning before an episode ends and on continuing tasks — at the cost of coupling errors across states.

3. TD(0) for prediction

Prediction means: evaluate a fixed policy \(\pi\) — learn \(V^{\pi}\), not yet control. After taking \(a_t\sim\pi(\cdot\mid s_t)\) and observing \(r_{t+1},s_{t+1}\),

(2) \[ V(s_t) \;\leftarrow\; V(s_t) + \alpha \Big[ \underbrace{r_{t+1} + \gamma V(s_{t+1})}_{\text{TD target}} - V(s_t) \Big]. \]

Compare to the DP evaluation backup (Chapter 03): DP replaces \(r_{t+1}+\gamma V(s_{t+1})\) by its expectation under \(P\) and \(\pi\). TD(0) uses a single sample of that expectation. Under standard step-size conditions (Robbins–Monro: \(\sum\alpha_t=\infty\), \(\sum\alpha_t^2<\infty\)) and a tabular representation with adequate exploration of states under \(\pi\), \(V\to V^{\pi}\) with probability 1.

Definition 3.1 · TD(0)

The one-step tabular TD algorithm for policy evaluation using target \(r+\gamma V(s')\) and learning rate \(\alpha\in(0,1]\). Terminal states are usually assigned \(V(\text{terminal})=0\) and do not bootstrap beyond the episode.

4. The TD error \(\delta_t\)

(3) \[ \delta_t = r_{t+1} + \gamma V(s_{t+1}) - V(s_t). \]

\(\delta_t\) is a one-step prediction error:

  • \(\delta_t > 0\): the transition was better than \(V(s_t)\) predicted — raise \(V(s_t)\).
  • \(\delta_t < 0\): worse than predicted — lower \(V(s_t)\).
  • \(\delta_t = 0\): local consistency with the current table on this sample.

The same scalar reappears as the one-step advantage estimate in actor–critic (\(\hat A_t \approx \delta_t\)) and as the building block of GAE. Treat \(\delta_t\) as a first-class object, not a notational convenience.

Analysis · Sample of the Bellman residual

The Bellman residual at \(s\) is \(V(s) - \mathbb{E}[r+\gamma V(s')\mid s]\). TD’s \(\delta_t\) is a noisy measurement of the negative residual along one transition. Driving average \(\delta\) to zero along the visitation distribution is what “solving” the Bellman equation means empirically.

5. Bias–variance trade-off

Monte Carlo’s target \(G_t\) is unbiased for \(V^{\pi}(s_t)\) but depends on the entire future — high variance. TD’s target is biased whenever \(V\neq V^{\pi}\), yet only one step of environment noise enters — low variance. As \(V\) converges, TD’s bias vanishes. In finite-data regimes, variance usually hurts more, which is why TD often learns faster than MC in practice.

n-step TD and TD(\(\lambda\)) (Section 9) interpolate: longer backups reduce bias toward MC and increase variance.

6. SARSA — on-policy control

Control means learning a good policy, typically via \(Q(s,a)\). SARSA updates using the action that was actually taken next:

(4) \[ Q(s_t,a_t) \leftarrow Q(s_t,a_t) + \alpha \Big[ r_{t+1} + \gamma Q(s_{t+1}, a_{t+1}) - Q(s_t,a_t) \Big]. \]

The name is the tuple \((s_t,a_t,r_{t+1},s_{t+1},a_{t+1})\). Because the target uses \(a_{t+1}\sim\pi(\cdot\mid s_{t+1})\) from the same behavior policy that collected the data, SARSA is on-policy: it learns \(Q^{\pi}\) for the exploring policy you are following (e.g. \(\varepsilon\)-greedy), not for the pure greedy policy.

Remark · Cliff walking intuition

On a cliff grid, \(\varepsilon\)-greedy SARSA learns to walk a safe path away from the cliff because its Q-values include the risk of occasional exploratory steps off the edge. Q-learning (next section) can learn values for the optimal greedy path along the cliff — then suffer falls at execution if you still explore. The laboratory reproduces a miniature version of this contrast.

7. Q-learning — off-policy control

(5) \[ Q(s_t,a_t) \leftarrow Q(s_t,a_t) + \alpha \Big[ r_{t+1} + \gamma \max_{a'} Q(s_{t+1}, a') - Q(s_t,a_t) \Big]. \]

One word changes everything: \(Q(s_{t+1},a_{t+1})\) becomes \(\max_{a'}Q(s_{t+1},a')\). The target is a sample of the Bellman optimality backup \(\mathcal{T}^*\) (Chapter 02–03), not of \(\mathcal{T}^{\pi}\). You may behave with an exploratory policy while learning about the greedy/optimal policy — that is off-policy learning.

Off-policy control is what later enables experience replay: store transitions and reuse them even after the behavior policy has changed. DQN is Q-learning with a neural \(Q_\theta\), a replay buffer, and a lagged target network.

Theorem 7.1 · Tabular Q-learning (informal)

In a finite MDP, with all state–action pairs visited infinitely often, Robbins–Monro step sizes, and \(\gamma<1\), tabular Q-learning converges to \(Q^*\) with probability 1. (Watkins & Dayan, 1992; refined analyses since.)

On-policy vs off-policy — the fork that lasts

SARSA / on-policy actor–critic / TRPO / PPO: learn about the policy that generated the data; discard or carefully correct old data.
Q-learning / DQN / DDPG / TD3 / SAC: learn about a different (often greedy or entropy-regularized optimal) policy while behaving otherwise; replay becomes natural.

8. Exploration

DP never explores — it backs up every \((s,a)\) from the model. TD only sees what it tries. If the behavior policy is purely greedy from a bad initialization, it may never discover better actions.

Definition 8.1 · ε-greedy

With probability \(\varepsilon\), draw \(a\) uniformly from \(A\); otherwise \(a\in\arg\max_{a'}Q(s,a')\). Guarantees every action is tried infinitely often if \(\varepsilon>0\) is held fixed (or decays sufficiently slowly).

Cruder than necessary, but the right conceptual placeholder. Softmax / Boltzmann exploration, UCB-style bonuses, parameter noise, and SAC’s entropy term are later refinements of the same tension: exploit what \(Q\) knows vs visit what it does not.

9. n-step returns and TD(\(\lambda\))

The n-step target bootstraps after \(n\) real rewards:

(6) \[ G_t^{(n)} = r_{t+1} + \gamma r_{t+2} + \cdots + \gamma^{n-1} r_{t+n} + \gamma^n V(s_{t+n}). \]

\(n=1\) recovers TD(0); \(n\to\infty\) recovers Monte Carlo (episodic). TD(\(\lambda\)) averages all \(n\) with weights proportional to \(\lambda^{n-1}\), implemented efficiently with eligibility traces rather than storing every n-step return. Remember the dial exists; PPO-era methods use a related tool (GAE) on advantages instead of values.

10. The deadly triad

Tabular TD is safe. Trouble starts when you combine:

  1. Function approximation (neural nets — not an independent entry per state),
  2. Bootstrapping (TD targets),
  3. Off-policy sampling (replay / behavior ≠ target).

Together these are the deadly triad: convergence guarantees disappear; training can diverge. That is why DQN needed target networks and replay design care, and part of why trust-region / clipped policy gradients (TRPO/PPO) were developed as stabler alternatives to naive deep bootstrapped updates. Flag now; it will click when we reach deep RL.

11. Laboratory · SARSA vs Q-learning

Classic-style cliff (4×8): start bottom-left, goal bottom-right, cliff cells along the bottom row between them; safe plateau on the three rows above. Stepping into the cliff yields \(-100\) and a reset to start. Every other transition costs \(-1\) (including entering the goal — Sutton & Barto convention). Compare learned greedy paths under SARSA vs Q-learning with \(\varepsilon=0.1\) and decaying \(\alpha\).

Control algorithms

Sample backups · ε-greedy
Algorithm

Active: SARSA (on-policy)

Hyperparameters
Run

Grid · greedy arrows from Q

Numbers = maxa Q(s,a). Cliff = red. Goal = G.

Episodes0
Last return
Avg last 20
Last δ

Update ledger

Run episodes to see SARSA / Q-learning updates with explicit δ.

12. Worked update

Suppose \(Q(s,a)=2.0\), \(\alpha=0.5\), \(\gamma=0.9\), reward \(r=−1\), and next estimates \(Q(s',a_{\mathrm{taken}})=3.0\), \(\max_{a'}Q(s',a')=4.0\).

(7) \[ \begin{aligned} \delta^{\mathrm{SARSA}} &= -1 + 0.9\cdot 3 - 2 = -0.3,\\ Q &\leftarrow 2 + 0.5(-0.3) = 1.85.\\[6pt] \delta^{\mathrm{QL}} &= -1 + 0.9\cdot 4 - 2 = 0.6,\\ Q &\leftarrow 2 + 0.5(0.6) = 2.3. \end{aligned} \]

Same transition, different targets — SARSA pulls toward the action you took; Q-learning pulls toward the best action in the table. That is the entire on-/off-policy distinction in one numerical example.

  1. Write δ = target − Q(s,a).
  2. SARSA target = r + γ Q(s′, a′); QL target = r + γ max Q(s′,·).
  3. Plug in numbers → δ_S = −0.3, δ_Q = +0.6.
  4. Apply Q ← Q + α δ.
  5. Interpret: exploratory a′ can make SARSA more conservative near hazards.

13. What follows

Next: a dedicated deep dive into Q-learning and Deep Q-Networks — convergence, maximisation bias, experience replay, target networks, Double DQN, and Rainbow. Policy gradients follow after that.

See also

  • Sutton & Barto, Ch. 6 — TD learning.
  • Chapter 03 — the full backups TD approximates.
  • Chapter 02 — \(\mathcal{T}^{\pi}\) vs \(\mathcal{T}^*\).

Previous

← Dynamic Programming

Next chapter

Q-learning & Deep Q-Networks

Off-policy control in depth — then DQN, Double DQN, Rainbow.

Continue →