Temporal Difference vs Monte Carlo
Two ways to learn how good a state is from experience alone. Monte Carlo waits for the episode to end and averages what actually happened. Temporal difference learning updates after every step, using its own current guess about the next state. This page explains both, puts their equations side by side, and lets you run them on the same episodes.
01The shared goal
Both methods solve the prediction problem. You follow a fixed policy π, and you want the state-value function: the expected return from each state.
Dynamic programming could compute vπ exactly, but it needs a model of the environment: every transition probability and expected reward. Monte Carlo and TD are model-free. They see only sampled experience, a stream of states and rewards, and they nudge a table of estimates V(s) toward a target with step size α. The whole difference between them is which target they use.
02Monte Carlo: wait for the real return
Monte Carlo plays an episode to the end, then walks back through it and computes the actual return Gt that followed each visit. That return is the target.
Because 𝔼[Gt | St=s] is exactly vπ(s), the target is an unbiased sample of the true value. The cost is that Gt adds up every random reward, transition and action from time t to the end, so it can vary a lot from one episode to the next. And nothing can be learned until the episode is over.
03TD(0): learn from a guess
TD(0) takes one step, sees one reward Rt+1 and the next state St+1, and immediately updates. It replaces the unknown rest of the return with its own current estimate of the next state's value. Using an estimate to update an estimate is called bootstrapping.
The bracketed quantity δt = Rt+1 + γV(St+1) − V(St) is the TD error: how surprised the agent is by one step of experience. The target depends on only one random transition, so it is low variance. But V(St+1) is a guess, not the truth, so the target is biased until the estimates become accurate. (By convention V(terminal) = 0.)
04Side by side
| Monte Carlo | TD(0) | |
|---|---|---|
| Target | Actual return Gt | Rt+1 + γV(St+1) |
| Bootstraps? | No: uses only real rewards | Yes: uses its own estimate of the next state |
| When it updates | After the episode ends | After every step (online) |
| Bias of target | Unbiased | Biased while V is wrong |
| Variance of target | High: sums many random steps | Low: one random step |
| Continuing tasks | Not directly: needs episodes that end | Works naturally |
| Relies on Markov states | No | Yes, to exploit structure fully |
| Sensitivity to the start values | Low | Higher: early guesses feed later targets |
05Worked example: the five-state random walk
This is the classic test bed from Sutton & Barto (Example 6.2). There are five non-terminal states A–E in a row. Every episode starts in the centre, C. At each step the agent moves left or right with equal probability. Stepping off the left end ends the episode with reward 0; stepping off the right end ends it with reward +1. All other rewards are 0, and γ = 1.
The value of a state is then simply the probability of finishing on the right, so the true values are 1/6, 2/6, 3/6, 4/6, 5/6 for A to E. Both learners start every estimate at 0.5. They see exactly the same episodes, so any difference comes from the update rule.
Step through episodes
Estimated value by state
RMS error vs episodes (this run)
The latest update, number by number
This episode, step by step
Each row is one transition (St, Rt+1, St+1). TD fills its columns the moment the step happens. The Monte Carlo columns stay empty until the episode ends, because Gt isn't known before then. γ = 1 throughout.
What to watch for
- Click Take one step a few times. The TD estimate changes on the step the agent reaches a terminal, and later also mid-episode, once a neighbouring estimate has moved away from 0.5. The MC numbers never change until the episode ends.
- On the very first episode TD only updates the last state before termination: every other TD error is 0 + 0.5 − 0.5 = 0. Information then spreads one state per visit. MC updates every visited state at once, because it knows the final outcome.
- Run +100 episodes with the default step sizes. TD usually ends closer to the true line. Raise MC's α to 0.2 and its estimates start jumping around: each update chases one noisy return.
- Raise TD's α to 0.4. It learns fast early, then jitters around the truth instead of settling. A constant step size never fully averages out the noise.
The MC learner here is every-visit: when a state appears several times in one episode, each visit is an update toward the return that followed it. In this task γ = 1 and only the final reward is non-zero, so every return in an episode equals that final reward.
06Bias, variance, and what the averages show
One run is noisy. To compare the methods properly, repeat the experiment many times and average the RMS error over runs. The chart below does that for 100 independent runs of 100 episodes. All methods see the same episodes in each run. The shaded band shows where the middle 80% of individual runs fell.
Average over 100 runs
Average RMS error vs episodes
With moderate step sizes, TD reaches a low error in fewer episodes than MC on this task. That is the bias/variance trade-off in action:
- Monte Carlo pays for its unbiased target with variance. To average noisy returns it needs a small α, which makes it slow. With a large α it learns fast but its band stays wide: each estimate follows the last few returns.
- TD starts with bias, because the next-state values it bootstraps from are wrong. But every update also improves the values other states bootstrap from, and its targets are much less noisy, so the bias shrinks quickly.
- Neither method converges exactly with a constant α. Both keep fluctuating near the truth, and a larger α means a larger floor. With a step size that decays appropriately (for example 1/N(s)), both converge to vπ in the tabular case.
Batch view. If you replay a fixed set of episodes until convergence, the two methods find different answers. Batch MC finds the values that best fit the observed returns. Batch TD(0) finds the values of the maximum-likelihood Markov model of the data, the certainty-equivalence estimate. When the states really are Markov, the TD answer usually generalises better to future data.
07When learning happens: online, episodic, continuing
Monte Carlo is episodic. The return Gt is not known until the episode ends, so every update waits for the end. Long episodes mean long delays, and an agent that does badly early in an episode cannot use that lesson until the episode is over.
TD is online and incremental. Each update needs only the tuple (St, Rt+1, St+1), so learning happens during the episode, memory per step is constant, and nothing needs to be stored until the end.
Non-terminating (continuing) tasks are where the difference becomes decisive. A server that runs forever, or a thermostat that never stops, has no episode end, so Gt is an infinite sum that is never observed. Plain Monte Carlo has no target to use, short of artificially cutting the stream into pieces. TD(0) works unchanged, using discounting (γ < 1) or the average-reward formulation, because it only ever looks one step ahead.
08The bridge: n-step TD and TD(λ)
MC and TD(0) are two ends of one spectrum. An n-step return uses n real rewards, then bootstraps from the estimate n steps later:
With n = 1 this is TD(0). When n is at least the remaining length of the episode, the bootstrap term disappears and it becomes Monte Carlo. In between, larger n means less bias and more variance, and updates are delayed by n steps. Try the n slider above. On this tiny five-state walk TD(0) is hard to beat at its best α, but set all three step sizes to 0.02 and n = 3 beats both TD(0) and MC. On longer chains (Sutton & Barto use 19 states) intermediate n wins more clearly. Either way the best α shrinks as n grows, because longer returns are noisier.
TD(λ) averages all n-step returns at once, weighting the n-step return by (1−λ)λn−1:
λ = 0 gives TD(0) and λ = 1 gives Monte Carlo. Implemented with eligibility traces, TD(λ) still updates online after every step: each TD error is sent back to recently visited states in proportion to how recently and how often they were visited. In practice λ is a single dial for the bias/variance trade-off, and values between about 0.4 and 0.9 often work best.
09Which should you choose?
Lean toward Monte Carlo when
- Episodes are short and always end.
- The state is not fully Markov (partial observability, aliased states). MC does not assume one state's value explains the next.
- You use function approximation off-policy and need stability: bootstrapping is one leg of the “deadly triad”.
- You want unbiased estimates and can afford many samples.
Lean toward TD when
- The task is continuing, or episodes are long.
- You need to learn during the episode, online.
- Returns are very noisy and samples are expensive.
- The state is (close to) Markov, so bootstrapping uses the structure well.
In practice most modern methods sit in between. Q-learning, SARSA and DQN are TD methods; A3C, PPO and GAE-based actor-critics use n-step or λ-returns; AlphaZero-style value targets use full-game outcomes, which is Monte Carlo. If you are unsure, start with an n-step or λ-return and tune n or λ the way you tune α.