Chapter 110.5
📝Draft

Monte Carlo Methods

Learning value functions and policies from complete episodes, no model required

What You'll Learn

  • Estimate value functions by averaging complete returns from sampled episodes
  • Distinguish first-visit from every-visit MC and state their bias properties
  • Explain why model-free control needs Q-values and an exploration mechanism
  • Implement ε-greedy Monte Carlo control on GridWorld
  • Use importance sampling to learn about one policy from another’s episodes

Dynamic programming gave us exact value functions—but only because we handed it the full transition model p(ss,a)p(s'|s,a). Now take blackjack: could you write down the probability of every next hand, for every action, from every configuration of cards? In principle, maybe. In practice, nobody does. What you can do is play. Deal a hand, follow your strategy, see how it ends. Then deal another.

Monte Carlo methods turn that into a learning algorithm: play complete episodes, record the returns you actually got, and average. No model, no equations to solve—just experience. In the demo below, an agent wanders a small random walk and estimates each state’s value purely by averaging returns. Watch the MC estimates converge toward the true values as episodes accumulate—and notice that they only move at the end of each episode. (The demo also shows a TD learner; that’s the subject of the next chapter, so keep your eyes on the Monte Carlo curve for now.)

TD(0) vs Monte Carlo Learning

Compare how TD and MC learn value estimates on the Random Walk problem.

Random Walk: Agent starts at S3, moves left or right with 50% probability. Left terminal gives 0 reward, right terminal gives +1 reward.
TD(0)
0
S1
0.50
S2
0.50
S3
0.50
S4
0.50
S5
0.50
+1
S1
S2
S3
S4
S5
Episodes: 0|RMSE: 0.236
Monte Carlo
0
S1
0.50
S2
0.50
S3
0.50
S4
0.50
S5
0.50
+1
S1
S2
S3
S4
S5
Episodes: 0|RMSE: 0.236
True Values (dashed lines)
S1: 0.17S2: 0.33S3: 0.50S4: 0.67S5: 0.83
Key Difference:
  • TD(0) updates after every step using bootstrap estimates (V(s) depends on V(s'))
  • Monte Carlo waits until episode end to update, using actual returns
Watch how TD values change immediately while MC values only update when an episode terminates. TD typically learns faster due to its online updates!

Where Monte Carlo Sits

Monte Carlo methods occupy a specific spot in the RL landscape, defined by three properties:

Model-free
Learns from sampled episodes. Never needs p(s’|s,a)—the environment itself does the sampling.
Episodic
Updates require a complete return, so episodes must terminate. Continuing tasks are out of reach.
No bootstrapping
Targets are actual returns, not estimates built from other estimates. Each state’s value is learned independently—unbiased, but noisy.

Compare this with the dynamic programming methods you just saw:

🗺️
Dynamic Programming

Computes values from the model: sweep all states, apply Bellman backups over every possible transition.

Needs p(s’|s,a) for everything. Exact, but only as good as the model—and most real problems don’t come with one.

🎲
Monte Carlo

Estimates values from experience: run episodes, average the returns you observed.

Needs only the ability to interact. Noisy at first, converges with data. Can focus effort on states you actually visit.

One more difference matters in practice: DP updates every state on every sweep, whether or not it’s relevant. MC only spends effort on states that appear in real episodes. In a huge state space where your policy visits a tiny corner, that’s a feature, not a bug.

📖Monte Carlo Methods (in RL)

Methods that estimate value functions and improve policies by averaging complete sampled returns. The name comes from the casino: any estimation technique built on repeated random sampling is called a Monte Carlo method.

Chapter Overview

The chapter follows the same prediction-then-control arc as dynamic programming, then adds a third idea—learning about one policy from another’s data—that will echo through the rest of the book:

Throughout, we use the same 4×4 GridWorld from Value Functions: wall at (1,1), goal at (3,3), −1 per step, +10 for reaching the goal, γ=0.9\gamma = 0.9. Because we solved that grid exactly with DP, we can check every Monte Carlo estimate against the true answer.

ℹ️Note

Monte Carlo’s one non-negotiable requirement: episodes must end. A blackjack hand ends, a maze run ends, a game of Go ends. A server that runs forever doesn’t—and for that you’ll need TD learning, which is exactly where this book goes next.

Summary & Check Your Understanding

Key Takeaways

1
Values are expectations, so sample them
V(s) is defined as the expected return from s. Monte Carlo estimates it the obvious way: run episodes, average the returns you observed. The law of large numbers does the rest.
2
Model-free, episodic, no bootstrapping
MC never touches p(s’|s,a) and never builds estimates from other estimates. The price: updates wait until the episode ends, and the targets are noisy (unbiased, high variance).
3
Control needs Q-values and exploration
Being greedy with respect to V requires a model to look one step ahead; being greedy with respect to Q is just an argmax. And since MC only learns about what it visits, ε-greedy exploration keeps every action’s estimate alive.
4
Off-policy learning reweights experience
Importance sampling corrects returns from a behavior policy to estimate a target policy’s values. Ordinary IS is unbiased but wildly high-variance; weighted IS is biased but far more stable. Over long horizons, both struggle—the opening for TD methods.

Quick Quiz

1. Dynamic programming and Monte Carlo both estimate V(s). What does MC need from the environment that DP doesn’t, and vice versa?
Show answer

MC needs the ability to interact: it must generate complete episodes, so the environment (or a simulator) has to be runnable, and episodes must terminate. DP needs the full model: the transition probabilities p(s’|s,a) and rewards for every state-action pair, but it never runs a single episode. That’s the trade: MC swaps knowledge of the model for samples from it.

2. A state is visited three times within one episode. How do first-visit and every-visit MC treat this episode differently?
Show answer

First-visit MC uses only the return following the first occurrence of the state—one sample from this episode. Every-visit MC averages the returns following all three occurrences—three samples, but they overlap (they share future rewards), so they’re correlated. That correlation is why first-visit is unbiased while every-visit is slightly biased—though both converge to the true V(s) as episodes accumulate.

3. Why does model-free MC control learn Q(s,a) instead of V(s)?
Show answer

To act greedily using V, you must compute “which action leads to the best next state?”—which requires knowing where each action leads, i.e., the model. With Q, the greedy action is just argmax_a Q(s,a): a table lookup, no model required. Model-free control therefore estimates action values directly.

4. You’re estimating a target policy’s value from episodes generated by a different behavior policy. When would you prefer ordinary importance sampling over weighted, despite its variance?
Show answer

When you need an unbiased estimate—for example, inside another estimator whose analysis assumes unbiasedness, or when averaging over many independent problems where errors cancel. Ordinary IS is unbiased at any sample size; weighted IS is biased in finite samples (its first estimate is just the observed return, regardless of the weight) but consistent, with much lower variance. In most practical settings weighted IS wins, which is why it’s the default recommendation.

Next ChapterIntroduction to TD Learning