Exercises — SARSA algorithm
Throughout, we reuse the one update rule that defines SARSA:
The single visual idea behind every calculation is below — keep it in view.

The red dot is the old estimate. The target is a fixed point on the number line. Every update slides the red dot a fraction of the way toward the target. That's all SARSA ever does.
Level 1 — Recognition
Exercise 1.1 (L1)
Which of the five values in the tuple is the one that makes SARSA on-policy rather than off-policy?
Recall Solution
The last element, — the next action actually chosen by the policy. Because SARSA plugs that specific action's Q-value into the target (not the maximum over actions), it evaluates the policy the agent is truly following. That is the definition of on-policy.
Exercise 1.2 (L1)
Identify the TD target and the TD error in this expression:
Recall Solution
- TD target .
- TD error . Here , , , .
Exercise 1.3 (L1)
A terminal state has been reached. What value does SARSA assign to , and therefore what is the TD target on the final step?
Recall Solution
By convention . So on the step that lands in a terminal state, the target collapses to just the reward: .
Level 2 — Application
Exercise 2.1 (L2)
Given , , , , . Compute the updated .
Recall Solution
Target . . .
Exercise 2.2 (L2)
A terminal-step update. , reward , next state is terminal, , . Find the new .
Recall Solution
Terminal ⇒ , so target . . .
Exercise 2.3 (L2)
The Windy Gridworld numbers from the parent note: , , , , . Reproduce the update.
Recall Solution
Target . (positive: we were too pessimistic). .
Level 3 — Analysis
Exercise 3.1 (L3)
Show algebraically that the SARSA update is a weighted average: Then use it to explain why always lies between and the target when .
Recall Solution
Start from . Distribute: Since , both weights and are positive and sum to 1 — this is a convex combination. A convex combination of two numbers always sits on the segment joining them (look at the red dot in the figure sliding along that segment). Hence can never overshoot past the target.
Exercise 3.2 (L3)
With target fixed at and , compute for , then again for . Interpret what does.
Recall Solution
: . : . With we discard the old estimate entirely and jump straight onto the target. That is maximally fast but maximally unstable — one noisy target and the estimate lurches with it.
Exercise 3.3 (L3)
The sign of the TD error tells a story. For each case say whether we over- or **under-**estimated and whether moves up or down: (a) (b) (c) .
Recall Solution
(a) : outcome better than expected ⇒ we underestimated ⇒ moves up (by ). (b) : outcome worse than expected ⇒ we overestimated ⇒ moves down (by ). (c) : estimate already matched the target ⇒ does not move. This is the fixed point the algorithm hunts for.
Level 4 — Synthesis
Exercise 4.1 (L4)
Run a two-step episode by hand. All Q-values start at . , . Step 1: , , , , . Step 2: , , , . Compute and after both updates, in order.
Recall Solution
Step 1 update for : at update time is still . Target . . . Step 2 update for : next state terminal ⇒ target . . . Note the ordering: SARSA updates the earlier pair using the later pair's value, and here that value was still because hadn't been visited yet — reward information propagates backward only across future episodes.
Exercise 4.2 (L4)
Repeat the same episode a second time (values now start at the from Exercise 4.1, same trajectory, same , ). Compute the new . Does the terminal reward finally reach state ?
Recall Solution
Now at the moment we update , the value (learned last episode). Target . . . Yes — the cost of the second step ('s ) has now flowed back into . Each replay lets information seep one step further back. This backward trickle is the heartbeat of temporal-difference learning.
Exercise 4.3 (L4)
Stochastic-slip case (parent Example 3): "forward" from slips to . , , at the policy picks with , , . Find the new and explain why SARSA now knows "forward is riskier than it looks."
Recall Solution
Target . (negative: this outing was worse than the stored optimism). . Because slips happen occasionally, SARSA folds their bad outcomes into the average value of "forward". Over many episodes settles at the mean over both lucky and slippery results — the estimate is honest about risk.
Level 5 — Mastery
Exercise 5.1 (L5)
Same transition, two algorithms. State has action-values : {left , right , up }. The -greedy policy happened to sample . Given , , , , compute the update under SARSA and under Q-learning. Explain the gap.
Recall Solution
SARSA uses the sampled action left (): Target ; ; . Q-learning uses : Target ; ; . The gap: SARSA lowers the value because the actually taken action (left) was mediocre; Q-learning raises it because it optimistically assumes the best action (right) will be taken next. SARSA describes the world as it is explored; Q-learning describes an idealised greedy future.
Exercise 5.2 (L5)
Under what single condition do the SARSA and Q-learning updates become identical for a given transition? Prove it from the two targets.
Recall Solution
They coincide exactly when the sampled next action is a greedy action: Then both targets equal , so both 's and both updates match. Practically this happens whenever the -greedy coin lands on "exploit", or in the limit where SARSA's policy becomes greedy and SARSA Q-learning.
Exercise 5.3 (L5)
Fixed-point reasoning. Suppose has converged so that the expected TD error is zero. Write the equation this implies and name it. Then argue why, for SARSA, that equation is the Bellman equation for the behaviour policy and not for the optimal policy.
Recall Solution
Expected error zero means the Bellman expectation equation for . Because is drawn from the same policy that generated the data (e.g. -greedy), the expectation is taken under , not under a max. Therefore the fixed point is — the value of the policy actually being followed — which includes the cost of its own exploration, not the optimistic that a max would produce.
Exercise 5.4 (L5)
Cliff-walking intuition made numeric. A state sits one cell above the cliff, with an action set of exactly 4 actions {up, down, left, right}. The greedy action is "right" (reward ), while "down" steps into the cliff (reward ). The agent runs -greedy with and uniform exploration over all 4 actions. Using and the continuation values for the right-branch and (episode ends after the fall), estimate the SARSA expected next-value contribution from the "right" and "down" branches only, and contrast it with the Q-learning target.
Recall Solution
First fix the action probabilities. Under -greedy with and 4 actions, the greedy action gets and every non-greedy action (including "down") gets Why we look at only two branches: "left" and "up" are also possible (each with probability ), but the exercise asks only for the contribution of the risky "down" versus the good "right", so we form a partial expected value from those two terms. This is an intuition estimate, not the full expectation — the remaining two actions would add their own small terms.
SARSA partial expected next-value (reward folded in, ):
- "right" branch value: , weighted by .
- "down" branch value: , weighted by . Q-learning target takes only the best branch, ignoring the slip risk entirely: Intuition: SARSA's estimate () is worse (more negative) than Q-learning's () because SARSA charges the state for the small-but-real chance of exploring "down" into the cliff. That extra pessimism makes SARSA down-value cliff-edge states and steer the learned policy onto the safer route — which is exactly why SARSA "walks the safe path."