ML Systems Lab Open interactive version →
Intermediate 50 min read td learningsarsaq-learningeligibility tracesdeadly triad

Temporal Difference Learning

TD(0), TD(λ), SARSA vs Q-learning, deadly triad, divergence with FA

Consider a stock trading system. After each trade you receive a reward — profit or loss. But the final profit of a multi-leg strategy is not known until all positions close, potentially hours later. You cannot wait for the episode to end before updating your value estimates. You need to learn from partial information, updating as you go. Temporal difference learning does exactly this: update V(s_t) based on the observed reward R_{t+1} and the current estimate V(s_{t+1}), without waiting for the final return.

The TD(0) update is V(s_t) ← V(s_t) + α [R_{t+1} + γ V(s_{t+1}) - V(s_t)]. The term in brackets is the TD error δ_t — the difference between what you predicted and what the next step says you should have predicted. V(s_{t+1}) is a bootstrapped estimate: you are using one estimate to update another. This is the fundamental difference from Monte Carlo, which waits for the full return G_t = R_{t+1} + γR_{t+2} + ... before updating.

The tradeoff is bias versus variance. Monte Carlo is unbiased because it uses actual future rewards, but it has high variance because the full trajectory includes noise from every subsequent step. TD is biased because V(s_{t+1}) is an approximation, but it has lower variance because only one step of noise is introduced per update. TD can update after every step — online learning. Monte Carlo requires complete episodes. For long-horizon tasks where episode lengths are in the hundreds or thousands, Monte Carlo gradient variance is too high to train reliably — TD is not just faster, it is the only practical option.

TD(λ) interpolates between the two. λ = 0 is pure one-step TD. λ = 1 is Monte Carlo. Values in between accumulate a geometric average of n-step returns via eligibility traces — each state's update is weighted by how recently and frequently it was visited. λ around 0.7–0.9 typically outperforms both extremes.

NOT this: TD is just a faster version of Monte Carlo. The bias-variance distinction is not an implementation detail. In long-horizon tasks — game episodes of 1000+ steps, multi-day trading strategies — the variance of a full Monte Carlo return is enormous, and the gradient signal becomes noise. TD's bias from an imperfect V estimate is a feature, not a bug: it gives you a low-variance signal every step.

Key points

Takeaway

TD learning updates value estimates after every step using a bootstrapped target — trading some bias for dramatically lower variance than Monte Carlo, enabling online learning in long-horizon tasks where waiting for full episode returns is impractical.

Recap

Check your understanding

Q1. SARSA and Q-learning have identical updates except for one term. Which two of the following statements about that difference are correct?

Q2. Explain Baird's counterexample intuitively. Why does Q-learning with linear function approximation diverge even in a simple MDP?

Q3. You are training a Q-learning agent on a game environment and observe that the Q-values grow from ~10 to ~10^6 over 500k steps, with training reward staying flat. Diagnose and fix.

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →