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…

Key topics
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: , where is the action the policy actually sampled.
- Q-learning: , the best estimated next action.
SARSA's target inherits the randomness of . 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 , it computes the policy-weighted average of every next-state action value:
The expectation is taken with respect to the target policy — 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.
- : current state and action.
- : reward received after taking in .
- : the next state.
- : a candidate next action, ranging over all actions available in .
- : discount factor.
- : step size.
- : the tabular action-value estimate.
- : the target policy's probability of choosing in .
The assumptions are the usual tabular ones: finite state and action spaces, a table of values, a one-step bootstrap, and a policy whose probabilities over next actions you can evaluate. That last assumption is the load-bearing one. If 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.
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:
Now expand as the policy-weighted sum of action values in that state:
Substitute, and you have the Expected SARSA target:
The update applies the TD error — the bracket, not the target itself:
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 is greedy, all probability mass sits on the argmax, the sum collapses to , and you recover Q-learning.
- If 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 you take , receive and land in , then branch over every — each branch weighted by , each leaf holding . The expectation is the weighted average across those leaves.
Knowledge check
Check your understanding
Answer this question before you continue.
Worked Example: Computing the Target by Hand
Set up a tiny case. From state you take action , receive , and land in . The discount is . In there are three actions with these values:
| 4 | |
| 8 | |
| 6 |
Case 1: Uniform policy
Suppose for each action. Compute the weighted sum term by term:
| product | |||
|---|---|---|---|
| 0.333 | 4 | 1.333 | |
| 0.333 | 8 | 2.667 | |
| 0.333 | 6 | 2.000 |
The sum is . The target is:
Now apply the update. Suppose and . The TD error is , so the new value is .
Knowledge check
Check your understanding
Answer this question before you continue.
Case 2: ε-greedy policy
Let . The greedy action is (value 8). Under ε-greedy, gets , and the other two get each.
| product | |||
|---|---|---|---|
| 0.0667 | 4 | 0.267 | |
| 0.8667 | 8 | 6.933 | |
| 0.0667 | 6 | 0.400 |
The sum is . The target is . The TD error against is , and the new value is .
Knowledge check
Check your understanding
Answer this question before you continue.
Case 3: Greedy policy
Set and the rest to zero. The sum is just . The target is — 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 and read off , 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.
| Algorithm | Target | Policy role | Variance from next-action sampling |
|---|---|---|---|
| SARSA | On-policy; sampled from behavior policy | Higher (sampled ) | |
| Q-learning | Off-policy; greedy target | None from action sampling (deterministic max) | |
| Expected SARSA | Either; depends on chosen | None 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 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 and next state are still sampled from the environment, and the 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 equals the behavior policy, the update is on-policy and matches SARSA's intent. If is greedy with respect to , you get Q-learning. If 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 . 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 . 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.
References
Build a stronger RL mental model
Use structured resources to connect equations, algorithms, and observable agent behavior.


