Temporal-difference learning
Learn after every step by bootstrapping: TD(0), the TD error, and how it compares with Monte Carlo.
Animated explanation
The idea
Suppose you predict your commute will take 30 minutes. Five minutes in, you hit a traffic jam and now expect 40 in total. You don't need to wait until you arrive to realise your original estimate was too low: you can update it now, using your new prediction.
That is temporal-difference learning. After every step, move the value of the state you just left towards the reward you got plus the (discounted) value of the state you landed in. The difference between the two is the TD error . TD combines Monte Carlo's model-free learning from experience with dynamic programming's bootstrapping (learning a guess from a guess). It works online, in continuing tasks, and usually converges faster than Monte Carlo.
Bias and variance of the targets
The MC target is an unbiased sample of but sums many random rewards (high variance). The TD target depends on one random step plus the current estimate: lower variance but biased while is wrong. With a suitable step size, TD(0) still converges to for tabular problems, and in practice it is often much more data-efficient.
The step size α
A constant α lets estimates track changing environments but leaves some noise; decaying α (e.g. ) gives convergence in stationary problems. Typical constant values are 0.01–0.5. Too large and estimates jitter; too small and learning crawls.
The algorithm, step by step
- 1Initialise V(s) arbitrarily (terminal states 0).
- 2For each episode, start in ; for each step: take an action from π, observe and .
- 3Compute and update .
- 4Move to ; continue until the episode ends (or forever, for continuing tasks).
The maths
TD(0) update
TD error
MC error as a sum of TD errors (V fixed)
Strengths and weaknesses
Strengths
- Learns online after every step
- Works in continuing (non-episodic) tasks
- Lower variance than Monte Carlo; often faster
Weaknesses
- Biased targets while estimates are wrong
- Sensitive to the step size
- Can be unstable with function approximation and off-policy data
Reach for it when…
Check your understanding
Lambdian Lab
TD(0) versus Monte Carlo on a random walk
The random walk is provided (true values TRUE_V = 1/6 … 5/6, γ = 1).
- td0(episodes, alpha, seed): TD(0) prediction starting from V = 0.5 everywhere; return (V, rms_history) with the RMS error after every episode.
- mc(episodes, alpha, seed): constant-α Monte Carlo (every-visit) with the same interface.