Concepts / n-Step TD Algorithm and Error Reduction Property

n-Step TD Algorithm and Error Reduction Property

The n-Step TD Algorithm waits for several steps of experience before updating an earlier state's value estimate.

  • Programming

Why Delay the Update

The n-Step TD Algorithm does not immediately update the value estimate for the current state after one transition. Instead, it waits for several steps of experience, uses that information to construct an n-step return G, and then updates an earlier state estimate. The central idea is to connect two operations: first calculate the return for the state at time τ, then move V(Sτ) toward that return.

The algorithm updates an earlier state after enough later experience has become available. The state being updated is identified by τ, not simply by the most recently observed time index.

later experiencethrough n stepsinformation becomes availableSτvalue being updatedSτ+1later stateSτ+nn-step endpointtexperience currentlyobserved
Which earlier state Sτ is being updated after the agent has observed n steps of experience?

Finding the Update Index

The time index τ identifies the state whose estimate is eligible for updating. The algorithm first gathers the next reward and state while t is still before the episode's terminal time T. It then identifies τ. Only an eligible τ proceeds to return calculation and value-function updating.

observecontinue across stepsreach boundarycombineadd later estimate when episode continuesSτstate being updatedRτ+1discounted rewardRτ+ndiscounted rewardSτ+nlater value estimate wheneligibleGn-step return
How do the observed rewards from time τ+1 through τ+n combine with a later value estimate at the endpoint?

Constructing G

The n-step return G is the target used for the update. It combines the discounted rewards observed from the state at time τ through the n-step boundary. If that boundary occurs before the episode terminates, G also includes a later value estimate. If the episode terminates before the boundary, the continuation value term is absent.

G = discounted rewards from time τ+1 through the n-step boundary, plus a discounted later value estimate when the boundary occurs before termination

The important distinction is whether the episode has already ended. Continuing episodes provide both observed reward information and a later value estimate. An episode that ends early provides only the rewards that were actually observed before termination; there is no continuation value beyond the terminal point.

enough timing informationτ is eligibleuse G as targetCollect experiencerewards and statesIdentify τeligible earlier stateConstruct Gdiscounted rewards andcontinuation when allowedUpdate V(Sτ)move toward G
What happens in order as experience is collected, the n-step return is formed, and V(Sτ) is updated?

Applying the Value Update

Moving an Estimate Toward G

Suppose the current estimate is V(Sτ) = 6, the constructed n-step return is G = 10, and the step size is α = 0.5.

Find the difference: The difference between the target and the current estimate is G - V(Sτ) = 10 - 6 = 4.

Scale the difference: The step size uses half of that difference: α[G - V(Sτ)] = 0.5 × 4 = 2.

Apply the update: Add the scaled difference to the current estimate: V(Sτ) becomes 6 + 2 = 8.

The estimate moves from 6 to 8. It moves toward G rather than replacing the old estimate with G.

V(Sτ) ← V(Sτ) + α[G - V(Sτ)]

The step size α controls how much of the difference is applied. The source specifies that α lies in the interval (0, 1], so the update uses a fraction controlled by α of the gap between G and the current estimate. The old estimate is not directly replaced by G.

add α[G - V(Sτ)]direction of movementV(Sτ)current estimateGn-step returnV(Sτ)updated estimate
How does V(Sτ) change when the n-step return G is above or below the current estimate?

Understanding Error Reduction

The update reduces the gap between the current estimate and the constructed n-step target whenever α is in (0, 1]. The amount changed is α[G - V(Sτ)]. Thus, if G is above V(Sτ), the estimate increases; if G is below V(Sτ), the estimate decreases; and if they are equal, the update makes no change.

compare with targetscale by αapply changemoves towardV(Sτ)current estimateG - V(Sτ)current differenceα[G - V(Sτ)]applied portionUpdated V(Sτ)closer to GGn-step target
How does the update reduce the gap between the current value estimate and the n-step target?

Terminal Episodes

The terminal time T controls both the reward sum and the end of the repeated process. The algorithm gathers the next reward and state only while t is before T. When an episode terminates before the n-step boundary, the n-step return contains the discounted rewards that were observed, but not a continuation value estimate beyond termination.

t before Tepisode terminatesidentify update indexcontinue eligibility checkτ ≥ 0Check t against Tterminal-time checkGather reward andstateepisode continuesStop at Tno later continuation termCheck τeligibility conditionUpdate V(Sτ)τ is nonnegative
When an episode terminates before n steps, which terms disappear, and under what condition is the update for Sτ performed?

Implementation Mistakes

  • Updating when τ is negative

    The algorithm is attempting to update an earlier state that is not yet eligible in its indexing scheme.

    Fix: Check the eligibility condition before calculating G and updating V(Sτ).

  • Never updating when τ is nonnegative

    The problem is likely in the condition surrounding the update or in the calculation of τ.

    Fix: Trace τ and the conditional branch that should lead to return calculation and value updating.

  • Adding a continuation value after termination

    A continuation term is used when the n-step boundary occurs before termination; it is absent when the episode terminates first.

    Fix: Use the terminal time T to limit the reward sum and omit the continuation value term after termination.

  • Replacing the estimate with G

    The algorithm moves the estimate toward G by the amount α[G - V(Sτ)] rather than replacing the old estimate directly.

    Fix: Apply V(Sτ) ← V(Sτ) + α[G - V(Sτ)].

Practice Check

MEDIUM

An implementation has collected enough experience to identify a nonnegative τ. The episode terminated before the n-step boundary. Describe which information belongs in G, state whether a continuation value estimate is included, and write the update that changes V(Sτ).

Hints
  • Use the terminal time T to decide whether the continuation term is allowed.
  • The observed discounted rewards remain part of the return.
  • The update uses the difference between G and the current estimate.

What do you think happens?

If G is below the current value estimate and α is in (0, 1], does the update increase or decrease V(Sτ)?

  • Increase
  • Decrease
  • Leave it unchanged in every case
Reveal answer

Answer: Decrease

When G is below V(Sτ), the difference G - V(Sτ) is negative. Multiplying that difference by α and adding it to the current estimate moves V(Sτ) downward toward G.

Key Takeaways

  1. The n-Step TD Algorithm waits for several steps of experience before updating an earlier state estimate.
  2. τ identifies the state Sτ whose value estimate is updated; only an eligible nonnegative τ should proceed to the update.
  3. The n-step return G combines discounted observed rewards and, when the n-step boundary occurs before termination, a later value estimate.
  4. The update moves V(Sτ) toward G by α[G - V(Sτ)] rather than replacing the old estimate directly.
  5. The terminal time T, the continuation term, and the condition on τ are essential control-flow checkpoints.

Key Takeaways

  • The algorithm delays updating an earlier state until several steps of experience are available.
  • The index τ determines which state estimate receives the update.
  • The return G contains discounted rewards and may contain a later value estimate, but termination removes the continuation term.
  • The update V(Sτ) ← V(Sτ) + α[G - V(Sτ)] moves the estimate toward the n-step target.
  • Debugging should begin by checking τ, the terminal time T, and whether the continuation term is used only when allowed.