No model, only experience. Monte Carlo against temporal-difference learning, Q-learning against SARSA on the cliff, and eligibility traces that pass credit down a trail.
Dynamic programming needed the rules of the world: every transition probability, every reward. Real agents don’t get the rules — they act and see what happens. Temporal-difference learning updates a guess from a slightly better guess one step later: it learns a prediction from a prediction.
Step the token along the chain and watch the two learners react: blue TD arrows after every step, pink Monte Carlo arrows only when the episode ends. Then drag the step sizes and compare the error curves.
Both learners see exactly the same episodes and start every estimate at 0.5; dashed ticks mark the true values 1/6 … 5/6 (the chance of leaving on the right). Blue arrows are TD updates, made as the token leaves a state and aimed at the next state’s estimate; pink arrows are Monte Carlo updates, made only when the episode ends and aimed at the outcome. Orange dots count this episode’s visits. Large α learns fast but stays noisy; small α is smoother but slow.
Both learners estimate Vπ(s), the expected return from s, from experience alone. Constant-α Monte Carlo waits until the episode is over and moves each visited state toward the return that actually followed it: V(Sₜ) ← V(Sₜ) + α[Gₜ − V(Sₜ)]. TD(0) updates as soon as it has taken one step, using its own estimate of the next state in place of the rest of the return: V(Sₜ) ← V(Sₜ) + α[Rₜ₊₁ + γV(Sₜ₊₁) − V(Sₜ)]. The bracket δₜ = Rₜ₊₁ + γV(Sₜ₊₁) − V(Sₜ) is the TD error, the surprise between what was predicted and what one step revealed. Using an estimate to update an estimate is called bootstrapping.
The trade-off is bias against variance. The Monte Carlo target Gₜ is an unbiased sample of Vπ(Sₜ) but carries the noise of every later coin flip; the TD target depends on one step of luck but leans on V(Sₜ₊₁), which may be wrong. With suitably decreasing step sizes both converge to Vπ — that is a theorem. Which one gets there faster is not: no one has proved that TD always wins, but on tasks like this walk it usually does, and with Sutton and Barto’s step sizes TD ends lower after 100 episodes (an empirical result, which the chart reproduces). TD also learns online, mid-episode, and works on tasks that never end.
The walk is Example 6.2 of Richard Sutton and Andrew Barto’s Reinforcement Learning: An Introduction: five states, a fair coin, +1 for leaving on the right, γ = 1, so the true values are 1/6, 2/6, …, 5/6. The method itself was set out by Sutton in 1988 in “Learning to predict by the methods of temporal differences”.
TD learning turned up in the brain. In 1997 Wolfram Schultz, Peter Dayan and Read Montague reported in Science that dopamine neurons in monkeys fire in a pattern resembling a TD error: a burst for an unexpected reward, nothing for a reward that was predicted, and a dip when a predicted reward fails to arrive. Once a cue reliably predicts the reward, the burst moves to the cue — a reward prediction error.
Press Play and watch Q-learning settle on the path along the edge while SARSA keeps a safe distance; the chart shows who actually earns more while exploring. Then try ε = 0 and Reset, or turn on ε decay.
Each step costs −1; stepping into the rose cliff costs −100 and sends the agent back to the start. Cells are coloured by the learned maxa Q(s, a) (deeper rose = worse); the thick line is each learner’s greedy path, dashed while it does not yet reach the goal; red rings mark falls. Changes to ε and α apply at once; press Reset to start over with them. With ε = 0 both learners turn into the same greedy learner and both find the edge path.
To act, an agent needs action values. Q-learning updates Q(s, a) ← Q(s, a) + α[r + γ maxₐ′ Q(s′, a′) − Q(s, a)]: whatever the agent actually does next, the target assumes it will act greedily from s′. It is off-policy — it learns the value of the greedy policy while behaving ε-greedily — so it converges to Q* and its greedy path hugs the cliff, the optimal 13-step route. Chris Watkins introduced Q-learning in his 1989 PhD thesis, and Watkins and Peter Dayan proved in 1992 that it converges to Q* with probability 1, provided every state–action pair keeps being tried and the step sizes shrink suitably.
SARSA updates toward the action it will really take next: Q(s, a) ← Q(s, a) + α[r + γQ(s′, a′) − Q(s, a)], with a′ drawn from the same ε-greedy policy. It is on-policy: it learns the value of the policy it is actually following, exploration included. Next to the cliff, one step in ten is random and a random step can be a fall worth −100, and SARSA’s values price that in, so it walks a row or two higher. Gavin Rummery and Mahesan Niranjan described the method in 1994; the name, suggested by Sutton, spells out the quintuple it learns from: (S, A, R, S′, A′).
Neither answer is wrong; they answer different questions. Q-learning knows the best path for an agent that never explores, which is why its online reward is worse: its exploratory steps along the edge keep falling off. SARSA finds the best path for an agent that does explore. If ε is set to 0 the two updates coincide and both find the edge; with exploration decaying in the right way, SARSA also converges to the optimal policy in the limit, though in practice its learned caution can linger for a long time, as the decay toggle shows.
Expected SARSA replaces the sampled Q(s′, a′) with its average under the policy, Σₐ′ π(a′ | s′) Q(s′, a′). That removes the noise of choosing a′ and usually lets it use larger step sizes; on the cliff it settles between the other two. With a greedy target policy it becomes Q-learning.
Press Play and watch the orange trace trail the agent until it finds the goal and the whole trail lights up at once. Then set λ to 0 and see credit crawl back one cell per episode.
SARSA(λ) with α = 0.2, γ = 0.95, ε = 0.1; the only reward is +1 at the goal. Cells are coloured by maxa Q(s, a), arrows show the greedy action where it has learned something, orange glow is the trace and emerald outlines show where the last goal’s update landed. The chart averages 10 background runs per λ on this maze (geometric mean of episode lengths, capped at 5,000 steps); triangles mark the median episode at which the greedy path first reached the goal. Changing λ or the trace type restarts the agent.
One-step TD and Monte Carlo are two ends of a spectrum. The n-step return looks n rewards ahead and then bootstraps: G⁽ⁿ⁾ₜ = Rₜ₊₁ + γRₜ₊₂ + … + γⁿ⁻¹Rₜ₊ₙ + γⁿV(Sₜ₊ₙ). The λ-return mixes all of them with geometrically decaying weights, Gₜ(λ) = (1 − λ) Σₙ λⁿ⁻¹ G⁽ⁿ⁾ₜ. With λ = 0 it is the one-step TD target; as λ → 1 it becomes the full Monte Carlo return.
Waiting for the λ-return would mean looking into the future. Eligibility traces get the same effect looking backward: each state keeps a fading memory of how recently it was visited, e(s) ← γλe(s), plus 1 for the state just visited, and every TD error updates all states in proportion to their traces, V(s) ← V(s) + αδe(s). For offline updating this backward view is exactly equivalent to the forward one. SARSA(λ) does the same for state–action pairs. In a maze where the only reward is the goal, one-step SARSA moves credit back by one cell per visit, so it needs roughly as many successful episodes as the path is long; with λ = 0.9 the first success already marks the whole trail.
Accumulating traces add 1 on each visit, so a state revisited in a loop can build up a trace above 1 and be over-credited; replacing traces reset it to 1. Satinder Singh and Sutton studied replacing traces in 1996. On this maze the difference is stark at λ = 0.9: the random wandering of the first episodes is full of loops, and accumulating traces learn noticeably slower — try both in the comparison.
Credit assignment over time is an old problem. Arthur Samuel’s checkers player (1959) already adjusted its evaluation of a position toward the evaluation of positions later in the game, a forerunner of TD learning. Gerald Tesauro’s TD-Gammon, in the early 1990s, trained a neural network by TD(λ) through self-play and reached near world-champion backgammon. The demos here are tabular; see also Function Approximation and Deep Q-Learning for what happens when the table becomes a network.
Spot a mistake or have an idea?