> ## Documentation Index
> Fetch the complete documentation index at: https://aegean.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# n-step TD and the λ-return

> Targets between TD(0) and Monte Carlo.

TD(0) builds its target from one step of experience and the estimate at the next state. Monte Carlo waits for the whole return. [MC vs TD(0)](https://aegean.ai/aiml-common/lectures/reinforcement-learning/value-based-algorithms/prediction/td-vs-mc-mrp/td-vs-mc-mrp.md) showed that the two trade bias against variance. *n-step TD* sits between them: its target uses $n$ steps of experience and then bootstraps from the estimate at the state reached after $n$ steps.

![](https://mintcdn.com/aegeanaiinc/CFTczIUu7w-BdE7V/aiml-common/lectures/reinforcement-learning/value-based-algorithms/prediction/images/n-step-TD.png?fit=max\&auto=format\&n=CFTczIUu7w-BdE7V\&q=85\&s=b9b138645bcc07e08a689d69d9865369)

*Backup trees of n-step TD for increasing $n$. The leftmost tree is TD(0), and the rightmost, which runs to the end of the episode, is MC.*

## The n-step return

The *n-step return* adds $n$ rewards and the discounted estimate at $S_{t+n}$:

$$
G_{t:t+n} = R_{t+1} + \gamma R_{t+2} + \dots + \gamma^{n-1} R_{t+n} + \gamma^{n} V_{t+n-1}(S_{t+n}),
$$

where $V_{t+n-1}$ is the estimate available at time $t+n$. If the episode ends before $t+n$, the n-step return is the full return, $G_{t:t+n} = G_t$. The update of $S_t$ moves its estimate toward the n-step return:

$V_{t+n}(S_t) = V_{t+n-1}(S_t) + \alpha \bigl[ G_{t:t+n} - V_{t+n-1}(S_t) \bigr].$

The estimates of all other states stay the same. The update for $S_t$ has to wait until time $t+n$, when $R_{t+n}$ and $S_{t+n}$ are known. With $n = 1$ this is TD(0), and with $n$ at least as long as the rest of the episode it is constant-$\alpha$ MC (Sutton & Barto, 2018).

## Choosing n

The n-step return has the *error reduction property*. If $V$ has a worst-case error of $\epsilon$, the expected n-step return has a worst-case error of at most $\gamma^n \epsilon$:

$$
\max_s \bigl| \mathbb{E}_\pi[G_{t:t+n} \mid S_t = s] - v_\pi(s) \bigr| \le \gamma^n \max_s \bigl| V_{t+n-1}(s) - v_\pi(s) \bigr|.
$$

A larger $n$ therefore reduces the bias that comes from the estimate. It also adds more random rewards to the target, which increases its variance. On random-walk tasks, intermediate values of $n$ learn faster than either end, TD(0) or MC (Sutton & Barto, 2018). The best $n$ depends on the task and on the step size.

## Optional: the λ-return

This section is optional. The rest of the chapter does not use it.

Instead of choosing one $n$, you can average the n-step returns for all $n$. The *λ-return* weights the n-step return by $(1-\lambda)\lambda^{n-1}$, with $\lambda \in [0, 1]$:

![](https://mintcdn.com/aegeanaiinc/CFTczIUu7w-BdE7V/aiml-common/lectures/reinforcement-learning/value-based-algorithms/prediction/images/TD-lambda-returns.png?fit=max\&auto=format\&n=CFTczIUu7w-BdE7V\&q=85\&s=fb5e038ecaf9af0f1851ddc442f8ffe4)

*Backup diagram of the λ-return: the n-step returns for every $n$, each with its weight.*

$G_t^{\lambda} = (1-\lambda) \sum_{n=1}^{T-t-1} \lambda^{n-1} G_{t:t+n} + \lambda^{T-t-1} G_t.$

All n-step returns that reach the end of the episode equal $G_t$, so their weights are collected in the last term. The weights sum to 1: $(1-\lambda)\sum_{n=1}^{T-t-1}\lambda^{n-1} = 1 - \lambda^{T-t-1}$.

![lambda-weighting-function](https://mintcdn.com/aegeanaiinc/CFTczIUu7w-BdE7V/aiml-common/lectures/reinforcement-learning/value-based-algorithms/prediction/images/lambda-weighting-function.png?fit=max\&auto=format\&n=CFTczIUu7w-BdE7V\&q=85\&s=27d73280290a20d400f34989b4336e0e)

*Weight of each n-step return in the λ-return. The weight falls by a factor of $\lambda$ with every extra step, and the full return gets the remaining weight.*

The *offline λ-return algorithm* waits until the end of the episode and then updates every visited state toward its λ-return:

$V(S_t) \leftarrow V(S_t) + \alpha \bigl[ G^{\lambda}_t - V(S_t) \bigr].$

With $\lambda = 0$ the target is the one-step return of TD(0), and with $\lambda = 1$ it is the full return of MC. TD($\lambda$) reaches a close approximation of the same updates online, one step at a time, with *eligibility traces*. Chapter 12 of Sutton and Barto covers them (Sutton & Barto, 2018).

**Key references**: (Sutton & Barto, 2018)

## References

* Sutton, R., Barto, A. (2018). *Reinforcement Learning: An Introduction*. [Link](https://www.andrew.cmu.edu/course/10-703/textbook/BartoSutton.pdf)

***

Found an error on this page? [Report an issue](https://github.com/aegean-ai/eaia-feedback/issues/new?template=page-issue.yml\&page=/aiml-common/lectures/reinforcement-learning/value-based-algorithms/prediction/n-step-td).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.