Exercises — Temporal Difference learning
Before we start, three things everything on this page leans on:
Level 1 — Recognition
Exercise 1.1 (L1 Recognition)
Match each symbol to its plain-English meaning: , , , .
Recall Solution
- = learning rate — the fraction of the error we actually apply. Small = timid nudges; = throw away the old guess entirely.
- = discount factor — how much a reward one step in the future is worth now. means future rewards count fully; means only the immediate reward matters.
- = TD error = — the surprise: target minus old estimate.
- = value estimate of the state we are currently leaving — our best guess of total future (discounted) reward from that state.
Exercise 1.2 (L1 Recognition)
A transition gives , , , with . Write the TD target (do not update yet).
Recall Solution
The TD target is reward + discounted next-state value — that is keystroke (1) of the workhorse update, stopped before we subtract or step: We stop here — the target is just the "improved estimate" we will later chase.
Exercise 1.3 (L1 Recognition)
Which of these does TD(0) require? (a) full transition model , (b) complete episodes, (c) an observed sample transition .
Recall Solution
Only (c). TD is model-free like Monte Carlo, so (a) is false. And it bootstraps like Dynamic Programming, updating every step, so it does not need complete episodes — (b) is false.
Level 2 — Application
Exercise 2.1 (L2 Application)
, , , , . Compute and the new .
Recall Solution
We are just running the three keystrokes of the workhorse update, so first write it down and then substitute — never plug numbers blind. (1) Build the target (reward + discounted next value): (2) Subtract the old guess to get the error — this is why we compute a target at all: to measure how wrong we were: (3) Step a fraction along — why only a fraction? so one noisy sample never overwrites everything we knew: We were too optimistic (guessed ); the step actually cost , so we lower the estimate.
Exercise 2.2 (L2 Application)
Chain (terminal). Estimates ; . Process transition with reward , then with reward . Give both updates.
Recall Solution
Note because is terminal (see the definition above). Each transition runs the same three keystrokes: build target, subtract old guess, step by .
(reward ):
- (1) target — why and not the true future? because bootstrapping means we trust our current best guess of what follows.
- (2) error (positive — good news, leads somewhere better than we thought).
- (3) step
(reward ):
- (1) target (the term vanishes because is terminal).
- (2) error
- (3) step
The picture in words (no figure needed): for , the old guess sits below the target ; the update carries it only up to — one-tenth of the gap. For , old vs target ; the step reaches just . In both cases the new value never touches the target, because crosses only a tenth of the distance each time. That "always fall short of the target" behaviour is exactly what keeps a single noisy sample from hijacking the estimate.
Exercise 2.3 (L2 Application)
A single state is visited three times in a row. Each visit: reward , and the next state is terminal (), , , starting from . Compute after each of the 3 updates.
Recall Solution
Target every time is (the next state is terminal, so its value is ). Substituting the fixed target into the workhorse update collapses it to , which closes half the remaining gap each visit — why half? because is literally "step half-way to the target."
- Visit 1:
- Visit 2:
- Visit 3: Notice it approaches the true value geometrically but never overshoots — the hallmark of a fixed target with .
Level 3 — Analysis
Exercise 3.1 (L3 Analysis)
A 100-step corridor ends in reward ; , , all values start at . Compare Monte Carlo vs TD for how value reaches the start state . After one episode, what is under each method, and how many episodes until first becomes non-zero under TD?
Recall Solution
Recall is the return: the actual total discounted reward collected from a state to episode end. Here every path ends in the single reward , and with nothing is shrunk, so the return from any state on the corridor is .
Monte Carlo: waits for the full return , then updates every visited state once: after episode 1. MC reaches immediately (only because carried the raw return back).
TD: in episode 1 only the last state sees the reward; . All earlier states still update toward next states that are still , so they stay . Value crawls back one state per episode.
Inline mini-derivation of "one state per episode": let mean the state steps before the goal, so is adjacent to the goal. The reward only enters through a non-zero next-state value. In the TD update (here every step-reward on the corridor is except the final one), the term can only be non-zero once itself has already become non-zero. So:
- Episode 1: only (next to the goal) sees the reward → becomes non-zero, all others stay .
- Episode 2: now finds in its target → becomes non-zero.
- Episode : exactly turns non-zero for the first time — the "wavefront" of learning advances one state per episode.
Counting from the goal, the start is the state steps away, so first becomes non-zero at episode 100. The figure below draws this wavefront: after each episode the shaded (learned) region extends one cell further back from the green goal.

Reading it: MC propagates in one shot here but pays with high variance (the whole noisy return); TD is slower to reach far states in this deterministic chain but each update uses low-variance one-step information. TD(λ) blends the two speeds.
Exercise 3.2 (L3 Analysis)
A slot machine pays with probability , else . Its true value is . You have already learned exactly. Compute for a "pays 10" pull and for a "pays 0" pull. Does a perfect estimate make ? What quantity does go to zero?
Recall Solution
The next state after a pull is terminal, so (terminal-value definition).
- Pays 10:
- Pays 0: Even with a perfect estimate the individual errors are — they never vanish, because the environment is random. What vanishes is the expected error: So convergence means , not .
Exercise 3.3 (L3 Analysis)
With , redo Exercise 3.2's two pulls in sequence starting from the correct . Show why destroys a good estimate.
Recall Solution
means target. The old value is thrown away.
- After "pays 10":
- Then "pays 0": A perfectly-known value of ping-pongs between and — pure noise, no memory. Small instead averages samples, so the estimate stays near .
Level 4 — Synthesis
Exercise 4.1 (L4 Synthesis)
Two full episodes on chain , rewards , ; , , start . Process each episode in order then (online). Report after episode 1 and after episode 2.
Recall Solution
is terminal so throughout.
Episode 1
- : target ; ; .
- : target ; ; .
- End of ep 1: .
Episode 2
- : target ; ; .
- : target ; ; .
- End of ep 2: .
Watch the cascade: had to learn about the reward first (ep 1); only then could inherit value from (ep 2). This backward "teaching" is bootstrapping in action.
Exercise 4.2 (L4 Synthesis)
Same chain and numbers as 4.1, but use Monte Carlo (update each state once per episode toward the full return ). Compute after episode 1 and after episode 2. Then compare with the TD answer.
Recall Solution
Recall is the return from state — the actual total (here undiscounted, ) reward collected from to the end — and likewise from :
Episode 1 (both start at 0):
Episode 2:
Comparison: After 2 episodes MC gives vs TD's . MC pushed value to immediately (it used the whole return ), while TD needed the signal to walk back through first. In a noisy environment this MC speed would come bundled with higher variance — that is the Bias-Variance Tradeoff TD is designed to soften.
Level 5 — Mastery
Exercise 5.1 (L5 Mastery)
On a 3×3 grid, start , goal , reward per step, , , all (goal terminal, ). The agent takes the path , one "Right/Down" step each, reward every move. Process the episode online and give the final value of each visited non-terminal state.
Recall Solution
The goal is terminal, so . Every transition runs the three keystrokes; because all next-state values are still in this first episode, every target is the same: Applying that to each visited state in turn:
- :
- :
- :
- : next state is terminal (), so still target ;
Final answer: all four visited non-terminal states end episode 1 at ; the terminal goal stays at .
The figure below shows the grid: the four visited cells are shaded blue and each carries the label , the red arrows trace the path , and the green goal cell is marked . Visually, the whole path is flat at — the true landscape (states nearer the goal should be less negative) has not appeared yet; it only emerges over many episodes as these values feed back into future targets.

Exercise 5.2 (L5 Mastery)
Continue Exercise 5.1 into episode 2 along the same path. Now next-state values are no longer all zero. Compute the TD target, error, and new value at the first two transitions and .
Recall Solution
Start ep 2 with at each of .
: target
: ( is still when we leave it, next is ) target
Now the values are drifting more negative because the bootstrapped future value has itself become negative — the true cost-to-go is genuinely below . This is the value landscape sinking toward its correct (negative) fixed point given by the Bellman Equations.
Exercise 5.3 (L5 Mastery — synthesis)
Explain, using one TD update, why Q-Learning and SARSA can both be seen as TD applied to action-values instead of state-values , and name the single symbol that differs between them in the target.
Recall Solution
An action-value is "the value of being in state and taking action " — same idea as but pinned to a chosen action. Replace by in the workhorse update and it still has the exact TD skeleton — old guess, reward, discounted next value, step : So both are TD; the only thing that differs between them is which next action goes into the bootstrapped term:
- SARSA uses , the action actually taken next (on-policy). Written out:
- Q-Learning uses , the greedy best action (off-policy): The single differing symbol: (Q-Learning) versus the chosen action (SARSA) inside the bootstrap. Everything else — the online-per-step update, the bootstrapping, the shape — is inherited straight from TD(0). For large state spaces the lookup table is swapped for a learned function, giving Value Function Approximation.
Recall Self-check summary
TD target is R plus what, times gamma? ::: The next-state value (only the future term is discounted). Does a perfect estimate give zero TD error every step? ::: No — only the expected error is zero; single steps fluctuate with environment noise. What does do to a good estimate? ::: Overwrites it with a single noisy target, destroying memory. Why does value crawl back one state per episode in a deterministic chain? ::: Because each state can only learn from a next state that has already learned — the bootstrap chain. Which symbol separates Q-Learning from SARSA in the target? ::: The next action used — greedy (Q-Learning) vs actual (SARSA).