12  Monte Carlo Methods

Core idea. Monte Carlo reinforcement learning estimates value functions from complete sampled episodes instead of from a known transition model. The mathematical object being estimated is still a conditional expectation:

\[ V^\pi(s)=\mathbb E_\pi[G_t\mid S_t=s], \qquad Q^\pi(s,a)=\mathbb E_\pi[G_t\mid S_t=s,A_t=a]. \]

The difference from dynamic programming is not the target. The difference is the information available. Dynamic programming assumes the transition law \(P\) and reward model \(r\) are known. Monte Carlo methods assume that we can sample trajectories and average observed returns.

12.1 Learning goals

After reading this chapter, students should be able to:

  1. define episodic tasks, stopping times, returns, and sampled episodes;
  2. explain Monte Carlo prediction as estimation of a conditional expectation;
  3. distinguish first-visit and every-visit Monte Carlo evaluation;
  4. derive the incremental sample-mean update;
  5. implement Monte Carlo prediction for state values and action values;
  6. explain why Monte Carlo methods are model-free but usually require complete episodes;
  7. describe Monte Carlo control with exploring starts and with \(\epsilon\)-greedy exploration;
  8. explain on-policy and off-policy Monte Carlo learning;
  9. derive ordinary and weighted importance sampling estimators;
  10. compare Monte Carlo error, variance, and bias with dynamic programming and temporal-difference learning;
  11. use AI tools responsibly to check algorithms, diagnose variance problems, and critique experimental design.

12.2 9.1 Why Monte Carlo methods enter reinforcement learning

In Chapters 5–8, we studied Bellman equations under the assumption that the model is known. For example, policy evaluation solves

\[ V^\pi(s) = r_\pi(s)+\gamma\sum_{s'}P_\pi(s,s')V^\pi(s'). \]

This equation is exact, but it requires \(P_\pi\) and \(r_\pi\). In many applications, those quantities are unknown. A robot can try actions. A recommendation system can observe user responses. A tutoring system can observe student progress. But the full transition kernel is rarely available.

Monte Carlo methods replace exact expectation by empirical averaging. If we observe returns

\[ G_1(s),G_2(s),\ldots,G_n(s) \]

from visits to state \(s\), then the Monte Carlo estimator is

\[ \widehat V_n(s)=\frac{1}{n}\sum_{i=1}^n G_i(s). \]

This is the same statistical idea as estimating a population mean from samples. What makes reinforcement learning different is that the samples are generated by a policy interacting with a Markovian environment. Therefore the returns are random variables built from long trajectories, not independent one-step labels.

Monte Carlo RL should be read as conditional expectation estimation from dependent sequential data. The algorithmic update is simple averaging, but the mathematical interpretation depends on states, actions, stopping times, and policies.

12.3 9.2 Episodic tasks and returns

Monte Carlo methods are most natural for episodic problems. An episode begins at time \(0\) and ends at a random terminal time \(T\). The terminal time is a stopping time with respect to the observed history:

\[ H_t=(S_0,A_0,R_1,S_1,A_1,R_2,\ldots,S_t). \]

A typical episode has the form

\[ S_0,A_0,R_1,S_1,A_1,R_2,\ldots,S_{T-1},A_{T-1},R_T,S_T, \]

where \(S_T\) is terminal. The return from time \(t\) is

\[ G_t=R_{t+1}+\gamma R_{t+2}+\gamma^2R_{t+3}+\cdots+\gamma^{T-t-1}R_T. \]

Equivalently,

\[ G_t=\sum_{k=0}^{T-t-1}\gamma^kR_{t+k+1}. \]

The finite terminal time makes \(G_t\) well-defined even when \(\gamma=1\). In continuing tasks, one usually requires \(0\leq\gamma<1\) or uses average-reward methods, which are treated later in Chapter 21.

For a fixed policy \(\pi\), the state-value and action-value functions are

\[ V^\pi(s)=\mathbb E_\pi[G_t\mid S_t=s] \]

and

\[ Q^\pi(s,a)=\mathbb E_\pi[G_t\mid S_t=s,A_t=a]. \]

These definitions are the same as in earlier chapters. Monte Carlo methods only change how the expectations are approximated.

12.3.1 Interactive: distribution of sampled returns

A value function is an expectation, but Monte Carlo methods observe individual random returns. The following Plotly figure shows a simple simulated return distribution and its sample average.

12.4 9.3 Monte Carlo prediction as conditional mean estimation

Suppose a policy \(\pi\) is fixed. The prediction problem is to estimate \(V^\pi\) or \(Q^\pi\) from episodes sampled under \(\pi\).

For a state \(s\), let

\[ \mathcal I_n(s)=\{i: \text{episode } i \text{ contains at least one visit to } s\}. \]

For each selected episode \(i\), choose a return sample \(G_i(s)\) associated with a visit to \(s\). Then

\[ \widehat V_n(s) = \frac{1}{|\mathcal I_n(s)|} \sum_{i\in\mathcal I_n(s)}G_i(s). \]

When the returns are sampled from the correct conditional distribution and suitable regularity assumptions hold, the law of large numbers gives

\[ \widehat V_n(s)\longrightarrow V^\pi(s). \]

This statement is conceptually simple but important. Monte Carlo prediction is not a Bellman backup. It does not use the recursion

\[ V^\pi(s)=r_\pi(s)+\gamma\sum_{s'}P_\pi(s,s')V^\pi(s'). \]

Instead, it estimates the expectation defining \(V^\pi(s)\) directly.

12.4.1 First-visit and every-visit samples

There are two common ways to define the samples.

First-visit Monte Carlo. For each episode, use only the return following the first time state \(s\) appears. If

\[ \tau_i(s)=\min\{t:S_t^{(i)}=s\}, \]

then the sample is

\[ G_i(s)=G_{\tau_i(s)}^{(i)}. \]

Every-visit Monte Carlo. Use the return following every visit to \(s\) in the episode. If state \(s\) appears at times \(t_1,t_2,\ldots,t_m\), then the samples are

\[ G_{t_1}^{(i)},G_{t_2}^{(i)},\ldots,G_{t_m}^{(i)}. \]

First-visit Monte Carlo often has a cleaner statistical interpretation because there is at most one sample per episode for a given state. Every-visit Monte Carlo can use more data, but returns within the same episode are dependent.

12.4.2 Interactive: first-visit versus every-visit Monte Carlo

This figure compares the convergence behavior of first-visit and every-visit averages in a small episodic process.

12.5 9.4 Incremental averaging

Monte Carlo estimates are usually updated online. Suppose \(X_1,X_2,\ldots\) are observed returns for a fixed state or state-action pair. The sample average after \(n\) observations is

\[ \bar X_n=\frac{1}{n}\sum_{i=1}^n X_i. \]

A useful recursion is

\[ \bar X_n=\bar X_{n-1}+\frac{1}{n}(X_n-\bar X_{n-1}). \]

Thus a value estimate can be updated by

\[ V(S_t)\leftarrow V(S_t)+\alpha_t(S_t)(G_t-V(S_t)), \]

where the sample-average step size is

\[ \alpha_t(S_t)=\frac{1}{N_t(S_t)}. \]

More generally, one may use a constant step size \(\alpha\), giving

\[ V(S_t)\leftarrow V(S_t)+\alpha(G_t-V(S_t)). \]

The sample-average update is appropriate for a stationary target. The constant-step-size update is often used when the environment, policy, or data distribution changes over time.

Theorem 9.1: incremental mean identity. If

\[ \bar X_n=\frac{1}{n}\sum_{i=1}^nX_i, \]

then

\[ \bar X_n=\bar X_{n-1}+\frac{1}{n}(X_n-\bar X_{n-1}). \]

Proof. Since

\[ \bar X_{n-1}=\frac{1}{n-1}\sum_{i=1}^{n-1}X_i, \]

we have

\[ \bar X_n = \frac{1}{n}\left(\sum_{i=1}^{n-1}X_i+X_n\right) = \frac{1}{n}\left((n-1)\bar X_{n-1}+X_n\right). \]

Therefore

\[ \bar X_n = \bar X_{n-1}+\frac{1}{n}(X_n-\bar X_{n-1}). \]

12.5.1 Interactive: Monte Carlo sample mean convergence

The following Plotly figure shows the sample mean of noisy returns converging to the true expected return.

12.6 9.5 Python example: Monte Carlo prediction in a random-walk environment

Consider a small random-walk process with terminal states at the left and right ends. The nonterminal states are

\[ \{1,2,3,4,5\}. \]

At each nonterminal state, the agent moves left or right with equal probability. Reaching the right terminal state gives reward \(1\); all other rewards are \(0\). With \(\gamma=1\), the value is the probability of eventually reaching the right terminal state.

import numpy as np
import pandas as pd

rng = np.random.default_rng(7339)

nonterminal_states = np.array([1, 2, 3, 4, 5])
left_terminal = 0
right_terminal = 6

def generate_episode(start_state=3):
    """Generate one random-walk episode.

    Return a list of (state, reward_after_action) pairs for nonterminal states.
    The reward attached to state s_t is R_{t+1}.
    """
    s = start_state
    episode = []
    while s not in (left_terminal, right_terminal):
        step = rng.choice([-1, 1])
        next_s = s + step
        reward = 1.0 if next_s == right_terminal else 0.0
        episode.append((s, reward))
        s = next_s
    return episode

def returns_from_episode(episode, gamma=1.0):
    G = 0.0
    returns = []
    for s, r in reversed(episode):
        G = r + gamma * G
        returns.append((s, G))
    returns.reverse()
    return returns

# First-visit Monte Carlo prediction
V = {s: 0.0 for s in nonterminal_states}
N = {s: 0 for s in nonterminal_states}

num_episodes = 5000
for _ in range(num_episodes):
    episode = generate_episode(start_state=3)
    returns = returns_from_episode(episode, gamma=1.0)
    visited = set()
    for s, G in returns:
        if s not in visited:
            visited.add(s)
            N[s] += 1
            V[s] += (G - V[s]) / N[s]

true_values = {s: s / 6 for s in nonterminal_states}
summary = pd.DataFrame({
    "state": nonterminal_states,
    "MC estimate": [V[s] for s in nonterminal_states],
    "true value": [true_values[s] for s in nonterminal_states],
    "visits": [N[s] for s in nonterminal_states]
})
summary
state MC estimate true value visits
0 1 0.165094 0.166667 2968
1 2 0.335479 0.333333 3729
2 3 0.504400 0.500000 5000
3 4 0.669321 0.666667 3768
4 5 0.834547 0.833333 3022

The true value here is known because the process is a simple symmetric random walk. For general reinforcement learning problems, the true value is not known. The point of the example is to show how the estimator is built from complete returns.

12.7 9.6 Monte Carlo action-value prediction

For control, estimating \(V^\pi(s)\) is not enough. A policy chooses actions, so the agent needs to compare actions. The action-value function is

\[ Q^\pi(s,a)=\mathbb E_\pi[G_t\mid S_t=s,A_t=a]. \]

A Monte Carlo estimator is

\[ \widehat Q_n(s,a) = \frac{1}{N_n(s,a)}\sum_{i,t:S_t^{(i)}=s,A_t^{(i)}=a}G_t^{(i)}. \]

The incremental update is

\[ Q(S_t,A_t) \leftarrow Q(S_t,A_t)+\frac{1}{N(S_t,A_t)}(G_t-Q(S_t,A_t)). \]

If every state-action pair is visited infinitely often, then the estimates can converge to the true action values under suitable conditions.

The phrase visited infinitely often is not a computational detail. It is a mathematical assumption about exploration. Without enough visits to an action, no sample-based method can estimate its value reliably.

12.8 9.7 Monte Carlo control with exploring starts

Monte Carlo control tries to improve the policy using the estimated action values. A conceptually clean version assumes exploring starts: every state-action pair has positive probability of being the initial pair of an episode.

The algorithm alternates between two operations:

  1. generate an episode using the current policy;
  2. update \(Q(s,a)\) using returns;
  3. improve the policy greedily:

\[ \pi(s)\in\arg\max_a Q(s,a). \]

This is a sample-based analogue of generalized policy iteration. Instead of exact evaluation, it uses Monte Carlo estimates. Instead of waiting for exact convergence, it continuously improves the policy as estimates change.

Monte Carlo control with exploring starts

Initialize \(Q(s,a)\) arbitrarily and initialize a policy \(\pi\).

For each episode:

  1. choose an initial pair \((S_0,A_0)\) with positive probability for all pairs;
  2. generate an episode following \(\pi\) after the start;
  3. compute returns \(G_t\) backward from the episode;
  4. for each first visit to \((S_t,A_t)\), update the average estimate of \(Q(S_t,A_t)\);
  5. for each visited state \(s\), set \(\pi(s)\) to a greedy action with respect to \(Q(s,\cdot)\).

Exploring starts are mathematically convenient but often unrealistic. In most applications, the initial state distribution is not under the designer’s control, and the first action may not be freely randomized over all possibilities.

12.9 9.8 On-policy Monte Carlo control with epsilon-greedy policies

A more practical approach is to use an exploratory policy throughout learning. An \(\epsilon\)-greedy policy with respect to \(Q\) chooses a greedy action most of the time and a random action sometimes.

For a finite action set \(\mathcal A(s)\), define

\[ A^*(s)\in\arg\max_a Q(s,a). \]

An \(\epsilon\)-greedy policy is

\[ \pi(a\mid s) = \begin{cases} 1-\epsilon+\epsilon/|\mathcal A(s)|, & a=A^*(s),\\ \epsilon/|\mathcal A(s)|, & a\neq A^*(s). \end{cases} \]

This policy is stochastic. It balances exploitation of the current best action with exploration of other actions.

A common theoretical condition is called GLIE, meaning greedy in the limit with infinite exploration. It requires that all state-action pairs continue to be visited infinitely often while the policy becomes greedy in the limit. Informally,

\[ \epsilon_t\to 0 \]

but not so quickly that exploration stops too early.

12.9.1 Interactive: epsilon-greedy exploration

The next Plotly figure shows how \(\epsilon\) changes action probabilities and how decay schedules affect exploration over time.

12.10 9.9 Python example: Monte Carlo action-value control in a tiny MDP

The following example uses a small episodic MDP. The agent begins in state \(0\). From each nonterminal state it chooses action \(0\) or \(1\). Rewards are stochastic, so Monte Carlo averaging is needed.

import numpy as np
import pandas as pd

rng = np.random.default_rng(2026)

states = [0, 1, 2]
actions = [0, 1]
terminal = 3

def step(s, a):
    """Tiny stochastic episodic MDP."""
    if s == 0:
        if a == 0:
            return 1, rng.normal(0.0, 0.2)
        return 2, rng.normal(0.1, 0.2)
    if s == 1:
        if a == 0:
            return terminal, rng.normal(1.0, 0.2)
        return terminal, rng.normal(0.2, 0.2)
    if s == 2:
        if a == 0:
            return terminal, rng.normal(0.4, 0.2)
        return terminal, rng.normal(1.3, 0.2)
    return terminal, 0.0

def epsilon_greedy_action(Q, s, epsilon):
    if rng.random() < epsilon:
        return int(rng.choice(actions))
    return int(np.argmax(Q[s]))

def generate_episode(Q, epsilon):
    s = 0
    episode = []
    while s != terminal:
        a = epsilon_greedy_action(Q, s, epsilon)
        next_s, r = step(s, a)
        episode.append((s, a, r))
        s = next_s
    return episode

Q = np.zeros((len(states), len(actions)))
N = np.zeros_like(Q)
gamma = 1.0

def update_first_visit_Q(episode, Q, N):
    G = 0.0
    visited = set()
    for t in reversed(range(len(episode))):
        s, a, r = episode[t]
        G = r + gamma * G
        pair = (s, a)
        if pair not in visited:
            visited.add(pair)
            N[s, a] += 1
            Q[s, a] += (G - Q[s, a]) / N[s, a]

for k in range(5000):
    epsilon = max(0.05, 1.0 / np.sqrt(k + 1))
    episode = generate_episode(Q, epsilon)
    update_first_visit_Q(episode, Q, N)

policy = {s: int(np.argmax(Q[s])) for s in states}
q_table = pd.DataFrame(Q, columns=["action 0", "action 1"])
q_table.insert(0, "state", states)
print("Greedy policy:", policy)
q_table
Greedy policy: {0: 1, 1: 0, 2: 1}
state action 0 action 1
0 0 1.001152 1.377881
1 1 1.024678 0.176235
2 2 0.438143 1.298328

This example illustrates a common pattern. The algorithm never estimates the transition probabilities. It learns action values directly from experience.

12.11 9.10 Off-policy Monte Carlo prediction

So far, the data-generating policy and the policy being evaluated have been the same. This is called on-policy learning.

In off-policy learning, we distinguish two policies:

  • the target policy \(\pi\), whose value we want;
  • the behavior policy \(b\), which generates the data.

The central difficulty is distribution shift. Episodes generated by \(b\) are not distributed as episodes generated by \(\pi\). Importance sampling corrects this mismatch by reweighting returns.

For a trajectory segment from time \(t\) to time \(T-1\), define the importance ratio

\[ \rho_{t:T-1} = \prod_{k=t}^{T-1}\frac{\pi(A_k\mid S_k)}{b(A_k\mid S_k)}. \]

Then, under support conditions,

\[ V^\pi(s)=\mathbb E_b[\rho_{t:T-1}G_t\mid S_t=s]. \]

The support condition is essential. If \(\pi(a\mid s)>0\), then one must have

\[ b(a\mid s)>0. \]

Otherwise the behavior policy never samples an action that the target policy might choose, and no importance reweighting can recover the missing data.

12.11.1 Ordinary importance sampling

Given returns \(G_i\) and ratios \(\rho_i\), the ordinary importance sampling estimator is

\[ \widehat V_{\mathrm{OIS}} = \frac{1}{n}\sum_{i=1}^n\rho_iG_i. \]

This estimator is often unbiased under ideal assumptions, but it may have very large variance.

12.11.2 Weighted importance sampling

The weighted importance sampling estimator is

\[ \widehat V_{\mathrm{WIS}} = \frac{\sum_{i=1}^n\rho_iG_i}{\sum_{i=1}^n\rho_i}. \]

This estimator is usually biased for finite \(n\), but it often has much lower variance and is consistent under standard assumptions.

12.11.3 Interactive: ordinary versus weighted importance sampling

The following Plotly figure shows why off-policy Monte Carlo can be unstable. Ordinary importance sampling may have large spikes when a few trajectories receive very large weights.

12.12 9.11 Variance and confidence intervals

For a fixed state \(s\), Monte Carlo prediction estimates a mean. If the sampled returns have variance

\[ \sigma_s^2=\operatorname{Var}(G_t\mid S_t=s), \]

then an approximate standard error is

\[ \operatorname{SE}(\widehat V_n(s))\approx \frac{\widehat\sigma_s}{\sqrt{n}}. \]

This explains both the strength and weakness of Monte Carlo methods:

  • they are simple and directly estimate the desired expectation;
  • they can have high variance because \(G_t\) contains many future rewards;
  • they learn only after complete returns are observed;
  • their error decreases at the usual statistical rate \(1/\sqrt n\) for independent samples.

A confidence interval can be approximated by

\[ \widehat V_n(s)\pm 1.96\frac{\widehat\sigma_s}{\sqrt n}, \]

provided the central limit approximation is reasonable. In sequential data, this approximation should be treated carefully because samples from one long trajectory can be dependent.

12.13 9.12 Monte Carlo versus dynamic programming and TD learning

Monte Carlo methods differ from dynamic programming in one fundamental way: they do not require a model. They differ from temporal-difference methods in another fundamental way: they wait for complete returns.

Method Uses model \(P,r\)? Uses complete return? Bootstraps? Typical target
Dynamic programming yes no yes Bellman expectation or optimality equation
Monte Carlo no yes no empirical return average
Temporal difference no no yes one-step bootstrapped target

The Monte Carlo target is

\[ G_t. \]

The TD(0) target, introduced in the next chapter, is

\[ R_{t+1}+\gamma V(S_{t+1}). \]

Monte Carlo methods are unbiased for the return target but can have high variance. TD methods introduce bootstrapping bias but often have lower variance and can update before the episode ends.

Monte Carlo and TD learning estimate the same value function but use different statistical targets. Monte Carlo estimates a full conditional expectation directly. TD estimates a fixed point through local one-step consistency.

12.14 9.13 AI-assisted learning components

AI tools can be helpful in this chapter, especially for checking logic and debugging simulations. They should not replace mathematical verification.

AI prompt: distinguish estimator and estimand

Give an AI system the following question:

In first-visit Monte Carlo prediction, what is the estimand and what is the estimator? Explain the role of the return \(G_t\) and why averaging returns estimates a conditional expectation.

A strong answer should clearly identify \(V^\pi(s)\) as the estimand and \(\widehat V_n(s)\) as the estimator. It should also state the assumptions needed for consistency.

AI prompt: audit an off-policy experiment

Ask an AI system to review an off-policy Monte Carlo experiment and identify whether the support condition

\[ \pi(a\mid s)>0\implies b(a\mid s)>0 \]

is satisfied. Then ask it to explain what can go wrong if the condition fails.

AI prompt: debug return computation

Give an AI system a Python function that computes returns from an episode. Ask it to check whether the code correctly implements

\[ G_t=R_{t+1}+\gamma G_{t+1}. \]

Then verify the response manually on a short episode with known rewards.

12.15 9.14 Common pitfalls

  1. Confusing value with reward. The value \(V^\pi(s)\) is an expected sum of future rewards, not the immediate reward.
  2. Averaging rewards instead of returns. Monte Carlo prediction averages \(G_t\), not just \(R_{t+1}\).
  3. Ignoring repeated visits. First-visit and every-visit methods use different sampling conventions.
  4. Stopping exploration too early. Monte Carlo control needs enough visits to state-action pairs.
  5. Using off-policy data without support. Importance sampling cannot repair missing actions.
  6. Treating all samples as independent. Returns from the same trajectory are often dependent.
  7. Forgetting variance. A Monte Carlo estimate can be unbiased and still practically unreliable if its variance is large.

12.16 9.15 Summary

Monte Carlo methods are the first fully sample-based reinforcement learning methods in this book. They estimate value functions by averaging complete observed returns. The mathematical foundation is conditional expectation and the law of large numbers.

The central prediction update is

\[ V(S_t)\leftarrow V(S_t)+\alpha_t(G_t-V(S_t)). \]

For action values, the corresponding update is

\[ Q(S_t,A_t)\leftarrow Q(S_t,A_t)+\alpha_t(G_t-Q(S_t,A_t)). \]

Monte Carlo control combines return averaging with policy improvement, usually through exploring starts or \(\epsilon\)-greedy exploration. Off-policy Monte Carlo uses importance sampling to correct distribution mismatch, but this may introduce high variance.

The next chapter introduces temporal-difference learning, which replaces the full return \(G_t\) with a one-step bootstrapped target. That change is one of the most important algorithmic ideas in reinforcement learning.

12.17 Exercises

12.17.1 Conceptual exercises

  1. Explain why Monte Carlo prediction is model-free.
  2. Explain why Monte Carlo methods are naturally suited to episodic tasks.
  3. Compare first-visit and every-visit Monte Carlo prediction.
  4. Explain why \(G_t\) is a random variable even when the policy is fixed.
  5. Explain the difference between on-policy and off-policy Monte Carlo learning.
  6. Why can ordinary importance sampling have high variance?
  7. Explain why an unbiased estimator is not necessarily a useful estimator in finite samples.

12.17.2 Mathematical exercises

  1. Prove the incremental mean identity

\[ \bar X_n=\bar X_{n-1}+\frac{1}{n}(X_n-\bar X_{n-1}). \]

  1. Let \(G_1,\ldots,G_n\) be independent samples with mean \(V\) and variance \(\sigma^2\). Compute the mean and variance of

\[ \widehat V_n=\frac{1}{n}\sum_{i=1}^nG_i. \]

  1. For an episodic task with deterministic rewards \(R_{t+1}=1\) for \(t=0,1,\ldots,T-1\), compute \(G_0\) for \(\gamma<1\) and for \(\gamma=1\).

  2. Derive the ordinary importance sampling identity

\[ \mathbb E_b[\rho_{t:T-1}G_t\mid S_t=s]=V^\pi(s) \]

under the assumption that \(b(a\mid s)>0\) whenever \(\pi(a\mid s)>0\).

  1. Show that an \(\epsilon\)-greedy policy assigns positive probability to every action when \(\epsilon>0\).

12.17.3 Computational exercises

  1. Implement first-visit and every-visit Monte Carlo prediction for the random-walk example.
  2. Estimate the empirical standard error of the Monte Carlo value estimate for each state.
  3. Modify the random-walk example so that \(\gamma=0.9\). Compare the estimated values with the undiscounted case.
  4. Implement Monte Carlo action-value prediction for a fixed stochastic policy in a small MDP.
  5. Implement Monte Carlo control with an \(\epsilon_t=1/\sqrt{t}\) exploration schedule.
  6. Simulate ordinary and weighted importance sampling in a two-action bandit-style episodic problem.
  7. Plot the distribution of returns for a selected state and explain why the variance is large or small.

12.17.4 AI-assisted exercises

  1. Ask an AI system to generate pseudocode for first-visit Monte Carlo prediction. Then check whether it correctly handles repeated visits in an episode.
  2. Ask an AI system to explain GLIE in plain language and then rewrite the explanation mathematically.
  3. Give an AI system an incorrect return-computation function and ask it to identify the bug.
  4. Ask an AI system to compare ordinary and weighted importance sampling. Then verify whether it correctly distinguishes finite-sample bias from consistency.
  5. Ask an AI system to design a Monte Carlo experiment for a small gridworld. Critique whether the proposed experiment has enough exploration.

12.18 Notes for instructors

For MA Applied Math students, emphasize conditional expectation, stopping times, sample-average convergence, and the connection to generalized policy iteration. For MS Statistics students, emphasize estimation, variance, importance sampling, support conditions, and dependence among trajectory samples.

A good lecture sequence is:

  1. start from \(V^\pi(s)=\mathbb E_\pi[G_t\mid S_t=s]\);
  2. define first-visit and every-visit estimators;
  3. derive the incremental mean update;
  4. run the random-walk Python example;
  5. introduce action values and Monte Carlo control;
  6. close with off-policy learning and importance sampling variance.

Monte Carlo methods are conceptually simple but statistically rich. They provide an ideal bridge from exact dynamic programming to sample-based reinforcement learning.

12.19 Further reading

The classical introduction is (sutton2018reinforcement?), especially the chapters on Monte Carlo methods. For the dynamic programming background behind policy improvement, see (bellman1957dynamic?), (puterman1994markov?), and (bertsekas2012dynamic?). For a compact mathematical treatment of sample-based reinforcement learning algorithms, see (szepesvari2010algorithms?).