Skip to content
intermediate

Deriving the Expected SARSA Target With a Worked Action-Value Example

You know the SARSA target. You know the Q-learning target. Then someone says "Expected SARSA" and your brain stalls on one word: expectation. What action…

Published 2026-10-02Updated 2026-10-049 min read
Detailed view of network cables plugged into a server rack in a data center.
Detailed view of network cables plugged into a server rack in a data center. Photo by Brett Sayles on Pexels.

You know the SARSA target. You know the Q-learning target. Then someone says "Expected SARSA" and your brain stalls on one word: expectation. What action is it expecting?

None. That is the whole trick. Expected SARSA does not wait to see which action gets sampled next. It averages over all of them, weighted by how likely the policy is to pick each one. Once that clicks, the arithmetic is ordinary multiplication and addition.

Here is the full derivation, then a worked number you can check by hand.

Where the Expected SARSA Target Comes From

You already have the two targets this article builds on:

  • SARSA: R+γQ(S′,A′)R + \gamma Q(S', A'), where A′A' is the action the policy actually sampled.
  • Q-learning: R+γmax⁡a′Q(S′,a′)R + \gamma \max_{a'} Q(S', a'), the best estimated next action.

SARSA's target inherits the randomness of A′A'. If the policy is stochastic, the same state-action pair can produce a different target on every visit, even when the reward and next state are identical. That variance slows learning and makes updates jumpy.

Expected SARSA removes that source of noise. Instead of waiting to observe A′A', it computes the policy-weighted average of every next-state action value:

R+γ∑a′π(a′∣S′) Q(S′,a′)R + \gamma \sum_{a'} \pi(a' \mid S')\, Q(S', a')

The expectation is taken with respect to the target policy π\pi — the policy you are learning about — not the environment and not the behavior policy that generated the data. That distinction matters, and it is exactly what makes Expected SARSA's on-policy or off-policy status a design choice rather than a fixed label.

Notation and Assumptions Before the Derivation

Pin the symbols down first so the derivation reads as arithmetic.

  • S,AS, A: current state and action.
  • RR: reward received after taking AA in SS.
  • S′S': the next state.
  • a′a': a candidate next action, ranging over all actions available in S′S'.
  • γ\gamma: discount factor.
  • α\alpha: step size.
  • Q(s,a)Q(s, a): the tabular action-value estimate.
  • π(a′∣S′)\pi(a' \mid S'): the target policy's probability of choosing a′a' in S′S'.

The assumptions are the usual tabular ones: finite state and action spaces, a table of QQ values, a one-step bootstrap, and a policy π\pi whose probabilities over next actions you can evaluate. That last assumption is the load-bearing one. If π\pi is unknown or you cannot compute its probabilities, you cannot form the expectation. If the action space is continuous, the sum becomes an intractable integral.

Knowledge check

Check your understanding

Answer this question before you continue.

A learner has the next-state action values but cannot evaluate the target policy's action probabilities. What follows for the Expected SARSA backup?
Scenario Interpretation

Focus: Identify when the action-value expectation can be computed from the information available.

Deriving the Target Step by Step

Start from the general one-step backup. The target is the immediate reward plus the discounted value of the next state under the policy:

target=R+γ V(S′)\text{target} = R + \gamma\, V(S')

Now expand V(S′)V(S') as the policy-weighted sum of action values in that state:

V(S′)=∑a′π(a′∣S′) Q(S′,a′)V(S') = \sum_{a'} \pi(a' \mid S')\, Q(S', a')

Substitute, and you have the Expected SARSA target:

target=R+γ∑a′π(a′∣S′) Q(S′,a′)\text{target} = R + \gamma \sum_{a'} \pi(a' \mid S')\, Q(S', a')

The update applies the TD error — the bracket, not the target itself:

Q(S,A)←Q(S,A)+α[ R+γ∑a′π(a′∣S′) Q(S′,a′)−Q(S,A) ]Q(S, A) \leftarrow Q(S, A) + \alpha \Big[\, R + \gamma \sum_{a'} \pi(a' \mid S')\, Q(S', a') - Q(S, A) \,\Big]

The bracket is the correction signal. It says: here is how far my current estimate was from a better one-step guess. The target is the guess; the TD error is the gap.

Two boundary cases fall out of the same formula:

  • If π\pi is greedy, all probability mass sits on the argmax, the sum collapses to max⁡a′Q(S′,a′)\max_{a'} Q(S', a'), and you recover Q-learning.
  • If π\pi puts all mass on a single action, you recover SARSA's sampled form.

Q-learning is not a separate species. It is Expected SARSA with a greedy target policy.

Picture the backup as a small tree: from SS you take AA, receive RR and land in S′S', then branch over every a′a' — each branch weighted by π(a′∣S′)\pi(a' \mid S'), each leaf holding Q(S′,a′)Q(S', a'). The expectation is the weighted average across those leaves.

Knowledge check

Check your understanding

Answer this question before you continue.

For one transition, suppose the policy-weighted next-state action-value sum is 6, with reward 2, discount 0.9, current value Q(S, A) = 5, and step size 0.1. What is the updated Q(S, A)?
Single Choice

Focus: Use the Expected SARSA target and TD error to compute a tabular value update.

Worked Example: Computing the Target by Hand

A transition with reward 2 leads to a next state with three action branches. The branches show probability times action value: 0.0667 × 4, 0.8667 × 8, and 0.0667 × 6. Their weighted values sum to 7.6; adding reward 2 and discounting the sum by 0.9 gives a target of 8.84.
Expected SARSA combines next-action values using their policy probabilities, then adds the discounted average to the reward.

Set up a tiny case. From state SS you take action AA, receive R=2R = 2, and land in S′S'. The discount is γ=0.9\gamma = 0.9. In S′S' there are three actions with these values:

a′a'Q(S′,a′)Q(S', a')
a1a_14
a2a_28
a3a_36

Case 1: Uniform policy

Suppose π(a′∣S′)=1/3\pi(a' \mid S') = 1/3 for each action. Compute the weighted sum term by term:

a′a'π(a′∣S′)\pi(a' \mid S')Q(S′,a′)Q(S', a')product
a1a_10.33341.333
a2a_20.33382.667
a3a_30.33362.000

The sum is 1.333+2.667+2.000=6.01.333 + 2.667 + 2.000 = 6.0. The target is:

2+0.9×6.0=2+5.4=7.42 + 0.9 \times 6.0 = 2 + 5.4 = 7.4

Now apply the update. Suppose Q(S,A)=5Q(S, A) = 5 and α=0.1\alpha = 0.1. The TD error is 7.4−5=2.47.4 - 5 = 2.4, so the new value is 5+0.1×2.4=5.245 + 0.1 \times 2.4 = 5.24.

Knowledge check

Check your understanding

Answer this question before you continue.

In the worked example, the next-action values are 4, 8, and 6, the policy is uniform, the reward is 2, and γ = 0.9. What is the Expected SARSA target?
Output Prediction

Focus: Calculate an Expected SARSA target from a uniform policy and a small action-value table.

Case 2: ε-greedy policy

Let ϵ=0.2\epsilon = 0.2. The greedy action is a2a_2 (value 8). Under ε-greedy, a2a_2 gets 1−ϵ+ϵ/3=0.8+0.0667=0.86671 - \epsilon + \epsilon/3 = 0.8 + 0.0667 = 0.8667, and the other two get ϵ/3=0.0667\epsilon/3 = 0.0667 each.

a′a'π(a′∣S′)\pi(a' \mid S')Q(S′,a′)Q(S', a')product
a1a_10.066740.267
a2a_20.866786.933
a3a_30.066760.400

The sum is 0.267+6.933+0.400=7.60.267 + 6.933 + 0.400 = 7.6. The target is 2+0.9×7.6=8.842 + 0.9 \times 7.6 = 8.84. The TD error against Q(S,A)=5Q(S, A) = 5 is 3.843.84, and the new value is 5+0.1×3.84=5.3845 + 0.1 \times 3.84 = 5.384.

Knowledge check

Check your understanding

Answer this question before you continue.

For the article's ε = 0.2 policy, the weighted sum of next-state action values is 7.6. With reward 2 and γ = 0.9, what target results?
Output Prediction

Focus: Compute the Expected SARSA target under the article's ε-greedy policy example.

Case 3: Greedy policy

Set π(a2∣S′)=1\pi(a_2 \mid S') = 1 and the rest to zero. The sum is just Q(S′,a2)=8Q(S', a_2) = 8. The target is 2+0.9×8=9.22 + 0.9 \times 8 = 9.2 — identical to the Q-learning target. Same formula, different weights.

Common mistake: Treating the expectation as a fourth action or a special case. It is a weighted average. If you can write down π\pi and read off QQ, you can compute it with a loop and a running sum.

Policy Dependence: On-Policy, Off-Policy, or Both

The confusion around Expected SARSA usually comes from asking whether it is on-policy. The honest answer: it depends on which policy supplies the expectation, and you choose that.

AlgorithmTargetPolicy roleVariance from next-action sampling
SARSAR+γQ(S′,A′)R + \gamma Q(S', A')On-policy; A′A' sampled from behavior policyHigher (sampled A′A')
Q-learningR+γmax⁡a′Q(S′,a′)R + \gamma \max_{a'} Q(S', a')Off-policy; greedy targetNone from action sampling (deterministic max)
Expected SARSAR+γ∑a′π(a′∣S′)Q(S′,a′)R + \gamma \sum_{a'} \pi(a' \mid S') Q(S', a')Either; depends on chosen π\piNone from action sampling (weighted average)

The variance column needs a careful reading. It describes only the randomness that comes from which next action gets selected. SARSA samples one action, so its target moves with that draw. Expected SARSA and Q-learning both replace the draw with a deterministic function of the current QQ values — a weighted average and a max, respectively — so neither inherits action-sampling noise on a fixed transition.

That is not the same as saying their whole updates are low-variance. The reward RR and next state S′S' are still sampled from the environment, and the QQ estimates themselves carry noise. If the environment is stochastic, the target can still swing from step to step. The precise claim is narrower: the expectation removes the variance contributed by next-action sampling, conditional on the same transition and the same value estimates. Whether that translates into smoother learning curves depends on how much of the total noise came from action sampling in the first place.

If π\pi equals the behavior policy, the update is on-policy and matches SARSA's intent. If π\pi is greedy with respect to QQ, you get Q-learning. If π\pi is some other target policy, you can compute the expectation off-policy without importance sampling on the final step — a practical advantage over sampled SARSA, which needs importance ratios to correct for the mismatch.

That flexibility is the point. Expected SARSA is not "between" SARSA and Q-learning on some spectrum. It is a generalization that contains both as special cases.

When the Expectation Pays Off

SARSA's target varies with the sampled A′A'. Expected SARSA removes that specific variance by averaging over all next actions. The trade is computation: you must evaluate every next action at every update.

In a table with a handful of actions, that cost is trivial. In a continuous action space, it is a different story — you cannot sum over infinitely many actions, so you fall back to sampling.

Where the expectation helps:

  • Stochastic policies where the sampled action swings the target wildly.
  • Settings where stable learning matters more than raw update speed.
  • Off-policy learning where you want to avoid importance sampling on the last step.

Where it does not:

  • Very large or continuous action spaces.
  • Unknown target policies you cannot evaluate.
  • Cases where the extra computation per update outweighs the variance reduction.

Common mistake: Assuming lower variance means lower bias. The expectation is still a bootstrapped estimate, so it inherits the bias of QQ. Variance reduction and bias reduction are different problems.

This expectation-over-actions idea reappears in modern actor-critic methods, where the policy is a distribution and the value target averages over sampled or reparameterized actions. The tabular version is the cleanest place to see the mechanism before the policy becomes a neural network.

What to Do Next

If you can enumerate next actions and you know the policy, use the expectation. If the action space is continuous or the policy is unknown, fall back to sampling.

The concrete next step: implement the Expected SARSA update on a small tabular environment — a gridworld with four actions is enough — and log the TD error at each step. Run it alongside SARSA and compare the variance of the updates. You will see the difference in the numbers, not just in the formula. From there, the same expectation appears in policy-gradient and actor-critic targets, where the policy is a distribution you sample from rather than a table you read.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

Data are generated by a behavior policy, but the Expected SARSA backup uses a greedy target policy. Which interpretation matches the article?
Question 1 of 2Comparison Reasoning

Focus: Distinguish Expected SARSA's behavior policy from the policy used in its expectation.

With the same transition and fixed action-value estimates, which statement best describes Expected SARSA's variance effect compared with SARSA?
Question 2 of 2Misconception Check

Focus: Explain which source of target variability Expected SARSA removes and what variability remains.

References

  1. [1703.01327] Multi-Step Reinforcement Learning: A Unifying Algorithmar5iv.labs.arxiv.org
  2. An Analysis of Action-Value Temporal-Difference Methods ...rlj.cs.umass.edu
Practical resource

Build a stronger RL mental model

Use structured resources to connect equations, algorithms, and observable agent behavior.

Browse resources
Related sites

Continue across related AI foundations

Use LearnPyFast for Python foundations and LearnLLMFast for practical language-model and agent application concepts.

Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related reinforcement learning tutorials

Continue with nearby RL concepts, algorithms, and experiments that build on the same decision process.