Unit 1 of 13 · about 4 hours · derive, implement

Model-free prediction and first-visit and every-visit Monte Carlo

Without a transition model, complete episode returns are the only exact target available. Monte Carlo turns those returns into value estimates and sets the baseline every TD method is compared against.

You'll be able to

  • Derive first-visit and every-visit Monte Carlo return averages for a fixed policy from sampled episodes.
  • Implement first-visit Monte Carlo prediction from a blank file and match exact return averages on fixed episodes.
  • Explain why first-visit averages are unbiased through the law of large numbers and why every-visit averages need more care.

Covers

Model-free prediction and controlFirst-visit and every-visit Monte Carlo estimation
1

Read

In this order, only the parts listed

0 of 3 done

  1. Monte Carlo prediction and action valuesReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7

    Chapter 5, Sections 5.1 and 5.2, with the first-visit MC prediction box and Example 5.1 on blackjack (printed pp. 91 to 96)

    The linked page is the publisher page; the authors post the full text free at incompleteideas.net. In this unit read only Sections 5.1 and 5.2; control and off-policy sections belong to later units.

  2. Tabular policy evaluationStanford CS234 Winter 2026 - Policy Evaluation and Q-Learning

    Lecture 3, Monte Carlo (MC) Policy Evaluation through Monte Carlo (MC) Policy Evaluation Summary (slides 10 to 29): first-visit, every-visit and incremental MC, and the bias, variance and consistency properties

    Follow the policy-evaluation framing; leave TD and control material for later units.

  3. DP, MC and TD as three ways to edit one tableHands-on Modern RL - Dynamic Programming, Monte Carlo and TD

    Section Monte Carlo with the corridor trajectory and the every-visit update table (page read 2026-10-14)

    Read as a worked comparison, not as the update contract. Keep the Sutton box as the contract.

2

Focus on

  • Model-free means no or table is used. The target comes from a sampled episode, with .
  • First-visit averages the return after the first visit to in each episode. Every-visit averages all visits. The boxed algorithm implements first-visit; every-visit drops the earlier-occurrence check.
  • Monte Carlo waits for termination, so it applies to episodic tasks only. No update happens step by step.
  • Each first-visit return is an independent unbiased sample of , so the average converges by the law of large numbers with standard deviation falling as .
Common mistakes · 3
  • Averaging all visits while calling the result first-visit. A repeated state in one episode contributes once under first-visit and every time under every-visit.
  • Updating before the episode ends. The return is not known until termination, so an intermediate average uses an incomplete sum.
  • Reading the blackjack figure as exact values. Figure 5.1 after 500,000 games is a close approximation, and usable-ace states stay noisier because they are visited less often.
3

Try it yourself

Closed-book, before checking anything

From a blank file, implement first-visit Monte Carlo prediction and run it on [[(0, 1.0), (1, 1.0), (2, 1.0)]] with gamma=1.0 to get [3.0, 2.0, 1.0], then on [[(0, 1.0), (1, 1.0), (0, 1.0), (2, 0.0)]] to get [3.0, 2.0, 0.0]. State which visit each return belongs to.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Why does Monte Carlo prediction require episodic tasks?Answer

    Its target is the complete return, which is only known at termination. Without a terminal step there is no to average.

  2. 2.One episode yields state rewards 1, 1, 1, 0 across visits to states 0, 1, 0, 2. What does each method record for state 0?Answer

    First-visit records one return, 3. Every-visit records two returns, 3 and 1.

  3. 3.Why is the first-visit average unbiased for ?Answer

    Each first-visit return is an independent sample of with finite variance, so its expectation equals and the average converges to it.

  4. 4.How do the error bars shrink with more first visits?Answer

    The standard deviation of the average falls as with first visits.

5

Practice

  • Deep-ML 272: First-Visit Monte Carlo Prediction

    Implement first_visit_mc_prediction; the two public tests check [3.0, 2.0, 1.0] on a clean episode and [3.0, 2.0, 0.0] when state 0 repeats

    Do it closed-book after your own implementation passes fixed returns. The tests only check these two episodes, so also test a two-episode average with a second seed.

You're done when

Your blank-file implementation matches both Deep-ML 272 public tests, and you can state from memory which visits each method averages and why the target needs termination.

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.