Chapter 7 Temporal-Difference learning
Chapter 7 Temporal-Difference learning
In this section we will first introduce TD learning, which refers a wide range of algorithms. It can solve Bellman equation of a given policy without model. We refer TD learning in the first chapter specifically as a classic algorithm for estimating state values. Then we will introduce other algorithms belonging to the wide range of TD learning in the next section.
Basic TD learning algorithms
Recall the RM algorithm we learn in Chapter 6:
where is the sample of our optimizing objective obtained through the model (e.g., expectation in mean estimation or the gradient expectation in the gradient descent).
In TD learning, our objective is estimating the state value. Recall that in the model-based methods, we derive state values through solving the Bellman equation (BE). But here we do not have models (TD learning is model free), so in essence, it is a special stochastic approximation algorithm for solving the BE. Recall the definition of the state value is
We can write it into another form by definition:
Conditioning additionally on the action gives . The sampled quantity inside this expectation is a one-step target, not the exact action value itself. Averaging action values over the policy gives:
which we will see it in the Chapter 10. From this persepctive, we can also clearly understand the relationship between action value and state value.
Our objective is to find a value vector whose Bellman residual is zero:
A sample estimate of this residual is . Substituting it into RM gives the TD update. We use the convention that the TD error is target minus current estimate, consistently with Chapter 10:
Here, is the TD target. The TD error measures how much this target exceeds the current estimate. It provides an innovation from the experience sample . A truly terminal successor has value zero; equivalently, multiply the bootstrap term by , where for true termination. This terminal convention also applies to the action-value targets below. A sampling time limit in a continuing task can still require bootstrapping from the actual final observation.
TD can update after one transition, while ordinary full-return MC waits until the episode ends to obtain its target. Both methods can maintain incremental value estimates; the distinction is whether the target requires a complete return.
Tabular TD policy evaluation converges under appropriate assumptions, including a fixed policy, sufficient state visits, and suitable step sizes. Stochastic approximation provides tools for this analysis; it does not guarantee convergence for every function approximator or off-policy setting.
Other TD learning algorithms
The TD algorithm can only estimate the state values of a given policy. To find optimal policies, we still need to further calculate the action values and then conduct policy improvement. In this section, we introduce the TD algorithms that can directly estimate action values.
These action-value methods can also be viewed through stochastic approximation: choose an appropriate expected backup and replace it with a sampled target. Here we introduce three algorithms: Sarsa, n-step Sarsa, and Q-learning.
Sarsa
For Sarsa, We can replace state value estimation with action value estimation. The expression of the action value can be written as:
This can be understood as follows: q value equals the immediate reward plus the future reward for reaching the next state. Since the next state is unknown, its state value can be obtained by considering all action values in the next step after that. Therefore, we can view it as first fixing the next state , then calculating the expectation of all actions in that state to obtain the state value of . Finally, we calculate the expectation of to obtain the future reward for state .
So the expression of Sarsa can be written as:
For Sarsa, we the experience samples .

n-step Sarsa
This section introduces n-step Sarsa, an extension of Sarsa. We will see that Sarsa and MC learning are two extreme cases of n-step Sarsa.Recall that the definition of the action value is
where is the discounted return satisfying
We can construct targets that use different numbers of actual rewards before bootstrapping. The expressions below use the true to show their expectation relationship; an implementation replaces it with a current estimate:
These targets are generally different random variables, not equal sample by sample. Under the same fixed policy, with true in the bootstrap and correct terminal handling, their conditional expectations agree:
For an episode ending at , sum rewards only through and omit the bootstrap. If the bootstrap uses an approximate action value, its error may introduce bias relative to . The sampled targets can have different variances even when their expectations are equal.
- When , we have
The corresponding stochastic approximation algorithm for solving this equation is
which is the Sarsa algorithm.
- When , we have
The corresponding algorithm for solving this equation is
Here is the complete sampled return of a terminating episode. Setting replaces the estimate with one return, but averaging over repeated visits is needed to reduce sampling noise. A step size of gives the incremental sample mean.
- For a general value of , we have
The corresponding algorithm for solving the above equation is
This algorithm is called n-step Sarsa.
In summary, n-step Sarsa becomes one-step Sarsa for . When the backup reaches the end of an episode and contains no bootstrap term, it becomes an MC update with the chosen step size; is not required. A nonterminal n-step update waits for the experience through time . If the episode ends earlier, the remaining returns can be calculated at termination.
To that end, the q value in n-step Sarsa can be rewritten as
Here is the estimate at time . Larger n uses more actual rewards and less immediate dependence on an approximate bootstrap, often reducing bootstrap bias while increasing variance. This is a common trade-off, not a guarantee that performance or variance changes monotonically with n. The algorithm above evaluates a given policy and must be combined with policy improvement for control.
Q-learning
Sarsa based algorithms are solving BE, followed by policy improvement to iteratively derive the optimal policy. Q-learning directly solve the BOE. The BOE want to find the policy that maximizes the state value. And the state value is maximized by choosing the action with maximum value. So the BOE in the q-value form can be written as
So Q-learning can be written as:
Proof of the BOE in q-value
By the definition of expectation, we have
Taking the maximum of both sides of the equation gives
By denoting , we can rewrite the above equation as
which is clearly the BOE.
on-policy and off-policy
Two policies exist in any reinforcement learning task: a behavior policy and a target policy. The behavior policy is the one used to generate experience samples. The target policy is the one that is constantly updated to converge to an optimal policy. When the behavior policy is the same as the target policy, such a learning process is called on-policy. Otherwise, when they are different, the learning process is called off-policy.
Off-policy learning can use data from other policies, such as an exploratory controller or a human operator, while learning about a target policy. This can improve data reuse, but sufficient coverage remains essential. Neither on-policy nor off-policy learning automatically guarantees efficient exploration or an optimal result from an arbitrary dataset.
The ordinary Sarsa and MC algorithms introduced here are on-policy. MC also has off-policy variants, for example those using importance sampling to correct the mismatch between behavior and target policies.
Q-learning is an off-policy algorithm because its max backup targets a greedy policy even when behavior is exploratory. The behavior may happen to be greedy too, but this does not remove the algorithm's ability to learn off-policy. Purely greedy behavior can fail to visit useful actions, so coverage must still be considered.


Another concept that may be confused with on-policy/off-policy is online/offline. Online learning refers to the case where the agent updates the values and policies while interacting with the environment. Offline learning refers to the case where the agent up dates the values and policies using pre-collected experience data without interacting with the environment. If an algorithm is on-policy, then it can be implemented in an online fashion, but cannot use pre-collected data generated by other policies. If an algorithm is off-policy, then it can potentially use either online or offline data. However, ordinary off-policy DQN or SAC does not automatically solve offline RL: limited coverage and inaccurate values for out-of-distribution actions can make learning from a fixed dataset unreliable.
Summary
| Algorithm | Expression of the TD target |
|---|---|
| Sarsa | |
| -step Sarsa | |
| Q-learning | |
| Monte Carlo |
| Algorithm | Equation to be solved |
|---|---|
| Sarsa | BE: |
| -step Sarsa | BE: |
| Q-learning | BOE: |
| Monte Carlo | BE: |
*Table : A unified point of view of TD algorithms.
