Skip to content
Lambdian

n-step TD and TD(λ) with eligibility traces

Blend one-step TD and Monte Carlo: n-step returns, the λ-return and eligibility traces.

Animated explanation

The idea

TD(0) looks one step ahead; Monte Carlo looks all the way to the end. Neither is always best. n-step TD looks nn real rewards ahead and then bootstraps, trading bias against variance with a single integer.

TD(λ) goes further and averages all n-step returns, giving weight (1−λ)λn−1(1-\lambda)\lambda^{n-1} to the nn-step return. With λ=0\lambda=0 it is TD(0); with λ=1\lambda=1 it is Monte Carlo. The elegant way to compute it online is with eligibility traces: a short-term memory of which states were visited recently. Each TD error then updates all of them at once, so credit for a reward flows back quickly along the path that led to it.

Forward and backward views

The forward view defines the target (the λ-return), which needs future rewards. The backward view uses traces to apply equivalent updates incrementally, one step at a time; for offline updating the two are exactly equivalent (and "true online TD(λ)" makes them match online too). Intermediate λ (0.7–0.9) often learns fastest.

Traces for control

SARSA(λ) keeps traces over state–action pairs. For Q-learning, traces must be cut whenever a non-greedy action is taken (Watkins's Q(λ)), because the greedy target no longer describes what happened. In deep RL the same bias–variance dial appears as n-step returns and GAE, used by A2C and PPO.

The algorithm, step by step

  1. 1
    Initialise V(s) and traces e(s) = 0 at the start of each episode.
  2. 2
    At each step: compute δ = r + γV(s′) − V(s).
  3. 3
    Decay all traces e ← γλe, then e(s) ← e(s) + 1 (accumulating) for the current state.
  4. 4
    Update all states: V ← V + αδe. Move on to s′.

The maths

n-step return

Gt(n)=∑k=0n−1γkrt+k+1+γnV(st+n)G_t^{(n)} = \sum_{k=0}^{n-1}\gamma^kr_{t+k+1} + \gamma^nV(s_{t+n})

λ-return

Gtλ=(1−λ)∑n≥1λn−1Gt(n)G_t^\lambda = (1-\lambda)\sum_{n\ge1}\lambda^{n-1}G_t^{(n)}

Trace update

et=γλet−1+1st\mathbf{e}_t = \gamma\lambda\mathbf{e}_{t-1} + \mathbf{1}_{s_t}

TD(λ) update

V←V+α δt etV \leftarrow V + \alpha\,\delta_t\,\mathbf{e}_t

Strengths and weaknesses

Strengths

  • One knob spans TD(0) to Monte Carlo
  • Faster credit assignment than one-step TD
  • Online and incremental via traces

Weaknesses

  • Extra memory and computation for traces
  • Another hyperparameter (λ)
  • Off-policy traces need care

Reach for it when…

Prediction or control where rewards are delayed and one-step TD propagates credit too slowly.

Check your understanding

Question 1

Q1TD(λ) with λ = 0 is…

Question 2

Q2What does an eligibility trace record?

Question 3

Q3The weight on the 3-step return in the λ-return is…

Lambdian Lab

n-step TD and TD(λ) on the random walk

The random walk is provided (γ = 1).

  • n_step_td(episodes, n, alpha, seed): n-step TD prediction from V = 0.5; return V.
  • td_lambda(episodes, lam, alpha, seed): backward-view TD(λ) with accumulating traces (reset each episode); return V.
  • With lam = 0, td_lambda must equal TD(0) step for step.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.