Skip to content
Lambdian

Monte Carlo methods

Learn values and policies from complete sampled episodes, with no model of the environment.

Animated explanation

The idea

Dynamic programming needs the rules of the world. Usually we don't have them: we can only play. Monte Carlo methods learn from experience in the most direct way possible: play an episode to the end, look at the return that actually followed each state, and average those returns over many episodes. By the law of large numbers the averages converge to the true values.

For control, estimate action values Q(s,a)Q(s,a) the same way, make the policy greedy with respect to them, but keep a little randomness (ε-greedy) so that every action keeps being tried. Monte Carlo is unbiased and simple, but it must wait until an episode ends and its estimates can be noisy.

First-visit and every-visit

If a state appears several times in one episode, first-visit MC uses only the return after its first appearance; every-visit MC uses all of them. Both converge; first-visit gives independent samples and is easier to analyse.

The exploration problem

A deterministic greedy policy never tries some actions, so their values are never learned. Fixes: exploring starts (start episodes from random state–action pairs), ε-soft policies (on-policy control), or off-policy learning with importance sampling, where a behaviour policy explores and the greedy target policy is evaluated by reweighting returns by ∏π(a∣s)/b(a∣s)\prod\pi(a|s)/b(a|s).

The algorithm, step by step

  1. 1
    Initialise Q(s, a) = 0 and counts N(s, a) = 0; policy ε-greedy in Q.
  2. 2
    Generate an episode s0,a0,r1,…,sTs_0, a_0, r_1, \dots, s_T with the current policy.
  3. 3
    Walk backwards with G←γG+rt+1G \leftarrow \gamma G + r_{t+1}; at the first visit of each (st,at)(s_t, a_t), update NN and Q←Q+(G−Q)/NQ \leftarrow Q + (G - Q)/N.
  4. 4
    The policy improves automatically (ε-greedy in the new Q); repeat for many episodes.

The maths

Return

Gt=rt+1+γGt+1G_t = r_{t+1} + \gamma G_{t+1}

Incremental average

Q(s,a)←Q(s,a)+1N(s,a)(Gt−Q(s,a))Q(s,a) \leftarrow Q(s,a) + \frac{1}{N(s,a)}\big(G_t - Q(s,a)\big)

Importance sampling ratio (off-policy)

ρt:T−1=∏k=tT−1π(ak∣sk)b(ak∣sk)\rho_{t:T-1} = \prod_{k=t}^{T-1}\frac{\pi(a_k\mid s_k)}{b(a_k\mid s_k)}

Strengths and weaknesses

Strengths

  • No model needed; learns from experience
  • Unbiased estimates of returns
  • Simple; works for non-Markov features

Weaknesses

  • Only for episodic tasks; must wait for episodes to finish
  • High variance; needs many episodes
  • Exploration must be handled explicitly

Reach for it when…

Episodic problems with short episodes and a simulator: games, simulated control, evaluating a fixed strategy.

Check your understanding

Question 1

Q1What does Monte Carlo use as the target for V(s)?

Question 2

Q2Why must MC control keep the policy ε-greedy (or use exploring starts)?

Question 3

Q3A key limitation of Monte Carlo methods is that…

Lambdian Lab

Monte Carlo prediction and control

Using the provided gridworld and env_step:

  • run_episode(policy_fn, rng, max_steps): a list of (state, action, reward) from START; policy_fn(state, rng) returns an action.
  • mc_prediction(policy, episodes, gamma, seed): first-visit MC estimate of V for a deterministic policy dict.
  • mc_control(episodes, eps, gamma, seed): on-policy first-visit MC control with an ε-greedy policy; return Q as a dict state → array of 4.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.