def eps_greedy(q, s):
"""The eps-greedy policy of @def-eps-greedy at s, for the two actions here."""
g = int(q[s, 1] > q[s, 0])
return 1 - g if RNG.random() < EPS / 2 else g3 Temporal Difference Learning, Sarsa, and Q-learning
\[ \newcommand{\E}{\mathbb{E}} \newcommand{\R}{\mathbb{R}} \newcommand{\Prob}{\mathbb{P}} \newcommand{\BR}{\operatorname{BR}} \newcommand{\eps}{\varepsilon} \newcommand{\given}{\,\vert\,} \newcommand{\argmax}{\operatorname*{arg\,max}} \newcommand{\argmin}{\operatorname*{arg\,min}} \newcommand{\sm}{\setminus} \newcommand{\defeq}{\equiv} \]
Ben Moll’s RL lecture slides, Lecture 3.
3.1 Incremental versus episodic learning
Chapter 2’s policy evaluation step was Equation 2.7, the average return over \(N\) episodes. Written as a running average in the manner of Equation 2.8, the \(j\)-th update is
\[ q_{\pi_{k}}^{j+1}(s,a)=q_{\pi_{k}}^{j}(s,a)+\frac{1}{j}\left[\sum_{t=0}^{T}\gamma^{t}r(s_{t}^{j},a_{t}^{j})-q_{\pi_{k}}^{j}(s,a)\right]. \tag{3.1}\]
The return is available after \(T\). Incremental updates have the form
\[ q_{t+1}(s_{t},a_{t})=q_{t}(s_{t},a_{t})+\alpha_{t}\times(\text{something available at time } t), \tag{3.2}\]
where \(q_{t}\) is the time-\(t\) estimate of \(q_{\pi_{k}}\).
Definition 3.1 (Incremental and episodic learning) An algorithm is incremental if it updates in real time, at every transition, as in Equation 3.2. It is episodic (or non-incremental) if it must wait for an episode to finish, as in Equation 3.1.
Sutton and Barto call this same distinction online versus offline. Here we use incremental and episodic because “offline RL” has since come to mean something else entirely: learning from a fixed, previously collected dataset with no further interaction.
3.2 TD learning of state values
Estimate \(v_{\pi}\) for a fixed policy \(\pi\), with no improvement step. Write \(v_{t}(s)\) for the time-\(t\) estimate of \(v_{\pi}(s)\).
Let \(\mathcal H_t\) contain the history through \(s_t\), before drawing the current action under \(\pi\) and the next transition. Use \(\mathcal H_t\)-measurable effective step sizes: \(\alpha_t(s)=0\) for \(s\ne s_t\) and \(0<\alpha_t(s_t)\le1\). All step-size sums below use this convention.
Given experience \(\{(s_{t},r_{t+1},s_{t+1})\}_{t}\) generated by following \(\pi\), at each \(t=0,1,2,\dots\)
\[ v_{t+1}(s_{t})=v_{t}(s_{t})+\alpha_{t}(s_{t})\big[\,r_{t+1}+\gamma v_{t}(s_{t+1})-v_{t}(s_{t})\,\big], \tag{3.3}\]
\[ v_{t+1}(s)=v_{t}(s)\qquad\text{for all } s\neq s_{t}. \tag{3.4}\]
Annotating Equation 3.3 names its parts:
\[ \underbrace{v_{t+1}(s_{t})}_{\text{new estimate}} =\underbrace{v_{t}(s_{t})}_{\text{current estimate}} +\alpha_{t}(s_{t})\Big[\underbrace{\overbrace{r_{t+1}+\gamma v_{t}(s_{t+1})}^{\text{TD target }\bar{v}_{t}}-v_{t}(s_{t})}_{\text{TD error }\delta_{t}}\Big]. \tag{3.5}\]
Definition 3.2 (TD target and TD error) \[ \bar{v}_{t}\defeq r_{t+1}+\gamma v_{t}(s_{t+1}), \qquad \delta_{t}\defeq \bar{v}_{t}-v_{t}(s_{t}). \]
This extends Equation 1.3 to state-dependent step sizes.
Why \(\bar{v}_{t}\) deserves the name “target”
Rewrite Equation 3.3 in terms of \(\bar{v}_{t}\) and subtract \(\bar{v}_{t}\) from both sides: \[ \begin{aligned} v_{t+1}(s_{t})&=v_{t}(s_{t})+\alpha_{t}(s_{t})\big[\bar{v}_{t}-v_{t}(s_{t})\big]\\ \implies\quad v_{t+1}(s_{t})-\bar{v}_{t}&=v_{t}(s_{t})-\bar{v}_{t}+\alpha_{t}(s_{t})\big[\bar{v}_{t}-v_{t}(s_{t})\big]\\ \implies\quad v_{t+1}(s_{t})-\bar{v}_{t}&=\big[1-\alpha_{t}(s_{t})\big]\big[v_{t}(s_{t})-\bar{v}_{t}\big]\\ \implies\quad \big|v_{t+1}(s_{t})-\bar{v}_{t}\big|&=\big|1-\alpha_{t}(s_{t})\big|\,\big|v_{t}(s_{t})-\bar{v}_{t}\big| . \end{aligned} \] Since \(0<\alpha_t(s_t)\le1\), we have \(0\le1-\alpha_t(s_t)<1\), and therefore \[ \big|v_{t+1}(s_{t})-\bar{v}_{t}\big|\leq\big|v_{t}(s_{t})-\bar{v}_{t}\big| . \]
Each update strictly shrinks the distance to \(\bar v_t\) when the distance is nonzero. At \(\alpha_t>2\), the factor \(|1-\alpha_t|\) exceeds one and the distance grows. The target itself changes with \(v_t\).
Convergence
Theorem 3.1 (Convergence of TD learning) Assume \(\gamma\in(0,1)\), finite \(\mathcal{S}\), bounded rewards, a fixed policy \(\pi\), and the effective step sizes defined above. Then under Equation 3.3, \(v_t(s)\to v_\pi(s)\) almost surely for every \(s\), provided the following conditions hold almost surely: \[ \sum_{t}\alpha_{t}(s)=\infty \qquad\text{and}\qquad \sum_{t}\alpha_{t}^{2}(s)<\infty \qquad\text{for all } s\in\mathcal{S}. \]
These are the Robbins-Monro conditions of Theorem 2.1, imposed separately at each state.
The proof uses a multi-variable variant of Theorem 2.2, with one variable per state. It allows noise bias bounded by the current error and adds the requirement \(\E[\beta_{k}]\leq\E[\alpha_{k}]\), which the Chapter 2 form does not impose. Neither theorem contains the other; the Robbins-Monro corollary uses the Chapter 2 form.
Theorem 3.2 (Dvoretzky, multi-variable form) Let \(\mathcal S\) be a finite index set and \(\{\mathcal H_k\}\) a filtration. Assume \(\Delta_k\), \(\alpha_k\), and \(\beta_k\) are \(\mathcal H_k\)-measurable, with \(0\le\alpha_k(s)\le1\) and \(\beta_k(s)\ge0\). For the stochastic process \[ \Delta_{k+1}(s)=\big(1-\alpha_{k}(s)\big)\Delta_{k}(s)+\beta_{k}(s)\eta_{k}(s), \tag{3.6}\] \(\Delta_{k}(s)\) converges to zero almost surely for every \(s\in\mathcal{S}\) provided that, for every \(s\in\mathcal{S}\),
- \(\sum_{k}\alpha_{k}(s)=\infty\), \(\sum_{k}\alpha_{k}^{2}(s)<\infty\), \(\sum_{k}\beta_{k}^{2}(s)<\infty\), and \(\E[\beta_{k}(s)\given\mathcal{H}_{k}]\leq\E[\alpha_{k}(s)\given\mathcal{H}_{k}]\), uniformly almost surely;
- \(\big\|\E[\eta_{k}(s)\given\mathcal{H}_{k}]\big\|_{\infty}\leq\gamma\|\Delta_{k}\|_{\infty}\) for some \(\gamma\in(0,1)\);
- \(\operatorname{var}[\eta_{k}(s)\given\mathcal{H}_{k}]\leq C\big(1+\|\Delta_{k}\|_{\infty}\big)^{2}\) for a constant \(C\),
where \(\|x(s)\|_{\infty}\defeq\max_{s\in\mathcal{S}}|x(s)|\) is the maximum over the index set.
Condition 2 allows conditional bias bounded by \(\gamma<1\) times the current error. For TD, \(\gamma\) is the discount factor.
Fix an arbitrary state \(s\in\mathcal{S}\) and define the estimation error \[ \Delta_{t}(s)\defeq v_{t}(s)-v_{\pi}(s). \]
Step 1: put the algorithm in the form Equation 3.6. When \(s=s_{t}\), subtract \(v_{\pi}(s)\) from both sides of Equation 3.3: \[ \Delta_{t+1}(s)=\big(1-\alpha_{t}(s)\big)\Delta_{t}(s)+\alpha_{t}(s)\underbrace{\big[r_{t+1}+\gamma v_{t}(s_{t+1})-v_{\pi}(s)\big]}_{\eta_{t}(s)} . \] When \(s\neq s_{t}\), Equation 3.4 gives \(\Delta_{t+1}(s)=\Delta_{t}(s)\), which is the same expression with \(\alpha_{t}(s)=0\) and \(\eta_{t}(s)=0\). So regardless of which state was visited, \[ \Delta_{t+1}(s)=\big(1-\alpha_{t}(s)\big)\Delta_{t}(s)+\alpha_{t}(s)\eta_{t}(s), \] which is Equation 3.6 with \(\beta_{t}(s)=\alpha_{t}(s)\), so the requirement \(\E[\beta_{t}]\leq\E[\alpha_{t}]\) holds with equality.
Step 2: condition 1. This is precisely the hypothesis of Theorem 3.1.
Step 3: condition 2, the contraction. Conditioning on \(\mathcal{H}_{t}\) fixes \(v_{t}\) and \(s_{t}\). The remaining randomness is \((r_{t+1},s_{t+1})\), whose law under the fixed policy depends on \(s_{t}\). For \(s\neq s_{t}\), \(\eta_{t}(s)=0\) and the bound is trivial. For \(s=s_{t}\), \[ \E[\eta_{t}(s)\given\mathcal{H}_{t}]=\E\big[r_{t+1}+\gamma v_{t}(s_{t+1})\given\mathcal{H}_{t}\big]-v_{\pi}(s_{t}). \] Now use the Bellman equation for \(v_{\pi}\), which says \(v_{\pi}(s_{t})=\E[r_{t+1}+\gamma v_{\pi}(s_{t+1})\given s_{t}]\). Subtracting, the reward terms cancel exactly and only the value gap survives: \[ \E[\eta_{t}(s)\given\mathcal{H}_{t}]=\gamma\,\E\big[v_{t}(s_{t+1})-v_{\pi}(s_{t+1})\given\mathcal{H}_{t}\big] =\gamma\sum_{s'\in\mathcal{S}}p(s'\given s_{t})\big[v_{t}(s')-v_{\pi}(s')\big], \] where \(v_{t}\) passes outside the expectation because \(\mathcal{H}_{t}\) determines it. Bound the sum by its largest term, using that \(p(\cdot\given s_{t})\) is a probability distribution and therefore sums to one: \[ \big|\E[\eta_{t}(s)\given\mathcal{H}_{t}]\big| \leq\gamma\sum_{s'}p(s'\given s_{t})\max_{s'}\big|v_{t}(s')-v_{\pi}(s')\big| =\gamma\max_{s'}\big|v_{t}(s')-v_{\pi}(s')\big| =\gamma\|\Delta_{t}\|_{\infty}. \] This holds whether or not \(s=s_{t}\), so \(\|\E[\eta_{t}(s)\given\mathcal{H}_{t}]\|_{\infty}\leq\gamma\|\Delta_{t}\|_{\infty}\), with \(\gamma\in(0,1)\) the discount factor. Note both sides are \(\mathcal{H}_{t}\)-measurable random variables, which is exactly the form condition 2 of Theorem 3.2 asks for.
Step 4: condition 3. For \(s\ne s_t\), \(\operatorname{var}[\eta_t(s)\given\mathcal H_t]=0\). For \(s=s_t\), subtracting \(v_\pi(s_t)\) leaves \(\operatorname{var}[r_{t+1}+\gamma v_t(s_{t+1})\given\mathcal H_t]\). This is bounded by a constant multiple of \((1+\|\Delta_t\|_\infty)^2\): rewards and \(v_\pi\) are bounded, and \(\|v_t\|_\infty\le\|v_\pi\|_\infty+\|\Delta_t\|_\infty\).
All three conditions hold, so Theorem 3.2 gives \(\Delta_{t}(s)\to0\) almost surely for every \(s\). \(\square\)
At \(\gamma=1\), the strict contraction argument in Step 3 is unavailable.
Recall from Equation 3.4 that \(\alpha_{t}(s)>0\) only when \(s_{t}=s\), and \(\alpha_{t}(s)=0\) otherwise. So:
- \(\sum_{t}\alpha_{t}(s)=\infty\) for every \(s\) can only hold if every state is visited infinitely often. Thus the step-size condition also imposes an exploration requirement, which is why \(\eps\)-greedy policies from Definition 2.2 reappear the moment we start improving the policy.
- \(\sum_t\alpha_t^2(s)<\infty\) requires the steps on visits to \(s\) to vanish. With a small constant step and persistent sampling noise, a stable recursion generally keeps fluctuating, as in Figure 2.2. A constant-step deterministic contraction can still converge; the theorem supplies sufficient conditions for the noisy case.
TD against Monte Carlo
| TD / Sarsa | Monte Carlo | |
|---|---|---|
| Timing | incremental: updates immediately after each reward | episodic: waits until an episode is complete |
| Task type | handles episodic and continuing tasks | complete returns for terminating tasks; finite rollouts of continuing tasks need tail control |
| Bootstrapping | yes: the update uses the previous estimate of the same value, so it needs an initial guess | no: estimates values directly from returns, no initial guess needed |
| Sampling noise | one reward and a bootstrapped continuation per update | the full return combines reward and transition noise over the episode |
3.3 Sarsa: TD learning of action values
For model-free policy search, apply TD to action values.
Given experience \(\{(s_{t},a_{t},r_{t+1},s_{t+1},a_{t+1})\}_{t}\) generated by following \(\pi\),
\[ q_{t+1}(s_{t},a_{t})=q_{t}(s_{t},a_{t})+\alpha_{t}(s_{t},a_{t})\big[\,r_{t+1}+\gamma q_{t}(s_{t+1},a_{t+1})-q_{t}(s_{t},a_{t})\,\big], \tag{3.7}\]
with \(q_{t+1}(s,a)=q_{t}(s,a)\) for all \((s,a)\neq(s_{t},a_{t})\).
The name comes from the update’s quintuple \((s_{t},a_{t},r_{t+1},s_{t+1},a_{t+1})\): state, action, reward, state, action.
Equation 3.7 is a stochastic approximation algorithm, in the sense of Theorem 2.1, for the equation
\[ q_{\pi}(s,a)=\E\big[R+\gamma q_{\pi}(S',A')\ \given\ s,a\big], \qquad\text{for all } s,a, \tag{3.8}\]
which is the action-value form of the policy-evaluation Bellman equation for the fixed policy \(\pi\): Equation 2.2 with the maximization over \(\pi\) removed.
Write \(p(r\given s,a)\) for the distribution of the immediate reward and \(p(s'\given s,a)\) for the transition, both conditional on the pair \((s,a)\). Expanding the right-hand side of Equation 3.8 by the definition of expectation,
\[ \E\big[R+\gamma q_{\pi}(S',A')\given s,a\big] =\sum_{r}r\,p(r\given s,a) +\gamma\sum_{s'\in\mathcal{S}}\sum_{a'\in\mathcal{A}}q_{\pi}(s',a')\,p(s',a'\given s,a). \]
Linearity separates the expectations over \(R\) and \(S'\) using their marginal distributions, without an independence assumption. The first term is the expected immediate reward, which collapses to \(r(s,a)\) whenever the reward is a deterministic function of the pair, as it was in Chapters 1 and 2. The second term needs the joint law of the next state and the next action.
Factor the joint. By the chain rule: \[ p(s',a'\given s,a)=p(s'\given s,a)\,p(a'\given s',s,a). \] The second factor is where the policy enters. The agent produces \(a'\) by drawing from \(\pi(\cdot\given s')\), using a stationary Markov policy \(\pi\) that depends only on the current state. So conditionally on \(s'\), the next action is independent of \((s,a)\), and \[ p(a'\given s',s,a)=p(a'\given s')=\pi(a'\given s'). \] For a history-dependent policy, this factorization need not hold; \(q\) would generally require history as an additional argument beyond \((s,a)\).
Substitute and collect. Putting the factorization back and summing over \(a'\) first, \[ q_{\pi}(s,a)=\underbrace{\sum_{r}r\,p(r\given s,a)}_{\E[R\given s,a]} +\gamma\sum_{s'\in\mathcal{S}}p(s'\given s,a)\underbrace{\sum_{a'\in\mathcal{A}}\pi(a'\given s')\,q_{\pi}(s',a')}_{v_{\pi}(s')}, \] because the action value averaged against the policy at \(s'\) is by definition the state value there. Hence \[ q_{\pi}(s,a)=\E[R\given s,a]+\gamma\sum_{s'\in\mathcal{S}}p(s'\given s,a)\,v_{\pi}(s'), \] which is Equation 2.5, the model-based form of the action value.
And back to state values. Average that last display against \(\pi(a\given s)\) and use Definition 2.1: \[ v_{\pi}(s)=\sum_{a\in\mathcal{A}}\pi(a\given s)\,q_{\pi}(s,a) =r_{\pi}(s)+\gamma\sum_{s'\in\mathcal{S}}p_{\pi}(s'\given s)\,v_{\pi}(s'), \] which is Equation 1.6, the policy-evaluation equation, written one state at a time. \(\square\)
Sarsa samples \((r_{t+1},s_{t+1},a_{t+1})\) in Equation 3.8 and substitutes \(q_{t}\) for the unknown \(q_{\pi}\) in the target.
Theorem 3.3 (Convergence of Sarsa) For a fixed policy \(\pi\), assume \(\gamma\in(0,1)\), finite \(\mathcal S\) and \(\mathcal A\), and bounded rewards. Here \(\mathcal H_t\) includes the current \((s_t,a_t)\) but precedes the next reward, state, and action. Use \(\mathcal H_t\)-measurable step sizes, with \(\alpha_t(s,a)=0\) for unvisited pairs and \(0<\alpha_t(s_t,a_t)\le1\). Then Equation 3.7 gives \(q_t(s,a)\to q_\pi(s,a)\) almost surely for every pair if \(\sum_t\alpha_t(s,a)=\infty\) and \(\sum_t\alpha_t^2(s,a)<\infty\) almost surely for every \((s,a)\).
The proof of Theorem 3.1 carries over from states to state-action pairs with the following substitutions.
The index set. Replace \(\mathcal{S}\) by \(\mathcal{S}\times\mathcal{A}\) everywhere. It is still finite, which is all Theorem 3.2 ever asked of it, and the norm becomes \(\|x\|_{\infty}=\max_{(s,a)}|x(s,a)|\), a maximum over pairs.
Step 1, the error process. In place of \(\Delta_{t}(s)\defeq v_{t}(s)-v_{\pi}(s)\), define \(\Delta_{t}(s,a)\defeq q_{t}(s,a)-q_{\pi}(s,a)\). Subtracting \(q_{\pi}(s,a)\) from Equation 3.7 when \((s,a)=(s_{t},a_{t})\) gives \[ \Delta_{t+1}(s,a)=\big(1-\alpha_{t}(s,a)\big)\Delta_{t}(s,a)+\alpha_{t}(s,a)\underbrace{\big[r_{t+1}+\gamma q_{t}(s_{t+1},a_{t+1})-q_{\pi}(s,a)\big]}_{\eta_{t}(s,a)}, \] and every other pair is left untouched, which is the same line with \(\alpha_{t}(s,a)=0\) and \(\eta_{t}(s,a)=0\). That is Equation 3.6 on the new index set, again with \(\beta_{t}=\alpha_{t}\).
Step 2, condition 1. The hypothesis of the theorem, now imposed on pairs.
Step 3, the contraction. Conditioning on \(\mathcal H_t\) fixes \(q_t\) and \((s_t,a_t)\). The remaining randomness is the next reward, state, and action under the fixed policy \(\pi\). The bound is trivial for unvisited pairs. For \((s,a)=(s_t,a_t)\), \[ \E[\eta_t(s,a)\given\mathcal H_t] =\E\big[r_{t+1}+\gamma q_t(s_{t+1},a_{t+1})\given\mathcal H_t\big]-q_\pi(s_t,a_t). \] Subtracting Equation 3.8 cancels the reward terms, as in the proof for \(v_{\pi}\), and leaves the error at the next pair: \[ \E[\eta_t(s,a)\given\mathcal H_t] =\gamma\sum_{s'\in\mathcal S}\sum_{a'\in\mathcal A} p(s'\given s_t,a_t)\,\pi(a'\given s')\big[q_t(s',a')-q_\pi(s',a')\big]. \] The joint probabilities \(p(s',a'\given s_{t},a_{t})\) are nonnegative and sum to one, so \[ \big|\E[\eta_t(s,a)\given\mathcal H_t]\big| \le\gamma\max_{(s',a')}\big|q_t(s',a')-q_\pi(s',a')\big| =\gamma\|\Delta_t\|_\infty, \] where \(\gamma\in(0,1)\) is the discount factor.
Step 4, the variance. The constant \(q_{\pi}(s,a)\) drops out of the variance, \(r_{t+1}\) is bounded, and \(q_{t}\) differs from the fixed \(q_{\pi}\) by at most \(\|\Delta_{t}\|_{\infty}\), so condition 3 holds.
All three conditions of Theorem 3.2 hold on the index set \(\mathcal{S}\times\mathcal{A}\), so \(\Delta_{t}(s,a)\to0\) almost surely for every pair. \(\square\)
Since \(\alpha_{t}(s,a)>0\) only when \((s_{t},a_{t})=(s,a)\), the condition \(\sum_{t}\alpha_{t}(s,a)=\infty\) for every pair requires infinitely many visits to each pair. A fixed deterministic policy can visit every state in \(\mathcal{S}\) infinitely often while sampling one action per state, leaving \(|\mathcal{S}|(|\mathcal{A}|-1)\) entries with \(\alpha_{t}=0\) at every \(t\). These entries retain their initial values. With \(|\mathcal A|>1\), the policy cannot meet the pairwise visitation condition.
The theorem evaluates \(q_{\pi}\) for a fixed policy \(\pi\). Policy search also requires an improvement step.
Policy searching by Sarsa
The evaluation and improvement steps of Equation 2.3 are interleaved: take one TD step, improve the policy, and generate the next transition.
For each episode: generate \(a_{0}\) at \(s_{0}\) following \(\pi_{0}(s_{0})\). Then for each \(t=0,1,2,\dots\) while \(s_{t}\) is not the target state:
Interact. Take \(a_{t}\), observe \(r_{t+1}\) and \(s_{t+1}\), and generate \(a_{t+1}\) following \(\pi_{t}(s_{t+1})\).
Update the \(q\)-value of \((s_{t},a_{t})\) by Equation 3.7.
Update the policy at \(s_{t}\) to be \(\eps\)-greedy with respect to the new \(q_{t+1}\), in the sense of Definition 2.2.
Set \(s_{t}\leftarrow s_{t+1}\), \(a_{t}\leftarrow a_{t+1}\).
For the two-action chain in Figure 3.1, \(|\mathcal{A}|=2\), so Definition 2.2 assigns \(1-\eps/2\) to the greedy action and \(\eps/2\) to the other. Here q is the table, g selects its larger entry at s, and \(\eps=0.2\).
3.4 Q-learning: TD learning of optimal action values
Sarsa estimates \(q_{\pi}\) for the policy currently being followed, and therefore must be combined with an improvement step. Q-learning skips the intermediary and estimates \(q^{*}\) directly.
\[ q_{t+1}(s_{t},a_{t})=q_{t}(s_{t},a_{t})+\alpha_{t}(s_{t},a_{t})\Big[\,r_{t+1}+\gamma\max_{a\in\mathcal{A}}q_{t}(s_{t+1},a)-q_{t}(s_{t},a_{t})\,\Big], \tag{3.9}\]
with \(q_{t+1}(s,a)=q_{t}(s,a)\) for all \((s,a)\neq(s_{t},a_{t})\).
The loop for Figure 3.1 uses step(s, a) to return the next state, reward, and termination flag. There are N_DEC = 4 decision states and \(\gamma=0.9\). use_max selects the \(\max\) target of Equation 3.9 or the next action drawn by eps_greedy for Equation 3.7; Sarsa then takes that same action. Both use \(\alpha_{t}=1/n(s,a)\) and uniform starting states. qs, vals, and checks record the plotted outcomes.
def run(use_max, n_ep, checks):
"""One learning run; `use_max` selects the TD target of @tbl-sarsa-q.
use_max=True is Q-learning @eq-qlearning, whose target maxes over the table.
use_max=False is Sarsa @eq-sarsa, whose target uses the action a_{t+1} that the
eps-greedy behavior policy actually draws. Everything else is shared.
Both algorithms use uniform starting states and steps 1/n(s, a).
Separate runs consume different random draws and generate different trajectories;
the start distribution is shared, not the realized state-action visits.
"""
q = RNG.normal(0.0, 1e-3, (N_DEC, 2)) # jitter only breaks the initial ties
n = np.zeros((N_DEC, 2)) # visit counts, so alpha = 1 / n(s, a)
wanted = set(checks.tolist())
qs, vals = [], []
for ep in range(1, n_ep + 1):
s = int(RNG.integers(N_DEC)) # uniform state starts, not exploring starts:
# the first action is still drawn eps-greedily
a = eps_greedy(q, s)
while True:
s2, r, done = step(s, a)
if done:
target = r
elif use_max:
target = r + GAMMA * q[s2].max() # Q-learning: max over the table
else:
a2 = eps_greedy(q, s2) # Sarsa: the action drawn next,
target = r + GAMMA * q[s2, a2] # read before the update
n[s, a] += 1
q[s, a] += (target - q[s, a]) / n[s, a]
if done:
break
s, a = s2, (eps_greedy(q, s2) if use_max else a2)
if ep in wanted:
qs.append(q.copy())
vals.append(policy_value(q[:, 1] > q[:, 0]))
return np.array(qs), np.array(vals)| TD target | |
|---|---|
| Sarsa | \(r_{t+1}+\gamma\,q_{t}(s_{t+1},a_{t+1})\) |
| Q-learning | \(r_{t+1}+\gamma\,\max_{a\in\mathcal{A}}q_{t}(s_{t+1},a)\) |
That single substitution changes what equation the algorithm solves. Sarsa is stochastic approximation on the Bellman equation Equation 3.8; Q-learning is stochastic approximation on the Bellman optimality equation,
\[ q(s,a)=\E\Big[R+\gamma\max_{a'\in\mathcal{A}}q(S',a')\ \given\ s,a\Big], \qquad\text{for all } s,a, \tag{3.10}\]
whose solution is \(q^{*}\).
Step 1: expand the expectation. Exactly as for Sarsa, write the right-hand side as sums against the conditional distributions of the reward and of the next state. What differs is that the next action is not random: the \(\max\) has already chosen it, so only \(r\) and \(s'\) are integrated out and no policy is needed to average over \(a'\), \[ q(s,a)=\sum_{r}r\,p(r\given s,a)+\gamma\sum_{s'\in\mathcal{S}}p(s'\given s,a)\max_{a'\in\mathcal{A}}q(s',a'). \] Step 2: maximize both sides over the first action. Both sides are functions of \(a\) and \(\mathcal{A}\) is finite, so the maximum exists and \[ \max_{a\in\mathcal{A}}q(s,a)=\max_{a\in\mathcal{A}}\Big\{\sum_{r}r\,p(r\given s,a)+\gamma\sum_{s'\in\mathcal{S}}p(s'\given s,a)\max_{a'\in\mathcal{A}}q(s',a')\Big\}. \]
Step 3: name the maximized value. Define \[ v(s)\defeq\max_{a\in\mathcal{A}}q(s,a). \] The inner maximum on the right is that same definition evaluated at \(s'\), so it is \(v(s')\), and the left-hand side is \(v(s)\): \[ v(s)=\max_{a\in\mathcal{A}}\Big\{\sum_{r}r\,p(r\given s,a)+\gamma\sum_{s'\in\mathcal{S}}p(s'\given s,a)\,v(s')\Big\}, \] which is Equation 1.4, using \(\gamma\) and the expected reward.
Step 4: maximizing over actions is maximizing over policies. Chapter 2 wrote the optimality equation with the maximum taken over stochastic policies, Equation 2.1, so the two forms still have to be reconciled. Abbreviate the bracket in Step 3 as \[ f(a)\defeq\sum_{r}r\,p(r\given s,a)+\gamma\sum_{s'\in\mathcal{S}}p(s'\given s,a)\,v(s'). \] For any distribution \(\pi(\cdot\given s)\) on the finite set \(\mathcal{A}\), \[ \sum_{a\in\mathcal{A}}\pi(a\given s)f(a)\leq\sum_{a\in\mathcal{A}}\pi(a\given s)\max_{b\in\mathcal{A}}f(b)=\max_{b\in\mathcal{A}}f(b), \] since a weighted average of finitely many numbers cannot exceed the largest of them, the weights summing to one. The bound is attained: finiteness of \(\mathcal{A}\) makes \(\argmax_{a}f(a)\) nonempty, and putting all the mass on one of its elements turns the average into the maximum. Hence \[ \max_{\pi}\sum_{a\in\mathcal{A}}\pi(a\given s)f(a)=\max_{a\in\mathcal{A}}f(a), \] the maximum over the simplex of distributions being attained at a vertex, that is, at a point mass. Step 3 is therefore Equation 2.1, and Equation 3.10 is the Bellman optimality equation written one state-action pair at a time. \(\square\)
An optimal policy is \(\pi(s)=\argmax_{a}q^{*}(s,a)\); the \(\eps\)-greedy behavior policy supplies exploration. Conversely, define \(q\) from a solution \(v\) of Equation 1.4 by Step 1’s one-step formula. Then \(\max_{a}q(s,a)=v(s)\), and substitution gives Equation 3.10. Thus the fixed points determine each other, with \(v^{*}(s)=\max_{a}q^{*}(s,a)\).
Why Q-learning is off-policy
The updates require different data. Sarsa Equation 3.7 needs \(a_{t+1}\), the action the current policy will actually take next. Q-learning Equation 3.9 does not: the \(\max\) is taken over the table, not over what happens. Its update needs only \((s_{t},a_{t},r_{t+1},s_{t+1})\).
Definition 3.3 (On-policy and off-policy) An algorithm is on-policy if the data must be generated by the policy being learned. It is off-policy if the data may come from any behavior policy, while the target policy being learned is a different one.
Sarsa is on-policy: \(a_{t+1}\) ties the update to the policy in force. Q-learning is off-policy: its convergence theorem permits an exploratory behavior policy, with every state-action pair sampled infinitely often and the required step sizes. Stored transitions can also be used for updates; their coverage limits what can be learned.
For each episode, for each \(t=0,1,2,\dots\) while \(s_{t}\) is not the target state:
Interact. Generate \(a_{t}\) following \(\pi_{t}(s_{t})\); observe \(r_{t+1}\) and \(s_{t+1}\).
Update the \(q\)-value of \((s_{t},a_{t})\) by Equation 3.9.
Update the policy at \(s_{t}\) to be \(\eps\)-greedy with respect to \(q_{t+1}\).
The behavior policy here changes with the \(q\) table. The target remains \(\max_a q(s_{t+1},a)\), so this implementation is off-policy too. A fixed exploratory behavior policy is another way to collect the required transitions.
240 runs of each algorithm on the ledge chain, scored two ways
n_runs, n_ep = 240, 1200
checks = np.unique(np.round(np.logspace(0, np.log10(n_ep), 44)).astype(int))
algos = (("qlearning", True, COL["vermilion"]), ("sarsa", False, COL["sky"]))
ref = {name: reference_q(0.0 if use_max else EPS) for name, use_max, _ in algos}
err = {name: np.zeros(len(checks)) for name, _, _ in algos}
val = {name: np.zeros(len(checks)) for name, _, _ in algos}
for _ in range(n_runs):
for name, use_max, _ in algos:
qs, vs = run(use_max, n_ep, checks)
err[name] += np.sqrt(np.mean((qs - ref[name]) ** 2, axis=(1, 2)))
val[name] += vs
for name, _, _ in algos:
# as a share of the gap the run started from, so the two rates are comparable
err[name] /= n_runs * np.sqrt(np.mean(ref[name] ** 2))
val[name] /= n_runs
# The figure's two claims, checked on the runs it plots. Q-learning must end nearer the
# values its own equation defines AND imply the better greedy policy; Sarsa settles
# strictly below because it prices in the exploration it actually does.
assert err["qlearning"][-1] < err["sarsa"][-1]
assert val["qlearning"][-1] > val["sarsa"][-1] + 1.0
fig, (axL, axR) = plt.subplots(1, 2, figsize=(9.4, 3.9))
trim(axL), trim(axR)
# --- left: distance to each algorithm's own fixed point ------------------
for name, _, colour in algos:
axL.loglog(checks, err[name], color=colour, lw=2.0, zorder=4)
lo = err["qlearning"][-1]
axL.set_xlim(1, n_ep)
axL.set_ylim(lo * 0.5, 1.3) # room under the curves for the two direct labels
axL.set_xlabel("episodes")
axL.set_ylabel("share of the initial error left")
label_at(axL, 1.6, lo * 2.4, r"Sarsa $\to q_{\pi}$", color=COL["sky"], fontsize=9.5)
label_at(axL, 1.6, lo * 0.8, r"Q-learning $\to q^{*}$",
color=COL["vermilion"], fontsize=9.5)
axL.set_title("Each settles on its own fixed point, Q-learning sooner", fontsize=11)
# --- right: value of the greedy policy each one implies ------------------
# The reference lines stop at the last episode, leaving the right margin clear for
# their tags rather than running a dashed rule straight through the text.
v_star, v_safe = policy_value([False] * N_DEC), R_SAFE
for v in (v_star, v_safe):
axR.plot([0, n_ep], [v, v], color=GREY, lw=0.9, ls="--", zorder=2)
for name, _, colour in algos:
axR.plot(checks, val[name], color=colour, lw=2.0, zorder=4)
axR.set_xlim(0, n_ep * 1.42) # headroom on the right for the two reference tags
axR.set_xticks([0, 400, 800, 1200]) # the headroom carries tags, not data
axR.set_xlabel("episodes")
axR.set_ylabel(r"value at $s_{0}$ of the implied greedy policy")
label_at(axR, n_ep * 1.04, v_star, r"$v^{*}(s_{0})$: the ledge",
color=GREY, fontsize=8.5)
label_at(axR, n_ep * 1.04, v_safe, "the safe route", color=GREY, fontsize=8.5)
label_at(axR, n_ep * 0.42, 6.25, "Q-learning", color=COL["vermilion"], fontsize=9.5)
label_at(axR, n_ep * 0.42, 4.05, "Sarsa", color=COL["sky"], fontsize=9.5)
axR.set_title("Sarsa keeps the safe route it can afford to explore", fontsize=11)
fig.tight_layout()
plt.show()
3.5 TD learning with function approximation
Tabular values have two limitations:
- Storage. A table has \(|\mathcal{S}|\) or \(|\mathcal{S}|\times|\mathcal{A}|\) entries. Storage is impractical for large or continuous state spaces, and in economics the state space is routinely continuous.
- Generalization. Nothing learned at \(s\) transfers to a nearby \(s'\). Every state must be visited, many times, on its own account.
Values as parameterized functions
Replace the table with a function
\[ \hat{v}(s,w)\approx v_{\pi}(s), \qquad w\in\R^{m}, \tag{3.11}\]
where \(m\) can be far smaller than \(|\mathcal{S}|\). Updating shared parameters changes estimates at multiple states.
Example 3.1 (Fitting a line) Suppose the states \(s_{1},\dots,s_{|\mathcal{S}|}\) are one-dimensional and \(|\mathcal{S}|\) is very large. The crudest approximation is a straight line: \[ \hat{v}(s,w)=as+b=\underbrace{[\,s\ \ 1\,]}_{\phi^{\!\top}(s)}\underbrace{\begin{bmatrix}a\\ b\end{bmatrix}}_{w}=\phi^{\!\top}(s)\,w . \] Here \(w\) is the parameter vector, \(\phi(s)\) is the feature vector of \(s\), and \(\hat{v}\) is linear in \(w\).
Figure 3.2 compares a table, a line, and six Gaussian features by fit and by the effect of one update on other states.
Fitting a 200-state value function with six numbers
# A 200-state chain on [0, 1]. The policy induces a reflecting random walk, so v_pi
# solves (I - gamma P) v = r exactly: nothing below is estimated, this is the object
# the algorithms of this chapter are chasing and the object a table has to store.
N, GAMMA = 200, 0.98
S = np.linspace(0.0, 1.0, N)
i = np.arange(N)
P = np.zeros((N, N))
np.add.at(P, (i, np.clip(i - 1, 0, N - 1)), 0.5)
np.add.at(P, (i, np.clip(i + 1, 0, N - 1)), 0.5)
R = (1.0 - GAMMA) * (8.0 * np.exp(-0.5 * ((S - 0.30) / 0.13) ** 2)
- 4.0 * np.exp(-0.5 * ((S - 0.78) / 0.15) ** 2)
+ 1.6 * S + 0.4)
V = np.linalg.solve(np.eye(N) - GAMMA * P, R)
# Two feature maps. PHI_L is @exm-linear-fa verbatim, so w is two numbers; PHI_R
# spreads six bumps across the state space, so w is six.
CENTRES, WIDTH = np.linspace(0.05, 0.95, 6), 0.13
PHI_L = np.stack([S, np.ones_like(S)], axis=-1)
PHI_R = np.exp(-0.5 * ((S[:, None] - CENTRES) / WIDTH) ** 2)
# Least squares against v_pi under a uniform state distribution is exactly the
# minimizer of @eq-fa-objective, so these are the two families at their best.
fit_l = PHI_L @ np.linalg.lstsq(PHI_L, V, rcond=None)[0]
fit_r = PHI_R @ np.linalg.lstsq(PHI_R, V, rcond=None)[0]
# One update at one visited state. w starts at zero, so the estimate starts flat and
# the error at s_t is v_pi(s_t) itself. The step size is the one that lands the
# estimate at s_t exactly on that target, which is what a table with alpha = 1 does
# too. All three therefore move by the SAME amount at s_t, and differ only elsewhere.
IT = int(np.argmin(np.abs(S - 0.32)))
ST, TARGET = S[IT], V[IT]
up_l = TARGET * (PHI_L @ PHI_L[IT]) / (PHI_L[IT] @ PHI_L[IT])
up_r = TARGET * (PHI_R @ PHI_R[IT]) / (PHI_R[IT] @ PHI_R[IT])
# The two claims the figure makes, checked on the numbers it plots. Six parameters
# track v_pi an order of magnitude better than two; and one update at s_t moves every
# state within 0.15 of it closer to the truth while leaving distant states nearly
# untouched, which the two-feature update cannot do.
near, far = np.abs(S - ST) < 0.15, np.abs(S - ST) > 0.40
assert np.sqrt(np.mean((V - fit_r) ** 2)) < 0.3 < 2.0 < np.sqrt(np.mean((V - fit_l) ** 2))
assert np.all(np.abs(V[near] - up_r[near]) < np.abs(V[near]))
assert np.max(np.abs(up_r[far])) < 0.15 * np.min(np.abs(up_l[far]))
fig, (axL, axR) = plt.subplots(1, 2, figsize=(9.4, 3.9))
trim(axL), trim(axR)
# Left: the same curve, stored three ways.
axL.plot(S, fit_l, color=COL["orange"], lw=2.0, zorder=4)
axL.plot(S, fit_r, color=COL["blue"], lw=1.8, zorder=5)
axL.plot(S, V, ls="none", marker="o", ms=2.0, color=GREY, zorder=6)
axL.set_xlim(-0.02, 1.02)
axL.set_ylim(-4.4, 11.0)
axL.set_xlabel("state $s$")
axL.set_ylabel("value")
axL.set_title("Six numbers stand in for two hundred", fontsize=11)
label_at(axL, 0.02, 10.2, r"$v_{\pi}$: 200 table entries", color=GREY, fontsize=9.5)
label_at(axL, 0.47, 8.4, "6 features: 6 numbers", color=COL["blue"], fontsize=9.5)
label_at(axL, 0.66, 2.6, r"$[\,s\ \ 1\,]$: 2 numbers", color=COL["orange"], fontsize=9.5)
# Right: one update at one state. The dots on zero are the table entries that
# @eq-td-untouched leaves alone; the arrow is the one entry the table does move.
axR.plot(S, V, ls="none", marker="o", ms=2.0, color=GREY, alpha=0.5, zorder=2)
axR.plot(S[::4], np.zeros(N)[::4], ls="none", marker="o", ms=2.6,
color=COL["vermilion"], alpha=0.75, zorder=3)
axR.plot(S, up_l, color=COL["orange"], lw=2.0, zorder=4)
axR.plot(S, up_r, color=COL["blue"], lw=1.8, zorder=5)
axR.annotate("", xy=(ST, TARGET), xytext=(ST, 0.0),
arrowprops=dict(arrowstyle="->", color=COL["vermilion"], lw=1.3))
axR.plot([ST], [TARGET], marker="o", ms=6.5, color=COL["vermilion"], zorder=7)
axR.set_xlim(-0.02, 1.02)
axR.set_ylim(-4.4, 11.0)
axR.set_xlabel("state $s$")
axR.set_ylabel("estimate after one update")
axR.set_title("One visit, and the neighbours move too", fontsize=11)
label_at(axR, 0.02, 10.3, "2 features: every state moves", color=COL["orange"], fontsize=9.5)
label_at(axR, 0.55, 6.3, "6 features: nearby states move", color=COL["blue"], fontsize=9.5)
label_at(axR, 0.02, -1.7, "a table moves this entry only", color=COL["vermilion"], fontsize=9.5)
label_at(axR, 0.345, 0.8, r"$s_{t}$", color=COL["vermilion"], fontsize=10)
label_at(axR, 0.84, -3.3, r"$v_{\pi}$", color=GREY, fontsize=9.5)
fig.tight_layout()
plt.show()
The approximator \(\hat{v}(s,w)\) may also be nonlinear, including a deep neural network.
Fitting \(w\) by stochastic gradient descent
For a fixed policy \(\pi\) and a fixed distribution over states \(S\), choose \(w\) to minimize the mean squared error against the true value function,
\[ J(w)=\E\Big[\big(v_{\pi}(S)-\hat{v}(S,w)\big)^{2}\Big]. \tag{3.12}\]
This is exactly Equation 2.12, so Equation 2.13 applies. With \(f(w,s)=(v_{\pi}(s)-\hat{v}(s,w))^{2}\) the gradient is \(-2(v_{\pi}(s)-\hat{v}(s,w))\nabla_{w}\hat{v}(s,w)\), and absorbing the \(2\) into the step size gives gradient ascent on the negative error:
\[ w_{t+1}=w_{t}+\alpha_{t}\big(v_{\pi}(s_{t})-\hat{v}(s_{t},w_{t})\big)\nabla_{w}\hat{v}(s_{t},w_{t}). \tag{3.13}\]
The unknown \(v_{\pi}(s_{t})\) in Equation 3.13 must be replaced by a sample return or a bootstrapped target.
| Method | Substitute for \(v_{\pi}(s_{t})\) | Update |
|---|---|---|
| Monte Carlo with FA | \(g_{t}\), the realized discounted return from \(s_{t}\) | \(w_{t+1}=w_{t}+\alpha_{t}\big(g_{t}-\hat{v}(s_{t},w_{t})\big)\nabla_{w}\hat{v}(s_{t},w_{t})\) |
| TD with FA | the TD target \(r_{t+1}+\gamma\hat{v}(s_{t+1},w_{t})\) | \(w_{t+1}=w_{t}+\alpha_{t}\big[r_{t+1}+\gamma\hat{v}(s_{t+1},w_{t})-\hat{v}(s_{t},w_{t})\big]\nabla_{w}\hat{v}(s_{t},w_{t})\) |
For a complete return under fixed \(\pi\), \(\E[g_t\mid s_t]=v_\pi(s_t)\). Sampling states with the weights in Equation 3.12 therefore makes the Monte Carlo increment an unbiased estimate of \(-\tfrac12\nabla J(w_t)\). An artificial cutoff can bias the return.
The true value \(v_\pi\) in \(J\) is fixed, so \(\nabla J\) has no next-state derivative. TD substitutes a target that depends on \(w\) and holds that target fixed during differentiation. For comparison, differentiating the squared sample TD error gives \[ \nabla_w\frac12\big[r+\gamma\hat v(s',w)-\hat v(s,w)\big]^2 =\delta(w)\big[\gamma\nabla_w\hat v(s',w)-\nabla_w\hat v(s,w)\big]. \] Semi-gradient TD keeps only the current-state derivative, giving the update in Table 3.3. This sample loss is a different objective from \(J\).
For a finite ergodic MRP, bounded rewards, full-column-rank linear features and deterministic nonincreasing steps satisfying the Robbins-Monro conditions, on-policy TD converges to the projected Bellman fixed point in the stationary-distribution norm (Tsitsiklis and Van Roy, 1997). That point generally differs from the least-squares minimizer of \(J\). Off-policy sampling changes the state weights and can make the TD recursion diverge, as the next example shows.
Figure 3.3 gives a two-state, one-weight example that the approximator represents exactly. Changing the sampling policy makes the TD updates diverge.
Off-policy semi-gradient TD diverging on a two-state chain
RNG = np.random.default_rng(20260725)
# --- two states, one parameter -----------------------------------------------
# phi(s1) = 1 and phi(s2) = 2, so v(s1, w) = w and v(s2, w) = 2w: one weight moves
# both estimates, which is the whole point of function approximation. Every reward is
# mean zero, so v_pi = 0 at both states and the approximator represents the answer
# EXACTLY, at w = 0. Nothing is wrong with the function class. At s1 there are two
# actions, advance to s2 and dawdle at s1; at s2 the single action returns to s1.
GAMMA, PHI = 0.9, np.array([1.0, 2.0])
ALPHA, SIGMA = 0.01, 0.3 # step size, and the sd of the mean-zero reward
Q_TARGET, P_BEHAV = 1.0, 0.1 # P(advance | s1) under the target, and behavior, policy
W0, N_STEPS, N_RUNS = 1.0, 1600, 60
def run(p_advance, n_steps, alpha=ALPHA, rng=None):
"""Semi-gradient TD, the TD row of @tbl-fa-methods, on data from `p_advance`.
The update is w += alpha * rho * delta * phi(s). The importance ratio
rho = pi(a|s) / b(a|s) corrects the ACTION the behavior policy took, and nothing
corrects the STATE distribution its dawdling induces. Setting p_advance = Q_TARGET
makes rho equal 1 at every step, which is exactly the on-policy run.
"""
rng = RNG if rng is None else rng
w, s, out = W0, 0, np.empty(n_steps + 1)
out[0] = w
for t in range(n_steps):
if s == 0:
adv = rng.random() < p_advance
pi_a, b_a = (Q_TARGET, p_advance) if adv else (1.0 - Q_TARGET, 1.0 - p_advance)
rho, s2 = (pi_a / b_a if b_a > 0 else 0.0), (1 if adv else 0)
else:
rho, s2 = 1.0, 0 # s2 offers a single action, so no correction is needed
delta = rng.normal(0.0, SIGMA) + GAMMA * PHI[s2] * w - PHI[s] * w
w += alpha * rho * delta * PHI[s]
out[t + 1] = w
s = s2
return out
def drift(d1):
"""Coefficient in E[dw] = alpha * c(d1) * w when a share d1 of updates land at s1.
At s1 the TD error is gamma*2w - w and the gradient is phi = 1, contributing
2*gamma - 1. That is POSITIVE for gamma > 1/2: raising the estimate w at s1 raises
its own bootstrapped target 2w twice as fast, so the correction enlarges the error
it was sent to fix. Only updates at s2, worth 2*(gamma - 2), pull back.
"""
return d1 * (2 * GAMMA - 1) + (1 - d1) * 2 * (GAMMA - 2)
def rms(p_advance):
paths = np.array([run(p_advance, N_STEPS) for _ in range(N_RUNS)])
return np.sqrt((paths ** 2).mean(axis=0))
on, off = rms(Q_TARGET), rms(P_BEHAV)
steps = np.arange(N_STEPS + 1)
# The blow-up is structural, not a step size chosen to produce it: the drift
# coefficient is positive whatever alpha is, so shrinking alpha only slows the exit.
for a in (0.001, 0.003, 0.01):
ends = [abs(run(P_BEHAV, 2000, alpha=a, rng=np.random.default_rng(k))[-1])
for k in range(5)]
assert min(ends) > W0, (a, min(ends))
assert off[-1] > 100 * off[0] and on[-1] < on[0] / 10, (off[-1], on[-1])
fig, (axL, axR) = plt.subplots(1, 2, figsize=(9.4, 3.9))
trim(axL), trim(axR)
# --- left: one approximator, one update rule, two sampling distributions ------
axL.semilogy(steps, off, color=COL["vermilion"], lw=2.0, zorder=4)
axL.semilogy(steps, on, color=COL["sky"], lw=2.0, zorder=4)
axL.set_xlim(0, N_STEPS)
axL.set_ylim(on.min() * 0.45, off.max() * 8) # air above the red curve, and below the blue
axL.set_xlabel("updates")
axL.set_ylabel("root mean square weight ($w=0$ is exact)")
axL.set_title("Off-policy, the weight runs away geometrically", fontsize=11)
label_at(axL, N_STEPS * 0.30, off.max() * 2.2, "off-policy sampling",
color=COL["vermilion"], fontsize=9.5)
label_at(axL, N_STEPS * 0.42, on[-1] * 6.0, "on-policy sampling",
color=COL["sky"], fontsize=9.5)
# --- right: the sign of the drift, against where the updates land -------------
# Each tag sits just below and to the right of its own dot, on the empty side of
# the line, so neither the line nor the other tag comes near it.
d = np.linspace(0, 1, 200)
axR.plot([0, 1.02], [0, 0], color=GREY, lw=0.9, ls="--", zorder=2)
axR.plot(d, drift(d), color=INK, lw=1.6, zorder=3)
for x, colour in ((0.5, COL["sky"]), (1.0 / (1.0 + P_BEHAV), COL["vermilion"])):
axR.plot([x], [drift(x)], "o", color=colour, ms=7, zorder=5)
axR.set_xlim(0, 1.22)
axR.set_xticks([0, 0.25, 0.5, 0.75, 1.0]) # the margin past 1 gives the tag room to sit
axR.set_ylim(-2.45, 1.05)
axR.set_xlabel("share of updates made at $s_{1}$")
axR.set_ylabel(r"drift in $w$ per update, in units of $\alpha w$")
axR.set_title("The sign of the drift is set by the sampling", fontsize=11)
label_at(axR, 0.95, 0.22, "off-policy", color=COL["vermilion"], fontsize=9.5)
label_at(axR, 0.56, -1.00, "on-policy", color=COL["sky"], fontsize=9.5)
label_at(axR, 0.03, 0.30, "$w$ runs away", color=GREY, fontsize=9)
label_at(axR, 0.03, -0.36, "$w$ contracts to $0$", color=GREY, fontsize=9)
fig.tight_layout()
plt.show()
Sarsa with function approximation
Approximate \(\hat{q}(s,a,w)\approx q_{\pi}(s,a)\) and substitute it into the TD-with-FA row of Table 3.3:
\[ w_{t+1}=w_{t}+\alpha_{t}\big[\,r_{t+1}+\gamma\hat{q}(s_{t+1},a_{t+1},w_{t})-\hat{q}(s_{t},a_{t},w_{t})\,\big]\nabla_{w}\hat{q}(s_{t},a_{t},w_{t}). \tag{3.14}\]
Pair Equation 3.14 with \(\eps\)-greedy improvement for policy search.
Q-learning with a neural network for \(\hat{q}\) is Deep Q-learning.