Temporal Difference Learning
Learn from a single step instead of waiting for the episode to finish. TD nudges your estimate of a state's value toward the reward you just saw plus your current guess about where you landed - learning from a guess, using a guess.
Prerequisites: The Bellman Equations, Markov Decision Processes
To learn what a state is worth, the obvious approach is to visit it, play the episode out, and average the totals. That is Monte Carlo estimation: unbiased, but painfully slow, and for a trading problem often impossible — if an "episode" is a full trading day, you get 252 lessons a year. Temporal difference learning refuses to wait.
The idea: revise as you go
You leave the office estimating 45 minutes to get home. Ten minutes in you hit traffic on a road that is normally clear. You do not suspend judgement until you park — you revise immediately, because the traffic itself is evidence.
That revision is the whole method. Your estimate of the trip changed because your estimate of the rest of the trip changed: you learned from a guess, before the truth arrived. TD does this arithmetically, treating the reward just received plus your current opinion of where you landed as a better estimate than your current opinion of where you left.
The update
For a state with current estimate , observing reward and next state :
In words: form a target out of what you actually got plus a discounted guess about what comes next, see how far your old estimate was from it, and move a small fraction of the way there. The bracketed quantity is the TD error, written . It is the surprise — positive means things went better than the current value function predicted.
Notice the target is just the right-hand side of the Bellman equation with the expectation replaced by a single sample. That is the entire relationship between the two ideas.
TD bootstraps: it updates a guess toward another guess. That is what lets it learn from one step instead of one episode, and it is also the source of every problem TD has — bias, instability with function approximation, and a hard dependence on the next state's estimate being roughly sane.
Worked example: two updates in one episode
Take and . Current estimates are , , .
Step one. From you receive a reward of and land in . The target is , so the TD error is and
Step two, immediately after. From you receive and land in . The target is , the error is , and
Two states improved before the episode was anywhere near over. A Monte Carlo learner would still be waiting, holding no information at all.
Worked example: why the variance is lower
State always leads to with zero reward; from the episode ends with or , equally likely. Take , so the true value of is . Start with and suppose is already learned.
Monte Carlo, using the realised episode return:
- Episode 1 returns : .
- Episode 2 returns : .
TD, using the target both times:
- Episode 1: .
- Episode 2: .
Both head toward , but look at the path. The Monte Carlo updates carry the full episode noise — a standard deviation of , so each one jerks by about in a random direction. TD's updates carry none of it: the coin flip lands entirely on , where it belongs. TD trades a little bias, from bootstrapping off an imperfect , for a large cut in variance. On noisy financial data that trade is almost always worth making.
Running averages settle on the truth at roughly one over the square root of the sample count, and TD estimates behave the same way. Drag the sample count below and note how much of the early wobble is pure sampling noise rather than signal.
In practice
The step size is the main dial. Large values track a changing world quickly but leave the estimate rattling; small values are stable but slow to notice a regime change. For a stationary problem should decay; for markets a small constant is usually preferred, precisely because it lets the estimate forget.
TD is also the engine under the algorithms you will actually deploy. Q-learning is TD on action values with a max in the target; SARSA is the same with the action you actually took; actor-critic methods use the TD error directly as the signal telling the policy whether a move beat expectations.
Bootstrapping is not free. Because the target contains your own estimate, an error in one state propagates into its neighbours, and with a function approximator — where fitting one state changes every state — TD can diverge outright rather than merely converging slowly. This is the well-known "deadly triad" of bootstrapping, function approximation and off-policy learning. Two out of the three is usually fine; all three needs care.
Log the mean TD error during training. It should trend toward zero. If it plateaus at a positive value, the value function is systematically under-predicting and the problem is usually a discount factor too small for the horizon you actually care about.
Related concepts
Practice in interviews
Further reading
- Sutton & Barto, Reinforcement Learning: An Introduction (ch. 6)
- Sutton, Learning to Predict by the Methods of Temporal Differences (1988)
- Szepesvári, Algorithms for Reinforcement Learning (ch. 2)