Unit 4 of 13 · about 4 hours · derive, implement
n-step returns, target boundaries and fixed-trajectory update oracles
One-step and full-episode targets are two ends of one spectrum. n-step returns fill the middle, and fixed-trajectory oracles make every later update testable without running an agent.
You'll be able to
- Derive n-step returns with their TD, Sarsa and off-policy update forms for a fixed trajectory.
- State the target boundary rule: zero-pad at termination and bootstrap only inside the trajectory.
- Implement fixed-trajectory update oracles that match hand-computed n-step targets exactly.
Covers
Read
In this order, only the parts listed
0 of 2 done
- n-step TD, n-step Sarsa and n-step off-policy updatesReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7
Chapter 7, Sections 7.1 to 7.3, equations (7.1) to (7.5) with Figures 7.1 to 7.3, ending before starred Section 7.4 (printed pp. 142 to 150)
Stop before starred Section 7.4. Eligibility traces stay out; this unit ends at n-step oracles.
- Fixed-trajectory checks in the control laboratoryReinforcement Learning Control Laboratory - Tabular Control
Milestone A and Correctness requirements: exact and fixed-trajectory update checks
Read for what an oracle must check, not for DQN or policy-gradient material.
Focus on
- The n-step return sums real rewards then bootstraps. At it is TD(0); as grows it approaches Monte Carlo.
- If reaches termination, missing terms are zero and no bootstrap is added. The boundary sits at , not at a fixed .
- The error-reduction bound (7.3) says the worst-case error contracts by : longer trusts the current less.
- An oracle takes an explicit trajectory and returns the exact target. It never steps an environment, so failures in it are update bugs, not sampling noise.
Common mistakes · 3
- Shifting the reward index by one. follows ; pairing with corrupts every n-step sum.
- Bootstrapping past termination. Once the episode ends at , the target is the zero-padded sum, never plus .
- Testing updates through a live agent. Environment noise then masks off-by-one errors that a fixed trajectory would expose exactly.
Try it yourself
Closed-book, before checking anything
gamma=0.9 and alpha=0.1. Show n=1 gives [0.1, 0.1, 0.1] and n=2 gives [0.19, 0.19, 0.1], with the n=2 target at the first state equal to 1.9.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Write in full.Answer
, or the zero-padded sum when the episode ends first.
2.Why can no real algorithm use at time ?Answer
It needs rewards and states up to , which have not happened yet. The update for waits until time .
3.What does (7.3) promise when the current values are already exact?Answer
Nothing beyond exactness: if the worst error is zero, the n-step expectation is exact for every .
4.An oracle and a live run disagree. Which do you trust?Answer
The oracle, after checking its hand computation. It removes sampling noise, so a mismatch points at the live update or its boundary handling.
Practice
- Deep-ML 273: n-Step TD Prediction
Implement
n_step_td_prediction; the public tests check[0.1, 0.1, 0.1]forn=1and[0.19, 0.19, 0.1]forn=2Match the 1.9 hand target first. The tests use single episodes from zero values, so add your own two-episode and terminal-boundary cases.
You're done when
Your oracle matches both Deep-ML 273 tests and your hand-derived 1.9 target, and you can state the termination boundary without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 5 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.
- 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.
Also in this section
- 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/chapterNNfolders do not follow the site chapter numbers. - 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,stepreturnsterminatedandtruncated, 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: 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.