Monte Carlo Methods
Monte Carlo methods price a state by playing whole episodes from it and averaging what actually came back, with no model and no bootstrapping.
Why Does This Exist?
Pricing a state means answering how much total reward follows from it. Dynamic programming answers exactly but demands the full transition model , which real problems rarely provide, plus sweeps over huge state spaces that do not scale.
Monte Carlo methods answer from experience instead. Play an episode to termination, add up the rewards, repeat, average. No model is consulted and no estimate is built on another estimate, so there is no bootstrapping bias to unwind. The law of large numbers does the work: as independent returns accumulate, their average converges to the true expected value. This page covers that prediction mechanism. The counting variants live in first visit versus every visit, the bootstrapping alternative is temporal difference learning, and the control loop on top is Monte Carlo control.
The one idea you need first
The state value is the expected discounted total from onward under policy . Value functions define it and its action value sibling . Monte Carlo replaces that expectation with a plain average of finished totals.
Think of It Like This
Timing a maze route with a stopwatch
You need the average drive time from an intersection to the maze exit but own no map. So you drive it: first trip takes 9 minutes through the north passage, second takes 12 through the south. Your estimate for that intersection is the average, 10.5 minutes. No map, no traffic model, just finished trips and a mean.
More trips sharpen the number and a detour ridden twice still counts once per trip under first visit rules. Where it stops: traffic is far less random than a stochastic MDP, where whole trajectory randomness inflates the spread of totals and learning needs many more trips.
How It Actually Works
Follow policy until termination to get . The return from step is the discounted sum to the end:
with discount in . Then update every visited state with an incremental average over visits:
The update uses the sampled total only, never the current estimate of a successor, which is exactly what non bootstrapping means. When a state appears several times in one episode, first visit uses only the first while every visit uses each one; both converge, with slightly different variance. Episodes must terminate, or never materializes.
Worked example
Price intersection with from two finished trips. Rewards are written per visited state as (state, reward arriving there):
Episode 1: , , , . Adding the rewards along the trip, the total from is . Episode 2: , , , giving from .
Averaging the two first visit totals at :
Two trips, one addition, one division: the estimate is 10.5 with no model and no bootstrapped value anywhere in the computation.
Code
This snippet implements first visit prediction over the two worked episodes and prints the value of .
import numpy as np
def first_visit_values(episodes: list[list[tuple[str, float]]]) -> dict[str, float]: totals: dict[str, list[float]] = {} for episode in episodes: g = 0.0 seen: set[str] = set() for t in range(len(episode) - 1, -1, -1): state, reward = episode[t] g = g + reward earlier = [s for s, _ in episode[:t]] if state not in earlier and state not in seen: seen.add(state) totals.setdefault(state, []).append(g) return {s: float(np.mean(v)) for s, v in totals.items()}
episodes = [ [("A", 0.0), ("B", 1.0), ("C", -2.0), ("T", 10.0)], [("A", 0.0), ("C", 2.0), ("T", 10.0)],]values = first_visit_values(episodes)print(values["A"]) # -> 10.5The printout is 10.5, matching from the hand computation. Adding more episodes to the list would keep sharpening every visited state's average.
Watch Out For
Requiring termination in tasks that never end
Monte Carlo waits for the final step before any update, so a continuing task such as an always on controller or a loop that rarely terminates yields no and therefore no learning, while trajectories consume memory without bound.
Symptom: buffers grow, updates never fire, values stay at initialization. Fix: impose a horizon with resets for episodic evaluation, or use temporal difference updates that learn from every single transition.
High variance drowning the signal
Each carries the randomness of the entire rest of the trajectory, so estimates swing widely episode to episode and learning crawls, especially with long episodes or sparse big rewards.
Symptom: value curves that oscillate for hundreds of episodes where a bootstrapping method settles quickly. Fix: average over more episodes, shorten effective horizons with discounting, or switch the update to a lower variance TD target when bias is the cheaper problem.
The Quick Version
- Monte Carlo prices states by averaging finished episode returns, using no model and no bootstrapping.
- The update is an incremental mean: moves of the way toward each sampled .
- In the worked example, totals 9 and 12 from average to .
- First visit counts one per state per episode, every visit counts each occurrence.
- Episodes must terminate, and long trajectories bring high variance, which is the core tradeoff against TD methods.