Skip to content
Lambdian

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 δ\delta. 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 GtG_t is an unbiased sample of Vπ(st)V^\pi(s_t) but sums many random rewards (high variance). The TD target r+γV(s′)r + \gamma V(s') depends on one random step plus the current estimate: lower variance but biased while VV is wrong. With a suitable step size, TD(0) still converges to VπV^\pi 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. 1/N(s)1/N(s)) 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

  1. 1
    Initialise V(s) arbitrarily (terminal states 0).
  2. 2
    For each episode, start in ss; for each step: take an action from π, observe rr and s′s'.
  3. 3
    Compute δ=r+γV(s′)−V(s)\delta = r + \gamma V(s') - V(s) and update V(s)←V(s)+αδV(s) \leftarrow V(s) + \alpha\delta.
  4. 4
    Move to s′s'; continue until the episode ends (or forever, for continuing tasks).

The maths

TD(0) update

V(s)←V(s)+α[r+γV(s′)−V(s)]V(s) \leftarrow V(s) + \alpha\big[r + \gamma V(s') - V(s)\big]

TD error

δt=rt+1+γV(st+1)−V(st)\delta_t = r_{t+1} + \gamma V(s_{t+1}) - V(s_t)

MC error as a sum of TD errors (V fixed)

Gt−V(st)=∑k=tT−1γk−tδkG_t - V(s_t) = \sum_{k=t}^{T-1}\gamma^{k-t}\delta_k

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…

Value prediction from streaming experience, and as the core of SARSA, Q-learning and actor-critic.

Check your understanding

Question 1

Q1The TD(0) target for V(s) is…

Question 2

Q2V(s) = 0.4, reward 0, γ = 1, V(s′) = 0.6, α = 0.5. The new V(s) is…

Question 3

Q3Compared with Monte Carlo, TD targets have…

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.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.