Learning from Experience: Monte Carlo & TD
Learning values from samples alone, by averaging returns or bootstrapping from guesses.
A chess program can be handed the rules of chess, but nobody can hand a walking robot the exact physics of its own motors. When an agent has no model of its world, it has to learn what situations are worth from experience alone. There are two ways to do that: wait for the outcome and average what actually happened, or learn from every single step by trusting your own next guess. The second idea, learning a guess from a guess, runs through almost all of modern reinforcement learning, and through a leading theory of how dopamine neurons work.
What you’ll learn
- Explain how a Monte Carlo method estimates a state's value by averaging the returns that followed visits to it.
- Explain bootstrapping, and compute a temporal-difference (TD) update and its TD error by hand.
- Compare Monte Carlo and TD learning on a random walk: what each needs, and what each risks.
- Explain why SARSA learns a safe route beside a cliff while Q-learning learns the edge, and why a maximum over noisy estimates is too optimistic.
- Describe n-step returns and λ as dials between TD and Monte Carlo.
Learning without a map
In the planning lessons, dynamic programming computed values by sweeping the Bellman equation over every state. That requires the transition probabilities and the expected rewards: a complete model of the world. A robot, a trading agent or a game-playing program rarely has one.
A model-free method learns from experience instead: episodes of states, actions and rewards that the agent actually lives through. This lesson starts with prediction: estimating how good each state is under a fixed way of behaving. Then it turns to control: improving the behavior itself.
Our test bed is Sutton and Barto's random walk. Five states, A to E, sit in a row. Every episode starts in the middle, at C, and each step moves left or right with probability ½. Leaving off the right end pays ; leaving off the left end pays . There is no discounting and no choice to make, so the return of an episode is just its final reward, and the value of a state is the probability that a walk starting there leaves on the right.
What is the true value of state D, the probability that a walk starting at D leaves on the right?
Show the answer
0.67
Number the states A = 1 to E = 5, with the left exit at 0 and the right exit at 6. Each value is the average of its two neighbors, , so the values lie on a straight line from 0 at the left exit to 1 at the right exit: . D is , so . The five values are .
Monte Carlo: average what actually happened
The value of a state is an average return, so the most direct estimate is the average of the returns you actually observed after visiting it. After each episode, every visit to a state supplies one new sample: the return that followed it. In the random walk that sample is simply the episode's final reward, 1 or 0. As episodes pile up, the law of large numbers drives each average to the true value.
This is a Monte Carlo method, named like Monte Carlo integration for its use of random samples. It keeps the average up to date with one line per visited state:
An episode goes C, B, C, D, E and leaves on the right. Which estimates does Monte Carlo change, and toward what?
- Only E, toward 1, because only E touched the exit
- B, C, D and E, each toward 1
- Only C, toward 1, because every episode starts there
- B, C, D and E, each toward its neighbor's estimate
Show the answer
B. B, C, D and E, each toward 1
Every visited state was followed by the same outcome, a right exit with reward 1, so each one's return is 1 and each estimate moves toward 1. States the walk never reached (A here) are left alone.
TD: learn a guess from a guess
Monte Carlo has to wait for the end. Is that necessary? Picture driving home from a friend's house. You leave guessing the trip will take 30 minutes. Ten minutes in you hit a jam at the bridge and now expect 25 more minutes, 35 in total. Do you really need to get home before admitting that your first guess was too low?
| Moment | Minutes so far | Predicted minutes left | Predicted total |
|---|---|---|---|
| Leave your friend's house | 0 | 30 | 30 |
| Jam at the bridge | 10 | 25 | 35 |
| Jam clears | 25 | 8 | 33 |
| Stuck behind a bus on your street | 33 | 5 | 38 |
| Arrive home | 38 | 0 | 38 |
Written for state values, the temporal-difference update after one step from to with reward is
The bracket is the TD error : what you just got plus your new guess about the rest, minus your old guess. It is a surprise: positive when things went better than predicted, negative when worse. Updating a guess toward another guess is called bootstrapping, and this one-step version is TD(0). It learns during the episode, from every step, and it needs no ending at all.
In the random walk every estimate is 0.5 except . The walk steps from C to D, with reward 0 and no discounting. With , what is the new estimate ?
Show the answer
0.51
The TD error is , so . C moved toward D's estimate without waiting to see how the episode ends.
The race: TD against Monte Carlo
Which wins on the random walk? TD's target contains one random step; Monte Carlo's contains every random step until the walk falls off an end. But TD's target leans on estimates that start out wrong. Only an experiment can say which effect matters more here.
Which statement is true of TD(0) but not of Monte Carlo?
- Its target is an unbiased sample of the true value
- It needs the transition probabilities of the walk
- It can change an estimate before the episode has ended
- It ignores the rewards and uses only estimates
Show the answer
C. It can change an estimate before the episode has ended
TD(0) updates after every step, from the reward and the next state's estimate. Monte Carlo must wait for the complete return. It is Monte Carlo's target that is unbiased; TD's is biased while the estimates are wrong.
From predicting to acting: SARSA and Q-learning
To choose actions without a model, the agent learns action values : how good it is to take action in state and behave well afterwards. Both popular TD rules behave ε-greedily, taking the best-looking action most of the time and a random one with probability . They differ in a single term of the target.
SARSA uses the action the agent actually takes next; its name lists the five things the update uses, . Q-learning uses the best next action instead, whatever the agent actually does next.
Both agents explore with the same . Q-learning's target assumes the next action will be the best one, so it learns the values of the greedy route along the edge, the right answer for an agent that will stop exploring. But it keeps exploring, and a random step next to the edge is a step off the cliff. SARSA's target includes the random steps it actually takes, so the cells beside the cliff look dangerous to it, and it learns the safer route that is best for an agent that keeps exploring. If shrinks to zero slowly enough, SARSA ends up on the optimal route too.
When the maximum fools you
Q-learning's hides a trap. Imagine ten slot machines that all lose 10 cents per play on average. You try each a few times and estimate its payout. By luck some estimates come out above and some below, and the largest one is very likely above , perhaps even positive. Take the maximum of noisy estimates and you systematically overestimate how good the best option is. This is maximization bias.
Sutton and Barto built an MDP around it. From state A, "right" ends the episode with reward 0. "Left" leads, with reward 0, to state B, where every one of many actions ends the episode with a random reward averaging . Going left is a mistake, yet the max over B's noisy estimates looks attractive.
Dials between TD and Monte Carlo
TD(0) looks one step ahead and then trusts its estimate; Monte Carlo looks all the way to the end and trusts nothing. Why not look steps ahead? The n-step return adds up real rewards and then bootstraps:
With this is the TD(0) target; with beyond the end of the episode it is the full Monte Carlo return.
A second dial, between 0 and 1, averages all the -step returns at once, giving each a weight that shrinks by a factor per extra step: the λ-return. is TD(0) and is Monte Carlo. Remarkably, almost the same learning can happen online, step by step, with an eligibility trace: a fading memory of recently visited states that tells each new TD error which earlier states deserve the credit.
The Bootstrapping lessons build all of this properly. Monte Carlo Methods covers first-visit and every-visit estimation, control with exploring starts and with soft policies, and off-policy learning by importance sampling. Temporal-Difference Learning derives TD(0), compares it with Monte Carlo and dynamic programming, and explains what batch TD converges to. SARSA & Q-Learning develops TD control, the cliff and maximization bias. n-Step Returns & Eligibility Traces derives the λ-return and the traces that implement it.
Key takeaways
- Model-free methods learn values from experienced episodes, without transition probabilities.
- Monte Carlo averages complete returns: unbiased, but noisy, and it must wait for each episode to end.
- TD learning updates a guess toward reward plus the next guess (bootstrapping). The TD error is a surprise signal, and TD learns online, even in tasks that never end.
- SARSA (on-policy) learns the value of how it actually behaves, exploration included; Q-learning (off-policy) learns the value of acting greedily. On the cliff, that is the difference between the safe route and the edge.
- The maximum of noisy estimates is biased upward, which misleads Q-learning; double Q-learning fixes it with two independent estimates.
- n-step returns and the λ-return are dials between TD and Monte Carlo, and settings in the middle often learn fastest.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A thermostat controller runs forever: there are no episodes. Which method can learn its values directly from experience?
- Monte Carlo, because it averages real outcomes
- Neither, because both need a model
- TD learning, because each update needs only one step and the current estimates
- Neither, because values are undefined without episodes
Show the answer
C. TD learning, because each update needs only one step and the current estimates
TD updates after every step toward , so it never needs an episode to end. Monte Carlo's sample is the complete return, which a never-ending task never delivers. With discounting, values are perfectly well defined in continuing tasks.
An agent estimates . It receives reward 1 and lands in with ; the discount is . What is the TD error, and what does it say?
- : things went worse than predicted
- : exactly as predicted
- : things went much better than predicted
- : things went better than predicted
Show the answer
D. : things went better than predicted
. The reward plus the discounted new estimate exceeds the old estimate, so should rise: with it moves from 4 to 4.1.
On the cliff, both agents explore with . Why does SARSA learn the route away from the edge while Q-learning learns the edge?
- SARSA's target uses the action it actually takes next, random steps included, so it values cells by what really happens while exploring; Q-learning's target assumes the best next action
- SARSA uses a smaller step size, so it has not yet found the shorter route
- Q-learning is not allowed to see the penalty
- SARSA uses a discount factor and Q-learning does not
Show the answer
A. SARSA's target uses the action it actually takes next, random steps included, so it values cells by what really happens while exploring; Q-learning's target assumes the best next action
SARSA is on-policy: next to the cliff, its target includes the occasional random step off the edge, so those cells look bad and the safer route wins. Q-learning is off-policy: its target takes the max, so it learns the greedy route's values even though its own exploring sometimes throws it off. Each answers a different question, and if exploration fades away slowly, SARSA's route also becomes the shortest.
Ten actions all have a true average reward of . Each is estimated from a handful of noisy rewards. What is most likely true of the largest of the ten estimates?
- It is exactly , because the estimates are unbiased
- It is above , because the maximum picks out the luckiest estimate
- It is below , because noise makes estimates pessimistic
- It cannot be predicted at all
Show the answer
B. It is above , because the maximum picks out the luckiest estimate
Each single estimate is unbiased, but the maximum of several unbiased, noisy estimates is biased upward: whichever came out highest probably got lucky. Q-learning's inherits this maximization bias; double Q-learning removes it with a second, independent estimate.
What does the -step return become when is larger than the number of steps left in the episode?
- The TD(0) target
- Zero, because it bootstraps from the terminal state
- The Q-learning target
- The full Monte Carlo return
Show the answer
D. The full Monte Carlo return
The sum then contains every reward up to the end, and the bootstrap term is the value of the terminal state, which is 0. What remains is the complete return , the Monte Carlo target. gives the TD(0) target, so is a dial between the two.
Go deeper
- Monte Carlo Methods · Averaging complete returns: prediction, control and exploring starts.
- Temporal-Difference Learning · Learning a guess from a guess: TD(0) and the TD error.
- SARSA & Q-Learning · On-policy and off-policy TD control, and the cliff that separates them.
- n-Step Returns & Eligibility Traces · Interpolating between TD and Monte Carlo with n-step and λ-returns.
Further reading
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 5–7 and 12 (MIT Press, 2nd ed., 2018).
- W. Schultz, P. Dayan & P. R. Montague, "A neural substrate of prediction and reward," Science 275, 1593–1599 (1997).