n-Step TD Prediction in RL
n-step TD waits a few real rewards before guessing the rest, so you tune how far to look ahead between fast one step updates and full episode returns.
Why Does This Exist?
Value estimates need a target. One step TD bootstraps after a single reward as . Variance stays low because only one random step enters, but bias stays high when starts poorly, and a far reward crawls back one state per episode. Monte Carlo waits for the full return with no bootstrap. Bias falls because real rewards replace guesses, but variance rises with each added random step, and learning must wait for termination.
n-step TD sets the lookahead to an integer of at least 1. It sums real rewards, then bootstraps the rest. That dials the bias variance trade directly. Small behaves like one step TD. Large behaves like Monte Carlo. The averaging view that extends this idea is forward view TD lambda.
This page covers the return, the delayed update, and one traced episode that shows reward travel.
Think of It Like This
Daily notes versus quarterly reviews versus a career memoir
A manager judges a new hire three ways.
Daily notes use today output plus a hunch about tomorrow. Fast feedback, heavy hunch. A memoir waits for retirement, sums a whole career with no hunch, but arrives too late and mixes in years of luck. A quarterly review logs three months of real output, then adds a forecast for the rest. Enough fact to anchor judgment, early enough to steer work.
The map holds. Daily notes are TD with . Memoirs are Monte Carlo. Quarterly reviews are n-step with around 3 to 10. Real logged output is summed rewards. The forecast is bootstrapped scaled by .
Where it stops: reviews use judgment for the forecast, while n-step multiplies by an exact discount power and applies a fixed step size through a sliding buffer.
How It Actually Works
At step the agent sits in , takes , lands in , and banks . Discount is in . Episode ends at . Value estimates expected discounted return from .
The n-step return
Sum the first discounted rewards, then bootstrap from the state steps ahead:
is the reward steps ahead. discounts it. is the stored estimate for the landing state. When reaches , cut the sum at and drop the bootstrap because terminal value is zero. Then gives the TD target and past the end gives the Monte Carlo return.
Expected n-step returns shrink worst case error by at least relative to current . Larger cuts bias faster in expectation while adding sample variance. That is why mid values often win in practice.
Updates wait n steps
needs future data, so the update for runs at time . Write for the state due now:
Here in is step size and the bracket is n-step TD error. Other states sit still. After , drain the buffer so states through all get their updates.
Worked example on one episode
Track to terminal with , , , . Set , , all .
- 1-step: . Update gives . The 10.0 reward has no effect on this episode.
- 2-step: . Update gives .
- 3-step: . Update gives 1.40.
- Monte Carlo with : . Update gives .
Longer pulls the far 10.0 back to the start in one episode. Shorter needs more episodes for the same travel.
Code
This snippet recomputes the four start returns and their updates from the worked example.
def n_step_return(rewards: list[float], gamma: float, n: int) -> float: total: float = 0.0 for i in range(min(n, len(rewards))): total += (gamma ** i) * rewards[i] return total
rewards: list[float] = [1.0, 2.0, 0.0, 10.0]for n in [1, 2, 3, 4]: g: float = n_step_return(rewards, 0.9, n) print(f"{n} {g:.2f} {0.5 * g:.3f}") # -> 1 1.00 0.500 etcThe printed triples are , , and updated . They read 1 1.00 0.500, 2 2.80 1.400, 3 2.80 1.400, and 4 10.09 5.045.
Watch Out For
Bootstrapping past termination adds phantom value
Adding when is at or past is a common bug. Symptom: index errors near episode end, or terminal states that slowly gain value that should stay zero.
Sum rewards only to . Add the bootstrap term only when is still before . Keep terminal value fixed at zero.
Stopping at T drops the last buffer
Quitting the loop when time hits skips updates for the final states. Symptom: late states learn slowly or never, while early states look fine.
Loop until so each visited state gets its delayed update. Size the buffer for entries of states and rewards from the start.
The Quick Version
- n-step TD sums real discounted rewards then bootstraps, sitting between TD with and Monte Carlo at the episode end.
- Updates for run at , so recent states and rewards live in a short buffer.
- Expected error shrinks by at least , which cuts bias as grows while variance rises.
- With gamma 0.9, the start return is 1.00 for , 2.80 for , and 10.09 for full Monte Carlo.
- Corresponding start values after step 0.5 are 0.50, 1.40, and 5.045.
- Clamp sums at termination, drop the bootstrap there, and drain the buffer to the last state.