Type at least 2 letters. Enter opens the first result, Esc closes.

    Reinforcement Learning · Lesson 5 · Learning from experience

    TD control: SARSA, Q-learning and the maximization bias

    Intermediate 70 min 3 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 02 (Bellman optimality operator, policy improvement, generalized policy iteration), lesson 03 (ε-greedy exploration, incremental means), lesson 04 (TD(0) and the TD error); Jensen's inequality (information theory 03). Time: about 70 minutes, plus the notebook.


    Why this matters #

    Q-learning is the simplest RL method that learns to act from experience alone, and it is the base of DQN, the network that learned to play Atari games from pixels (lesson 06). The difference between on-policy and off-policy learning, which you first meet here, decides which data an algorithm may use: off-policy methods can learn from old data, from other agents or from logs, on-policy methods cannot without corrections. The maximization bias explains why every modern value-based method has some "double" trick: double DQN, and the clipped double Q-learning of TD3 and SAC (lesson 08). It also explains why Q-learning on a fixed dataset (offline RL) overestimates so badly.

    1. Predict first #

    2. The picture #

    From prediction to control: generalized policy iteration with QQ #

    Lesson 02 showed generalized policy iteration (GPI): evaluate the policy a little, improve it a little, and repeat. Two changes make GPI work without a model.

    1. Learn QQ, not VV. Greedy improvement with VV needs the model: π′(s)=arg⁡max⁡a[R(s,a)+γ∑s′P(s′∣s,a)V(s′)]\pi'(s) = \arg\max_a \big[R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) V(s')\big]. With action values, the improvement step is just π′(s)=arg⁡max⁡aQ(s,a)\pi'(s) = \arg\max_a Q(s, a). No PP, no RR.

    2. Keep exploring. A greedy policy never tries the actions it thinks are worse, so it never learns that they may be better. So we act ε-greedily, as in the bandits of lesson 03: with probability 1−ε1 - \varepsilon take a greedy action, otherwise a uniformly random action. With ∣A∣|\mathcal A| actions and one greedy action,

    π(a∣s)={1−ε+ε∣A∣if a=arg⁡max⁡bQ(s,b),ε∣A∣otherwise.\pi(a \mid s) = \begin{cases} 1 - \varepsilon + \dfrac{\varepsilon}{|\mathcal A|} & \text{if } a = \arg\max_b Q(s, b), \\[2mm] \dfrac{\varepsilon}{|\mathcal A|} & \text{otherwise.} \end{cases}

    The greedy action also gets its share of the random choice. With 4 actions and ε=0.1\varepsilon = 0.1, the greedy action has probability 0.9250.925, not 0.90.9; each other action has 0.0250.025. A policy with π(a∣s)≥ε/∣A∣\pi(a \mid s) \ge \varepsilon / |\mathcal A| for every action is called ε-soft.

    So the loop of every method in this lesson is: in state ss, pick aa ε-greedily from QQ; observe rr and s′s'; move Q(s,a)Q(s, a) towards a one-step target yy:

    Q(s,a)←Q(s,a)+α [y−Q(s,a)].Q(s, a) \leftarrow Q(s, a) + \alpha\,\big[y - Q(s, a)\big].

    The policy improves by itself: it is always ε-greedy with respect to the newest QQ.

    Three targets for one transition #

    The three methods use the same reward rr and differ only in how they value s′s':

    • SARSA: the action a′a' the agent really takes next: y=r+γ Q(s′,a′)y = r + \gamma\,Q(s', a'). The name lists the five things the update uses: s,a,r,s′,a′s, a, r, s', a'.
    • Expected SARSA: the average over the agent's own policy: y=r+γ∑a′π(a′∣s′)Q(s′,a′)y = r + \gamma \sum_{a'} \pi(a' \mid s') Q(s', a').
    • Q-learning: the best action, whatever the agent does next: y=r+γmax⁡a′Q(s′,a′)y = r + \gamma \max_{a'} Q(s', a').

    When the episode ends at s′s', all three targets are just y=ry = r (the value after the end is 0).

    sars′a′SARSAsars′average with π(a′|s′)expected SARSAsars′max over a′Q-learning
    Backup diagrams of the three targets. Read each one from the top: the state ss, the action aa the agent took (orange), the reward rr (aqua) and the next state s′s'. The violet part is what the target uses to value s′s': one sampled next action (SARSA), the π-weighted average of all next actions (expected SARSA), or the best next action, marked by the arc (Q-learning).
    Interactive figure: loading…

    Switch the next action a′a' and move the sliders. The blue bars are Q(s′,a1)Q(s', a_1) and Q(s′,a2)Q(s', a_2), with their ε-greedy probabilities in orange. The three targets are SARSA, expected SARSA and Q-learning. Only the SARSA target changes when you switch a′a'. Press "Sample a′ 200 times": the dashed line, the mean of many SARSA targets, lands on the expected SARSA target. Set ε=0\varepsilon = 0 and expected SARSA becomes Q-learning.

    Cliff walking: careful or bold? #

    Cliff walking (Sutton and Barto, Example 6.6) is a 4×124 \times 12 grid. The agent starts at S (bottom left) and must reach G (bottom right). The ten cells between them are a cliff. Every step gives −1-1; a step into the cliff gives −100-100 and sends the agent back to S, and the episode goes on. There is no discounting (γ=1\gamma = 1). The shortest path walks along the edge of the cliff: 13 steps, return −13-13.

    Interactive figure: loading…

    Predict the two paths first, then press "Train 500 episodes". The grid shows run 1 of the selected method: the shading is max⁡aQ(s,a)\max_a Q(s, a) (blue, darker is higher), the small arrow is the greedy action, and the orange line is the greedy path from S. The chart shows the online return, the total reward per episode while the agent explores (10 runs, smoothed). Compare Q-learning, SARSA and expected SARSA: which one has the shortest greedy path, and which one collects the most reward? Then set ε=0\varepsilon = 0 and train again.

    What you see (and what Sutton and Barto report):

    • Q-learning learns Q∗Q^*. Its greedy path walks along the edge: 13 steps.
    • SARSA and expected SARSA learn the values of the ε-greedy policy they follow. Next to the cliff, a random step "down" (probability ε/4=0.025\varepsilon / 4 = 0.025 in each step) costs −100-100. So states next to the edge get low values, and the greedy path keeps a distance from the cliff.
    • Online return: SARSA and expected SARSA collect more reward per episode (about −25-25 and −21-21 in the widget, episodes 400–500) than Q-learning (about −52-52), because Q-learning's random steps on the edge often fall into the cliff.
    • With ε=0\varepsilon = 0 the three methods are the same (the ε-greedy policy is greedy), and all learn the edge path here, because the initial values Q=0Q = 0 are optimistic and drive exploration.
    Quick check

    The agent explores with ε=0.1\varepsilon = 0.1 on cliff walking. Which statement is true after training?

    3. Definitions and main results #

    On-policy and off-policy #

    Two policies play a role in learning. The behaviour policy πb\pi_b chooses the actions that produce the data. The target policy π\pi is the policy whose values we learn.

    • A method is on-policy if the target policy is the behaviour policy: it evaluates and improves the same policy that acts. SARSA is on-policy: its target uses a′∼πba' \sim \pi_b, so it learns QπbQ^{\pi_b} of the ε-greedy policy.
    • A method is off-policy if the target policy differs from the behaviour policy. Q-learning is off-policy: the data come from the ε-greedy policy, but the target uses the greedy action, so it learns Q∗Q^*.

    Expected SARSA can be either. With π=πb\pi = \pi_b in its sum it is on-policy. With a different target policy it is off-policy; with the greedy target policy, ∑a′π(a′∣s′)Q(s′,a′)=max⁡a′Q(s′,a′)\sum_{a'} \pi(a' \mid s') Q(s', a') = \max_{a'} Q(s', a'), and expected SARSA is Q-learning.

    Why does Q-learning need no importance sampling, unlike off-policy Monte Carlo (lesson 04)? Its target uses only (s,a,r,s′)(s, a, r, s'). The action aa is given (we learn Q(s,a)Q(s, a) for that action), and r,s′r, s' come from the environment, not from the policy. The next action a′a' is not sampled at all: the max replaces it. So the target has the same distribution whatever policy chose aa.

    Quick check

    To make one update, which of these does Q-learning need from the agent's experience?

    The three updates in one table #

    All three use Q(s,a)←Q(s,a)+α [y−Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha\,[y - Q(s, a)], with y=ry = r when the episode ends at s′s':

    method target yy needs the next action a′a'? learns
    SARSA r+γ Q(s′,a′)r + \gamma\,Q(s', a'), a′a' = the next action taken yes QπbQ^{\pi_b} (on-policy)
    expected SARSA r+γ∑a′π(a′∣s′) Q(s′,a′)r + \gamma \sum_{a'} \pi(a' \mid s')\,Q(s', a') no QπQ^{\pi}; less variance than SARSA
    Q-learning r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s', a') no Q∗Q^* (off-policy)

    Each target is one sample of a Bellman operator. Recall the operators of lesson 02, written for QQ:

    (TπQ)(s,a)=R(s,a)+γ∑s′P(s′∣s,a)∑a′π(a′∣s′)Q(s′,a′),(T^\pi Q)(s, a) = R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) \sum_{a'} \pi(a' \mid s') Q(s', a'),
    (TQ)(s,a)=R(s,a)+γ∑s′P(s′∣s,a)max⁡a′Q(s′,a′).(T Q)(s, a) = R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) \max_{a'} Q(s', a').

    For a fixed table QQ, the Q-learning target r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s', a') has expectation (TQ)(s,a)(TQ)(s, a): only rr and s′s' are random. So Q-learning is sampled value iteration. The SARSA target and the expected SARSA target both have expectation (TπQ)(s,a)(T^\pi Q)(s, a): they are sampled policy evaluation. Expected SARSA removes one source of noise, the random choice of a′a', so its target has lower variance. This is why it can use larger step sizes (on cliff walking it works even with α=1\alpha = 1).

    Quick check

    For one transition, r=1r = 1, γ=0.9\gamma = 0.9, and in s′s' the two actions have Q(s′,⋅)=(2,4)Q(s', \cdot) = (2, 4). The policy is ε-greedy with ε=0.2\varepsilon = 0.2, and the agent's next action is a′=a1a' = a_1. What are the SARSA, expected SARSA and Q-learning targets?

    ε-greedy improvement and GLIE #

    Is acting ε-greedily with respect to QπQ^\pi really an improvement? Yes, among ε-soft policies.

    Theorem (ε-greedy policy improvement; Sutton and Barto §5.4). Let π\pi be ε-soft, and let π′\pi' be ε-greedy with respect to QπQ^\pi. Then Vπ′(s)≥Vπ(s)V^{\pi'}(s) \ge V^\pi(s) for every state ss.

    Proof (one line of algebra, then lesson 02)
    1. By the policy improvement theorem of lesson 02, it is enough to show ∑aπ′(a∣s) Qπ(s,a)≥Vπ(s)\sum_a \pi'(a \mid s)\,Q^\pi(s, a) \ge V^\pi(s) for every ss.
    2. Write m=∣A∣m = |\mathcal A|. By the definition of π′\pi', ∑aπ′(a∣s)Qπ(s,a)=εm∑aQπ(s,a)+(1−ε)max⁡aQπ(s,a)\sum_a \pi'(a \mid s) Q^\pi(s, a) = \frac{\varepsilon}{m} \sum_a Q^\pi(s, a) + (1 - \varepsilon) \max_a Q^\pi(s, a).
    3. The weights wa=(π(a∣s)−εm)/(1−ε)w_a = \big(\pi(a \mid s) - \frac{\varepsilon}{m}\big)/(1 - \varepsilon) are ≥0\ge 0 (because π\pi is ε-soft) and sum to (1−ε)/(1−ε)=1\big(1 - \varepsilon\big)/(1 - \varepsilon) = 1. A maximum is at least any weighted average, so max⁡aQπ(s,a)≥∑awaQπ(s,a)\max_a Q^\pi(s, a) \ge \sum_a w_a Q^\pi(s, a).
    4. Put step 3 into step 2: ∑aπ′(a∣s)Qπ(s,a)≥εm∑aQπ(s,a)+∑a(π(a∣s)−εm)Qπ(s,a)=∑aπ(a∣s)Qπ(s,a)=Vπ(s)\sum_a \pi'(a \mid s) Q^\pi(s, a) \ge \frac{\varepsilon}{m} \sum_a Q^\pi(s, a) + \sum_a \big(\pi(a \mid s) - \frac{\varepsilon}{m}\big) Q^\pi(s, a) = \sum_a \pi(a \mid s) Q^\pi(s, a) = V^\pi(s). ■\blacksquare

    So GPI with ε-greedy policies converges to the best ε-soft policy, not to the optimal policy. To reach π∗\pi^*, exploration must fade out, but not too fast. A schedule is GLIE (greedy in the limit with infinite exploration) if

    1. every state–action pair is visited infinitely often, and
    2. the policy becomes greedy in the limit, πk(a∣s)→1\pi_k(a \mid s) \to 1 for a=arg⁡max⁡bQk(s,b)a = \arg\max_b Q_k(s, b).

    For example, ε-greedy with εk=1/k\varepsilon_k = 1/k in episode kk is GLIE. Under GLIE and suitable step sizes, tabular SARSA converges to Q∗Q^* with probability 1 (Singh, Jaakkola, Littman and Szepesvári, 2000).

    Convergence of Q-learning #

    Theorem (Watkins and Dayan, 1992). Take a finite MDP with bounded rewards and γ<1\gamma < 1. Suppose that every pair (s,a)(s, a) is updated infinitely often, and that the step sizes αt(s,a)\alpha_t(s, a) of each pair satisfy the Robbins–Monro conditions

    ∑tαt(s,a)=∞,∑tαt(s,a)2<∞.\sum_t \alpha_t(s, a) = \infty, \qquad \sum_t \alpha_t(s, a)^2 < \infty .

    Then tabular Q-learning converges to Q∗Q^* with probability 1.

    The behaviour policy does not appear in the theorem, apart from visiting every pair: this is what off-policy means. The first condition lets the steps add up to any distance; the second makes the noise die out. For example, αt=1/nt(s,a)\alpha_t = 1/n_t(s, a), where ntn_t counts the updates of the pair, satisfies both. A constant α\alpha violates the second condition: then QQ keeps moving around Q∗Q^* but stays close when α\alpha is small. The proof writes Q-learning as a noisy version of Q←TQQ \leftarrow TQ and uses the contraction of TT from lesson 02 (stochastic approximation; see Jaakkola, Jordan and Singh, 1994).

    Theorem: the maximization bias #

    Q-learning's target takes a max over estimates that contain noise. This creates a systematic error.

    Theorem (maximization bias). Let X^1,…,X^k\hat X_1, \dots, \hat X_k be unbiased estimates of μ1,…,μk\mu_1, \dots, \mu_k, that is E[X^i]=μi\mathbb E[\hat X_i] = \mu_i. Then

    E[max⁡iX^i]≥max⁡iμi,\mathbb E\big[\max_i \hat X_i\big] \ge \max_i \mu_i ,

    and the inequality is usually strict.

    Proof.

    1. For every jj: max⁡iX^i≥X^j\max_i \hat X_i \ge \hat X_j.
    2. Take expectations: E[max⁡iX^i]≥E[X^j]=μj\mathbb E[\max_i \hat X_i] \ge \mathbb E[\hat X_j] = \mu_j, for every jj.
    3. The left side does not depend on jj, so it is also ≥max⁡jμj\ge \max_j \mu_j. ■\blacksquare

    The same result follows from Jensen's inequality, because x↦max⁡ixix \mapsto \max_i x_i is convex (information theory 03). Why is it usually strict? Equality needs max⁡iX^i=X^j∗\max_i \hat X_i = \hat X_{j^*} almost surely for a best j∗j^*. With noise, some other estimate is often the largest, and then the max is above X^j∗\hat X_{j^*}.

    In Q-learning. max⁡a′Q(s′,a′)\max_{a'} Q(s', a') is a max of noisy estimates, so the target is too high on average, and bootstrapping carries the error to earlier states. When all actions are equally good, the bias is largest; when one action is much better than the rest, the max almost always picks it and the bias is small.

    Interactive figure: loading…

    Predict first, then press "Repeat 1000 times". Ten actions all have true value 0. Each estimate is the mean of n=10n = 10 samples of N(0,1)N(0, 1). The orange histogram is the single estimator max⁡iX^i\max_i \hat X_i: its mean is about +0.49+0.49, not 0. The violet histogram is the double estimator: choose i∗i^* with the blue set, read the value from the independent violet set. Its mean is 0. Make K=1K = 1 (no choice, no bias) and make nn large (less noise, less bias).

    The double estimator and double Q-learning #

    The bias comes from using the same noise to choose the maximiser and to evaluate it: the max picks the estimate whose noise happens to be large. The fix is to separate the two jobs.

    Double estimator (van Hasselt, 2010). Use two independent sets of unbiased estimates X^\hat X and Y^\hat Y. Choose i∗=arg⁡max⁡iX^ii^* = \arg\max_i \hat X_i with the first and evaluate it with the second: return Y^i∗\hat Y_{i^*}.

    Why it is not biased upward. i∗i^* depends only on X^\hat X, and Y^\hat Y is independent of X^\hat X. So, given i∗i^*, the expectation of Y^i∗\hat Y_{i^*} is μi∗\mu_{i^*}, and

    E[Y^i∗]=E[μi∗]≤max⁡iμi.\mathbb E[\hat Y_{i^*}] = \mathbb E[\mu_{i^*}] \le \max_i \mu_i .

    It can be biased downward (when X^\hat X picks a worse action), but never upward.

    Double Q-learning keeps two tables QAQ_A and QBQ_B. At each step, with probability 1/21/2 update QAQ_A:

    a∗=arg⁡max⁡a′QA(s′,a′),y=r+γ QB(s′,a∗),a^* = \arg\max_{a'} Q_A(s', a'), \qquad y = r + \gamma\, Q_B(s', a^*),
    QA(s,a)←QA(s,a)+α [y−QA(s,a)],Q_A(s, a) \leftarrow Q_A(s, a) + \alpha\,\big[y - Q_A(s, a)\big],

    otherwise update QBQ_B with the roles of AA and BB swapped. The agent acts ε-greedily with respect to QA+QBQ_A + Q_B. Each sample updates only one table, so the two tables stay (roughly) independent. Double DQN (lesson 06) applies the same idea with the online and target networks.

    Sutton and Barto, Example 6.7. From state A, "right" ends the episode with reward 0; "left" goes to state B with reward 0. From B, each of 10 actions ends the episode with a reward drawn from N(−0.1,1)N(-0.1, 1). So "left" is worse on average (−0.1<0-0.1 < 0). With ε=0.1\varepsilon = 0.1 the best possible frequency of "left" is ε/2=5%\varepsilon / 2 = 5\%. Q-learning chooses "left" in most early episodes (over 80% around episode 10 in our runs), because max⁡bQ(B,b)\max_b Q(B, b) is overestimated; double Q-learning comes close to 5%. The notebook reproduces this.

    Quick check

    X^1,…,X^5\hat X_1, \dots, \hat X_5 are independent unbiased estimates of five action values that are all equal to 22. What can you say about E[max⁡iX^i]−2\mathbb E[\max_i \hat X_i] - 2?

    Answers to section 1. (1) For SARSA, yes: it learns the values of the policy it follows, random steps included, and so it learns to keep away from the cliff. Q-learning's values ignore its own exploration. Which is right depends on the goal: if the agent will keep exploring (or a robot will keep slipping), SARSA's values are the honest ones. (2) Clearly above 0: about +0.49+0.49 (section 4).

    4. Worked example #

    (a) One transition, three targets. Q(s,a)=0Q(s, a) = 0, r=1r = 1, γ=0.9\gamma = 0.9, α=0.1\alpha = 0.1. In s′s' there are two actions with Q(s′,⋅)=(1.0,3.0)Q(s', \cdot) = (1.0, 3.0), and ε=0.2\varepsilon = 0.2.

    1. ε-greedy probabilities in s′s': action 2 is greedy, so π(a2∣s′)=1−0.2+0.2/2=0.9\pi(a_2 \mid s') = 1 - 0.2 + 0.2/2 = 0.9 and π(a1∣s′)=0.2/2=0.1\pi(a_1 \mid s') = 0.2/2 = 0.1.
    2. The agent's next action is the exploratory one, a′=a1a' = a_1 (value 1.0).
    3. SARSA: y=1+0.9⋅1.0=1.9y = 1 + 0.9 \cdot 1.0 = 1.9, so Q(s,a)←0+0.1 (1.9−0)=0.19Q(s, a) \leftarrow 0 + 0.1\,(1.9 - 0) = 0.19.
    4. Q-learning: y=1+0.9⋅max⁡(1.0,3.0)=1+2.7=3.7y = 1 + 0.9 \cdot \max(1.0, 3.0) = 1 + 2.7 = 3.7, so Q(s,a)←0.37Q(s, a) \leftarrow 0.37.
    5. Expected SARSA: ∑a′π(a′∣s′)Q(s′,a′)=0.1⋅1.0+0.9⋅3.0=2.8\sum_{a'} \pi(a' \mid s') Q(s', a') = 0.1 \cdot 1.0 + 0.9 \cdot 3.0 = 2.8, so y=1+0.9⋅2.8=3.52y = 1 + 0.9 \cdot 2.8 = 3.52 and Q(s,a)←0.352Q(s, a) \leftarrow 0.352.

    SARSA is pulled down by the exploratory action; Q-learning ignores it; expected SARSA uses its probability instead of the sample. If the agent had picked a′=a2a' = a_2, SARSA would give y=3.7y = 3.7, the same as Q-learning. Averaged over a′a', SARSA's target is 0.1⋅1.9+0.9⋅3.7=3.520.1 \cdot 1.9 + 0.9 \cdot 3.7 = 3.52: exactly the expected SARSA target.

    (b) Why Q-learning loses reward on the cliff. On the edge path, the agent spends 11 steps in cells where one action leads into the cliff (S and the ten cells above the cliff). In each of them, the random action is "the wrong one" with probability ε/4=0.025\varepsilon/4 = 0.025. So the chance to fall at least once in one pass is

    1−(1−0.025)11=1−0.97511≈0.243,1 - (1 - 0.025)^{11} = 1 - 0.975^{11} \approx 0.243 ,

    and each fall costs −100-100 and a restart. Policy evaluation on the grid gives exact numbers: the ε-greedy policy around Q∗Q^* (what Q-learning's agent does in the long run) has expected return −45.8-45.8 per episode; the best ε-greedy policy, which keeps two rows away from the cliff (15 steps), has −20.7-20.7. This is the policy that SARSA's values aim for; expected SARSA finds it in our runs, and SARSA with α=0.5\alpha = 0.5 often takes the top row (17 steps).

    (c) The size of the maximization bias. Ten actions, all with true value 0. Each estimate is the mean of 10 samples from N(0,1)N(0, 1), so it is N(0,1/10)N(0, 1/10), with standard deviation 1/10=0.3161/\sqrt{10} = 0.316.

    1. The expected maximum of 10 independent standard normals is E[max⁡iZi]=∫x⋅10 φ(x) Φ(x)9 dx=1.5388\mathbb E[\max_i Z_i] = \int x \cdot 10\,\varphi(x)\,\Phi(x)^9\,dx = 1.5388 (the density of the max of 10 is 10 φ Φ910\,\varphi\,\Phi^9).
    2. Scale by the standard deviation: E[max⁡iX^i]=1.5388/10=0.487\mathbb E[\max_i \hat X_i] = 1.5388 / \sqrt{10} = 0.487.

    Every true value is 0, but the max is 0.490.49 on average. The double estimator gives 0 on average.

    5. In code #

    The cells below check the worked example. The notebook adds the full SARSA and Q-learning agents on cliff walking and double Q-learning on Example 6.7.

    import numpy as np
    from scipy import integrate, stats
    
    def epsilon_greedy_probs(q, eps):
        p = np.full(len(q), eps / len(q))
        best = np.flatnonzero(q == q.max())
        p[best] += (1 - eps) / len(best)            # ties share the greedy mass
        return p
    
    # (a) one transition
    r, gamma, alpha, q_next, eps = 1.0, 0.9, 0.1, np.array([1.0, 3.0]), 0.2
    pi = epsilon_greedy_probs(q_next, eps)
    print(pi)                                        # [0.1 0.9]
    y_sarsa = r + gamma * q_next[0]                  # a' = a_1, the exploratory action
    y_exp = r + gamma * pi @ q_next
    y_q = r + gamma * q_next.max()
    print(alpha * np.array([y_sarsa, y_exp, y_q]))   # [0.19  0.352 0.37 ]
    
    # (b) chance to fall at least once on the edge path
    print(1 - 0.975 ** 11)                           # 0.2431
    
    # (c) maximization bias: E[max of k standard normals] = ∫ x k φ(x) Φ(x)^(k-1) dx
    k = 10
    e_max = integrate.quad(lambda x: x * k * stats.norm.pdf(x) * stats.norm.cdf(x) ** (k - 1), -np.inf, np.inf)[0]
    print(e_max, e_max / np.sqrt(10))                # 1.5388 0.4866
    
    rng = np.random.default_rng(0)
    A = rng.normal(0, 1, (100_000, 10, 10)).mean(axis=2)   # 100k repetitions, 10 actions, 10 samples each
    B = rng.normal(0, 1, (100_000, 10, 10)).mean(axis=2)   # an independent second set
    single = A.max(axis=1)
    double = B[np.arange(len(B)), A.argmax(axis=1)]
    print(single.mean().round(3), double.mean().round(3))  # 0.487 0.0 (up to Monte Carlo noise)
    

    6. Where it shows up #

    • Deep Q-learning. DQN is Q-learning with a neural network, a replay buffer and a target network; double DQN uses the double estimator (lesson 06).
    • Actor-critic methods for continuous control. TD3 and SAC train two critics and use the smaller of the two in the target ("clipped double Q"), a direct answer to the maximization bias (lesson 08).
    • Learning from logs. Because it is off-policy, Q-learning can learn from data collected by another policy: recommendation, ad placement, resource allocation. On a fixed dataset the max picks actions the data never tried, the extreme case of the maximization bias (offline RL, lesson 06).
    • Safe exploration. SARSA's caution is useful when exploration is costly during learning: a robot or a control system that must not fall while it learns. Expected SARSA is a common default for tabular problems.
    • ML for science. Value-based RL is used for sequential experimental design and for the control of physical systems, where each step is an experiment or a control action. There the reward is often a noisy measurement, so the maximization bias makes the learned values too optimistic; double estimators or an evaluation on fresh data give honest numbers.

    7. Common mistakes #

    • Wrong a′a' in SARSA. It must be the action you actually take next, and you must then execute that same action. Choosing a fresh action for the next step makes it a different algorithm.
    • Bootstrapping at the end. At a terminal transition the target is rr. A target r+γmax⁡a′Q(s′,⋅)r + \gamma \max_{a'} Q(s', \cdot) with a terminal s′s' adds a value that should be 0.
    • Wrong ε-greedy probabilities. The greedy action has 1−ε+ε/∣A∣1 - \varepsilon + \varepsilon/|\mathcal A|, not 1−ε1 - \varepsilon. With ties, the greedy mass is shared among the tied actions.
    • Mixing up greedy path and online return. Q-learning's greedy path is better on cliff walking, but its online return is worse. Say which one you mean.
    • "Q-learning needs importance sampling because it is off-policy." One-step Q-learning does not: its target does not depend on which policy chose aa. (Multi-step off-policy targets do need corrections.)
    • Updating both tables with the same sample in double Q-learning. Then they are no longer independent and the bias comes back. Update one table per sample, chosen at random.
    • Mixing reward conventions. Here rtr_t follows (st,at)(s_t, a_t); Sutton and Barto write Rt+1R_{t+1}. The updates are the same.

    8. Check yourself #

    1. Why does control without a model learn QQ and not VV?
      AnswerThe greedy step with VV needs the model: arg⁡max⁡a[R(s,a)+γ∑s′P(s′∣s,a)V(s′)]\arg\max_a [R(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V(s')]. With QQ the greedy step is arg⁡max⁡aQ(s,a)\arg\max_a Q(s, a), with no PP and no RR.
    2. With 5 actions and ε=0.2\varepsilon = 0.2, what are the ε-greedy probabilities? And with two tied greedy actions?
      AnswerOne greedy action: 1−0.2+0.04=0.841 - 0.2 + 0.04 = 0.84 for it and 0.040.04 for each other action. Two tied greedy actions: each gets 0.04+0.8/2=0.440.04 + 0.8/2 = 0.44, the other three get 0.040.04 each (sum 0.88+0.12=10.88 + 0.12 = 1).
    3. Why is Q-learning off-policy and SARSA on-policy?
      AnswerQ-learning's target uses max⁡a′Q(s′,a′)\max_{a'} Q(s', a'), the value of the greedy policy, whatever the behaviour policy does, so it learns Q∗Q^* from ε-greedy data. SARSA's target uses the action the behaviour policy really takes, so it learns the values of that policy.
    4. Expected SARSA uses the greedy policy as its target policy. What algorithm is it?
      AnswerQ-learning: under the greedy policy, ∑a′π(a′∣s′)Q(s′,a′)=max⁡a′Q(s′,a′)\sum_{a'} \pi(a' \mid s') Q(s', a') = \max_{a'} Q(s', a').
    5. Does αt=1/n\alpha_t = 1/\sqrt{n} satisfy the Robbins–Monro conditions? And αt=1/n2\alpha_t = 1/n^2?
      AnswerNeither. For 1/n1/\sqrt n the first condition holds (∑1/n=∞\sum 1/\sqrt n = \infty), but ∑(1/n)2=∑1/n=∞\sum (1/\sqrt n)^2 = \sum 1/n = \infty, so the second fails: the noise never dies out. For 1/n21/n^2, ∑1/n2<∞\sum 1/n^2 < \infty, so the first fails: the steps cannot add up to any distance. α=1/np\alpha = 1/n^p satisfies both for 1/2<p≤11/2 < p \le 1.
    6. Show in one line that E[max⁡iX^i]≥max⁡iμi\mathbb E[\max_i \hat X_i] \ge \max_i \mu_i, and say why the double estimator is not biased upward.
      Answermax⁡iX^i≥X^j\max_i \hat X_i \ge \hat X_j for every jj; take expectations, then the max over jj. The double estimator chooses i∗i^* with X^\hat X and evaluates with an independent Y^\hat Y, so E[Y^i∗]=E[μi∗]≤max⁡iμi\mathbb E[\hat Y_{i^*}] = \mathbb E[\mu_{i^*}] \le \max_i \mu_i.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Write the SARSA, expected SARSA and Q-learning targets.

    2. What is the difference between an on-policy and an off-policy method?

    3. What probability does ε-greedy give to the greedy action, with ∣A∣|\mathcal A| actions and no ties?

    4. Compute the expected SARSA target for r=1r = 1, γ=0.9\gamma = 0.9, Q(s′,⋅)=(1,3)Q(s', \cdot) = (1, 3), ε=0.2\varepsilon = 0.2.

    5. Under which conditions does tabular Q-learning converge to Q∗Q^*?

    6. Why does Q-learning have a worse online return than SARSA on cliff walking?

    7. State the maximization-bias inequality.

    8. Why is the double estimator not biased upward?

    9. What is GLIE?

    10. Ten actions all have true value 0; each estimate is N(0,1/10)N(0, 1/10). About how large is the expected max?

    Open the review →

    9. Interview angle #

    • "SARSA vs Q-learning?" On-policy vs off-policy target; SARSA learns the values of the exploring policy, Q-learning learns Q∗Q^*. Tell the cliff-walking story: Q-learning's greedy path is optimal, SARSA's online return is better.
    • "Why is Q-learning off-policy, and why does it not need importance sampling?" The target uses the max, not the behaviour action; aa is conditioned on and r,s′r, s' come from the environment.
    • "Where does overestimation in Q-learning come from, and how do you fix it?" The max over noisy estimates (Jensen), carried backwards by bootstrapping. Double Q-learning, double DQN, clipped double Q.
    • "When does tabular Q-learning converge?" Every pair visited infinitely often and Robbins–Monro step sizes; it is stochastic approximation of the contraction TT.
    • "Why does expected SARSA have lower variance than SARSA?" It averages over a′a' exactly instead of sampling it; both have the same expected target.

    10. Practice #

    The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:

    1. epsilon_greedy_probs: the exact action distribution of ε-greedy, with ties; compare with sampled frequencies.
    2. q_learning and sarsa on cliff walking: reproduce the greedy paths and the online-return plot.
    3. expected_sarsa: check that its greedy path is safe and its online return is the best of the three.
    4. max_estimates: single and double estimators by simulation; the single one averages +0.49+0.49, the double one 0.
    5. double_q_learning on Example 6.7: the fraction of "left" actions with Q-learning and with double Q-learning.

    11. Watch and read #

    GPI with Q, ε-greedy improvement and GLIE, SARSA and Q-learning, with the cliff-walking example.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A visual walk from TD prediction to SARSA and Q-learning; watch the part on on-policy vs off-policy targets.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Q-learning as sampled value iteration, explained on the board, with links to control engineering.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A more recent lecture by the author of double Q-learning; the overestimation part explains the double estimator.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play