Chapter 5 · Sutton & Barto

Monte Carlo methods

Dynamic programming needs the full model. Monte Carlo methods need only experience: play episodes, see what return actually followed each state, and average. No transition probabilities, no bootstrapping, just the law of large numbers. This page covers Monte Carlo prediction, control with exploring starts, ε-soft policies, and off-policy learning with importance sampling, all on blackjack.

01The idea: average actual returns

The value of a state is the expected return from it: vπ(s) = 𝔼π[Gt | St = s]. The most direct estimate of an expectation is a sample mean. So follow π, record the return after each visit to s, and average them. As the number of returns grows, the average converges to vπ(s).

This requires returns to be well defined, so Monte Carlo methods apply to episodic tasks: every episode must end, and updates happen only at the end. Each state's estimate is built from its own returns alone, independent of the estimates of other states. Monte Carlo does not bootstrap, which is also why the cost of estimating one state doesn't depend on how many states there are.

02Monte Carlo prediction

A state can be visited several times in one episode. First-visit MC averages only the returns following the first visit to s in each episode. Every-visit MC averages the returns after every visit. Both converge to vπ(s). For first-visit MC each return is an independent, unbiased sample, so the standard error of the average falls as 1/√n.

First-visit MC, written as an incremental meanN(St) ← N(St) + 1,    V(St) ← V(St) + (1 / N(St)) [ Gt − V(St) ]

Returns are computed backward from the end of the episode, using Gt = Rt+1 + γGt+1. Replacing 1/N with a constant α gives constant-α MC, which weights recent episodes more (see the TD vs MC page).

03Worked example: blackjack

Blackjack is a natural fit (Sutton & Barto, Example 5.1). Computing p(s′, r | s, a) for it is painful, but simulating a hand is trivial. The rules used here:

The policy being evaluated: stick on 20 or 21, otherwise hit.

First-visit MC prediction

episodes 0

The hand

dealer
you

Faded cards were dealt while your sum was below 12, where you always hit and no state is recorded.

Estimated V(s). Rows: your sum. Columns: dealer's showing card. Red is negative, teal positive. Outlined cells were updated by the last hand. Cells with no data yet show “·”.

After a few thousand hands the estimates for “no usable ace” are already smooth, because those states are common. The usable-ace states are rarer and stay noisy much longer: each state learns only from its own visits. After 500,000 hands both surfaces settle: values jump at 20 and 21, where the policy sticks, and the dealer showing an ace is bad news everywhere.

04Action values and the exploration problem

Without a model, state values aren't enough to choose actions: to look one step ahead you would need p. So for control, Monte Carlo estimates action values qπ(s, a), averaging the returns that followed each visit to the pair (s, a). Then improvement is easy: π(s) = argmaxa Q(s, a).

The catch: a deterministic policy only ever tries one action per state, so the returns for the other actions are never observed. Estimates of q must keep exploring. One way is exploring starts: begin each episode from a randomly chosen state–action pair, with every pair having nonzero probability.

05Monte Carlo control with exploring starts

Monte Carlo control is generalised policy iteration with episodes. After each episode, average the observed returns into Q for each first-visited pair, then make the policy greedy at the visited states:

Monte Carlo ES, after each episode, for each first visit (St, At)Q(St, At) ← average of Returns(St, At),    π(St) ← argmaxa Q(St, a)

Below, every episode starts from a random state and random first action, then follows the current greedy policy. The initial policy is the one evaluated above.

Monte Carlo ES on blackjack

episodes 0

S = stick, H = hit, by the current greedy policy. The number is maxa Q(s, a), the estimated optimal value.

With a million or more episodes the policy settles into the well-known optimal blackjack strategy, apart from a few cells near the boundary where the two actions are nearly equal and the estimates are still noisy. Without a usable ace: stick from 12 upward when the dealer shows 4–6, from 13 upward against 2 or 3, and hit up to 16 against 7 through ace. With a usable ace, hit more boldly: up to 17 always, and on 18 too when the dealer shows 9, 10 or ace. The ace can drop to 1, so a hit can't bust you.

06Without exploring starts: ε-soft policies

Exploring starts are rarely possible when learning from real interaction. The alternative is to keep the policy itself exploratory. An on-policy method uses an ε-soft policy, such as ε-greedy, where every action has probability at least ε/|𝒜|:

ε-greedy improvementπ(a|s) = 1 − ε + ε/|𝒜(s)|  if a = argmaxa′ Q(s, a′),    ε/|𝒜(s)|  otherwise

GPI still works: it converges to the best policy among ε-soft policies, which is close to optimal but still explores. To learn the truly optimal policy while behaving exploratively, separate the two roles.

07Off-policy prediction with importance sampling

Off-policy methods learn about a target policy π from episodes generated by a different behaviour policy b. This requires coverage: every action π might take must have b(a|s) > 0. The returns from b have the wrong expectation, so each return is reweighted by the relative probability of its trajectory under the two policies, the importance-sampling ratio. The unknown transition probabilities cancel out of it:

Importance-sampling ratioρt:T−1 = Πk=t..T−1 π(Ak|Sk) / b(Ak|Sk)

There are two ways to average the weighted returns over the set 𝒯(s) of visits to s:

Ordinary vs weighted importance samplingV(s) = Σt∈𝒯(s) ρt:T(t)−1 Gt / |𝒯(s)|    versus    V(s) = Σt∈𝒯(s) ρt:T(t)−1 Gt / Σt∈𝒯(s) ρt:T(t)−1

Ordinary importance sampling is unbiased, but its variance can be huge, even infinite, because ratios can be large. Weighted importance sampling is biased (with one return its estimate is just that return, whatever π), but the bias vanishes as data accumulates and its variance is far lower. In practice weighted is strongly preferred.

The test below repeats Sutton & Barto's Example 5.4. Estimate the value of a single state (your sum 13 with a usable ace, dealer showing 2) under the target policy “stick on 20 or 21” from episodes played by a uniformly random behaviour policy. The true value is about −0.27726. Because π is deterministic, ρ is 0 for any episode where b ever deviated from π, and 2k otherwise, for an episode with k decisions.

Ordinary vs weighted importance sampling

Mean squared error over 100 runs (episodes on a log scale)

Ordinary importance samplingWeighted importance sampling

Weighted importance sampling starts with a small error and keeps it small. Ordinary importance sampling, though unbiased, starts many times worse: a single episode with ρ = 2k and G = ±1 gives an estimate far outside [−1, 1]. It takes a few dozen episodes to come down to the weighted curve. In problems with longer episodes the ratios grow exponentially, and the gap can last far longer. The same weighting idea gives off-policy Monte Carlo control: behave with an exploratory b, estimate q for the greedy target policy using weighted importance sampling. Its weakness is that it learns only from the tails of episodes where b happened to act greedily, which can be slow.

08Strengths and weaknesses

Strengths

  • Needs no model, only sample episodes, or even a simulator.
  • Unbiased: targets are actual returns.
  • Each state is estimated independently, so you can focus on just the states you care about.
  • Less harmed when the Markov property fails, because it doesn't bootstrap.

Weaknesses

  • Only for episodic tasks; nothing is learned until an episode ends.
  • Returns can have high variance, so learning can be slow.
  • Needs exploration (exploring starts, ε-soft or off-policy), and off-policy IS can have very high variance.

Temporal-difference learning keeps the model-free part and fixes the waiting: it updates after every step by bootstrapping, as DP does. The TD vs MC page puts the two side by side on the same episodes.