Classical RL II: Sampling, Temporal Difference and Control
When a transition model is unavailable, values and policies must be learned from sampled experience. This stage derives every target from a fixed trajectory, separates on-policy from off-policy claims and makes coverage, support and terminal semantics visible.
Section 2 of 12 · 13 units · about 48 hours
By the end you can Implement Monte Carlo prediction, TD(0), SARSA and Q-learning from blank files, match fixed-trajectory updates, and explain a multi-seed on-policy versus off-policy result using coverage, target and terminal evidence.
Units
- Model-free prediction and first-visit and every-visit Monte Carlo4h
- Incremental means, constant step sizes and learning-rate conditions3h
- TD error, TD(0), bootstrapping and Monte Carlo versus TD bias and variance5h
- n-step returns, target boundaries and fixed-trajectory update oracles4h
- On-policy and off-policy learning with behavior and target policies3h
- Trajectory and per-decision importance sampling, ordinary and weighted estimators4h
- Support, coverage, effective sample size and visitation diagnostics3h
- SARSA and Expected SARSA control4h
- Q-learning and \(\epsilon\)-greedy exploration schedules4h
- SARSA versus Q-learning on risky paths, with multiple seeds and independent evaluation4h
- Terminal versus truncated bootstrapping and reward, action, next-state and discount alignment4h
- Trajectory schemas with environment, policy, seed and version identity3h
- Average-reward formulation for continuing tasks3h
Build it
Mini project · about 12 to 16 hours
Tabular sampling with exact trajectory oracles
Extend policylab with tabular Monte Carlo and TD control checked against fixed-trajectory oracles, plus behavior and target policy bookkeeping with support diagnostics. Every update is tested without environment noise before any multi-seed comparison runs.
- 1Add
tabular/samplers that emit the shared trajectory record with environment, policy, seed and version identity, and reject records with missing identity before any update runs; evaluate only on held-out seed streams. - 2Build fixed-trajectory oracles for returns, one-step and multi-step targets that cut the bootstrap at termination and keep it at truncation, pass Deep-ML 272, Deep-ML 257 and Deep-ML 273, and catch seeded alignment and bootstrap defects.
- 3Implement Sarsa, Expected Sarsa and Q-learning from explicit batches with no hidden environment calls, pass Deep-ML 175, Deep-ML 476 and Deep-ML 133, and label behavior against target policies on exploration-sensitive trajectories including a coverage failure.
- 4Compute ordinary, weighted and per-decision importance estimates on fixed trajectories, track effective sample size curves and visitation logs that name uncovered pairs, and refuse estimates where support is missing.
- 5Compare constant against decreasing step sizes under matched interaction budgets with visitation logs, and show which schedule tracks drift and which converges.
- 6Run the Sarsa against Q-learning cliff comparison across several seeds with independent evaluation streams and matched budgets, pass Deep-ML 518, and derive the average-reward ring rate with its value gaps.
What you hand in
- Tabular samplers with the versioned trajectory schema and fixed-trajectory update oracles.
- Tests: return and target boundaries, control updates, importance estimates, support refusal and seeded defects.
- Matched-budget step-size and cliff comparisons with per-seed raw curves and the ring derivation.
How to check it
- Self-grade with the rubric below
- Deep-ML 272: First-Visit Monte Carlo Prediction
Pass closed-book first; then add a two-episode average of your own.
- Deep-ML 257: Compute Temporal Difference Error
Pass closed-book first; also test your own one-step value update.
- Deep-ML 273: n-Step TD Prediction
Match the hand target first; add terminal-boundary cases of your own.
- Deep-ML 175: SARSA
Match the chain by hand first; ties break toward the first action.
- Deep-ML 476: Expected SARSA
Derive the expectation first; add a multi-step episode of your own.
- Deep-ML 133: Q-Learning
Match the update and sampling order exactly; re-run on more seeds.
- Deep-ML 518: Cliff Walking Sarsa versus Q-Learning
Treat pinned averages as regression values; report spread over more seeds.
How it's graded
Update oracles
Pass: Fixed-trajectory returns and targets match hand computation, and every seeded alignment defect fails loudly.
Great: A live-run mismatch is traced to the update rather than blamed on sampling noise.
Policy labeling
Pass: Behavior and target policies are labeled on every trajectory, and the coverage failure refuses estimation.
Great: The cliff gap is attributed to update distribution rather than tuning.
Support diagnostics
Pass: Visitation logs name uncovered pairs and effective sample size curves mark collapse.
Great: A planted zero-support pair is caught before any estimator runs.
Budget honesty
Pass: Comparisons use matched budgets with all seeds retained and evaluation on separate streams.
Great: A best-seed claim is shown to reverse under the full seed set.
Resources
- Reinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7Richard Sutton and Andrew Barto
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
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.
- Reinforcement Learning Control Laboratory - Tabular ControlRoadmap-Final
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.
- Hands-on Modern RL - Dynamic Programming, Monte Carlo and TDWalking Labs
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. - Deep-ML - Tabular Prediction and Control ProblemsDeep-ML
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
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.
- Gymnasium Core APIFarama Foundation
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.