Reinforcement learning · prediction

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.

Value of a state under policy πvπ(s) = 𝔼π[ Gt | St = s ],    Gt = Rt+1 + γRt+2 + γ2Rt+3 + …

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.

The common shape of the updateV(St) ← V(St) + α [ target − V(St) ]

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.

Constant-α Monte CarloV(St) ← V(St) + α [ Gt − V(St) ]

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.

Tabular TD(0)V(St) ← V(St) + α [ Rt+1 + γV(St+1) − V(St) ]

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 CarloTD(0)
TargetActual return GtRt+1 + γV(St+1)
Bootstraps?No: uses only real rewardsYes: uses its own estimate of the next state
When it updatesAfter the episode endsAfter every step (online)
Bias of targetUnbiasedBiased while V is wrong
Variance of targetHigh: sums many random stepsLow: one random step
Continuing tasksNot directly: needs episodes that endWorks naturally
Relies on Markov statesNoYes, to exploit structure fully
Sensitivity to the start valuesLowHigher: 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

Episode 0 · step 0 · RMS error MC 0.236 TD 0.236
0.04
0.10
6/s

Estimated value by state

True valueMonte CarloTD(0)

RMS error vs episodes (this run)

Monte CarloTD(0)

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

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

0.02
0.10
3
0.05

Average RMS error vs episodes

Monte CarloTD(0)n-step TD

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:

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:

n-step return and updateGt:t+n = Rt+1 + γRt+2 + … + γn−1Rt+n + γnV(St+n),   V(St) ← V(St) + α [ Gt:t+n − V(St) ]

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:

λ-returnGtλ = (1 − λ) Σn≥1 λn−1 Gt:t+n

λ = 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 α.