5.1.10 · D1Reinforcement Learning Foundations

Foundations — Monte Carlo methods

4,305 words20 min readBack to topic

This page assumes you have never seen the symbols in the parent Monte Carlo note. We will build every letter, subscript, and Greek symbol from the ground up, in an order where each new idea stands on the one before it. By the end you will be able to read the parent note line by line without stopping.

A quick note on how figures are named: each picture on this page is tagged s01, s02, s03 in the order it appears. When the text says "look at figure s01", it means the first embedded picture, and so on — the little tag matches the file name so you never lose track of which drawing we mean.


0. The stage: what is an "episode"?

Before any letter, picture the thing Monte Carlo methods live inside.

An episode is one complete run of a task from a start to a finish. A chess game from first move to checkmate. One hand of blackjack from deal to payout. One walk through a maze from entrance to exit.

Figure — Monte Carlo methods

Look at the picture above (figure s01). Time flows left to right. At each moment you are in some situation (a circle), you do something (an arrow), and you get a number back (a reward). Then the world moves you to the next situation. The chain ends — that is the crucial word. Monte Carlo only works when the chain ends, because we need to reach the finish before we can look back and add up what happened.

Everything below is just names for the parts of that picture.


1. State — "where you are right now"

  • Plain words: stands for "State". The little below it (a subscript) is a clock reading — is where you start, is where you are one step later, and so on.
  • The picture: each circle in figure s01. In blackjack a state might be (player total 19, dealer shows 2, no usable ace). In a maze it is which room you stand in.
  • Why the topic needs it: Monte Carlo estimates "how good is this state?" — so it must be able to point at "this state" with a name. is that name.

2. Action — "what you chose to do"

  • Plain words: for "Action", subscript = which step. is your first move.
  • The picture: the labelled arrows in figure s01 (hit, stick, right, down).
  • Random variable too: like , the action can vary run to run (especially when the policy chooses randomly), so it is also a random variable filled in by chance each episode.
  • Why needed: the control half of Monte Carlo has to compare actions to find the best one. No actions, nothing to improve.

3. Reward — "the number the world hands back"

  • The picture: the numbers floating above each arrow in figure s01 (0, 0, +1, -1).
  • Also a random variable: the same action in the same state can yield different rewards on different runs, so is random too.
  • Why needed: rewards are the only feedback we get. Monte Carlo has no rulebook, no map — just this trickle of numbers. Everything we learn is built by adding these up.

4. Terminal time — "when the episode ends"

  • Plain words: a plain capital = "Terminal". If a maze run takes 6 moves, then .
  • The picture: the double circle on the far right of figure s01.
  • Why needed: Monte Carlo must reach before it can compute anything — that is the whole "wait until the end, then look back" idea. This is exactly why MC only works on tasks that terminate.

5. The discount factor — "how much the future is worth"

Now the first Greek letter. It is pronounced "gamma" and drawn .

Figure — Monte Carlo methods

Look at figure s02. Each bar is the weight applied to a reward that is steps away. When the bars fade slowly — the far future still matters. When they collapse fast — you barely care about anything distant.

  • Reading the extremes: means "I only care about the very next reward" (myopic — near-sighted). (gamma creeping toward one) means "a reward 100 steps away is almost as precious as one right now" (far-sighted).
  • The superscript : this just means " multiplied by itself times". (no shrink), , , and so on. Since , each power is smaller than the last — that is the shrinking you see in s02.

6. The Return — "the total score, looking forward from now"

This is the star of the show. Everything before was setup.

Let us read it slowly, term by term, using figure s02's weights:

  • is the next reward, weighted by (full value, no delay).
  • is the reward two steps out, shrunk once.
  • shrunk twice, and so on.
  • The last term is the final reward at the end; the exponent just counts how many steps of delay separate "now" () from "the end" ().

The recursive shortcut

There is a beautiful shortcut. Factor out of everything except the first term:

The bracket is the return starting one step later, which is . So:

Figure — Monte Carlo methods

Read figure s03 right-to-left: the sweep begins at the terminal circle where , and at each earlier state we stack "my reward now" on top of " the return I already know from the next state".


7. Policy — "your rulebook for choosing actions"

The next Greek letter, "pi", drawn (same symbol as but here it means something totally different — do not confuse them).

  • The picture: a table pinned to each circle in figure s01 saying "in this state, do this".
  • Why needed: Monte Carlo evaluates a specific policy — "how good is state if I keep behaving like ?" Without naming the behaviour, "how good is this state" has no answer, because the future depends on how you act. The policy is also what decides the probabilities of each random — so it shapes the whole distribution of returns.

8. Expectation — "the long-run average"

Before we can define value we need the averaging symbol itself.

  • Plain words: think of rolling a fair die forever and averaging: the average settles on . That is — a number no single roll ever shows, yet the true center of them all.
  • The subscript : the outcomes of depend on how you behave. Writing says "average over the randomness produced when actions are chosen by policy ". Change and the whole distribution — and hence the average — changes.
  • The bar inside, as in : the bar reads "given that". So the whole thing means "the average return, over all -driven episodes, among only those runs where the state at time was ".
  • Why the topic needs it: the true value we chase is a long-run average of a random return. is the precise name for that average. Monte Carlo's whole job is to estimate this by literally averaging observed samples.

9. The value function — "how good a state is, on average"

Now averaging finally meets value.

Every symbol here is now earned: is the -driven long-run average (§8), the bar is "given that", and says the random state landed on the particular state .


10. First-visit vs every-visit — which returns we average

Inside a single episode you might pass through the same state more than once. That raises a fair question: if shows up twice in one run, do we record both returns or only the first? The answer defines two variants.


11. The action-value — "how good doing in is"

  • The subscript matters: just like , the action-value depends on the policy you follow after the first action — so it wears the same label. Different policy afterward, different .
  • Difference from : scores a state; scores a state-and-a-choice.
  • Why the topic desperately needs : to improve a policy you must compare actions. With only , choosing the best action needs a model of the world (if I do a, where do I land?) — which model-free MC does not have. But already bakes in the consequence, so "pick the best action" becomes simply "pick the with the largest ". No model required.

12. Counters, initialization, and the incremental mean: , ,

To average returns as they arrive (without hoarding every number), we keep two running quantities per state:

Initialization (before any episode): set and for every state . Starting the count at zero is essential — the very first update will make , so the first sample entirely replaces the initial guess (as it should, since a single sample is its own average). The starting value is arbitrary and gets washed out immediately by that first update.

Now the update loop. Each time a fresh return arrives for state (first-visit: only its first occurrence in the episode):

  1. First bump the count:
  2. Then nudge the estimate using the updated count:

The order matters: we increment first so that the fraction uses the new, correct visit number. On the very first visit this makes and the step is , so jumps straight to — exactly the mean of one sample.

Reading the update: the arrow means "becomes / gets updated to". The bracket is the error — how far the new sample sits from our current guess. We step a fraction of the way toward it.

Sometimes we replace with a fixed small number (Greek "alpha"), giving:

  • Why a fixed instead of ? With , every sample counts equally forever — great for a fixed world. A fixed forgets old samples slowly, letting the estimate track a changing world (useful when the policy is still improving). is a knob between "trust all history equally" and "chase recent evidence".

13. -greedy — "mostly best, sometimes curious"

Last Greek letter, "epsilon", drawn — think "a tiny bit".

  • reads "the action that makes largest" ( = "the argument giving the max", i.e. which , not the value).
  • Why needed: if you always pick what looks best right now (pure greedy, ), you might never try the action that would have been great — you'd never gather evidence for it. A dash of randomness guarantees every state-action pair gets sampled eventually, which is exactly what the "average over many samples" promise requires.

The prerequisite map

Episode ends at time T

State S_t where you are

Reward R_t+1 world gives back

Action A_t what you choose

Return G_t discounted total with base G_T = 0

Discount gamma

Expectation E long run average

True value v of a state

Policy pi rulebook

Action value Q pi of state and action

Average many returns first visit

Init and update counter N and estimate V

Monte Carlo methods

MC control finds best policy

Epsilon greedy exploration

Read top to bottom: episodes give you states, actions, and rewards; rewards fold into the return (with the help of and the base case ); the expectation turns returns into true value; and value plus plus exploration is exactly what Monte Carlo needs.


Equipment checklist

Cover the right side and see if you can answer each before revealing.

What is an episode?
A finite run of the task from a start to a terminal (ending) point — it must stop.
What does the subscript in , mean?
A clock reading — which time step. is the start, one step later, etc.
Are , , fixed labels or random variables?
Random variables — before a run, chance decides which value each takes; that is why we must average over many runs.
Why is the reward for acting at time written , not ?
The reward is the consequence of the action and arrives one tick later, so it wears the label .
What is ?
The terminal time step — when the episode ends. MC must reach it before computing anything.
What does (gamma) do, and what range is it in?
It shrinks each reward by for steps of delay; . It handles uncertainty about the future and keeps totals finite.
Why is safe here but risky in general?
With a terminating episode the sum has finitely many terms so it stays finite; in a never-ending task could make the return unbounded.
Write the return in full and in recursive form, including the base case.
; recursively with .
Why must we define ?
At the exponent becomes and there is no reward left to collect, so the sum is empty — we define it as to anchor the recursion.
Is one number or an average?
One number — a single (random) sample from one episode. The average of many is the value.
What does mean, and what does the subscript in add?
is the probability-weighted long-run average of random ; the says "average over the randomness produced when acting by policy ".
What is a policy ?
A rulebook mapping states to actions (or action probabilities) — your strategy.
Write in words and symbols.
The average return starting from and following : .
First-visit vs every-visit MC — what is the difference?
First-visit records the return only from 's first occurrence per episode (unbiased, independent samples); every-visit records all occurrences (more samples, correlated, slightly biased).
How does differ from , why the subscript, and why do we need it?
scores a state and a chosen action then follows ; the marks that dependence; it lets us pick the best action with no model of the world.
How do you initialize and ?
and for every state before any episode.
In the update, why increment before using ?
So the fraction uses the correct new visit count; on the first visit this gives , making jump straight to the first sample.
Why is the same as taking a mean?
Stepping of the way toward each new sample is algebraically identical to recomputing the running average, with no stored history.
Why use -greedy instead of always-greedy?
A small random chance keeps exploring every action, so all state-action pairs get sampled — needed for the averages to converge.