Unit 3 of 13 · about 5 hours · derive, measure

TD error, TD(0), bootstrapping and Monte Carlo versus TD bias and variance

TD learns from every step instead of every episode. That single change buys sample efficiency at the price of bootstrapping bias, and the rest of the section reuses the same error.

You'll be able to

  • Derive the TD(0) update and the TD error from the Bellman expectation equation.
  • Explain bootstrapping and why it trades lower variance for bias while is wrong.
  • Compare Monte Carlo and TD(0) on sample efficiency and bias using the random-walk evidence.

Covers

Temporal-difference error and TD(0)Bootstrapping and bias-variance trade-offsMonte Carlo and TD bias, variance and sample efficiency
1

Read

In this order, only the parts listed

0 of 3 done

  1. TD prediction and its advantagesReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7

    Chapter 6, Sections 6.1 and 6.2, equations (6.1), (6.2) and (6.5), with Example 6.2 on the random walk (printed pp. 119 to 126)

    Read Example 6.2 for the bias-variance comparison only; batch updating in Section 6.3 belongs to later study, not this unit.

  2. TD(0) inside tabular policy evaluationStanford CS234 Winter 2026 - Policy Evaluation and Q-Learning

    Lecture 3, Temporal Difference Learning through Summary: Temporal Difference Learning (slides 31 to 40), with the worked TD example (slides 35 to 36)

    Use the lecture's TD update framing; leave Q-learning to Lecture 4.

  3. Online TD updates on one trajectoryHands-on Modern RL - Dynamic Programming, Monte Carlo and TD

    Section Temporal Difference with the step-by-step corridor table that reuses updated values mid-episode (page read 2026-10-14)

    Treat the corridor numbers as one sampled trajectory, not as converged values.

2

Focus on

  • The TD error from (6.5) is the gap between the one-step target and the old estimate. TD(0) from (6.2) moves by .
  • TD(0) replaces the unknown tail with the current estimate . That substitution is bootstrapping: an estimate updates another estimate.
  • Monte Carlo is unbiased with high variance and waits for termination. TD(0) has lower variance, learns online each step, but is biased until the successor estimates are accurate. Example 6.2 finds TD consistently ahead of constant- Monte Carlo on RMS error for the step sizes it tries; Exercise 6.4 asks whether a wider range would change that.
  • The corridor TD table reads already-updated cells mid-episode, so later steps build on earlier corrections. That reuse is the online mechanism, not a bookkeeping error.
Common mistakes · 3
  • Calling TD(0) unbiased because it uses a real reward. The reward is real but is an estimate, so the target inherits its error.
  • Zeroing the bootstrap on any episode end. Termination sets the beyond-end value to zero; truncation must keep bootstrapping. The alignment unit makes this exact.
  • Comparing single-seed learning curves as proof. Example 6.2 averages many runs; one seed can favor either method.
3

Try it yourself

Closed-book, before checking anything

Closed-book, compute the TD error for , , and with no termination to get , and for , at termination to get . Then update with and to get .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What are the Monte Carlo and TD(0) targets for the same transition?Answer

    Monte Carlo uses the full sampled return . TD(0) uses .

  2. 2.Why does TD(0) learn before termination?Answer

    Its target needs only one reward and the next state's current estimate, so it updates each step instead of waiting for .

  3. 3.A learner zeroes the bootstrap at a time limit. What breaks?Answer

    The value loses the future return beyond the limit. Time limits truncate; only true termination zeroes the bootstrap.

  4. 4.When does TD(0) beat Monte Carlo on the random walk?Answer

    In Example 6.2, TD(0) stays below constant- Monte Carlo on RMS error over the first 100 episodes for every step size shown. The ranking depends on step sizes, so compare each method at its best .

5

Practice

  • Deep-ML 257: Compute Temporal Difference Error

    Implement compute_td_error; the public tests check 5.0 for the continuing case and 2.0 for the done case

    The done test passes v_s_prime 0.0 and expects the bootstrap dropped. Also test your own one-step value update against the 5.5 hand value.

You're done when

You derive the TD(0) update from the Bellman equation without notes, reproduce both TD errors and the 5.5 update, and can explain the bias-variance trade against Monte Carlo.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 4 more in the section
  • Reinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7Richard Sutton and Andrew Barto · primary

    The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Sections 2.4 and 2.5 (incremental and constant step-size estimates), Chapter 5 without the starred sections (Monte Carlo prediction and control, ordinary and weighted importance sampling), Chapter 6 Sections 6.1 to 6.7 (TD(0), Sarsa, Q-learning, Expected Sarsa, maximization bias) and Chapter 7 Sections 7.1 to 7.3 (n-step methods). Reproduce Example 5.4 (off-policy blackjack), Example 6.2 (random walk) and Example 6.6 (cliff walking), and implement each update from fixed hand-calculated trajectories before training an agent.

  • Stanford CS234 Winter 2026 - Policy Evaluation and Q-LearningStanford University · required

    Study Lecture 3 (tabular policy evaluation) and Lecture 4 (Q-learning) slides and the relevant independent Assignment 1 and 2 written questions. Use the course treatment to state model-access, convergence, coverage and function-approximation assumptions for each update.

  • Hands-on Modern RL - Dynamic Programming, Monte Carlo and TDWalking Labs · support

    Use the DP, MC and TD page and the bandit script as a code companion. Rebuild the algorithms in the shared track schema, and keep the bandit comparison as a seed-sensitivity case: which method wins changes with the seed. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; its code/chapterNN folders do not follow the site chapter numbers.

Also in this section

  • Gymnasium Core APIFarama Foundation · reference

    Use the reset, step, seeding and wrapper documentation as the behavior authority for the installed environment version: reset(seed=...) seeds once, step returns terminated and truncated, and vector environments autoreset on the next step by default. Test observation, reward, terminated, truncated and info fields at the boundary.

  • Deep-ML - Tabular Prediction and Control ProblemsDeep-ML · support

    Use named problems as closed-book checks after your own implementation passes: 272 (first-visit Monte Carlo), 257 (TD error), 273 (n-step TD), 175 (SARSA), 476 (Expected SARSA), 133 (Q-learning) and 518 (cliff walking, SARSA versus Q-learning). Read each problem's tests before trusting its statement.

  • Reinforcement Learning Control Laboratory - Tabular ControlRoadmap-Final · required

    Complete the MC, SARSA and Q-learning requirements with exact trajectory updates, stochastic and exploration-sensitive environments, visitation logs, matched interaction budgets and seeded alignment and bootstrap defects.

  • Reinforcement Learning: An Introduction (2nd ed.) - Starred Sections 5.8 and 5.9 and Chapter 10 Sections 10.3 to 10.5Richard Sutton and Andrew Barto · support

    Study starred Section 5.9 on per-decision importance sampling only for the per-decision estimator, and Chapter 10 Sections 10.3 to 10.5 on average reward and differential values only for the continuing-task unit. Cite printed pages; the authors post the full text free at incompleteideas.net.