Expected SARSA & the TD Control Family
Expected SARSA, tree backup and Q(σ): one family of control updates.
SARSA samples the next action, Q-learning maximizes over it, and expected SARSA averages over it. Make that choice at every step of a multi-step target and a whole family appears: n-step SARSA samples everything, tree backup averages over everything not taken, and Q(σ) slides between the two, one step at a time. This lesson proves why averaging lowers variance, shows how tree backup learns off-policy without a single importance ratio, and maps the targets of the TD control family in one table.
What you’ll learn
- Prove that expected SARSA's target has the same mean as SARSA's and lower variance, by exactly the expected variance of the next action's value, and identify Q-learning as its greedy special case.
- State what expected SARSA converges to on-policy and off-policy, and explain why it still suffers from maximization bias when its target policy is ε-greedy.
- Derive the n-step tree-backup return, write it as a weighted sum of expected TD errors, and show why its fixed point is under any behavior.
- Define Q(σ), identify its special cases, and compare the targets of the TD control family.
Averaging instead of sampling
Write for the value of state under a target policy and the current estimates. The three one-step targets for differ only in how they treat the next action:
When is drawn from , is the conditional expectation of given everything up to . Averaging out a source of randomness that the agent can compute exactly is Rao–Blackwellization, and it can only help.
Condition on ; everything is also conditioned on . Only is left random inside:
So the means agree, and the law of total variance splits SARSA's variance into the variance of this conditional mean plus the mean of the conditional variance:
The second term is nonnegative; it vanishes only when is deterministic or all the actions it might pick have equal estimates.
A deterministic step leads to with reward , where and the policy is ε-greedy with over the four actions; . Expected SARSA's target is exactly every time. What is the variance of SARSA's target?
Show the answer
0.71
The next action is right () with probability and each other action with . The mean next value is ; the mean of the squares is ; the variance is , a standard deviation of about 0.84. All of it comes from the policy, and expected SARSA removes all of it.
The identity also says when the gain is large: when the policy is very random and the actions' values differ a lot, while the environment's own noise, , is small. Van Seijen, van Hasselt, Whiteson and Wiering (2009) tested exactly this dependence on the randomness of the environment and of the policy. On the cliff walk, where all randomness comes from the policy, expected SARSA can use without losing asymptotic performance, while SARSA does well in the long run only with a small , at which it learns slowly (Sutton and Barto, Figure 6.3, adapted from van Seijen et al.).
One update for any target policy
Nothing in requires to be the policy that acts. Let a behavior choose actions and let be any target policy: the one-step update still needs no importance ratio, because is given (the update is for that pair) and the next action is averaged under , not sampled from . With greedy with respect to , and the update is Q-learning; with the behavior itself it is on-policy expected SARSA. Sutton and Barto credit the first appearance of the idea to George John (1994); van Hasselt (2011) called the general off-policy form "General Q-learning".
Lower variance is not lower bias. An ε-greedy target puts weight on the action with the largest estimate, so inherits most of the maximization bias of Q-learning's target. The widget measures it.
Which statement about on-policy expected SARSA with an ε-greedy policy is correct?
- It removes both the variance due to the next action and the maximization bias
- It removes the variance due to the next action, but its target still overestimates when the estimates are noisy
- It removes the maximization bias, but has higher variance than SARSA
- It is identical to SARSA in expectation and in variance
Show the answer
B. It removes the variance due to the next action, but its target still overestimates when the estimates are noisy
Averaging over the next action replaces a random sample with its mean, which cuts variance. But the mean is taken under a policy that is mostly greedy with respect to noisy estimates, so it still leans on their maximum, and the maximum of noisy estimates is biased upward.
Multi-step without importance ratios: tree backup
Multi-step targets contain later actions, and off-policy those are the behavior's choices. n-step SARSA corrects for them with products of importance ratios (the last one for the action it bootstraps from), with the variance problems of Off-Policy Monte Carlo & Importance Sampling. Tree backup (Precup, Sutton & Singh, 2000) uses no ratios at all: at each later state it averages over the actions not taken, using and the current estimates, and continues along the action that was taken, weighted by its target probability.
One step is expected SARSA's target. For steps, branch at : the untaken actions contribute their estimates, the taken one its own continuation:
Hold fixed and define the expected TD error . Since , subtracting from the recursion gives
Unroll (the sum stops at the end of the episode if it comes first):
At , every has mean zero given , whichever policy chose , because ; and each weight is a function of the history up to only, so it can be pulled out of that conditional expectation. So the expected update vanishes at under any behavior: tree backup's fixed point is , and no importance ratio appears anywhere.
import numpy as np
def tree_backup_return(R, S, A, Q, pi, gamma, terminal):
"""n-step tree-backup return for the pair (S[0], A[0]).
R[k] is R_(t+k+1) for k < n; S[k] is S_(t+k) for k <= n; A[k] is A_(t+k) for k < n.
pi(s) returns the target policy's action probabilities; terminal: S[n] is terminal.
"""
n = len(R)
G = 0.0 if terminal else pi(S[n]) @ Q[S[n]] # expected value at the horizon
for k in reversed(range(n)):
if k < n - 1: # branch at S[k+1]
p, s, a = pi(S[k + 1]), S[k + 1], A[k + 1]
G = p @ Q[s] - p[a] * Q[s, a] + p[a] * G # untaken actions + taken branch
G = R[k] + gamma * G
return GComputed backward from the horizon. is an array indexed by (state, action); the behavior's probabilities never appear, only the target policy's. The update is then .
The price is in the weights. They multiply probabilities even when the behavior follows exactly, so the multi-step information fades on-policy too: with an ε-greedy (, four actions) each greedy step keeps a factor 0.925. With a greedy , the weight drops to zero at the first exploratory action, as in Watkins's Q(). Off-Policy Traces: Tree Backup & Retrace compares this with importance-sampled traces and with Retrace, which cuts only as much as needed.
Q(σ): sampling and expectation on a dial
Rearranged, the tree-backup recursion reads . n-step SARSA in its per-decision importance-sampling form with control variates (Sutton & Barto, §7.4) is the same expression with the ratio in place of . Q(σ) slides between them, one step at a time, with a degree of sampling :
With , Q(σ) is tree backup; with it samples every action, the n-step SARSA end of the family. In this control-variate form the one-step target is always expected SARSA's, since the recursion stops at ; in De Asis et al.'s original form, whose one-step target is , gives SARSA and expected SARSA. Sampling keeps full-length multi-step information but pays for it with variance (and, off-policy, with ratios); expectation pays with shrinking weights. De Asis, Hernandez-Garcia, Holland and Sutton (2018), who studied the n-step algorithm in this unified form, found that an intermediate performed better than either extreme, and that varying during learning did better still.
| Method | What it does with later actions | Off-policy correction | Characteristic weakness |
|---|---|---|---|
| SARSA | samples from the behavior | none: on-policy | variance from the sampled action |
| Expected SARSA | averages under | none needed (one step) | ε-greedy target keeps most of the maximization bias |
| Q-learning | takes the maximum | none needed (one step) | maximization bias |
| Double Q-learning | selects with , values with | none needed (one step) | can underestimate |
| n-step SARSA | samples actions | products of ratios | variance grows with off-policy |
| Tree backup, TB() | averages untaken actions, follows the taken one with weight | none | short effective traces, even on-policy |
| Q() | samples () or averages (), step by step | weight per step | must be chosen |
| Retrace() | trace coefficient on the taken action | truncated ratios | cuts traces when the behavior strays (Off-Policy Traces) |
Key takeaways
- Expected SARSA replaces the sampled next action by its average under : same mean as SARSA's target, variance lower by exactly , zero in deterministic environments.
- The same one-step update works for any target policy without importance ratios; with a greedy it is Q-learning. With a fixed it converges to from any covering behavior, and to when the target (or, on-policy, the behavior) becomes greedy in the limit.
- Lower variance is not lower bias: an ε-greedy target still leans on the maximum of noisy estimates.
- Tree backup averages over untaken actions and follows the taken one with weight ; its return is a weighted sum of expected TD errors, so its fixed point is under any behavior, at the cost of traces that shrink even on-policy.
- Q(σ) chooses, step by step, between sampling (n-step SARSA, with ratios off-policy) and expectation (tree backup); intermediate and varying can beat both extremes.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
By how much does the variance of SARSA's target exceed that of expected SARSA's, for the same and ?
- By , the expected variance of the next action's value
- By the variance of the reward
- By nothing: the two targets have equal variance and differ only in mean
- By
Show the answer
A. By , the expected variance of the next action's value
Expected SARSA's target is SARSA's averaged over , so the law of total variance gives exactly this gap. The reward's and next state's randomness affect both targets equally.
Off-policy one-step expected SARSA, with behavior and target , needs no importance ratio. Why?
- Because importance ratios are always equal to 1 in tabular problems
- Because and must coincide for the method to work
- Because the update is for the pair actually taken, and the next action is averaged under instead of sampled from
- Because the reward does not depend on the action
Show the answer
C. Because the update is for the pair actually taken, and the next action is averaged under instead of sampled from
The only action the behavior contributes to the update is , which defines which entry is updated rather than entering the target. Everything after it is either environment randomness (reward, next state) or an exact average under . Multi-step targets lose this property, because they contain later actions chosen by .
Why is the fixed point of tree backup whatever behavior generated the data?
- Because tree backup reweights every trajectory by the product of ratios
- Because its target is a sum of expected TD errors, each with mean zero at given its state–action pair, weighted by target probabilities of earlier actions
- Because the behavior must equal for tree backup to be used
- Because tree backup only ever looks one step ahead
Show the answer
B. Because its target is a sum of expected TD errors, each with mean zero at given its state–action pair, weighted by target probabilities of earlier actions
. Given , the expected TD error averages over the environment and over , never over , so at its mean is zero however was chosen; the weights depend only on the history up to . The behavior affects how fast information flows, not where it flows to.
In Q(σ), what do and give?
- : Q-learning; : Monte Carlo
- : n-step SARSA; : tree backup
- : tree backup; : n-step SARSA, sampling every action (with per-decision ratios off-policy)
- Both give expected SARSA; only changes the step size
Show the answer
C. : tree backup; : n-step SARSA, sampling every action (with per-decision ratios off-policy)
is the degree of sampling. At 0 every later action is replaced by an expectation under , and the taken branch is weighted by : tree backup. At 1 every action is sampled and corrected by its ratio : the n-step SARSA end. In De Asis et al.'s one-step form, , the extremes on-policy are SARSA and expected SARSA.
Further reading
- H. van Seijen, H. van Hasselt, S. Whiteson & M. Wiering, "A theoretical and empirical analysis of Expected Sarsa," IEEE Symposium on Adaptive Dynamic Programming and Reinforcement Learning, 177–184 (2009).
- D. Precup, R. S. Sutton & S. Singh, "Eligibility traces for off-policy policy evaluation," Proceedings of the 17th International Conference on Machine Learning, 759–766 (2000).
- K. De Asis, J. F. Hernandez-Garcia, G. Z. Holland & R. S. Sutton, "Multi-step reinforcement learning: A unifying algorithm," AAAI Conference on Artificial Intelligence (2018).
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 6–7 (MIT Press, 2nd ed., 2018).