n-step Bootstrapping
n-step methods unify Monte Carlo and one-step TD: bootstrap after n real rewards instead of one (TD) or all of them (MC). The best methods are usually in between. They also free us from the "tyranny of the time step".
- The n-step return (7.1) and its truncation at T; why updates are delayed n steps.
- Error reduction property (7.3) — why n-step TD is sound.
- Effect of n and α together (Figure 7.2): intermediate n is best; larger n needs smaller α.
- n-step Sarsa (7.4)–(7.5) and n-step Expected Sarsa (7.7)–(7.8).
- Off-policy n-step: which ρ for state values (ρt:t+n−1) vs action values (ρt+1:t+n) vs Expected Sarsa (ρt+1:t+n−1), and why.
- Control variates (7.13): why they reduce variance without bias.
- Tree backup: off-policy without importance sampling; how the leaf weights work. Q(σ) unifies everything.
In one-step TD, the same time step determines how often the action can change and the interval over which bootstrapping is done. We often want to act quickly, but bootstrapping works best over a span in which a significant, recognisable state change has happened. n-step methods decouple the two. (They also introduce the ideas behind eligibility traces, Ch 12.)
7.1 n-step TD prediction
| Method | Target |
|---|---|
| Monte Carlo | $G_t\doteq R_{t+1}+\gamma R_{t+2}+\dots+\gamma^{T-t-1}R_T$ |
| One-step | $G_{t:t+1}\doteq R_{t+1}+\gamma V_t(S_{t+1})$ |
| Two-step | $G_{t:t+2}\doteq R_{t+1}+\gamma R_{t+2}+\gamma^2V_{t+1}(S_{t+2})$ |
If $t+n\ge T$, the missing terms are zero and $G_{t:t+n}\doteq G_t$ (the full return). The n-step return isn't available until time $t+n$, so:
No updates in the first $n-1$ steps of an episode; the same number of extra updates are made after termination.
Input: a policy π Algorithm parameters: step size α ∈ (0, 1], a positive integer n Initialize V(s) arbitrarily, for all s ∈ S All store and access operations (for S_t and R_t) can take their index mod n + 1 Loop for each episode: Initialize and store S₀ ≠ terminal T ← ∞ Loop for t = 0, 1, 2, …: If t < T, then: Take an action according to π(·|S_t) Observe and store the next reward as R_{t+1} and the next state as S_{t+1} If S_{t+1} is terminal, then T ← t + 1 τ ← t − n + 1 (τ is the time whose state's estimate is being updated) If τ ≥ 0: G ← Σ_{i=τ+1}^{min(τ+n,T)} γ^{i−τ−1} R_i If τ + n < T, then: G ← G + γⁿ V(S_{τ+n}) (G_{τ:τ+n}) V(S_τ) ← V(S_τ) + α [G − V(S_τ)] Until τ = T − 1
The expected n-step return is a better estimate of $v_\pi$ than $V$ is, in the worst-state sense — so all n-step TD methods converge to the correct predictions under appropriate conditions. TD(0) and MC are the extreme members of a family of sound methods.
Exercise 7.1 — with V fixed, the n-step error is a sum of TD errors: $G_{t:t+n}-V(S_t)=\sum_{k=t}^{\min(t+n,T)-1}\gamma^{k-t}\delta_k$ (generalising 6.6).
Example 7.1 — n-step TD on the random walk
5-state walk, first episode C → D → E → right (+1), all $V=0.5$: one-step updates only $V(E)$; two-step updates $V(D)$ and $V(E)$; three-step or more updates all three visited states toward 1, by the same amount.
Figure 7.2 uses a 19-state walk with outcome −1 on the left, +1 on the right, values initialised to 0. Measure: RMS error over the 19 states at the end of each episode, averaged over the first 10 episodes and 100 runs.
- Intermediate n works best (≈ 4–8 here) — better than both TD(0) (n = 1) and MC (n large).
- For every n the curve is U-shaped in α; the larger n, the smaller the best α (longer returns have more variance, so steps must be smaller). Large n with large α blows up the error.
Ex 7.3 — Why a 19-state walk instead of 5? Would a smaller walk shift the best n? What about the −1 left outcome?
Longer episodes are needed for n-step differences to show — on a 5-state walk episodes are short, so any n beyond a few is already MC and the best n would be smaller. With outcome 0 on the left and values initialised to 0, left-terminating episodes carry no learning signal for one-step methods; −1 makes both ends informative and the true values symmetric around 0 (= the initial values), which affects how much bootstrapping helps — it tends to favour somewhat larger n since both ends propagate information.
Ex 7.2 — Would using the sum of TD errors (with V changing each step) in place of the n-step error be better or worse?
It is a slightly different algorithm: each TD error would use the latest estimate, so information from updates made within the last n steps is used sooner. Empirically the two behave similarly on the random walk; the TD-error-sum version can be slightly better for small α and slightly less stable for large α. (The book leaves this as a programming experiment.)
7.2 n-step Sarsa
Switch states for state–action pairs and use an ε-greedy policy. One-step Sarsa is now called Sarsa(0).
Initialize Q(s,a) arbitrarily, for all s ∈ S, a ∈ A Initialize π to be ε-greedy with respect to Q, or to a fixed given policy Algorithm parameters: step size α ∈ (0, 1], small ε > 0, a positive integer n All store and access operations (for S_t, A_t, and R_t) can take their index mod n + 1 Loop for each episode: Initialize and store S₀ ≠ terminal Select and store an action A₀ ∼ π(·|S₀) T ← ∞ Loop for t = 0, 1, 2, …: If t < T, then: Take action A_t Observe and store the next reward as R_{t+1} and the next state as S_{t+1} If S_{t+1} is terminal, then: T ← t + 1 else: Select and store an action A_{t+1} ∼ π(·|S_{t+1}) τ ← t − n + 1 (τ is the time whose estimate is being updated) If τ ≥ 0: G ← Σ_{i=τ+1}^{min(τ+n,T)} γ^{i−τ−1} R_i If τ + n < T, then G ← G + γⁿ Q(S_{τ+n}, A_{τ+n}) (G_{τ:τ+n}) Q(S_τ, A_τ) ← Q(S_τ, A_τ) + α [G − Q(S_τ, A_τ)] If π is being learned, then ensure that π(·|S_τ) is ε-greedy wrt Q Until τ = T − 1
Figure 7.4 — why n-step methods speed up learning
All rewards are 0 except at G, and Q starts at 0. After one episode, one-step Sarsa strengthens only the last action before G; n-step Sarsa strengthens the last n actions — much more is learned from one episode.
Same task as Example 6.5 (ε = 0.1, Q = 0, reward −1 per step). Larger n spreads credit faster, but needs a smaller α.
n-step Expected Sarsa
$\bar V$ of a terminal state is 0. Expected approximate values recur throughout the rest of the book.
Exercise 7.4 — Sarsa's n-step return as a sum of "novel" TD errors: $G_{t:t+n}=Q_{t-1}(S_t,A_t)+\sum_{k=t}^{\min(t+n,T)-1}\gamma^{k-t}\big[R_{k+1}+\gamma Q_k(S_{k+1},A_{k+1})-Q_{k-1}(S_k,A_k)\big]$ — proven by telescoping: the $Q$ terms cancel pairwise, leaving the rewards plus $\gamma^nQ_{t+n-1}(S_{t+n},A_{t+n})$ (or nothing past T).
7.3 n-step off-policy learning (importance sampling)
Weight each n-step update by the relative probability of the n actions under π and $b$:
| Algorithm | Ratio used | Why |
|---|---|---|
| n-step TD (state values) | $\rho_{t:t+n-1}$ | All n actions $A_t\dots A_{t+n-1}$ influenced the return |
| n-step Sarsa (action values) | $\rho_{t+1:t+n}$ — starts and ends one step later | $A_t$ is given (we're learning its value) — only subsequent actions need correction; $A_{t+n}$ is used in the bootstrap $Q(S_{t+n},A_{t+n})$ |
| n-step Expected Sarsa | $\rho_{t+1:t+n-1}$ — one fewer factor | The last state uses an expectation over all actions; the action actually taken there doesn't matter |
If π would never take some action ($\pi=0$), the return gets zero weight. If an action is much likelier under π than $b$, the return is over-weighted (it's characteristic of π but rare in the data). On-policy ($\pi=b$) ⇒ ρ = 1, so (7.9) and (7.11) generalise the on-policy updates.
Input: an arbitrary behavior policy b such that b(a|s) > 0, for all s ∈ S, a ∈ A Initialize Q(s,a) arbitrarily, for all s ∈ S, a ∈ A Initialize π to be greedy with respect to Q, or as a fixed given policy Algorithm parameters: step size α ∈ (0, 1], a positive integer n All store and access operations (for S_t, A_t, and R_t) can take their index mod n + 1 Loop for each episode: Initialize and store S₀ ≠ terminal Select and store an action A₀ ∼ b(·|S₀) T ← ∞ Loop for t = 0, 1, 2, …: If t < T, then: Take action A_t Observe and store the next reward as R_{t+1} and the next state as S_{t+1} If S_{t+1} is terminal, then: T ← t + 1 else: Select and store an action A_{t+1} ∼ b(·|S_{t+1}) τ ← t − n + 1 If τ ≥ 0: ρ ← Π_{i=τ+1}^{min(τ+n,T−1)} π(A_i|S_i) / b(A_i|S_i) (ρ_{τ+1:τ+n}) G ← Σ_{i=τ+1}^{min(τ+n,T)} γ^{i−τ−1} R_i If τ + n < T, then: G ← G + γⁿ Q(S_{τ+n}, A_{τ+n}) (G_{τ:τ+n}) Q(S_τ, A_τ) ← Q(S_τ, A_τ) + α ρ [G − Q(S_τ, A_τ)] If π is being learned, then ensure that π(·|S_τ) is greedy wrt Q Until τ = T − 1
7.4 *Per-decision methods with control variates
Write the n-step return recursively, $G_{t:h}=R_{t+1}+\gamma G_{t+1:h}$ with $G_{h:h}\doteq V_{h-1}(S_h)$. Simply multiplying by $\rho_t$ would make the target 0 whenever $\rho_t=0$ (shrinking the estimate, high variance). Instead:
- If $\rho_t=0$ the target equals the current estimate ⇒ no change (ignore the sample) — sensible.
- The control variate has expected value zero ($\E[\rho_t]=1$, uncorrelated with the estimate) ⇒ the expected update is unchanged; variance is reduced. (Exercise 7.6)
- On-policy ($\rho_t=1$) it reduces to (7.1). Use it with the plain n-step TD update (7.2) — no extra ratios.
Recursion ends with $G_{h:h}\doteq Q_{h-1}(S_h,A_h)$ if $h\lt T$, or $G_{T-1:h}\doteq R_T$ if $h\ge T$. The first action is not importance-sampled. Exercise 7.8: with V fixed, (7.13) is $G_{t:h}=V(S_t)+\sum_{k=t}^{h-1}\gamma^{k-t}\big(\prod_{i=t}^{k}\rho_i\big)\delta_k$.
Importance sampling enables sound off-policy learning but gives high-variance updates (forcing small α, slow learning). Remedies: control variates, adaptive step sizes (Autostep), invariant updates, and — next — avoiding IS altogether.
7.5 Off-policy learning without importance sampling: n-step tree backup
Down the spine: sampled states, rewards and actions. Hanging off each state: the actions not selected — we have no data for them, so we bootstrap from their estimated values. The target is formed from the leaf nodes of the whole tree; interior (taken) action nodes don't contribute directly. Leaf weights:
- first-level non-selected $a$: $\pi(a\mid S_{t+1})$;
- second-level non-selected $a'$: $\pi(A_{t+1}\mid S_{t+1})\,\pi(a'\mid S_{t+2})$;
- third-level: $\pi(A_{t+1}|S_{t+1})\pi(A_{t+2}|S_{t+2})\pi(a''|S_{t+3})$, and so on.
Used with the plain n-step Sarsa update (7.5). Exercise 7.11: with fixed Q, $G_{t:t+n}=Q(S_t,A_t)+\sum_{k=t}^{\min(t+n-1,T-1)}\delta_k\prod_{i=t+1}^{k}\gamma\pi(A_i|S_i)$ with $\delta_t\doteq R_{t+1}+\gamma\bar V_t(S_{t+1})-Q(S_t,A_t)$.
Initialize Q(s,a) arbitrarily, for all s ∈ S, a ∈ A Initialize π to be greedy with respect to Q, or as a fixed given policy Algorithm parameters: step size α ∈ (0, 1], a positive integer n All store and access operations can take their index mod n + 1 Loop for each episode: Initialize and store S₀ ≠ terminal Choose an action A₀ arbitrarily as a function of S₀; Store A₀ T ← ∞ Loop for t = 0, 1, 2, …: If t < T: Take action A_t; observe and store the next reward and state as R_{t+1}, S_{t+1} If S_{t+1} is terminal: T ← t + 1 else: Choose an action A_{t+1} arbitrarily as a function of S_{t+1}; Store A_{t+1} τ ← t + 1 − n If τ ≥ 0: If t + 1 ≥ T: G ← R_T else: G ← R_{t+1} + γ Σ_a π(a|S_{t+1}) Q(S_{t+1}, a) Loop for k = min(t, T − 1) down through τ + 1: G ← R_k + γ Σ_{a≠A_k} π(a|S_k) Q(S_k, a) + γ π(A_k|S_k) G Q(S_τ, A_τ) ← Q(S_τ, A_τ) + α [G − Q(S_τ, A_τ)] If π is being learned, then ensure that π(·|S_τ) is greedy wrt Q Until τ = T − 1
7.6 *A unifying algorithm: n-step Q(σ)
Decide step by step whether to sample the action (σ = 1, like Sarsa) or take the expectation over actions (σ = 0, like tree backup). σt ∈ [0, 1] may even vary continuously and depend on the state/action.
Recursion ends with $G_{h:h}\doteq Q_{h-1}(S_h,A_h)$ if $h\lt T$, or $G_{T-1:T}\doteq R_T$. Use with the plain update (7.5).
| σ at each step | Algorithm obtained |
|---|---|
| σ = 1 always | n-step Sarsa (with per-decision IS / control variates off-policy) |
| σ = 0 always | n-step tree backup |
| σ = 1 except the last step (σ = 0) | n-step Expected Sarsa |
| mixtures / fractional σ | new intermediate algorithms |
Pseudocode: Off-policy n-step Q(σ) (click to expand)
Input: an arbitrary behavior policy b such that b(a|s) > 0, for all s ∈ S, a ∈ A Initialize Q(s,a) arbitrarily, for all s ∈ S, a ∈ A Initialize π to be greedy with respect to Q, or else it is a fixed given policy Algorithm parameters: step size α ∈ (0, 1], a positive integer n Loop for each episode: Initialize and store S₀ ≠ terminal Choose and store an action A₀ ∼ b(·|S₀) T ← ∞ Loop for t = 0, 1, 2, …: If t < T: Take action A_t; observe and store the next reward and state as R_{t+1}, S_{t+1} If S_{t+1} is terminal: T ← t + 1 else: Choose and store an action A_{t+1} ∼ b(·|S_{t+1}) Select and store σ_{t+1} Store π(A_{t+1}|S_{t+1}) / b(A_{t+1}|S_{t+1}) as ρ_{t+1} τ ← t − n + 1 If τ ≥ 0: If t + 1 < T: G ← Q(S_{t+1}, A_{t+1}) Loop for k = min(t + 1, T) down through τ + 1: if k = T: G ← R_T else: V̄ ← Σ_a π(a|S_k) Q(S_k, a) G ← R_k + γ (σ_k ρ_k + (1 − σ_k) π(A_k|S_k)) (G − Q(S_k, A_k)) + γ V̄ Q(S_τ, A_τ) ← Q(S_τ, A_τ) + α [G − Q(S_τ, A_τ)] If π is being learned, then ensure that π(·|S_τ) is greedy wrt Q Until τ = T − 1
7.7 Summary
| Method | Target / return | On/off | IS needed? | Key property |
|---|---|---|---|---|
| n-step TD | $G_{t:t+n}$ (7.1) with $V$ | on (off with $\rho_{t:t+n-1}$) | off-policy only | Bridges TD(0) and MC; error reduction |
| n-step Sarsa | (7.4) with $Q(S_{t+n},A_{t+n})$ | on (off with $\rho_{t+1:t+n}$) | off-policy only | Faster credit propagation (Fig 7.4) |
| n-step Expected Sarsa | (7.7) with $\bar V(S_{t+n})$ | on/off ($\rho_{t+1:t+n-1}$) | off-policy only | Lower variance at the last step |
| Control-variate versions | (7.13), (7.14) | off | per-decision | Same expectation, lower variance |
| Tree backup | (7.16) | off | No | Multi-step Q-learning; short effective span if π, b differ |
| Q(σ) | (7.17) | off | partially (σ) | Unifies all of the above |
(1) Delay of n steps before updating; (2) more computation per step; (3) more memory to store the last n states, actions, rewards. Eligibility traces (Ch 12) reduce memory/computation, but some extra cost always remains — "well worth paying to escape the tyranny of the single time step".
Hyperparameters in this chapter
| Parameter | Role | Guidance |
|---|---|---|
| n | How many real rewards before bootstrapping. n = 1 → TD(0); n ≥ episode length → MC | Intermediate n is usually best. Small n: more bias (relies on estimates), low variance. Large n: less bias, more variance, longer delay and memory. |
| α | Step size | Interacts with n: the best α shrinks as n grows (Fig 7.2). |
| σ (Q(σ)) | Degree of sampling per step | σ = 1 Sarsa-like (needs IS off-policy), σ = 0 tree-backup-like (no IS). |
| b vs π | Behaviour vs target policy | The more they differ, the higher the IS variance (or the shorter the tree-backup span). |
Self-test
Write the n-step return and say what happens when t + n ≥ T.
$G_{t:t+n}=R_{t+1}+\gamma R_{t+2}+\dots+\gamma^{n-1}R_{t+n}+\gamma^nV(S_{t+n})$; if $t+n\ge T$ it is just the full return $G_t$.
Why can't the n-step update for $S_t$ be made at time t+1?
The target needs $R_{t+n}$ and $V_{t+n-1}(S_{t+n})$, first available at time $t+n$.
What does the error reduction property guarantee?
The worst-case error of the expected n-step return is at most $\gamma^n$ times the worst-case error of the current V — so n-step TD methods converge (under conditions).
Why does off-policy n-step Sarsa use $\rho_{t+1:t+n}$ rather than $\rho_{t:t+n-1}$?
We are learning the value of the action $A_t$ that was actually taken, so its probability is irrelevant; only the subsequent actions $A_{t+1},\dots,A_{t+n}$ (including the one in the bootstrap term) must be corrected.
How does a control variate reduce variance without introducing bias?
It adds $(1-\rho_t)V(S_t)$, whose expectation is 0 since $\E[\rho_t]=1$; when ρ is 0 the target collapses to the current estimate (no change) instead of 0.
How does tree backup avoid importance sampling?
It never uses a sampled action's return beyond what π would weight: each step mixes the expected values of non-selected actions (weighted by π) with the continuation through the taken action weighted by π(A|S). Since everything is weighted by target-policy probabilities, no ratio π/b is needed.
Which σ settings in Q(σ) give Sarsa, tree backup and Expected Sarsa?
σ = 1 everywhere → Sarsa; σ = 0 everywhere → tree backup; σ = 1 except σ = 0 on the final step → Expected Sarsa.
Ex 7.6 — Prove the control variate in (7.13) doesn't change the expected return.
$\E[(1-\rho_t)V(S_t)\mid S_t]=V(S_t)(1-\E_b[\rho_t\mid S_t])=V(S_t)(1-\sum_ab(a|S_t)\frac{\pi(a|S_t)}{b(a|S_t)})=V(S_t)(1-1)=0$.
Three costs of n-step methods vs one-step methods?
n-step delay before updating, more computation per step, more memory for the last n transitions.
Chapter quiz — check your understanding
Scored quiz: pick an answer (or type a number) to get instant feedback and an explanation. Questions marked Exam-style give you values to interpret (sweeps, logs, returns, learning curves), the way exam problems do. Your best score is saved in this browser and shown on the home page. "Shuffle & restart" re-orders the questions.