How do the Q-learning and SARSA update rules differ, and which one is off-policy?
answer
- both move one action-value entry
- target policy versus behaviour policy
- one target maximises over next actions
- the name spells its own tuple
basics
~20 sQ-learning's target uses the maximum action value in the next state, so it learns the greedy policy while behaving some other way, which makes it off-policy. SARSA's target uses the action actually taken next, so it evaluates the exploring policy itself: on-policy.
solid answer
~50 sBoth keep a table of action values `Q(s,a)` and nudge one entry toward a one-step target; the difference is a single term. Q-learning's target is `r + gamma * max_a' Q(s',a')` — it bootstraps from the best action available in the next state, whatever the agent actually goes on to do. So it estimates the value of the greedy policy while following an exploratory one: target policy and behaviour policy differ, which is what off-policy means. SARSA's target is `r + gamma * Q(s',a')`, where `a'` is the action the behaviour policy actually selects next; the update needs the whole `s, a, r, s', a'` tuple, which is where the name comes from. It is on-policy — it evaluates and improves the very policy it is running, exploration included. If the behaviour policy is greedy, the two updates coincide.
code
python · 12 linesQ = {(1, "left"): 2.0, (1, "right"): -1.0} # action values in the next state
Q[(0, "right")] = 0.0 # the entry we are updating
alpha, gamma = 0.5, 0.9
s, a, r, s2 = 0, "right", -1.0, 1 # one observed transition
a2 = "right" # exploratory action actually taken next
greedy_next = max(Q[(s2, x)] for x in ("left", "right"))
q_learning = Q[(s, a)] + alpha * (r + gamma * greedy_next - Q[(s, a)])
sarsa = Q[(s, a)] + alpha * (r + gamma * Q[(s2, a2)] - Q[(s, a)])
print(round(q_learning, 3), round(sarsa, 3)) # 0.4 -0.95go deeper
Recall that both methods keep one value per state-action pair and move it toward reward plus a discounted next-state value. Be able to say which of the two uses a maximum over the next state's actions.
Write both targets from memory and point at the single differing term. An interviewer expects off-policy and on-policy defined as target policy versus behaviour policy, not by analogy, and expects you to handle the terminal-state case.
Explain what each method is actually estimating while an exploratory policy is running, and what that implies about which set of values you would trust when the agent is deployed.
Own the framing: should the team be learning the value of the policy it will actually run, or of an idealised greedy one it has never executed? Say what evidence would settle that for your system.
## The setting Both methods solve the same problem: **control** in a Markov decision process whose transition and reward model you do not have. The agent is in a state `s`, picks an action `a`, receives a reward `r`, and lands in `s'`. Learning happens in a **table** with one entry per state-action pair — this is the *tabular* case, where states and actions are finite and each `Q(s,a)` is stored and updated independently. The quantity being learned, `Q(s,a)`, is the **action value**: the expected discounted return from taking action `a` in state `s` and following a particular policy afterwards. `gamma` in `[0,1)` is the discount factor, which decides how much future reward counts. Once the table is good, acting is trivial: in state `s`, take `argmax_a Q(s,a)`. ## The two updates Both have the same shape — move the stored value a fraction `alpha` of the way toward a one-step target: ``` Q(s,a) <- Q(s,a) + alpha * ( target - Q(s,a) ) ``` Q-learning: ``` target = r + gamma * max over a' of Q(s', a') ``` SARSA: ``` target = r + gamma * Q(s', a') where a' is the action the agent actually takes next ``` At a terminal transition both targets collapse to just `r` — there is no next state to bootstrap from, and forgetting that is a classic implementation bug that quietly poisons the values near the goal. Equivalently, `Q <- (1 - alpha) * Q + alpha * target`: the new estimate is a blend of what you believed and what this one sample suggests. ## Why one is off-policy and the other on-policy Two policies are in play in any learning agent. The **behaviour policy** chooses the actions that generate the data — typically epsilon-greedy, taking the current best action most of the time and a random action with probability epsilon. The **target policy** is the one whose value you are estimating. - Q-learning's `max` operator hard-codes the target policy as *greedy*, regardless of what the behaviour policy did next. Behaviour and target differ, so it is **off-policy**. The payoff is real: you can behave cautiously — a deliberately conservative lane-change controller that only ever makes gentle merges — while the table underneath converges toward the value of the aggressive greedy policy you never actually ran. The behaviour policy only has to keep visiting every state-action pair; it does not have to be good. - SARSA's target contains the action its own policy actually selected, exploration and all. Behaviour and target are the same object, so it is **on-policy**. It answers "what is this epsilon-greedy agent worth?", and improving greedily with respect to those values improves that same agent. A useful consequence: if the behaviour policy is fully greedy, then `a' = argmax_a' Q(s',a')` on every step and the two updates are numerically identical. The algorithms only separate when the agent explores. ## What each converges to Under the usual tabular conditions — bounded rewards, every state-action pair visited infinitely often, and a step size that decays appropriately — Q-learning converges to `Q*`, the optimal action values, *without any requirement that the behaviour policy improve at all*. SARSA converges to the optimal values too, but only if its policy becomes greedy in the limit while still exploring infinitely often; with a fixed exploration rate it converges instead to the values of that fixed epsilon-greedy policy, which is a genuinely different fixed point. ## A near relative Expected SARSA replaces the sampled `Q(s',a')` with the expectation under the policy, `sum over a' of pi(a'|s') * Q(s',a')`. It removes the variance contributed by randomly drawing `a'` at the cost of a little more arithmetic per step, and if `pi` is greedy it reduces exactly to Q-learning — which shows the whole family is one design choice about which next-state value to bootstrap from. ## Confusions worth clearing "Off-policy means it does not explore" is wrong: Q-learning explores just as much, it simply refuses to let the exploratory action into its target. "SARSA is the safe one and Q-learning the good one" is a slogan, not a mechanism — the mechanism is which next-state value enters the bracket. And neither method needs a model of the environment; both learn purely from sampled transitions.
- Why can Q-learning learn an aggressive greedy policy while the car is driven by a deliberately cautious controller?Because the `max` in the target never asks what the cautious controller did next — it asks what the best action in the next state is worth. The cautious policy only has to supply data covering every state-action pair; the values it produces describe the greedy policy instead. That is the practical value of off-policy learning: safe or logged behaviour, ambitious target.
- When are the SARSA and Q-learning updates numerically identical?Whenever the action actually taken next happens to be the maximising one. With a fully greedy behaviour policy that is true on every step, so the two algorithms coincide exactly. They only diverge in proportion to how often exploration picks a non-greedy action, which is why the exploration rate controls the size of the gap.
- What does Expected SARSA change relative to these two?It bootstraps from `sum over a' of pi(a'|s') * Q(s',a')` — the policy-weighted average of next-state action values — instead of a single sampled `Q(s',a')`. That removes the variance introduced by randomly drawing the next action, at slightly more computation per step. With a greedy `pi`, the expectation is just the maximum, so it reduces to Q-learning.
SARSA grades the driver you actually are, jittery habits included. Q-learning grades the driver you intend to become, from the same drives.
saying these in an interview costs you the question
- Says SARSA and Q-learning differ only in convergence speed
- Claims off-policy means the agent does not explore
- Puts a maximum over next actions into the SARSA target
- Thinks either method needs a known transition model
- Defines on-policy as learning online and off-policy as learning offline