5.1.10 · D4Reinforcement Learning Foundations

Exercises — Monte Carlo methods

3,337 words15 min readBack to topic

Before we start, three reminders the whole page rests on.

(1) The return — the total discounted reward from time until the episode ends at time :

Here is the reward you receive just after being at time , (the discount factor, a number between and ) shrinks rewards that arrive later, and is the final step. Everything below is either computing this sum, or averaging it.

(2) The recursive form of the return. Factor a single out of every term after the first:

The bracket is exactly the return starting one step later, i.e. . So

Why we bother: this lets us build a whole episode's returns from the last step backwards without re-summing the tail each time — one multiply-and-add per step. We use it in L2.2.

(3) The action-value . Where answers "how good is state ?", the action-value answers "how good is taking action in state , then following the policy?" It is the expected return starting from , committing to first:

MC estimates it the same way it estimates : average the observed returns that followed each pair, using the incremental-mean update. We use in L4.1 and L5.2.

(4) Two model symbols used only in L3.2. A model of the environment is described by two functions. is the expected immediate reward you get for taking action in state . is the transition probability — the chance of landing in next-state after taking in . "Model-free" means we do not know either of these; we only see samples. Keep these two definitions in mind for L3.2.


Level 1 — Recognition

Recall Solution

Answer: (b).

  • (a) updates before the episode ends and uses its own estimate — that is bootstrapping (Temporal-Difference learning), not MC.
  • (c) uses a known model — that is Dynamic Programming, not model-free.
  • (b) waits for the complete episode, then averages actual returns — this is exactly the Monte Carlo definition: episode-based, model-free, uses real outcomes.
Recall Solution

With nothing is shrunk, so the return is just the plain sum of all rewards. Answer: .


Level 2 — Application

Recall Solution

Plug straight into the definition, one power of per step: Answer: .

Recall Solution

From step 2, only and remain: Now rebuild backwards with the recursive form , applied twice: Matches L2.1 — the recursion and the direct sum must always agree, since one was derived from the other. Answer: , and the recursion reproduces .

Recall Solution

This is the 6th visit, so the step size is : Answer: . Sanity check: the average of the running mean (weight 5) and the new (weight 1) is . Identical, as it must be.


Level 3 — Analysis

Recall Solution

Step 1 — return at each visit (undiscounted, so just sum remaining rewards).

  • First (time ): .
  • Second (time ): .

Step 2 — First-visit MC: use only the first occurrence of :

Step 3 — Every-visit MC: average all occurrences of :

Answer: first-visit , every-visit . Unlike the parent note's example, here the two returns differ, so the methods disagree on this episode. Both would still converge to the same over infinitely many episodes.

Recall Solution

Greedy policy improvement with requires Both (expected immediate reward) and (transition probability) appear explicitly — but model-free MC does not know them, so this maximisation cannot be evaluated. Fix: estimate the action-value instead (defined in reminder (3)). Then improvement collapses to which needs no model — already bundles the expected reward and the expected discounted future into one learned number. That is precisely why MC control learns , not .

Recall Solution

Unbiased. By definition . A single return is one sample of that random variable, and the expectation of a sample equals the quantity it samples — so on average it hits the true value; no systematic error. High variance: each is one whole path through a stochastic environment — every random action choice, every random transition, every random reward along the entire episode is baked into that one number. Long episodes accumulate many independent random effects, so returns scatter widely. Averaging many episodes shrinks this scatter (law of large numbers), which is why MC needs many samples.


Level 4 — Synthesis

Recall Solution

Step 1 — count steps remaining. Each move costs , , so the return from a state is simply . The figure below draws the path on the grid and labels each arrow with the return credited to that move — so you can see how the return grows more negative (deeper) the earlier in the path a move sits.

Move from Steps remaining Return
right 6
down 5
down 4
right 3
right 2
down 1

Figure — Monte Carlo methods
Figure: the same episode as a picture. Each coral arrow is a move; its label is the return credited to that move (equal to the negative of the moves still remaining). Notice the returns get deeper — from at the last move up to at the very first — because earlier moves must "pay for" every step that follows.

Step 2 — update (recall = expected return after taking in , from reminder (3)). With from initial : Answer: , and each earlier state-action gets credit for the return measured from when it was taken (the table and figure above).

Recall Solution

The random branch (probability ) picks uniformly among all actions, so every action — greedy included — gets an extra from it.

  • Greedy action: .
  • Each non-greedy action: .

Check: — a valid probability distribution. Answer: greedy , each other .


Level 5 — Mastery

Recall Solution

After each episode the visit count goes up by one, so the step size is .

Episode 1 (, , return ): Episode 2 (, , return ): Episode 3 (, , return ): Answer: . Plain average check: . The incremental mean lands exactly on the plain average at every step — that is precisely what the choice guarantees.

Recall Solution

(a) Greedy . Since , the greedy action is . (b) ; . (Sum .) (c) Third visit → : Now and . Since , the greedy action is still — but the gap shrank from to about . A couple more good returns for (only reachable because let us explore it) could flip the policy. Answer: ; greedy unchanged this step.

Recall Solution

Step 1 — MC only updates what it visits. The incremental-mean update touches only for pairs that actually occur in an episode. A pair never sampled keeps its initial value forever.

Step 2 — pure greedy self-censors. With the policy always plays the current . So any action that starts with a poor or merely un-tried estimate is never chosen, hence never sampled, hence never updated — even if it were truly the best. The agent locks onto whatever action first looked good, and has no mechanism to discover it was wrong.

Step 3 — the convergence condition. The guarantee that on-policy MC control reaches requires every pair to be visited infinitely often. A fixed delivers exactly this: from every state each action keeps probability , so no action is ever starved of samples. breaks the condition on line one, so the proof no longer applies and sub-optimal lock-in is possible.

Answer: pure greedy can freeze exploration and trap the policy; a fixed restores the "infinitely often" visitation that the convergence theorem needs.


Recall Self-test checklist (reveal after finishing)

Return exponent starts at on ::: yes — first reward is undiscounted Incremental step size for the -th visit ::: Method that averages full-episode returns ::: Monte Carlo (not TD, not DP) Why control learns not ::: needs no model ε-greedy probability of the greedy action (uniform-over-all convention) ::: Reason for exploration () ::: visit every infinitely often → convergence