v9.1.1
Menu

Chapter 4: Monte Carlo & Temporal-Difference Learning

Week 4

Drop the model and learn from experience: Monte Carlo prediction and control, TD(0), the bias-variance tradeoff between sampling and bootstrapping, and the n-step and TD(lambda) methods that interpolate between them.

Topics
5
Demos
2
Videos
3
Min read
20

Chapter Overview

Dynamic programming needed the transition dynamics. Almost no interesting problem provides them. This chapter removes that assumption and replaces the expectation in the Bellman update with something the agent can actually obtain: sampled experience.

There are two distinct ways to do this, and the difference between them is the central idea of the chapter. Monte Carlo methods wait until an episode ends and use the actual return — unbiased, but high variance and unusable until termination. Temporal-difference methods use a one-step reward plus the current estimate of the next state's value — biased, because the estimate is wrong early on, but low variance and updatable after every single step. Neither dominates. n-step methods and TD(lambda) span the space between them, and understanding that spectrum explains design choices in every algorithm that follows.

Chapter Roadmap

Click any topic to jump in

Explainer videoInteractive demo
1
Monte Carlo Prediction

Estimate a state's value by averaging the actual returns observed after visiting it — no model, no bootstrapping.

First-Visit and Every-VisitThe Cost of Waiting
Waiting for the episode end is the bottleneck
2
Monte Carlo Control

Plug sampled action-value estimates into generalized policy iteration, with exploration supplied by epsilon-greedy.

Exploring Starts and Epsilon-GreedyImprovement Without Full Evaluation
Two extremes of one spectrum

One-step TD and full-return Monte Carlo are the endpoints, not rival families.

3
TD(0) Prediction

Update after every step using the observed reward plus the discounted estimate of the next state.

The TD ErrorWhy TD Often Wins
4
Bootstrapping vs Sampling

The bias-variance axis that separates TD from Monte Carlo and explains when each one wins.

The Two AxesBias and Variance in Practice
The middle is usually best
5
n-step & TD(lambda)

Interpolate between the two extremes by looking n steps ahead, or by averaging all n at once with eligibility traces.

The n-step ReturnEligibility Traces and the Lambda Return

Topics

5 topics in this chapter

Monte Carlo prediction estimates vπ(s)v_\pi(s) in the most direct way imaginable: run episodes under π\pi, record the return that actually followed each visit to ss, and average them. The law of large numbers does the rest. No model is required, and — unusually for this course — no assumption about the Markov property is needed either. The method only needs episodes that terminate. That second point deserves emphasis because it is the one genuine advantage Monte Carlo holds over everything that follows. Because the method never uses one state's value to estimate another's, it never propagates a representation error from state to state. It samples the whole return directly, so it converges to the correct expected return for whatever representation you gave it. Temporal-difference methods bootstrap from neighbouring estimates and therefore inherit any error in the state representation, which makes Monte Carlo markedly more robust when the states are not quite Markov — a common situation in practice.

Definition

Monte Carlo prediction estimates the value of a state by averaging the actual returns observed after visiting it, using complete episodes and never bootstrapping from other estimates. It requires episodes that terminate, but it needs neither a model of the environment nor the Markov property, which makes it unusually robust when the state representation is imperfect.

In this topic

  1. 1First-Visit and Every-Visit
  2. 2The Cost of Waiting
1 of 2
First-Visit and Every-Visit

V(St)←V(St)+α[Gt−V(St)]V(S_t) \leftarrow V(S_t) + \alpha\left[ G_t - V(S_t) \right]

First-visit Monte Carlo averages returns following only the first occurrence of a state in each episode; every-visit averages returns after every occurrence. First-visit yields independent samples and is therefore the easier one to analyse, giving an unbiased estimate whose error falls as one over the square root of the number of visits. Every-visit is biased for finite samples because returns within a single episode are correlated — a state visited twice contributes two overlapping futures — but it is also consistent, converging to the same answer, and it often performs slightly better in practice by extracting more data per episode.

Mathematical Intuition

The bracket is the prediction error against the actual observed return. With α=1/N(St)\alpha = 1/N(S_t) this is exactly a running sample mean; with constant α\alpha it becomes an exponentially weighted average that tracks a changing target — which is what you want when the policy is still improving.

Example:

Why does Monte Carlo prediction not require the Markov property, when dynamic programming does?

2 of 2
The Cost of Waiting

Monte Carlo can only update once an episode terminates, because the return is not known until then. That rules out continuing tasks entirely and makes long episodes very slow to learn from. It also means credit is assigned to every state in the episode using the same noisy return, so variance grows with episode length as all the randomness along the trajectory accumulates into a single number. The practical consequence is a method that is statistically clean but operationally awkward: correct in the limit, and often unusably slow to get there on the problems people actually care about.

Mathematical Intuition

If the return is a sum of TT independent noisy rewards with unit variance, its variance is TT and its standard deviation T\sqrt{T}. Getting a value estimate to a standard error of 0.1 then takes T/0.01T / 0.01 returns: 500 for a 5-step episode, 10,000 for 100 steps and 100,000 for 1,000 steps. The number of episodes needed grows linearly with episode length, before counting that each episode also takes longer to run.

Example:

An episode is 1000 steps long and the only reward arrives at the end. What does Monte Carlo do, and why is it high variance?