Simple Algorithm
Reinforcement Learning · Fixed Point · 26 min

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 QπQ^\pi under any behavior.
  • Define Q(σ), identify its special cases, and compare the targets of the TD control family.

Averaging instead of sampling

Write Vˉ(s)=∑aπ(a∣s) Q(s,a)\bar V(s) = \sum_a\pi(a \mid s)\,Q(s,a) for the value of state ss under a target policy π\pi and the current estimates. The three one-step targets for Q(St,At)Q(S_t,A_t) differ only in how they treat the next action:

YSARSA=Rt+1+γ Q(St+1,At+1),YE=Rt+1+γ Vˉ(St+1),YQ=Rt+1+γmax⁡aQ(St+1,a).Y^{\text{SARSA}} = R_{t+1} + \gamma\,Q(S_{t+1},A_{t+1}),\qquad Y^{\text{E}} = R_{t+1} + \gamma\,\bar V(S_{t+1}),\qquad Y^{\text{Q}} = R_{t+1} + \gamma\max_aQ(S_{t+1},a).

When At+1A_{t+1} is drawn from π\pi, YEY^{\text{E}} is the conditional expectation of YSARSAY^{\text{SARSA}} given everything up to St+1S_{t+1}. Averaging out a source of randomness that the agent can compute exactly is Rao–Blackwellization, and it can only help.

DerivationProof by the law of total variance
  1. E[YSARSA∣Z]=Rt+1+γ∑a′π(a′∣St+1) Q(St+1,a′)=YE\E\big[Y^{\text{SARSA}} \mid Z\big] = R_{t+1} + \gamma\sum_{a'}\pi(a' \mid S_{t+1})\,Q(S_{t+1},a') = Y^{\text{E}}

    Condition on Z=(Rt+1,St+1)Z = (R_{t+1}, S_{t+1}); everything is also conditioned on St=s,At=aS_t = s, A_t = a. Only At+1A_{t+1} is left random inside:

  2. Var⁡(YSARSA)=Var⁡(E[YSARSA∣Z])+E[Var⁡(YSARSA∣Z)]=Var⁡(YE)+γ2 E[Var⁡A′Q(St+1,A′)]\Var\big(Y^{\text{SARSA}}\big) = \Var\big(\E[Y^{\text{SARSA}} \mid Z]\big) + \E\big[\Var(Y^{\text{SARSA}} \mid Z)\big] = \Var\big(Y^{\text{E}}\big) + \gamma^2\,\E\big[\Var_{A'}Q(S_{t+1},A')\big]

    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:

  3. The second term is nonnegative; it vanishes only when π\pi is deterministic or all the actions it might pick have equal estimates. ■\blacksquare

Predict before you look

A deterministic step leads to s′s' with reward −1-1, where Q(s′,⋅)=(−5,−3,−8,−4)Q(s',\cdot) = (-5, -3, -8, -4) and the policy is ε-greedy with ε=0.1\varepsilon = 0.1 over the four actions; γ=1\gamma = 1. Expected SARSA's target is exactly −4.2-4.2 every time. What is the variance of SARSA's target?

Show the answer

0.71

The next action is right (−3-3) with probability 0.9250.925 and each other action with 0.0250.025. The mean next value is −3.2-3.2; the mean of the squares is 0.925×9+0.025×(25+64+16)=10.950.925 \times 9 + 0.025 \times (25 + 64 + 16) = 10.95; the variance is 10.95−3.22=0.7110.95 - 3.2^2 = 0.71, 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, Var⁡(YE)\Var(Y^{\text{E}}), 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 α=1\alpha = 1 without losing asymptotic performance, while SARSA does well in the long run only with a small α\alpha, 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 YEY^{\text{E}} requires π\pi to be the policy that acts. Let a behavior bb choose actions and let π\pi be any target policy: the one-step update still needs no importance ratio, because AtA_t is given (the update is for that pair) and the next action is averaged under π\pi, not sampled from bb. With π\pi greedy with respect to QQ, Vˉ(s)=max⁡aQ(s,a)\bar V(s) = \max_aQ(s,a) and the update is Q-learning; with π\pi 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 1−ε+ε/∣A∣1 - \varepsilon + \varepsilon/\abs{\mathcal A} on the action with the largest estimate, so Vˉ(St+1)\bar V(S_{t+1}) inherits most of the maximization bias of Q-learning's target. The widget measures it.

Check your understanding

Which statement about on-policy expected SARSA with an ε-greedy policy is correct?

  1. It removes both the variance due to the next action and the maximization bias
  2. It removes the variance due to the next action, but its target still overestimates when the estimates are noisy
  3. It removes the maximization bias, but has higher variance than SARSA
  4. 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 ρt+1⋯ρt+n\rho_{t+1}\cdots\rho_{t+n} (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 π\pi and the current estimates, and continues along the action that was taken, weighted by its target probability.

DerivationThe tree-backup return
  1. Gt:t+n=Rt+1+γ ⁣ ⁣∑a≠At+1 ⁣ ⁣π(a∣St+1) Q(St+1,a)+γ π(At+1∣St+1) Gt+1:t+n,Gt:t+1=Rt+1+γVˉ(St+1)G_{t:t+n} = R_{t+1} + \gamma\!\!\sum_{a \ne A_{t+1}}\!\!\pi(a \mid S_{t+1})\,Q(S_{t+1},a) + \gamma\,\pi(A_{t+1} \mid S_{t+1})\,G_{t+1:t+n},\qquad G_{t:t+1} = R_{t+1} + \gamma\bar V(S_{t+1})

    One step is expected SARSA's target. For nn steps, branch at St+1S_{t+1}: the untaken actions contribute their estimates, the taken one its own continuation:

  2. Gt:t+n−Q(St,At)=δˉt+γ π(At+1∣St+1)(Gt+1:t+n−Q(St+1,At+1))G_{t:t+n} - Q(S_t,A_t) = \bar\delta_t + \gamma\,\pi(A_{t+1} \mid S_{t+1})\big(G_{t+1:t+n} - Q(S_{t+1},A_{t+1})\big)

    Hold QQ fixed and define the expected TD error δˉk=Rk+1+γVˉ(Sk+1)−Q(Sk,Ak)\bar\delta_k = R_{k+1} + \gamma\bar V(S_{k+1}) - Q(S_k,A_k). Since Vˉ(s)−π(A∣s)Q(s,A)=∑a≠Aπ(a∣s)Q(s,a)\bar V(s) - \pi(A \mid s)Q(s,A) = \sum_{a \ne A}\pi(a \mid s)Q(s,a), subtracting Q(St,At)Q(S_t,A_t) from the recursion gives

  3. Gt:t+n=Q(St,At)+∑k=tt+n−1δˉk∏i=t+1kγ π(Ai∣Si)G_{t:t+n} = Q(S_t,A_t) + \sum_{k=t}^{t+n-1}\bar\delta_k\prod_{i=t+1}^{k}\gamma\,\pi(A_i \mid S_i)

    Unroll (the sum stops at the end of the episode if it comes first):

  4. At Q=QπQ = Q^\pi, every δˉk\bar\delta_k has mean zero given (Sk,Ak)(S_k, A_k), whichever policy chose AkA_k, because E[Rk+1+γVˉπ(Sk+1)∣Sk,Ak]=Qπ(Sk,Ak)\E\big[R_{k+1} + \gamma\bar V^\pi(S_{k+1}) \mid S_k, A_k\big] = Q^\pi(S_k,A_k); and each weight is a function of the history up to (Sk,Ak)(S_k, A_k) only, so it can be pulled out of that conditional expectation. So the expected update vanishes at QπQ^\pi under any behavior: tree backup's fixed point is QπQ^\pi, and no importance ratio appears anywhere. ■\blacksquare

python
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 G

Computed backward from the horizon. QQ is an array indexed by (state, action); the behavior's probabilities never appear, only the target policy's. The update is then Q(St,At)←Q(St,At)+α [G−Q(St,At)]Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha\,[G - Q(S_t,A_t)].

The price is in the weights. They multiply probabilities π(Ai∣Si)≤1\pi(A_i \mid S_i) \le 1 even when the behavior follows π\pi exactly, so the multi-step information fades on-policy too: with an ε-greedy π\pi (ε=0.1\varepsilon = 0.1, four actions) each greedy step keeps a factor 0.925. With a greedy π\pi, the weight drops to zero at the first exploratory action, as in Watkins's Q(λ\lambda). 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 Gt:h=Rt+1+γ[π(At+1∣St+1)(Gt+1:h−Q(St+1,At+1))+Vˉ(St+1)]G_{t:h} = R_{t+1} + \gamma\big[\pi(A_{t+1} \mid S_{t+1})\big(G_{t+1:h} - Q(S_{t+1},A_{t+1})\big) + \bar V(S_{t+1})\big]. n-step SARSA in its per-decision importance-sampling form with control variates (Sutton & Barto, §7.4) is the same expression with the ratio ρt+1\rho_{t+1} in place of π(At+1∣St+1)\pi(A_{t+1} \mid S_{t+1}). Q(σ) slides between them, one step at a time, with a degree of sampling σk∈[0,1]\sigma_k \in [0,1]:

Gt:h=Rt+1+γ(σt+1 ρt+1+(1−σt+1) π(At+1∣St+1))(Gt+1:h−Q(St+1,At+1))+γ Vˉ(St+1)G_{t:h} = R_{t+1} + \gamma\Big(\sigma_{t+1}\,\rho_{t+1} + (1-\sigma_{t+1})\,\pi(A_{t+1} \mid S_{t+1})\Big)\Big(G_{t+1:h} - Q(S_{t+1},A_{t+1})\Big) + \gamma\,\bar V(S_{t+1})
The recursion ends with Gh:h=Q(Sh,Ah)G_{h:h} = Q(S_h,A_h) if the horizon h=t+nh = t+n comes before the end of the episode, and with GT−1:T=RTG_{T-1:T} = R_T otherwise. σk=1\sigma_k = 1 samples the action at step kk (and corrects with ρk\rho_k off-policy); σk=0\sigma_k = 0 takes the expectation over it.

With σ≡0\sigma \equiv 0, Q(σ) is tree backup; with σ≡1\sigma \equiv 1 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 Gt+1:t+1=Q(St+1,At+1)G_{t+1:t+1} = Q(S_{t+1},A_{t+1}); in De Asis et al.'s original form, whose one-step target is Rt+1+γ[σ Q(St+1,At+1)+(1−σ)Vˉ(St+1)]R_{t+1} + \gamma\big[\sigma\,Q(S_{t+1},A_{t+1}) + (1-\sigma)\bar V(S_{t+1})\big], σ=1\sigma = 1 gives SARSA and σ=0\sigma = 0 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 σ\sigma performed better than either extreme, and that varying σ\sigma during learning did better still.

MethodWhat it does with later actionsOff-policy correctionCharacteristic weakness
SARSAsamples At+1A_{t+1} from the behaviornone: on-policyvariance from the sampled action
Expected SARSAaverages under π\pinone needed (one step)ε-greedy target keeps most of the maximization bias
Q-learningtakes the maximumnone needed (one step)maximization bias
Double Q-learningselects with QAQ^A, values with QBQ^Bnone needed (one step)can underestimate
n-step SARSAsamples nn actionsproducts of ratios ρ\rhovariance grows with nn off-policy
Tree backup, TB(λ\lambda)averages untaken actions, follows the taken one with weight π(A∣S)\pi(A \mid S)noneshort effective traces, even on-policy
Q(σ\sigma)samples (σ=1\sigma = 1) or averages (σ=0\sigma = 0), step by stepweight σρ+(1−σ)π\sigma\rho + (1-\sigma)\pi per stepσ\sigma must be chosen
Retrace(λ\lambda)trace coefficient λmin⁡(1,π/b)\lambda\min(1, \pi/b) on the taken actiontruncated ratioscuts traces when the behavior strays (Off-Policy Traces)
One-step methods need no importance ratios because the only behavior action they contain defines which entry is updated. Every multi-step method must deal with the behavior's later actions: by sampling them on-policy, by ratios, by expectation, or by truncation.

Key takeaways

  • Expected SARSA replaces the sampled next action by its average under π\pi: same mean as SARSA's target, variance lower by exactly γ2E[Var⁡A′∼πQ(St+1,A′)]\gamma^2\E[\Var_{A'\sim\pi}Q(S_{t+1},A')], zero in deterministic environments.
  • The same one-step update works for any target policy without importance ratios; with a greedy π\pi it is Q-learning. With a fixed π\pi it converges to QπQ^\pi from any covering behavior, and to Q⋆Q^\star 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 π(A∣S)\pi(A \mid S); its return is a weighted sum of expected TD errors, so its fixed point is QπQ^\pi 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 σ\sigma can beat both extremes.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

By how much does the variance of SARSA's target exceed that of expected SARSA's, for the same QQ and (St,At)(S_t, A_t)?

  1. By γ2 E[Var⁡A′∼πQ(St+1,A′)]\gamma^2\,\E\big[\Var_{A' \sim \pi}Q(S_{t+1},A')\big], the expected variance of the next action's value
  2. By the variance of the reward
  3. By nothing: the two targets have equal variance and differ only in mean
  4. By γ E[max⁡aQ(St+1,a)−Vˉ(St+1)]\gamma\,\E\big[\max_aQ(S_{t+1},a) - \bar V(S_{t+1})\big]
Show the answer

A. By γ2 E[Var⁡A′∼πQ(St+1,A′)]\gamma^2\,\E\big[\Var_{A' \sim \pi}Q(S_{t+1},A')\big], the expected variance of the next action's value

Expected SARSA's target is SARSA's averaged over At+1A_{t+1}, so the law of total variance gives exactly this gap. The reward's and next state's randomness affect both targets equally.

Question 2

Off-policy one-step expected SARSA, with behavior bb and target π\pi, needs no importance ratio. Why?

  1. Because importance ratios are always equal to 1 in tabular problems
  2. Because bb and π\pi must coincide for the method to work
  3. Because the update is for the pair (St,At)(S_t,A_t) actually taken, and the next action is averaged under π\pi instead of sampled from bb
  4. Because the reward does not depend on the action
Show the answer

C. Because the update is for the pair (St,At)(S_t,A_t) actually taken, and the next action is averaged under π\pi instead of sampled from bb

The only action the behavior contributes to the update is AtA_t, 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 π\pi. Multi-step targets lose this property, because they contain later actions chosen by bb.

Question 3

Why is QπQ^\pi the fixed point of tree backup whatever behavior generated the data?

  1. Because tree backup reweights every trajectory by the product of ratios π/b\pi/b
  2. Because its target is a sum of expected TD errors, each with mean zero at QπQ^\pi given its state–action pair, weighted by target probabilities of earlier actions
  3. Because the behavior must equal π\pi for tree backup to be used
  4. 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 QπQ^\pi given its state–action pair, weighted by target probabilities of earlier actions

Gt:t+n−Q(St,At)=∑kδˉk∏i=t+1kγπ(Ai∣Si)G_{t:t+n} - Q(S_t,A_t) = \sum_k\bar\delta_k\prod_{i=t+1}^{k}\gamma\pi(A_i \mid S_i). Given (Sk,Ak)(S_k,A_k), the expected TD error δˉk\bar\delta_k averages over the environment and over π\pi, never over bb, so at QπQ^\pi its mean is zero however AkA_k was chosen; the weights depend only on the history up to (Sk,Ak)(S_k, A_k). The behavior affects how fast information flows, not where it flows to.

Question 4

In Q(σ), what do σ≡0\sigma \equiv 0 and σ≡1\sigma \equiv 1 give?

  1. σ≡0\sigma \equiv 0: Q-learning; σ≡1\sigma \equiv 1: Monte Carlo
  2. σ≡0\sigma \equiv 0: n-step SARSA; σ≡1\sigma \equiv 1: tree backup
  3. σ≡0\sigma \equiv 0: tree backup; σ≡1\sigma \equiv 1: n-step SARSA, sampling every action (with per-decision ratios off-policy)
  4. Both give expected SARSA; σ\sigma only changes the step size
Show the answer

C. σ≡0\sigma \equiv 0: tree backup; σ≡1\sigma \equiv 1: n-step SARSA, sampling every action (with per-decision ratios off-policy)

σ\sigma is the degree of sampling. At 0 every later action is replaced by an expectation under π\pi, and the taken branch is weighted by π(A∣S)\pi(A \mid S): tree backup. At 1 every action is sampled and corrected by its ratio ρ\rho: the n-step SARSA end. In De Asis et al.'s one-step form, Rt+1+γ[σQ(St+1,At+1)+(1−σ)Vˉ(St+1)]R_{t+1} + \gamma[\sigma Q(S_{t+1},A_{t+1}) + (1-\sigma)\bar V(S_{t+1})], 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).