Quant Memo
Core

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 StS_t with current estimate V(St)V(S_t), observing reward Rt+1R_{t+1} and next state St+1S_{t+1}:

V(St)V(St)+α[Rt+1+γV(St+1)targetV(St)].V(S_t) \leftarrow V(S_t) + \alpha \Big[ \underbrace{R_{t+1} + \gamma V(S_{t+1})}_{\text{target}} - V(S_t) \Big].

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 α\alpha of the way there. The bracketed quantity is the TD error, written δt\delta_t. 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.

5.00 old new 5.14 6.40 target TD error = 1.40 move alpha of the way: 0.14 estimate of the state you just left
One TD update. The target sits well to the right of the current estimate, but you only take a tenth of the gap - so noise in any single step cannot move the estimate far.

Worked example: two updates in one episode

Take α=0.1\alpha = 0.1 and γ=0.9\gamma = 0.9. Current estimates are V(A)=5.00V(A) = 5.00, V(B)=6.00V(B) = 6.00, V(C)=4.00V(C) = 4.00.

Step one. From AA you receive a reward of 11 and land in BB. The target is 1+0.9(6.00)=6.401 + 0.9(6.00) = 6.40, so the TD error is 6.405.00=1.406.40 - 5.00 = 1.40 and

V(A)5.00+0.1(1.40)=5.14.V(A) \leftarrow 5.00 + 0.1(1.40) = 5.14 .

Step two, immediately after. From BB you receive 22 and land in CC. The target is 2+0.9(4.00)=5.602 + 0.9(4.00) = 5.60, the error is 5.606.00=0.405.60 - 6.00 = -0.40, and

V(B)6.00+0.1(0.40)=5.96.V(B) \leftarrow 6.00 + 0.1(-0.40) = 5.96 .

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 AA always leads to BB with zero reward; from BB the episode ends with +10+10 or 4-4, equally likely. Take γ=1\gamma = 1, so the true value of AA is 33. Start with V(A)=5V(A) = 5 and suppose V(B)=3V(B) = 3 is already learned.

Monte Carlo, using the realised episode return:

  • Episode 1 returns +10+10:   V(A)=5+0.1(105)=5.50\;V(A) = 5 + 0.1(10 - 5) = 5.50.
  • Episode 2 returns 4-4:   V(A)=5.50+0.1(45.50)=4.55\;V(A) = 5.50 + 0.1(-4 - 5.50) = 4.55.

TD, using the target 0+V(B)=30 + V(B) = 3 both times:

  • Episode 1:   V(A)=5+0.1(35)=4.80\;V(A) = 5 + 0.1(3 - 5) = 4.80.
  • Episode 2:   V(A)=4.80+0.1(34.80)=4.62\;V(A) = 4.80 + 0.1(3 - 4.80) = 4.62.

Both head toward 33, but look at the path. The Monte Carlo updates carry the full episode noise — a standard deviation of 77, so each one jerks AA by about 0.70.7 in a random direction. TD's updates carry none of it: the coin flip lands entirely on BB, where it belongs. TD trades a little bias, from bootstrapping off an imperfect V(B)V(B), 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.

Convergence explorer
true meansamples →
after n = 200estimate -0.025error 0.025std error 0.071

In practice

The step size α\alpha 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 α\alpha should decay; for markets a small constant α\alpha 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)
ShareTwitterLinkedIn