Chapter 12 · Sutton & Barto

Eligibility traces

n-step methods blend TD and Monte Carlo, but they need to remember the last n states and they update late. Eligibility traces get a similar blend, and often a better one, with a single extra vector and an update on every step. The trick is to look backward: each new TD error is credited to recently visited states, in proportion to how recently and how often they were visited.

01The λ-return: averaging all n-step returns

Instead of picking one n, average all of them, giving weight (1−λ)λn−1 to the n-step return. The weights sum to 1. Any steps past the end of the episode put their weight on the full return Gt:

λ-returnGtλ = (1−λ) Σn=1T−t−1 λn−1 Gt:t+n + λT−t−1 Gt

λ = 0 gives the one-step TD target, and λ = 1 gives the Monte Carlo return. The offline λ-return algorithm waits until the episode ends and then updates every visited state toward Gtλ. That is the forward view: each update looks ahead into the future, which is why it must wait.

02TD(λ): the backward view

TD(λ) keeps an eligibility trace z, a vector the same size as w that records which weights recently contributed to the estimate. On every step the trace decays by γλ and is bumped by the current gradient. Then the one-step TD error is applied to every weight in proportion to its trace:

Semi-gradient TD(λ), accumulating tracesz ← γλ z + ∇v̂(St, w),   δ = Rt+1 + γ v̂(St+1, w) − v̂(St, w),   w ← w + α δ z

With λ = 0, z is just the current gradient, and TD(λ) is one-step semi-gradient TD. With λ = 1 and γ = 1, the trace never decays within an episode, so every earlier state shares in every later TD error: those errors telescope into the Monte Carlo error, giving an online version of MC. In between, credit fades geometrically with time since the visit. Offline, summed over an episode, TD(λ)'s updates approximate the λ-return algorithm's updates. With small α they are almost identical.

TD(λ) on the 19-state random walk

episode 1 · step 0 · RMS error –
0.80
0.30

Eligibility trace z (after this step's decay and bump)

Estimates v̂ vs true values

v̂ (w)true v

What to try

Parameter study: RMS error over the first 10 episodes

Average RMS error over 19 states and first 10 episodes (50 runs)

Reading the study

Each curve is one λ. The x-axis is the step size α. For TD(λ), an intermediate λ around 0.8 reaches the lowest error, much as intermediate n did for n-step TD. With high λ and large α, though, TD(λ) becomes unstable: the curves for λ near 1 shoot off the chart at α well below 1. True online TD(λ) degrades far more gracefully, and its best performance is at least as good.

03True online TD(λ)

TD(λ) only approximates the λ-return algorithm, and the difference grows with α. The online λ-return algorithm redoes all updates from the start of the episode at every step, using the truncated λ-return to the current horizon. It is exact but costly. True online TD(λ) computes exactly the same weight vector, for linear v̂, at the same cost as TD(λ). It does this with a slightly different trace and an extra correction term:

True online TD(λ), linearz ← γλ z + (1 − αγλ z⊤x) x,   w ← w + α (δ + v̂ − v̂old) z − α (v̂ − v̂old) x

Here v̂ = w⊤x for the current state, and v̂old is the value the previous step computed for it. The new trace is the dutch trace. Its form follows mathematically from the forward view; it was derived, not invented. Dutch traces also appear in an exact online equivalence for Monte Carlo, which shows eligibility traces are not tied to TD learning at all.

04Control: Sarsa(λ) and friends

Swap state values for action values and the same machinery gives Sarsa(λ): z ← γλz + ∇q̂(S, A, w), δ = R + γq̂(S′, A′, w) − q̂(S, A, w), w ← w + αδz. On a gridworld where reward comes only at the goal, one-step Sarsa strengthens just the last action after the first episode. Sarsa(λ) strengthens the whole path, with credit fading back toward the start. A true online Sarsa(λ) exists too, and on Mountain Car it beats both accumulating and replacing traces.

Further variants extend traces off-policy. Importance-sampling ratios can enter the trace (per-decision), or tree-backup style traces can weigh by π instead (Tree Backup(λ)). Watkins's Q(λ) cuts the trace whenever a non-greedy action is taken. Combining traces with gradient-TD gives GTD(λ) and its relatives. Finally, λ need not be a constant: making it a function of state lets an agent bootstrap fully in states it trusts and wait for real outcomes elsewhere.

05When to use traces

Traces cost one vector and a few multiplications per step. They pay off most when rewards are delayed and episodes are long, since that is where one-step methods propagate information slowly. In online settings where data is scarce, traces are usually worth it. In offline or replay-heavy settings, where data is cheap, large n or Monte Carlo targets can be computed directly instead.