Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Learning by playing many games
Suppose you cannot see the rules of a game, but you can play it as often as you like. To learn how good a position is, play many games from it and average how they ended. That is the Monte Carlo idea: no model, just experience and averages. To find a good strategy, keep track of how each move worked out on average, mostly play the move that looks best, and sometimes try another one in case it is better.
Game-playing programs, from backgammon to Go, have used Monte Carlo averages of simulated games to judge positions.
- 1. Monte Carlo learning estimates a value by…
- 2. Monte Carlo methods need a model of the environment?
- 3. Occasionally trying a different move helps to…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Monte Carlo prediction: V(s) ≈ the average of the returns G_t observed after visits to s. It needs complete episodes, so it applies to episodic tasks.
- First-visit vs every-visit: average the return after the first visit to s in each episode only, or after every visit. Both converge to V^π.
- Incremental form: V(s) ← V(s) + (G − V(s))/N(s), or with a constant step α to forget old returns.
- Monte Carlo control: estimate Q(s, a) instead of V (no model is available to look ahead), and improve the policy greedily, keeping it ε-greedy so every action is still tried.
- Exploring starts: start episodes in random state–action pairs, another way to keep visiting everything.
- Off-policy evaluation: learn about a target policy π from episodes of a behaviour policy b, reweighting each return by the importance ratio ρ = Π π(a_t|s_t)/b(a_t|s_t).
- 1. Monte Carlo prediction needs…
- 2. Control learns Q rather than V because…
- 3. The importance ratio compares…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Monte Carlo prediction on a gridworld
Exercise 1.
- The 4×4 slippery grid with four pits from the previous topic, γ = 0.95, and the uniformly random policy.
- Exact values from the linear system V = r_π + γP_πV, for comparison.
- An episode sampler, and first-visit and every-visit Monte Carlo prediction with exploring starts (each episode starts in a random live cell), 2,000 episodes.
Practice.
- Use Monte Carlo when episodes are short and a simulator is cheap: no model and no bootstrapping, so no bias from wrong value estimates.
- Expect variance: the error shrinks like 1/√n; long, noisy episodes need many samples.
- Keep exploring: an ε-greedy or ε-soft policy, or exploring starts, so every action keeps being tried.
- Use a constant step size when the policy keeps changing, so old returns from worse policies are forgotten.
- Prefer weighted importance sampling off-policy: it has far lower variance; watch the share of episodes with ρ > 0.
- Tools: Gymnasium environments as simulators; NumPy for tabular estimates.
def mc_prediction(P, R, term, pi, gamma, n, rng):
tot, cnt = np.zeros(len(R)), np.zeros(len(R))
for _ in range(n):
ep = episode(P, R, term, pi, rng, s0=rng.choice(np.flatnonzero(~term)))
G, first = 0.0, {}
for t, (s, a, r) in enumerate(ep): first.setdefault(s, t)
for t in reversed(range(len(ep))):
s, a, r = ep[t]; G = gamma * G + r
if first[s] == t: tot[s] += G; cnt[s] += 1
return tot / np.maximum(cnt, 1)- 1. Monte Carlo error with n episodes shrinks like…
- 2. When the policy keeps changing, use…
- 3. Off-policy, prefer…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Unbiasedness and convergence. In first-visit Monte Carlo the returns after first visits to s in different episodes are independent, identically distributed, with mean V^π(s), so their average is unbiased and converges by the law of large numbers, with standard error σ/√n. Every-visit returns within one episode are correlated, so its estimate is biased for finite n but still consistent.
Control. Generalised policy iteration: evaluate Q (approximately), make the policy ε-greedy with respect to Q, repeat. The ε-greedy policy with respect to Q^π is at least as good as π among ε-soft policies (policy improvement for ε-soft policies), so on-policy control converges to the best ε-soft policy, not to the optimal deterministic one.
Importance sampling. The probability of a trajectory under π divided by its probability under b is ρ = Π π(a_t|s_t)/b(a_t|s_t): the transition probabilities cancel, so no model is needed. Then E_b[ρG] = E_π[G], so ordinary importance sampling is unbiased, but its variance can be huge or infinite. Weighted importance sampling, Σ ρG / Σ ρ, is biased for finite n (with one episode it returns that episode’s return) but consistent and usually far less variable.
- 1. First-visit returns from different episodes are…
- 2. On-policy ε-greedy control converges to…
- 3. Ordinary importance sampling is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How fast does Monte Carlo prediction converge, how close does ε-greedy Monte Carlo control get to the optimal policy, and how do ordinary and weighted importance sampling compare off-policy?
History.
- The Monte Carlo method was named by N. Metropolis and S. Ulam (1949).
- A. Barto and M. Duff (1994) related Monte Carlo methods to dynamic programming in reinforcement learning.
- S. P. Singh and R. S. Sutton (1996) compared first-visit and every-visit estimators.
- D. Precup, R. S. Sutton and S. P. Singh (2000) developed off-policy evaluation with importance sampling for reinforcement learning.
Exercise 2.
- The 4×4 pit grid, γ = 0.95; the optimal start value V* = 0.3130 from policy iteration.
- Prediction: the random policy, exploring starts, 100, 1,000 and 5,000 episodes, RMSE over the 11 live cells averaged over 5 runs.
- Control: on-policy first-visit Monte Carlo with ε = 0.1, every episode from the start cell, 10,000 episodes; the exact start value of the greedy policy and of the ε-greedy policy actually followed.
- Off-policy: target = the optimal deterministic policy, behaviour = half optimal action, half uniformly random; 10, 100 and 1,000 episodes, 20 runs each. A weighted estimate with no episode having ρ > 0 is set to 0.
Seed 0:
| episodes | first-visit RMSE | every-visit RMSE |
|---|---|---|
| 100 | 0.0955 | 0.1140 |
| 1,000 | 0.0282 | 0.0281 |
| 5,000 | 0.0134 | 0.0156 |
| control episodes | greedy policy, start value | ε-greedy policy, start value |
|---|---|---|
| 100 | 0.0220 | −0.1512 |
| 1,000 | 0.0994 | −0.0461 |
| 3,000 | 0.2214 | 0.1000 |
| 10,000 | 0.3089 | 0.1392 |
| off-policy episodes | ordinary IS, mean ± sd | weighted IS, mean ± sd | share of episodes with ρ > 0 |
|---|---|---|---|
| 10 | 0.102 ± 0.878 | −0.304 ± 0.540 | 6.5% |
| 100 | 0.198 ± 0.357 | 0.138 ± 0.420 | 7.9% |
| 1,000 | 0.308 ± 0.131 | 0.314 ± 0.091 | 8.3% |
- Prediction error fell roughly as 1/√n: from 1,000 to 5,000 episodes it shrank 2.1 times (√5 = 2.24). First-visit was slightly better than every-visit at 100 and 5,000 episodes, and the two were equal at 1,000.
- Control got close to optimal, slowly: after 10,000 episodes the greedy policy’s start value was 0.3089 of the optimal 0.3130. The ε-greedy policy actually followed was much worse (0.139), because 10% random moves on a slippery grid with pits are costly.
- On-policy control can stall: in seeds 1 and 2 the greedy policy stopped improving at 0.2985 and 0.3020, after 3,000 and 1,000 episodes. Monte Carlo control converges to the best ε-soft policy, and its greedy part need not be optimal.
- Weighted importance sampling won only once there was enough data: at 1,000 episodes its standard deviation was 0.091 against 0.131. At 10 episodes it was badly biased (−0.304 against 0.313), because with few matching episodes it returns their average, and short episodes that end in a pit are the likeliest to match.
- Seeds 1 and 2: prediction RMSE at 5,000 episodes 0.0099 and 0.0126 (first-visit) against 0.0154 and 0.0152 (every-visit); weighted IS at 1,000 episodes 0.287 ± 0.114 and 0.319 ± 0.111, ordinary 0.275 ± 0.153 and 0.349 ± 0.190.
Monte Carlo methods in practice
- No bootstrapping: Monte Carlo targets are actual returns, so they are unbiased even when the value estimates of other states are wrong; the price is variance and waiting until the end of an episode.
- Temporal-difference learning (next topic) updates after every step using the current estimate of the next state, trading some bias for much lower variance.
- Monte Carlo tree search uses Monte Carlo averages of simulated games to guide planning.
- Policy-gradient methods such as REINFORCE use Monte Carlo returns to estimate gradients.
References
- R. S. Sutton and A. G. Barto (2018). Reinforcement Learning: An Introduction, 2nd ed., chapter 5. MIT Press.
- S. P. Singh and R. S. Sutton (1996). “Reinforcement learning with replacing eligibility traces.” Machine Learning.
- D. Precup, R. S. Sutton and S. P. Singh (2000). “Eligibility traces for off-policy policy evaluation.” ICML.
- N. Metropolis and S. Ulam (1949). “The Monte Carlo method.” Journal of the American Statistical Association.
- A. R. Mahmood, H. van Hasselt and R. S. Sutton (2014). “Weighted importance sampling for off-policy learning with linear function approximation.” NeurIPS.