Unit 8 of 13 · about 4 hours · derive, implement

SARSA and Expected SARSA control

These are the two on-policy TD control updates. They turn evaluation into improvement while staying on the behavior policy's data.

You'll be able to

  • Derive the Sarsa and Expected Sarsa targets and state which policy each evaluates.
  • Implement both updates from a blank file and match fixed-trajectory values exactly.
  • Explain when Expected Sarsa reduces variance relative to Sarsa and when it matches Q-learning.

Covers

SARSA controlExpected SARSA control
1

Read

In this order, only the parts listed

0 of 2 done

  1. Sarsa and Expected SarsaReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7

    Chapter 6, Sections 6.4 and 6.6, rules (6.7) and (6.9) with the Sarsa box and equation (6.9) (printed pp. 129 to 133)

    Skip Section 6.5 Q-learning and Section 6.7 maximization bias here.

  2. On-policy TD controlStanford CS234 Winter 2026 - Policy Evaluation and Q-Learning

    Lecture 4, Model-free Policy Iteration with TD Methods (slide 33) and the SARSA update under Properties of TD-Style Tabular Control (slide 38)

    Use the lecture's control framing; the update contracts live in Sutton.

2

Focus on

  • Sarsa's target uses the action actually taken next. It evaluates the behavior policy, including its exploration.
  • Expected Sarsa replaces the sampled with . Same expectation, less variance from random next actions.
  • With a greedy target policy the expectation becomes the max, so Expected Sarsa matches the Q-learning target while keeping the Sarsa update schema.
Common mistakes · 3
  • Using the max in the Sarsa target. That turns it into Q-learning and silently changes the evaluated policy.
  • Computing the expectation under instead of . Expected Sarsa averages with target-policy probabilities.
  • Forgetting the terminal mask in Expected Sarsa. At termination the expectation term drops out just like any bootstrap.
3

Try it yourself

Closed-book, before checking anything

Closed-book, load Q = [[1.0, 2.0], [3.0, 4.0]] and process the transition (0, 0, 1.0, 1, False) with alpha=0.1, gamma=0.9 and epsilon=0.1. Show the expected next value is and the updated value is .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Write both targets.Answer

    Sarsa: . Expected Sarsa: .

  2. 2.Why does Expected Sarsa have lower variance than Sarsa at fixed ?Answer

    It averages over next-action randomness analytically instead of sampling one .

  3. 3.Behavior turns fully greedy with a greedy target. What happens to the three targets?Answer

    Expected Sarsa's target becomes the Q-learning max exactly. Sarsa's sampled next action is greedy too, but it is chosen before the update, so when the update can change the greedy action and Sarsa can still differ from Q-learning (Exercise 6.12).

  4. 4.A learner updates with the action from two steps later. What broke?Answer

    Action alignment. The target must use the action at , not a later one; stale next actions bias control.

5

Practice

  • Deep-ML 175: SARSA

    Run sarsa_update on the deterministic chains; the public tests check Q('A', 'go') = 0.5000, Q('B', 'go') = 1.0000, Q('C', 'go') = 1.5000 and the three-start-state case

    Greedy ties break toward the first action, as the second test shows. Match the chain by hand first.

  • Deep-ML 476: Expected SARSA

    Process single-transition episodes with expected_sarsa; the public tests check 1.3555 for the continuing case and 2.5 for the terminal case

    Derive the 3.95 expectation before running. The tests cover one transition each, so add a multi-step episode of your own.

You're done when

Both Deep-ML suites pass unmodified, and you can write both targets and their evaluated policies from memory.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 5 more in the section
  • Reinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7Richard Sutton and Andrew Barto · primary

    The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Sections 2.4 and 2.5 (incremental and constant step-size estimates), Chapter 5 without the starred sections (Monte Carlo prediction and control, ordinary and weighted importance sampling), Chapter 6 Sections 6.1 to 6.7 (TD(0), Sarsa, Q-learning, Expected Sarsa, maximization bias) and Chapter 7 Sections 7.1 to 7.3 (n-step methods). Reproduce Example 5.4 (off-policy blackjack), Example 6.2 (random walk) and Example 6.6 (cliff walking), and implement each update from fixed hand-calculated trajectories before training an agent.

  • Stanford CS234 Winter 2026 - Policy Evaluation and Q-LearningStanford University · required

    Study Lecture 3 (tabular policy evaluation) and Lecture 4 (Q-learning) slides and the relevant independent Assignment 1 and 2 written questions. Use the course treatment to state model-access, convergence, coverage and function-approximation assumptions for each update.

Also in this section

  • Hands-on Modern RL - Dynamic Programming, Monte Carlo and TDWalking Labs · support

    Use the DP, MC and TD page and the bandit script as a code companion. Rebuild the algorithms in the shared track schema, and keep the bandit comparison as a seed-sensitivity case: which method wins changes with the seed. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; its code/chapterNN folders do not follow the site chapter numbers.

  • Gymnasium Core APIFarama Foundation · reference

    Use the reset, step, seeding and wrapper documentation as the behavior authority for the installed environment version: reset(seed=...) seeds once, step returns terminated and truncated, and vector environments autoreset on the next step by default. Test observation, reward, terminated, truncated and info fields at the boundary.

  • Deep-ML - Tabular Prediction and Control ProblemsDeep-ML · support

    Use named problems as closed-book checks after your own implementation passes: 272 (first-visit Monte Carlo), 257 (TD error), 273 (n-step TD), 175 (SARSA), 476 (Expected SARSA), 133 (Q-learning) and 518 (cliff walking, SARSA versus Q-learning). Read each problem's tests before trusting its statement.

  • Reinforcement Learning Control Laboratory - Tabular ControlRoadmap-Final · required

    Complete the MC, SARSA and Q-learning requirements with exact trajectory updates, stochastic and exploration-sensitive environments, visitation logs, matched interaction budgets and seeded alignment and bootstrap defects.

  • Reinforcement Learning: An Introduction (2nd ed.) - Starred Sections 5.8 and 5.9 and Chapter 10 Sections 10.3 to 10.5Richard Sutton and Andrew Barto · support

    Study starred Section 5.9 on per-decision importance sampling only for the per-decision estimator, and Chapter 10 Sections 10.3 to 10.5 on average reward and differential values only for the continuing-task unit. Cite printed pages; the authors post the full text free at incompleteideas.net.