Temporal-Difference Learning #
Temporal-Difference (TD) learning updates predictions from one transition at a time. It learns directly from experience like Monte Carlo methods, but it does not need to wait until the episode ends.
TD learning updates an estimate using an immediate reward and another current estimate.1. From Monte Carlo to Temporal Difference ☆ #
Monte Carlo learning uses the complete observed return:
\[ \text{MC target}=G_t \]TD learning uses a one-step target:
\[ \text{TD target} = R_{t+1}+\gamma V(S_{t+1}) \]The next-state value is an estimate rather than a complete return. Updating one estimate from another estimate is called bootstrapping.
| Method | Learns from experience? | Needs complete episode? | Bootstraps? |
|---|---|---|---|
| Dynamic Programming | No sampled experience required | No | Yes |
| Monte Carlo | Yes | Yes | No |
| Temporal Difference | Yes | No | Yes |
TD combines a useful property of each earlier approach: it learns from sampled experience like Monte Carlo, while updating from an estimated successor value like Dynamic Programming.
2. The General Update Pattern ☆ #
Many reinforcement learning algorithms follow:
\[ \text{New estimate} = \text{Old estimate} + \alpha\left[ \text{Target}-\text{Old estimate} \right] \]The quantity in brackets is the prediction error. A positive error raises the estimate; a negative error lowers it.
- \( \alpha \) controls how strongly new information changes the estimate.
- \( \gamma \) controls the importance of future rewards.
3. TD Error ☆ #
For state-value prediction, the TD error is:
\[ \delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t) \]It compares what was predicted for the current state with a revised one-step prediction after observing the reward and next state.
The TD error is the surprise in the transition: reward plus discounted next value minus current value.
4. TD(0) Prediction ☆ #
TD(0) evaluates a policy by updating the current state’s value after every transition:
\[ V(S_t) \leftarrow V(S_t) + \alpha \left[ R_{t+1}+\gamma V(S_{t+1})-V(S_t) \right] \]It is called TD(0) because it uses a one-step target rather than waiting for additional sampled rewards.
Worked Example #
Suppose:
- \( V(S_t)=5 \) ;
- \( R_{t+1}=2 \) ;
- \( V(S_{t+1})=6 \) ;
- \( \gamma=0.9 \) ;
- \( \alpha=0.1 \) .
The TD target is:
\[ 2+0.9(6)=7.4 \]The TD error is:
\[ \delta_t=7.4-5=2.4 \]The updated value is:
\[ V(S_t) \leftarrow 5+0.1(2.4) = 5.24 \]5. Prediction versus Control #
TD(0) predicts state values under a given policy. To improve behaviour, the agent usually learns action values \( Q(s,a) \) .
The main one-step TD control methods differ in how they choose the next value used in the target:
- SARSA uses the action actually selected next.
- Expected SARSA averages over actions under the policy.
- Q-learning uses the highest next-action value.
6. SARSA: On-Policy TD Control ☆ #
SARSA is named after the five items in a transition:
\[ S_t,\ A_t,\ R_{t+1},\ S_{t+1},\ A_{t+1} \]Its update is:
\[ Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha \left[ R_{t+1} + \gamma Q(S_{t+1},A_{t+1}) - Q(S_t,A_t) \right] \]The next action \( A_{t+1} \) is selected by the same policy used to generate behaviour. SARSA is therefore on-policy.
Simplified SARSA Algorithm #
Initialise Q(s,a)
Choose an action A using an epsilon-greedy policy
For each transition:
take action A
observe reward R and next state S'
choose next action A' using the same policy
update Q(S,A) using Q(S',A')
set S = S' and A = A'
Because SARSA learns about its exploratory behaviour, the possibility of taking a non-greedy action is reflected in its values.
7. Expected SARSA ☆ #
Expected SARSA replaces the sampled next action with the expected action value under the policy:
\[ Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha \left[ R_{t+1} + \gamma \sum_a \pi(a\mid S_{t+1})Q(S_{t+1},a) - Q(S_t,A_t) \right] \]Instead of depending on one randomly selected next action, it averages over all possible next actions using their policy probabilities. This generally reduces variance.
8. Q-Learning: Off-Policy TD Control ☆ #
Q-learning uses the maximum estimated value in the next state:
\[ Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha \left[ R_{t+1} + \gamma\max_a Q(S_{t+1},a) - Q(S_t,A_t) \right] \]The behaviour policy may remain \( \varepsilon \) -greedy, but the target assumes the greedy next action. Q-learning is therefore off-policy.
Simplified Q-Learning Algorithm #
Initialise Q(s,a)
For each transition:
choose A from S using an epsilon-greedy behaviour policy
take A and observe R and S'
update Q(S,A) using the maximum Q-value in S'
set S = S'
Q-learning is the tabular foundation of Deep Q-Learning, where a neural network replaces the Q-table.
9. One Transition, Three Different Targets ☆ #
Suppose the next state has three actions:
| Action | Q-value | Policy probability |
|---|---|---|
| Left | 4 | 0.1 |
| Right | 8 | 0.8 |
| Wait | 2 | 0.1 |
Let \( R_{t+1}=1 \) and \( \gamma=0.9 \) .
SARSA #
If the policy actually selects Left:
\[ \text{Target} = 1+0.9(4) = 4.6 \]Q-Learning #
The maximum next-action value is \( 8 \) :
\[ \text{Target} = 1+0.9(8) = 8.2 \]Expected SARSA #
The expected next value is:
\[ 0.1(4)+0.8(8)+0.1(2)=7 \]Therefore:
\[ \text{Target} = 1+0.9(7) = 7.3 \]The algorithms observe the same transition but learn from different assumptions about what happens next.
10. SARSA versus Q-Learning versus Expected SARSA ☆ #
| Method | Next value in target | Policy relationship | Main interpretation |
|---|---|---|---|
| SARSA | \( Q(S_{t+1},A_{t+1}) \) | On-policy | Learn from the next action actually selected |
| Expected SARSA | \( \sum_a\pi(a\mid S_{t+1})Q(S_{t+1},a) \) | Usually on-policy | Average over actions the policy may select |
| Q-learning | \( \max_aQ(S_{t+1},a) \) | Off-policy | Learn about the greedy target policy |
Memory Line #
- SARSA: what action will I actually take?
- Expected SARSA: what is the average value under my policy?
- Q-learning: what is the best action I could take?
11. Terminal States #
If \( S_{t+1} \) is terminal, it has no future return. Its successor value is treated as zero.
The target becomes:
\[ \text{Target}=R_{t+1} \]This applies to TD(0), SARSA, Expected SARSA and Q-learning.
12. Advantages and Limitations of TD Learning #
Advantages #
- learns without a transition model;
- updates after each transition;
- can learn during continuing tasks;
- usually learns earlier than methods that wait for complete returns;
- forms the basis of many deep reinforcement learning algorithms.
Limitations #
- bootstrapping can introduce bias;
- learning depends on step-size and exploration choices;
- correlated experience can destabilise learning with nonlinear function approximation;
- off-policy learning with function approximation requires particular care.
Common Mistakes ☆ #
- TD(0) does not wait for the complete return; it uses the next state’s current estimate.
- SARSA is on-policy because its target uses the next action selected by the behaviour policy.
- Q-learning may behave epsilon-greedily while learning about a greedy target policy.
- The max in Q-learning is taken over next-state actions, not over possible rewards.
- Expected SARSA uses a probability-weighted expectation, not a simple unweighted average.
Practice Questions #
- What does bootstrapping mean in TD learning?
- Calculate a TD(0) update for a given reward and pair of state values.
- Why is SARSA classified as on-policy?
- Why is Q-learning classified as off-policy?
- Compare the targets used by SARSA, Expected SARSA and Q-learning.
- What happens to the TD target when the next state is terminal?
- Explain how TD learning combines ideas from Dynamic Programming and Monte Carlo.
Key Takeaways ☆ #
- TD learning updates from the immediate reward and an estimated successor value.
- TD(0) predicts state values one transition at a time.
- SARSA uses the next action actually selected and is on-policy.
- Expected SARSA averages next-action values under the policy.
- Q-learning uses the maximum next-action value and is off-policy.
- Q-learning provides the tabular foundation for Deep Q-Networks.
Checklist #
- I can explain the TD target and TD error.
- I can perform a TD(0) value update.
- I can write the SARSA and Q-learning updates.
- I can distinguish on-policy from off-policy TD control.
- I can calculate an Expected SARSA target.
- I can handle terminal states correctly.
References #
- Sutton and Barto, Reinforcement Learning: An Introduction, Chapters 6 and 7.
- Supplied material on TD(0), SARSA, Expected SARSA and Q-learning.