Chapter 7 · Book pp. 141–158

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".

n-step return Gt:t+n n-step TD n-step Sarsa / Expected Sarsa Off-policy with ρ Control variates Tree backup Q(σ)
Exam focus for this chapter
The tyranny of the time step

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

Figure 7.1 — the spectrum of n-step updates. They are still TD methods: they change an earlier estimate based on how it differs from a later estimate — now n steps later.
MethodTarget
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})$
$$G_{t:t+n}\doteq R_{t+1}+\gamma R_{t+2}+\dots+\gamma^{n-1}R_{t+n}+\gamma^nV_{t+n-1}(S_{t+n}),\qquad n\ge1,\ 0\le t\lt T-n$$
n-step return (truncated, then corrected by V)(7.1)

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:

$$V_{t+n}(S_t)\doteq V_{t+n-1}(S_t)+\alpha\big[G_{t:t+n}-V_{t+n-1}(S_t)\big],\qquad 0\le t\lt T$$
n-step TD update (other states unchanged)(7.2)

No updates in the first $n-1$ steps of an episode; the same number of extra updates are made after termination.

n-step TD for estimating V ≈ vπon-policybootstrapsn-step
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
$$\max_s\Big|\E_\pi[G_{t:t+n}\mid S_t=s]-v_\pi(s)\Big| \le \gamma^n\max_s\Big|V_{t+n-1}(s)-v_\pi(s)\Big|$$
Error reduction property(7.3)

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.

Try it: Figure 7.2 — performance of n-step TD as a function of α and n
Reading Figure 7.2 (hyperparameter interaction)
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).

$$G_{t:t+n}\doteq R_{t+1}+\gamma R_{t+2}+\dots+\gamma^{n-1}R_{t+n}+\gamma^nQ_{t+n-1}(S_{t+n},A_{t+n}),\qquad G_{t:t+n}\doteq G_t \text{ if } t+n\ge T$$
n-step return for Sarsa(7.4)
$$Q_{t+n}(S_t,A_t)\doteq Q_{t+n-1}(S_t,A_t)+\alpha\big[G_{t:t+n}-Q_{t+n-1}(S_t,A_t)\big]$$
n-step Sarsa update(7.5)
Figure 7.3 — Sarsa diagrams start and end with an action. Expected Sarsa's last step branches over all actions weighted by π.
n-step Sarsa for estimating Q ≈ q* or qπon-policybootstrapsn-step
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.

See it: which action values one episode strengthens
Try it: n-step Sarsa on the windy gridworld

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

$$G_{t:t+n}\doteq R_{t+1}+\dots+\gamma^{n-1}R_{t+n}+\gamma^n\bar V_{t+n-1}(S_{t+n}),\quad t+n\lt T \qquad \bar V_t(s)\doteq\sum_a\pi(a\mid s)Q_t(s,a)$$
n-step Expected Sarsa return and expected approximate value(7.7), (7.8)

$\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$:

$$\rho_{t:h}\doteq\prod_{k=t}^{\min(h,T-1)}\frac{\pi(A_k\mid S_k)}{b(A_k\mid S_k)}$$
Importance-sampling ratio over a span(7.10)
$$V_{t+n}(S_t)\doteq V_{t+n-1}(S_t)+\alpha\,\rho_{t:t+n-1}\big[G_{t:t+n}-V_{t+n-1}(S_t)\big]$$
Off-policy n-step TD(7.9)
$$Q_{t+n}(S_t,A_t)\doteq Q_{t+n-1}(S_t,A_t)+\alpha\,\rho_{t+1:t+n}\big[G_{t:t+n}-Q_{t+n-1}(S_t,A_t)\big]$$
Off-policy n-step Sarsa(7.11)
AlgorithmRatio usedWhy
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 factorThe 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.

Off-policy n-step Sarsa for estimating Q ≈ q* or qπoff-policyimportance samplingbootstraps
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:

$$G_{t:h}\doteq\rho_t\big(R_{t+1}+\gamma G_{t+1:h}\big)+\underbrace{(1-\rho_t)V_{h-1}(S_t)}_{\text{control variate}},\qquad t\lt h\lt T$$
Off-policy n-step return with control variate (state values)(7.13)
$$G_{t:h}\doteq R_{t+1}+\gamma\Big(\rho_{t+1}G_{t+1:h}+\bar V_{h-1}(S_{t+1})-\rho_{t+1}Q_{h-1}(S_{t+1},A_{t+1})\Big) = R_{t+1}+\gamma\rho_{t+1}\big(G_{t+1:h}-Q_{h-1}(S_{t+1},A_{t+1})\big)+\gamma\bar V_{h-1}(S_{t+1})$$
Off-policy n-step return with control variate (action values)(7.14)

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:

$$G_{t:t+1}\doteq R_{t+1}+\gamma\sum_a\pi(a\mid S_{t+1})Q_t(S_{t+1},a)\quad\text{(= Expected Sarsa)}$$
One-step tree-backup return(7.15)
$$G_{t:t+n}\doteq R_{t+1}+\gamma\sum_{a\ne A_{t+1}}\pi(a\mid S_{t+1})Q_{t+n-1}(S_{t+1},a)+\gamma\,\pi(A_{t+1}\mid S_{t+1})\,G_{t+1:t+n}$$
n-step tree-backup return (recursive)(7.16)

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)$.

n-step Tree Backup for estimating Q ≈ q* or qπoff-policyno importance samplingbootstraps
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
The natural multi-step extension of Q-learning to stochastic target policies. Behaviour actions can be chosen "arbitrarily". Weakness: if π and b differ a lot, $\pi(A_k|S_k)$ is often small (0 for non-greedy actions under greedy π), so the effective bootstrapping span is only a few steps even with large n.

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.

$$G_{t:h}\doteq R_{t+1}+\gamma\Big(\sigma_{t+1}\rho_{t+1}+(1-\sigma_{t+1})\pi(A_{t+1}\mid S_{t+1})\Big)\Big(G_{t+1:h}-Q_{h-1}(S_{t+1},A_{t+1})\Big)+\gamma\bar V_{h-1}(S_{t+1})$$
n-step Q(σ) return(7.17)

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 stepAlgorithm obtained
σ = 1 alwaysn-step Sarsa (with per-decision IS / control variates off-policy)
σ = 0 alwaysn-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

MethodTarget / returnOn/offIS needed?Key property
n-step TD$G_{t:t+n}$ (7.1) with $V$on (off with $\rho_{t:t+n-1}$)off-policy onlyBridges 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 onlyFaster 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 onlyLower variance at the last step
Control-variate versions(7.13), (7.14)offper-decisionSame expectation, lower variance
Tree backup(7.16)offNoMulti-step Q-learning; short effective span if π, b differ
Q(σ)(7.17)offpartially (σ)Unifies all of the above
Costs of n-step methods

(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

ParameterRoleGuidance
nHow many real rewards before bootstrapping. n = 1 → TD(0); n ≥ episode length → MCIntermediate n is usually best. Small n: more bias (relies on estimates), low variance. Large n: less bias, more variance, longer delay and memory.
αStep sizeInteracts 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 policyThe 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.