Skip to content
AI360Xpert
Beta

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.

n-step returns sum n real rewards then bootstrap, so a 2-step return of 2.80 moves the start value to 1.40 while 1-step sees only 1.00
n-step returns sum n real rewards then bootstrap, so a 2-step return of 2.80 moves the start value to 1.40 while 1-step sees only 1.00

Why Does This Exist?

Value estimates need a target. One step TD bootstraps after a single reward as Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1}). Variance stays low because only one random step enters, but bias stays high when VV starts poorly, and a far reward crawls back one state per episode. Monte Carlo waits for the full return GtG_t 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 nn of at least 1. It sums nn real rewards, then bootstraps the rest. That dials the bias variance trade directly. Small nn behaves like one step TD. Large nn 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 n=1n = 1. Memoirs are Monte Carlo. Quarterly reviews are n-step with nn around 3 to 10. Real logged output is summed rewards. The forecast is bootstrapped V(St+n)V(S_{t+n}) scaled by γn\gamma^n.

Where it stops: reviews use judgment for the forecast, while n-step multiplies by an exact discount power and applies a fixed step size α\alpha through a sliding buffer.

How It Actually Works

At step tt the agent sits in StS_t, takes AtA_t, lands in St+1S_{t+1}, and banks Rt+1R_{t+1}. Discount is γ\gamma in [0,1][0, 1]. Episode ends at TT. Value V(s)V(s) estimates expected discounted return from ss.

The n-step return

Sum the first nn discounted rewards, then bootstrap from the state nn steps ahead:

Gt:t+n=Rt+1+γRt+2+⋯+γn−1Rt+n+γnV(St+n)G_{t:t+n} = R_{t+1} + \gamma R_{t+2} + \dots + \gamma^{n-1} R_{t+n} + \gamma^n V(S_{t+n})

Rt+kR_{t+k} is the reward kk steps ahead. γk−1\gamma^{k-1} discounts it. V(St+n)V(S_{t+n}) is the stored estimate for the landing state. When t+nt + n reaches TT, cut the sum at TT and drop the bootstrap because terminal value is zero. Then n=1n = 1 gives the TD target and nn past the end gives the Monte Carlo return.

Expected n-step returns shrink worst case error by at least γn\gamma^n relative to current VV. Larger nn cuts bias faster in expectation while adding sample variance. That is why mid values often win in practice.

Updates wait n steps

Gt:t+nG_{t:t+n} needs future data, so the update for StS_t runs at time t+nt + n. Write τ=t−n+1\tau = t - n + 1 for the state due now:

V(Sτ)←V(Sτ)+α[Gτ:τ+n−V(Sτ)]V(S_{\tau}) \leftarrow V(S_{\tau}) + \alpha [G_{\tau:\tau+n} - V(S_{\tau})]

Here α\alpha in (0,1](0, 1] is step size and the bracket is n-step TD error. Other states sit still. After TT, drain the buffer so states τ=T−n+1\tau = T - n + 1 through T−1T - 1 all get their updates.

Worked example on one episode

Track S0S_0 to S4S_4 terminal with R1=1.0R_1 = 1.0, R2=2.0R_2 = 2.0, R3=0.0R_3 = 0.0, R4=10.0R_4 = 10.0. Set γ=0.9\gamma = 0.9, α=0.5\alpha = 0.5, all V=0.0V = 0.0.

  • 1-step: G0:1=1.0+0.9×0.0=1.00G_{0:1} = 1.0 + 0.9 \times 0.0 = 1.00. Update gives 0.0+0.5×1.00=0.500.0 + 0.5 \times 1.00 = 0.50. The 10.0 reward has no effect on S0S_0 this episode.
  • 2-step: G0:2=1.0+0.9×2.0=2.80G_{0:2} = 1.0 + 0.9 \times 2.0 = 2.80. Update gives 0.0+0.5×2.80=1.400.0 + 0.5 \times 2.80 = 1.40.
  • 3-step: G0:3=1.0+1.80+0.81×0.0=2.80G_{0:3} = 1.0 + 1.80 + 0.81 \times 0.0 = 2.80. Update gives 1.40.
  • Monte Carlo with n=4n = 4: G0:4=1.0+1.80+0.0+0.729×10.0=10.09G_{0:4} = 1.0 + 1.80 + 0.0 + 0.729 \times 10.0 = 10.09. Update gives 0.0+0.5×10.09=5.0450.0 + 0.5 \times 10.09 = 5.045.

Longer nn pulls the far 10.0 back to the start in one episode. Shorter nn 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 etc

The printed triples are nn, G0:nG_{0:n}, and updated V(S0)V(S_0). 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 γnV(Sτ+n)\gamma^n V(S_{\tau+n}) when τ+n\tau + n is at or past TT 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 min⁡(τ+n,T)\min(\tau + n, T). Add the bootstrap term only when τ+n\tau + n is still before TT. Keep terminal value fixed at zero.

Stopping at T drops the last buffer

Quitting the loop when time hits TT skips updates for the final n−1n - 1 states. Symptom: late states learn slowly or never, while early states look fine.

Loop until τ=T−1\tau = T - 1 so each visited state gets its delayed update. Size the buffer for n+1n + 1 entries of states and rewards from the start.

The Quick Version

  • n-step TD sums nn real discounted rewards then bootstraps, sitting between TD with n=1n = 1 and Monte Carlo at the episode end.
  • Updates for StS_t run at t+nt + n, so recent states and rewards live in a short buffer.
  • Expected error shrinks by at least γn\gamma^n, which cuts bias as nn grows while variance rises.
  • With gamma 0.9, the start return is 1.00 for n=1n = 1, 2.80 for n=2n = 2, 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.