Foundations to Frontier
Tests

Temporal-difference learning

Temporal-difference (TD) learning updates a value estimate after every step, towards the reward plus the current estimate of the next state. It needs no model, like Monte Carlo, and it bootstraps, like dynamic programming. This page implements TD(0), constant-α Monte Carlo and n-step TD on random walks, compares their learning curves and their batch solutions, and finds the best number of steps to look ahead.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Updating a guess with a better guess

Driving home, you predict 30 minutes. Ten minutes in, you hit traffic and now expect 35 minutes in total. You do not need to wait until you are home to learn that your first prediction was too low: the new prediction already tells you. Temporal-difference learning works this way. After every step it compares its old guess with the reward just received plus its new guess, and moves the old guess a little towards it.

Reinforcement learning lab
Random walk on 5 states, start in the middle, +1 for leaving on the right (true values 1/6 … 5/6), all values start at 0.5. Solid: TD(0) with α = 0.1; dashed: constant-α Monte Carlo with α = 0.02; RMS error averaged over 30 runs. After 100 episodes: TD 0.054, Monte Carlo 0.081; averaged over all 100 episodes: TD 0.069, Monte Carlo 0.128. A large α learns fast but then jitters around the true values.
Why engineers care

TD learning taught a computer to play backgammon at world-class level in the early 1990s, and a TD-like error signal matches how dopamine neurons respond to reward.

📝 Quick check (Level 0)
0/3 answered
  1. 1. TD learning updates its prediction…
  2. 2. The new target uses…
  3. 3. TD learning needs a model of the environment?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • TD(0): after the step s → s’ with reward r, V(s) ← V(s) + α[r + γV(s’) − V(s)].
  • TD error: δ = r + γV(s’) − V(s), the surprise of one step. Terminal states have value 0.
  • Bootstrapping: the target uses the current estimate V(s’), not the full return. Dynamic programming bootstraps too; Monte Carlo does not.
  • Constant-α Monte Carlo: V(s) ← V(s) + α[G − V(s)], updated at the end of the episode.
  • n-step TD: target r_(t+1) + … + γ^(n−1)r_(t+n) + γ^nV(s_(t+n)). n = 1 is TD(0); n = ∞ is Monte Carlo.
  • Batch updating: replay a fixed set of episodes until the values converge. Batch Monte Carlo fits the observed returns; batch TD(0) finds the values of the maximum-likelihood Markov chain (certainty equivalence).
📝 Quick check (Level 1)
0/3 answered
  1. 1. The TD target for V(s) is…
  2. 2. n-step TD with n = 1 is…
  3. 3. Batch TD(0) converges to…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

TD(0) against Monte Carlo

Exercise 1.

  • The classic five-state random walk: start in the middle, step left or right with probability 1/2, +1 for leaving on the right, 0 otherwise, no discounting. True values 1/6, 2/6, …, 5/6.
  • TD(0) and constant-α Monte Carlo, both with α = 0.05, values starting at 0.5.
  • The RMS error of each over 200 episodes.

Practice.

  • Prefer TD when episodes are long or never end: it learns during the episode and its targets have far lower variance.
  • Tune α: a large step learns fast and then jitters; a small one converges slowly. Decay it if you need exact values.
  • Try a few steps of lookahead: intermediate n usually beats both TD(0) and Monte Carlo.
  • Watch for bootstrapping bias when the values of next states are badly wrong, for example early in training or with function approximation.
  • Replay data: repeated passes over stored transitions (batch TD) use every sample many times; experience replay in deep RL builds on this.
  • Tools: NumPy for tabular TD; the same update drives Q-learning, SARSA and the critic of actor-critic methods.
def td0(V, states, rewards, alpha, gamma=1.0):
    for t, s in enumerate(states):
        nxt = V[states[t + 1]] if t + 1 < len(states) else 0.0
        V[s] += alpha * (rewards[t] + gamma * nxt - V[s])
    return V
🐍 Exercise 1: TD(0) on the random walk
Implement the random walk, TD(0) and constant-α Monte Carlo, and compare their RMS errors against the true values.
📝 Quick check (Level 2)
0/3 answered
  1. 1. For long or continuing tasks, prefer…
  2. 2. A large step size…
  3. 3. Bootstrapping can hurt when…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Convergence. For a fixed policy in a finite MDP, tabular TD(0) converges to V^π with probability 1 if every state is visited infinitely often and the step sizes satisfy Σα = ∞ and Σα² < ∞ (Sutton 1988; Dayan 1992; Jaakkola, Jordan and Singh 1994). With a small constant α it converges in mean.

Bias and variance. The Monte Carlo target G is unbiased but sums the randomness of the whole episode. The TD target r + γV(s’) depends on one step of randomness, so its variance is lower, but it is biased while V is wrong. n-step targets interpolate between the two.

Monte Carlo error as a sum of TD errors. For a fixed V, G_t − V(s_t) = Σ_k γ^k δ_(t+k). Monte Carlo makes all these corrections at once, at the end; TD makes each one as soon as it is observed.

Batch solutions. Batch Monte Carlo minimises the squared error to the observed returns. Batch TD(0) gives the exact values of the Markov chain with the empirical transition probabilities and rewards. When the process really is Markov, the latter uses the data better.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The TD target has lower variance because…
  2. 2. For fixed V, the Monte Carlo error equals…
  3. 3. Tabular TD(0) converges if step sizes satisfy…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do TD(0) and Monte Carlo compare on learning speed and in batch mode, and how many steps of lookahead work best?

History.

  • A. Samuel (1959) used a TD-like update in his checkers program.
  • R. S. Sutton (1988) introduced TD(λ) and proved convergence in the mean for linear prediction.
  • G. Tesauro (1992–1995) trained TD-Gammon by self-play with TD(λ).
  • W. Schultz, P. Dayan and P. R. Montague (1997) linked dopamine responses to the TD error.

Exercise 2.

  • Learning curves on the five-state walk: TD(0) with α = 0.05, 0.1, 0.15 and constant-α Monte Carlo with α = 0.01, 0.02, 0.04, 100 episodes, 100 runs.
  • Batch updating on the five-state walk: batch Monte Carlo against batch TD(0) (certainty equivalence) after 10, 30 and 100 episodes, 50 runs.
  • n-step TD on a 19-state walk (same rules), n = 1, 2, 4, 8, 16, α from 0.1 to 1.0; RMS error averaged over the first 10 episodes and 50 runs, at the best α for each n.

Seed 0:

method, α RMS error, mean over 100 episodes after 100 episodes
TD(0), 0.05 0.0807 0.0378
TD(0), 0.1 0.0696 0.0557
TD(0), 0.15 0.0760 0.0709
Monte Carlo, 0.01 0.1491 0.0971
Monte Carlo, 0.02 0.1266 0.0830
Monte Carlo, 0.04 0.1295 0.1015
episodes batch Monte Carlo batch TD(0)
10 0.1759 0.1178
30 0.1069 0.0745
100 0.0534 0.0376
n best α RMS error, first 10 episodes
1 0.8 0.1703
2 0.6 0.1395
4 0.4 0.1309
8 0.2 0.1352
16 0.1 0.1626
  • TD(0) learned faster at every step size tried: its best mean error (0.0696) was little more than half Monte Carlo’s best (0.1266).
  • Step size trades speed for precision: the smallest TD step had the largest mean error but the smallest final error (0.0378).
  • Batch TD(0) was about 30% more accurate than batch Monte Carlo with the same data (0.0376 against 0.0534 at 100 episodes), because the walk really is a Markov chain.
  • Intermediate lookahead was best: n = 4 beat both n = 1 and n = 16, and the best α fell as n grew, since longer targets are noisier.
  • Seeds 1 and 2: best TD mean error 0.0687 and 0.0682 against best Monte Carlo 0.1248 and 0.1261; batch at 100 episodes 0.0585 / 0.0367 and 0.0619 / 0.0410 (MC / TD); n = 4 was best again, at 0.1340 and 0.1274.
🐍 Exercise 2 (research): how do TD(0) and Monte Carlo compare on learning speed and in batch mode, and how many steps of lookahead work best?
Compare TD(0) and Monte Carlo learning curves for several step sizes, their batch solutions, and n-step TD for several n on random walks.

TD learning in practice

  • Control: applying TD to action values gives SARSA and Q-learning (next topic).
  • Eligibility traces, TD(λ), average all n-step targets with weights (1 − λ)λ^(n−1) and can be computed online with one trace per state.
  • Deep RL: DQN, actor-critic methods and most value-based algorithms minimise squared TD errors with neural networks.
  • Neuroscience: phasic dopamine activity resembles a reward-prediction (TD) error.

References

  1. R. S. Sutton (1988). “Learning to predict by the methods of temporal differences.” Machine Learning.
  2. R. S. Sutton and A. G. Barto (2018). Reinforcement Learning: An Introduction, 2nd ed., chapters 6 and 7. MIT Press.
  3. P. Dayan (1992). “The convergence of TD(λ) for general λ.” Machine Learning.
  4. G. Tesauro (1995). “Temporal difference learning and TD-Gammon.” Communications of the ACM.
  5. W. Schultz, P. Dayan and P. R. Montague (1997). “A neural substrate of prediction and reward.” Science.