Bootstrapping on a blackboard: a late reward, one borrowed update and what borrowing costsPanel 1: eight rooms in a row pay 0 per step and +1 on leaving room 8. Waiting for the end (Monte Carlo) credits every room with 1 after one walk. Borrowing the next estimate (TD) learns during the walk: after walk 1 only room 8 is 1, after walk 2 rooms 7 and 8, after walk 3 rooms 6 to 8, after walk 8 all rooms. Panel 2: this room has estimate 0.2, the next room 0.8, reward 0. Target = 0 + 0.9 times 0.8 = 0.72. New value = 0.2 + 0.5 times (0.72 minus 0.2) = 0.46. Below it, the cost: if the borrowed 0.8 is 0.2 too high, this room comes out 0.09 too high, and the room before it 0.04 too high when it borrows next, because each update passes back 0.5 times 0.9 = 0.45 of the error.1. The reward comes at the endrooms123456780000000+1Wait for the end (Monte Carlo)walk 111111111every room is credited, but only after the walk is overBorrow the next estimate (TD)walk 100000001walk 200000011walk 300000111walk 811111111⋮learns during each walk, but credit moves back one room per walk2. One borrowed updateV(s)0.2V(s′)0.8move, reward r = 0this roomnext roomborrow its estimatetarget = r + γ · V(s′)= 0 + 0.9 × 0.8 =0.72new V(s) = V(s) + α · (target − V(s))= 0.2 + 0.5 × (0.72 − 0.2) =0.46γ = 0.9 is the discount, α = 0.5 is the step sizeWhat it costs: say the borrowed 0.8 is 0.2 too high+0.04+0.09+0.2× 0.45× 0.45room beforethis roomnext roomeach update passes back α · γ = 0.5 × 0.9 = 0.45 of the error

Bootstrapping means updating a guess from another guess. Imagine you walk down a corridor of eight rooms. Every step pays 0, and only leaving the last room pays 1. So how does the first room learn that it's on a good path, when the only reward shows up seven steps later?

Part 2. Previously: the Q table.

The reward comes at the end

The simplest answer is to wait. You finish the walk, add up the rewards that came after each room, and move each room's estimate toward that total. That's Monte Carlo prediction. With no discount, every room's total is 1, so with a step size of 1 a single walk sets all eight rooms to 1 (the top row of the drawing).

The catch is the waiting. If an episode lasts a million steps, nothing gets updated for a million steps, and a task that never ends never hands you a total at all. Some policies never finish at all, as in the windy gridworld of Sutton and Barto, Example 6.5.

So let's learn during the walk instead. When you leave a room you don't know the final total yet, but you do have a current estimate for the next room. Take the reward you just got, add that estimate, and use the sum as your target. That borrowing is bootstrapping, and its one-step version is called TD(0), for temporal difference.

Credit moves back one room per walk

In the corridor, start every estimate at 0 and update each room right after you leave it, walking forward, with step size and discount both 1. On walk 1 only room 8 sees the reward. On walk 2, room 7 reads the now-correct value of room 8, and so on, so room 1 has to wait for walk 8 (the lower rows). Every update happens immediately, but credit still travels slowly. That slowness comes from the forward order, and replaying the walk backward would carry the reward across in one pass.

One borrowed update

Here's one update with real numbers. You're in a room whose estimate is 0.2. You move on, the reward is 0, and the next room's estimate is 0.8. With a discount of 0.9, the target is 0+0.9×0.8=0.720 + 0.9 \times 0.8 = 0.72. With a step size of 0.5 you move halfway from 0.2 toward 0.72, which gives 0.46.

V(s)←V(s)+α [ r+γV(s′)−V(s) ]V(s) \leftarrow V(s) + \alpha\,\big[\,r + \gamma V(s') - V(s)\,\big]

The reward was zero. All of the new information came from the next room's estimate.

What borrowing costs

Borrowing is only as good as the estimate you borrow. Say the next room's 0.8 is really 0.2 too high. Then this room comes out 0.09 too high after its update, and the room before it picks up about 0.04 the next time it borrows. The error travels back along the same path as the credit. You stop waiting, and in exchange you trust a guess.

There's a middle ground. An n-step target adds up n real rewards and only then borrows an estimate. In the same corridor, a three-step target credits all eight rooms in three walks instead of eight, and once n reaches the end of the episode it's just the Monte Carlo total again. Longer targets lean more on real rewards, which can be noisy, and less on the guess, which can be wrong.

When many states share one set of estimates, a wrong borrowed value can spread even further. Part 3: function approximation picks up there.

The exact targets, their conditions and sources

Let tt be the current step, StS_t its state and Rt+1R_{t+1} the reward after the action taken there. The episode ends at step TT, γ\gamma is the discount between 0 and 1, and kk counts steps ahead. The return adds the discounted rewards, and a fixed policy's value is the expected return (Sutton and Barto, Chapter 3):

Gt=∑k=0T−t−1γkRt+k+1,vπ(s)=Eπ[Gt∣St=s].G_t=\sum_{k=0}^{T-t-1}\gamma^k R_{t+k+1},\qquad v_\pi(s)=\mathbb E_\pi[G_t\mid S_t=s].

With no discount, every nonterminal room in the deterministic corridor is worth 1.

Monte Carlo. Move each visited state toward its completed return, with step size 0<α≤10<\alpha\le1. With a fixed policy, finite-variance returns, enough visits and suitably shrinking step sizes, the estimate settles toward the expected return (Section 5.1).

V(St)←V(St)+α[Gt−V(St)]V(S_t)\leftarrow V(S_t)+\alpha[G_t-V(S_t)]

TD(0). Split the return as Gt=Rt+1+γGt+1G_t=R_{t+1}+\gamma G_{t+1} and replace Gt+1G_{t+1} by the current estimate of the next state. The terminal state's value is 0. For a finite discounted task, or an episodic task with bounded expected episode length, suitable on-policy visits and step sizes give convergence in the tabular case (Sections 6.1 and 6.2).

Yt=Rt+1+γV(St+1),V(St)←V(St)+α [Yt−V(St)].\begin{aligned} Y_t&=R_{t+1}+\gamma V(S_{t+1}),\\ V(S_t)&\leftarrow V(S_t)+\alpha\,[Y_t-V(S_t)]. \end{aligned}

n-step TD. Let h=min⁡(n,T−t)h=\min(n,T-t) be the number of real rewards available before the episode ends. The target uses those rewards and borrows only at the endpoint, unless the endpoint is terminal (Section 7.1):

Yt(n)=∑k=0h−1γkRt+k+1+{γhV(St+h),t+h<T,0,t+h=T.Y_t^{(n)}=\sum_{k=0}^{h-1}\gamma^kR_{t+k+1}+\begin{cases}\gamma^hV(S_{t+h}),&t+h<T,\\0,&t+h=T.\end{cases}

Corridor counts. Zero initial values, α=γ=1\alpha=\gamma=1, online updates in forward order. Monte Carlo credits all eight rooms after walk 1. Forward TD(0) credits the last mm rooms after walk mm, so room 1 needs 8 walks. Three-step TD with the usual delayed updates and a terminal flush credits rooms 6 to 8 on walk 1, rooms 3 to 5 on walk 2 and rooms 1 and 2 on walk 3. These counts come from the update order and settings. They don't rank the methods in general, and backward replay would credit the whole corridor in one pass.

Choosing the horizon. Let ε(s)=V(s)−vπ(s)\varepsilon(s)=V(s)-v_\pi(s) be the estimation error. Before termination, the borrowed tail adds γnε(St+n)\gamma^n\varepsilon(S_{t+n}) to the target's error. The discount shrinks a given endpoint error as nn grows, but the endpoint itself changes with nn, so this doesn't guarantee that bias or variance always rises or falls with the horizon. Noisy rewards, correlated transitions and the accuracy of different endpoint estimates all matter (Chapters 6 and 7).

Method Target Fails when Source
Monte Carlo Full return Episodes are long or never end Ch. 5
TD(0) One reward + estimate Credit moves slowly; the estimate is wrong Ch. 6
n-step TD n rewards + estimate Targets wait longer or get noisier Ch. 7
Give actions credit: Generalized Advantage Estimation (GAE)

So far we estimated values under a fixed policy. To improve the policy, a good outcome alone doesn't tell you whether an action helped, because the starting state may already have been promising. The advantage compares an action's expected return with the policy's usual return from that state:

Aπ(s,a)=Qπ(s,a)−Vπ(s).A_\pi(s,a)=Q_\pi(s,a)-V_\pi(s).

Here QπQ_\pi means taking aa first and then following π\pi, and VπV_\pi also averages over the first action. Generalized Advantage Estimation (GAE) estimates this credit from experience and an approximate state value VV. Collect a rollout with a fixed policy, keep the data and VV fixed, and take one segment ending at SHS_H, with ℓ\ell counting steps ahead:

δt=Rt+1+γV(St+1)−V(St),A^t=∑ℓ=0H−t−1(γλ)ℓδt+ℓ.\begin{aligned} \delta_t&=R_{t+1}+\gamma V(S_{t+1})-V(S_t),\\ \hat A_t&=\sum_{\ell=0}^{H-t-1}(\gamma\lambda)^\ell\delta_{t+\ell}. \end{aligned}

The parameter λ\lambda, between 0 and 1, sets how much later errors count. Adding the TD errors of one return length makes the intermediate values cancel, leaving that return minus the starting value, and GAE is a geometrically weighted mix of those return lengths (Schulman et al., Section 3, equations 14 to 16). It brings the same horizon tradeoff to action credit, and changing λ\lambda doesn't guarantee a particular change in variance.

A small example: three transitions ending at a terminal state, γ=1\gamma=1, rewards (0,0,4)(0,0,4) and frozen values (V(S0),V(S1),V(S2),V(S3))=(2,2,1,0)(V(S_0),V(S_1),V(S_2),V(S_3))=(2,2,1,0). The errors are (0,−1,3)(0,-1,3). With λ=0.5\lambda=0.5, the credit for A0A_0 is

TD error Weight Credit
δ0=0\delta_0=0 1 0
δ1=−1\delta_1=-1 0.5 −0.5
δ2=3\delta_2=3 0.25 0.75
Total 0.25

At λ=0\lambda=0 only δ0\delta_0 remains, so the credit is 0. At λ=1\lambda=1 the sum is 4−2=24-2=2, the completed return minus the starting value. A value like λ=0.95\lambda=0.95 is a decay factor, not a 95% and 5% mixture.

The same sum has a backward recurrence, A^H=0\hat A_H=0 and A^t=δt+γλA^t+1\hat A_t=\delta_t+\gamma\lambda\hat A_{t+1}. To apply it to a batch, split at every episode reset and use the last actual observation as SHS_H. Set V(SH)=0V(S_H)=0 only at a true terminal state. A time limit that cuts an ongoing task, or a rollout cutoff, keeps its bootstrap value, and the trace still stops at the boundary, as in Spinning Up's finish_path.

# One segment, with frozen values and no episode reset inside it.
endpoint = 0 if true_terminal else V(last_actual_observation)
v[H] = endpoint
advantage[H] = 0
for t = H-1 down to 0:
    delta = R_(t+1) + gamma * v[t+1] - v[t]
    advantage[t] = delta + gamma * lambda * advantage[t+1]
When the clock ends the episode

That rule assumes you know whether an ending is real. Training runs usually stop every episode after a fixed number of steps, and that cutoff can mean two different things. Pardo et al. (2018) separate them.

The time limit is part of the task. Say the robot really has 14 moves to reach a goal. Then a state's value depends on how many moves are left, and if the state doesn't include the remaining time, two different situations look identical and the learner averages targets that disagree. Their fix is to put the remaining time in the input. In their Last Moment example, the agent can stay for reward 0 or jump into a trap that pays +1 once and then −1 on every step until the end. The best plan is to stay and jump on the very last step. An agent that can't see the clock can't tell when the last step is, and the paper notes that even Monte Carlo methods, which don't bootstrap, can't learn the right plan without the time input.

The time limit is only for training. If the task would really go on, the cutoff isn't an end. Treating it as terminal teaches the learner that states near the cutoff have less future ahead, so their values come out wrong even though nothing about them changed. Their fix is partial-episode bootstrapping: at the cutoff, use R+γV(S′)R+\gamma V(S') instead of RR.

How the limit is meant Value depends on Target at the limit Fix from Pardo et al.
Part of the task State and time left Real end: last reward only Add the remaining time to the input
Only for training State only Not an end: bootstrap from V(S′)V(S') Partial-episode bootstrapping

My game Breaking Bellman has a pass-or-fail goal grid where this seems to happen: 14 moves, reward 1 at the goal and 0 otherwise, no discount. PPO with a learned value and GAE solved 13.3% of the tasks, and its value estimate at the start was about 1.44, above the largest possible return of 1. Scoring actions with real completed returns instead solved 95%, and with no baseline at all, 100% (3 seeds, simulation). The game's own reading is that the value bootstraps through the time limit and can't see the clock. Those are two different problems. The start value above 1 fits bootstrapping past a real ending, since no return in this task can exceed 1. Failing on the timed task also fits a missing clock, as in the Last Moment example, but the log has no run with the remaining time in the input, so that part is unconfirmed.

← All writing