Chapter 7 · Sutton & Barto

n-step bootstrapping

One-step TD looks one reward ahead and then trusts its estimate. Monte Carlo looks all the way to the end of the episode. n-step methods look n rewards ahead and then bootstrap, giving a dial between the two. The best setting is usually somewhere in the middle.

01The n-step return

The Monte Carlo target is the full return, and the TD(0) target is one reward plus the discounted estimate of the next state. The n-step return takes n real rewards and then the discounted estimate of the state reached n steps later:

n-step returnGt:t+n = Rt+1 + γRt+2 + … + γn−1Rt+n + γn Vt+n−1(St+n)

If the episode ends before step t+n, the bootstrap term is dropped and Gt:t+n is just the actual return Gt. With n = 1 this is the TD(0) target; with n at least the episode length it is the Monte Carlo target.

02n-step TD prediction

The catch is timing. The return for time t is not known until time t+n, so the state visited at time t is updated n steps later:

n-step TD update, made at time t+nVt+n(St) = Vt+n−1(St) + α [ Gt:t+n − Vt+n−1(St) ]

No updates happen during the first n−1 steps of an episode. After the episode ends, the last n−1 states still owe an update, which is made using the (now complete) returns. The reason this works is the error-reduction property: in expectation, the n-step return is a better estimate of vπ than V is, by a factor of at least γn in the worst state. So repeatedly moving toward it converges.

03Watch the delay on a 19-state random walk

A larger version of the walk from the TD vs MC page: 19 states, start in the middle, move left or right with equal chance. Leaving on the left gives −1, on the right +1, everything else 0, γ = 1. The true values run evenly from −0.9 to +0.9. Estimates start at 0.

n-step TD, one step at a time

episode 1 · time t = 0 · RMS error –
4
0.40

Amber: the state Sτ being updated now. Teal: the state Sτ+n whose estimate is bootstrapped from. Light outline: the n steps in between. Changing n resets.

Estimated values V(s)

True valuen-step TD estimate

What to try

04Which n is best?

The study below repeats Sutton & Barto's Figure 7.2: for each n and α, run 10 episodes, measure the RMS error over all 19 states after each episode, and average over episodes and over 100 runs. Every combination sees the same episodes. Lower is better; errors above 0.55 are cut off.

Error after 10 episodes, by n and α

Average RMS error over 19 states and the first 10 episodes

Intermediate n wins: n = 4 to 8 reaches a lower error than either one-step TD (n = 1) or the near-Monte Carlo extremes (n = 256, 512). Every curve also has its own best α, and it shrinks as n grows, because longer returns have more variance and need smaller steps to average out.

05n-step Sarsa

For control, switch from states to state–action pairs and bootstrap from an action value:

n-step Sarsa return and updateGt:t+n = Rt+1 + … + γn−1Rt+n + γn Q(St+n, At+n),   Q(St, At) ← Q(St, At) + α [ Gt:t+n − Q(St, At) ]

The payoff shows clearly in a gridworld where the only reward is at the goal. After the first episode, one-step Sarsa has strengthened only the last action before the goal; every other value is still zero. n-step Sarsa strengthens the last n actions from that single episode. In n-step Expected Sarsa the last term is replaced by the expected value Σa π(a|St+n) Q(St+n, a).

What one episode teaches

10

Path taken

Actions strengthened

one-step Sarsa
10-step Sarsa

All action values start at 0; reaching G gives +1 and every other reward is 0. Highlighted arrows are the action values that change after this one episode (γ = 1).

06Off-policy n-step learning

To learn about a target policy π from data generated by a behaviour policy b, weight each n-step update by how much more (or less) likely those n actions are under π than under b:

Off-policy n-step Sarsaρt+1:t+n = Πk=t+1min(t+n, T−1) π(Ak|Sk) / b(Ak|Sk),   Q(St, At) ← Q(St, At) + α ρt+1:t+n [ Gt:t+n − Q(St, At) ]

The ratio starts at t+1 because the first action At is the one being evaluated, so its probability doesn't matter. If π would never take one of the actions, ρ is 0 and the update is skipped. If π takes an action b rarely takes, ρ is large and the update can be wild. Weighting each reward by the ratios up to that point and adding control variates reduces the variance.

07Tree backup: off-policy without importance sampling

The tree-backup algorithm avoids ratios altogether. At each step along the path, it uses the actual next action for the branch that was taken, and the current estimates, weighted by π, for all the branches that were not:

Tree-backup return (recursive form)Gt:t+n = Rt+1 + γ Σa≠At+1 π(a|St+1) Q(St+1, a) + γ π(At+1|St+1) Gt+1:t+n

With n = 1 it reduces to Expected Sarsa, and with a greedy π, to Q-learning. Its weakness is that when π and b disagree often, the weights π(At+1|St+1) shrink the multi-step part quickly, and it behaves like a short-horizon method.

08Q(σ): one algorithm for all of them

At each step, choose between sampling (follow the action taken, as Sarsa does, with an importance ratio) and expectation (average over actions, as tree backup does). Let σt ∈ [0, 1] set the mix at step t. σ = 1 everywhere gives n-step Sarsa; σ = 0 everywhere gives tree backup; σ = 0 only on the last step gives n-step Expected Sarsa. Mixed settings, or σ that changes with the state, are also possible.

The cost of n-step methods is memory and delay: they must store the last n states, actions and rewards, and every update arrives n steps late. Chapter 12's eligibility traces get the same benefit with constant memory and immediate updates.