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:
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:
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
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)
What to try
- With n = 4, advance from the start of an episode. Nothing is updated for 3 steps; from step 4 onward each step updates the state visited 4 steps earlier.
- Advance past the end of an episode. The walk has stopped, but the last few states still get their updates, one per click, using returns that are now complete.
- In the first episode every estimate is 0, so only states within n steps of the end change. Larger n spreads the final reward further back in one episode.
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:
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
Path taken
Actions strengthened
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:
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:
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.