The Mathematics of Machine Intelligence

Chapter 14

Learning from Consequences

Revise a forecast by the gap between what you expected and what you got.

7 guided illustrations. Move a slider or choose a value, watch the mathematics change, and check your prediction. Each illustration includes its full calculation.

01 / 07

How much should a surprise change a forecast?

From Chapter 14, Dopamine cells burst at surprise juice and dipped when juice failed

A monkey that has learned a light predicts juice gets juice, or does not. What is the prediction error, and how far does the forecast move?

The forecast before the reward is what the learner expected. The error compares it with what came, plus whatever the learner forecasts afterwards. A positive error says things went better than forecast, a negative one worse, and zero means the forecast was right. The update then moves the forecast a share alpha of the way, so one outcome does not become the whole truth.

Predict first: The light has taught a forecast of 1. Predict the error when juice arrives, when it fails, and when juice arrives with no light, then check each against the controls.

Bars for forecast, reward, error and updated forecast, and lines of prediction error against forecast for reward 0 and 1.
The error is the gap between what arrived and what was forecast; the forecast moves by a share of that gap and stops moving when the gap is zero.
Forecast before the reward: 0.0 · Reward received (drops): 1 · Learning rate (alpha): 0.5

The learner forecast 0 and received 1, so the error is 1, which is positive (burst-like). With learning rate 0.5 the forecast moves by 0.5 to 0.5, not all the way to the outcome.

Prediction error
1
Sign
positive (burst-like)
Forecast after the update
0.5
Forecast moves by
0.5
Why it matters

A learner that corrects itself by the size and sign of its surprise needs no teacher to say what the right forecast was. When nothing is surprising, nothing changes.

In brains and behavior

The 1997 review set recordings of monkey dopamine cells beside this error: a burst at surprise juice, none at expected juice, a dip when expected juice failed. The match is in sign and timing only, and the book names a mismatch (early juice).

In machines

A learner computes the error explicitly and applies the update. A reward here is a number supplied by the task, not a claim that the machine feels pleasure.

Show the calculation

delta = r + gamma x V(next) - V(now) = 1 + 0 - 0 = 1, because the task ends after the reward and nothing more is forecast. V(new) = V(old) + alpha x delta = 0 + 0.5 x 1 = 0.5.

The equations and symbols

δt=rt+1+γV(St+1)−V(St),Vnew(St)=Vold(St)+αδt \delta_t=r_{t+1}+\gamma V(S_{t+1})-V(S_t),\qquad V_{\mathrm{new}}(S_t)=V_{\mathrm{old}}(S_t)+\alpha\delta_t

delta
prediction error: reward plus discounted next forecast, minus the forecast held before
r
the reward received, in drops of juice
V
a forecast of the rewards to come from a situation
gamma
the discount on later rewards; here nothing follows the juice, so it drops out
alpha
learning rate, between zero and one
Where the conclusion applies

The task ends after the reward, so the next situation is worth zero. The error is a number in a model; the book says it has no real units and cannot be compared with spike counts. A reward is a number the task supplies, not pleasure.

Check your understanding: The forecast is 0.5, the reward is 1, and alpha is 0.5. What are the error and the new forecast?
The error is 1 - 0.5 = 0.5, and the new forecast is 0.5 + 0.5 x 0.5 = 0.75.

Book source: Chapter 14, Dopamine cells burst at surprise juice and dipped when juice failed. Illustration C14-D01. Formula. Book formulas and the chapter's illustrative numbers: forecast 1 with juice gives 0, no juice gives minus 1, juice with no forecast gives plus 1, and the constructed forecast 0.4, outcome 1, rate 0.5 gives 0.7. The recordings behind the dopamine comparison are the 1997 review's Figure 1, which the book redraws as a schematic with no values.

02 / 07

How does credit travel back from the juice to the light?

From Chapter 14, Credit ran back from the juice to the light

If a reward follows the sixth of six cues, how many trials pass before the first cue gains value?

On the first trial only the last cue sees a reward, so only it gains value. On the next trial the fifth cue is followed by a cue that is now worth something, so it gains, and so on back to cue 1. A trace carries each error to the earlier cues in the same trial, so the whole chain learns together.

Predict first: With the plain method (trace 0) and learning rate 1, predict the trial on which cue 1 first gains value, and what a trace through all six changes.

0.01.0
A grid of cue values over eight trials for six cues, and lines of cue 1's value over trials for three trace lengths.
Bootstrapping passes value back one cue per trial; a trace lets one reward credit the whole chain at once.
Trace length (lambda; companion's addition, 0.5 is constructed): 0.0 · Learning rate (alpha): 1.0

With learning rate 1 and trace 0, cue 1 first gains value on trial 6. With no trace, value creeps back about one cue per trial.

Cue 1 first gains value
trial 6
Cue 6 after trial 1
1
Cue 1 after trial 1
0
Cue 1 after 8 trials
1
Why it matters

The earliest event that reliably predicts a reward is the one worth reacting to. A learner finds it by correcting each forecast with the next, without waiting to see the final reward in every case.

In brains and behavior

In monkeys learning a light-and-lever task, fewer cells burst at the juice and more at the light as training went on. That is a population average, and it does not show that one cell was followed through learning.

In machines

The update is a specification: each cue's value is corrected from the next cue's value. The trace is a design choice that speeds the spread of credit.

Show the calculation

Each cue is updated from the cue after it, using the values held at the start of the trial: error = reward + value of the next cue - value of this cue. Cue 6 gets reward 1 and no next cue. Trial 1 gives cue 6 value 1 and cue 1 value 0; trace 0 passes on the later errors scaled by 0 per cue. The trace parameter is the companion's addition; the chapter states only the plain method and a trace through all six cues.

The equations and symbols

δk=rk+1+V(Sk+1)−V(Sk),ΔV(Si)=α∑k≥iλk−iδk \delta_k=r_{k+1}+V(S_{k+1})-V(S_k),\qquad \Delta V(S_i)=\alpha\sum_{k\ge i}\lambda^{\,k-i}\,\delta_k

delta_k
the error at cue k, from values held at the start of the trial
lambda
how far a trace carries later errors back to earlier cues (0 is the plain method, 1 lasts through all six)
alpha
learning rate
V
the value of a cue
Where the conclusion applies

A single fixed chain, no discount, and every cue updated from values held at the start of the trial. In the brain, the corresponding shift of the burst from juice to light is a population average over cells recorded in different sessions; the cited reports do not follow one cell across days.

Check your understanding: With learning rate 1 and no trace, on which trial does cue 3 first gain value?
Cue 6 gains on trial 1, cue 5 on trial 2, and cue 3 on trial 4.

Book source: Chapter 14, Credit ran back from the juice to the light. Illustration C14-D02. Constructed example. The chapter's idealized chain: six cues, the reward after the sixth, no discount, learning rate 1, all values starting at 0. The companion adds the trace parameter lambda as one standard way to write a fading memory of recent cues; the chapter states only the two ends (no trace, and a trace through all six). Constructed example, not data.

03 / 07

Why does a cue added to a predicted outcome teach so little?

From Chapter 14, Blocking: rats learned little from a cue added to a predicted outcome

If a noise already predicts a shock, how much does a light added to it learn in eight pairings?

Each cue present learns a share of the gap between the outcome and the combined strength of all cues present. After enough noise-alone trials the noise already accounts for the outcome, so the gap at the first pairing is small and the light gains almost nothing. With no earlier trials the gap is large, and the two cues share it.

Predict first: Predict the light's strength after 8 pairings if the noise has already reached strength 1, then find how many noise-alone trials get close to that.

016
Strength of noise and light over trials for a chosen number of noise-alone trials, and light strength after 8 pairings against that number.
When the outcome is already predicted, the gap that drives learning is nearly zero, so a new cue is learned poorly.
Noise-alone trials before the light: 8 · Learning-rate constant (c, made up): 0.2

After 8 noise-alone trials the noise holds strength 0.832, leaving a gap of 0.168 for the light to learn from. After 8 pairings the light has strength 0.0825, against 0.492 when light and noise were learned together.

Light strength after 8 pairings
0.0825
Same, light and noise learned together
0.492
Gap left for learning at the first pairing
0.168
Light as a share of the together value
0.168
Why it matters

A learner driven by error learns about a cue only to the extent that the cue is needed to explain a surprise. In this model, a redundant signal would be learned about only a little; the chapter does not claim animals follow the rule.

In brains and behavior

In 2013 rats were given a laser pulse at reward time to switch on dopamine cells during the pairing of sound plus light; they then responded more to the light on the first test trial (across all three test trials the difference was a trend). This supports sufficiency in that task, not necessity, and the cells' firing was not measured.

In machines

The rule is the same error-driven update as in the previous cards, applied to several cues at once. It is a specification for a learner, and a model of behavior for animals.

Show the calculation

Each cue present gains rate x (1 - combined strength) = 0.2 x gap per trial. Noise alone: strength = 1 - (1 - 0.2)^8 = 0.832. First pairing gap = 0.168. Light after 8 pairings = gap x (1 - (1 - 2 x 0.2)^8) / 2 = 0.0825. The rate 0.2 is a made-up constant, not fitted to the rats.

The equations and symbols

ΔVX=c(λout−∑YpresentVY)for each cue X present \Delta V_X=c\,\Big(\lambda_{\mathrm{out}}-\sum_{Y\ \mathrm{present}}V_Y\Big)\quad\text{for each cue }X\text{ present}

V_X
associative strength of cue X (noise or light)
lambda_out
the strength the outcome can support; 1 here
c
a learning-rate constant, a disclosed made-up value in this illustration
sum
the combined strength of all cues present on the trial
Where the conclusion applies

Both cues have the same learning rate and the outcome is the same size on every trial. The rule shows what blocking would look like, which does not show that rats follow it. The link joins two models of learning, not a brain and a machine.

Check your understanding: If the noise has reached strength 1 exactly, what does the light gain on the first pairing?
The gap is 1 minus 1, which is zero, so the light gains nothing, however many pairings follow.

Book source: Chapter 14, Blocking: rats learned little from a cue added to a predicted outcome. Illustration C14-D03. Model. The rule is the Rescorla and Wagner update as the chapter states it (each cue present learns a share of the gap between the outcome and the combined strength of all cues present). The learning rates and trial counts follow the chapter's design (16 noise trials, then 8 pairings, against 8 pairings from the start); the rates are made up. Kamin's reported median suppression ratios, .05 for light and noise learned together and .45 for noise first, are context and are not fitted: a strength is not a suppression ratio.

04 / 07

How does a learner revise the value of an action?

From Chapter 14, Learning which action to take

Moving left is valued at 2, earns 1, and the best next option is valued at 3. What is the new estimate?

The target is what the experience suggests the action is worth: the reward now plus the discounted value of the best next option. The estimate is nudged toward it by a share alpha. A discount of zero ignores the future, and a learning rate of one replaces the estimate with the target.

Predict first: With a discount of 0.9 and a rate of 0.5, predict the new estimate, then predict what happens to it when the next situation ends the task.

Bars for the old estimate, target and new estimate, and lines of the new estimate against learning rate for three discounts.
The estimate moves a share alpha of the way toward a target built from the reward and the best next value.
Learning rate (alpha): 0.5 · Discount (gamma): 0.9 · The next situation ends the task (1 = yes): 0

The target is 3.7 (best next value 3 is counted), so the estimate of 2 moves a share 0.5 of the gap 1.7 to 2.85. A learning rate of 1 would jump all the way to the target.

Target
3.7
Gap, target minus estimate
1.7
New estimate
2.85
Share of the gap closed
0.5
Why it matters

Action values let a learner choose between actions from experience, without a model of the world. The discount sets how much later rewards count.

In machines

This is a specification of the update. A reward is a number the task supplies. A related update, with a neural network estimating the values from game images, was the 2015 Atari learner.

Show the calculation

Target = r + gamma x max Q(next) = 1 + 0.9 x 3 = 3.7. New = 2 + 0.5 x (3.7 - 2) = 2.85.

The equations and symbols

Qnew(s,a)=Qold(s,a)+α[r+γmaxa′Qold(s′,a′)−Qold(s,a)] Q_{\mathrm{new}}(s,a)=Q_{\mathrm{old}}(s,a)+\alpha\left[r+\gamma\max_{a'}Q_{\mathrm{old}}(s',a')-Q_{\mathrm{old}}(s,a)\right]

Q
estimated total future reward from taking an action in a situation
r
the reward just received
gamma
the discount on later rewards
max Q(s', a')
the best estimated value available in the next situation, zero if the task ends
alpha
learning rate, the share of the gap used for the correction
Where the conclusion applies

A table of action values for one situation and action. The target counts the best next action even when the learner explores and tries something else. A deep Q-network learns toward a related target by training a network, not by replacing a table entry.

Check your understanding: With estimate 2, reward 1, best next value 3, discount 0.5 and rate 0.5, what is the new estimate?
The target is 1 + 0.5 x 3 = 2.5, and the new estimate is 2 + 0.5 x (2.5 - 2) = 2.25.

Book source: Chapter 14, Learning which action to take. Illustration C14-D04. Formula. Book formula with the chapter's constructed numbers (estimate 2, reward 1, best next value 3, discount 0.9, rate 0.5, giving a target of 3.7 and a new estimate of 2.85). Not game results.

05 / 07

Why does trusting the best-looking estimate overrate an action?

From Chapter 14, Atari: 75 percent of a human tester's gain on 29 of 49 games, 0.0 percent on Montezuma's Revenge

If every action is equally good but the estimates are noisy, how much does the best-looking one overshoot?

Each estimate errs up or down with equal chance. Picking the largest picks, among other things, the one that erred upward the most. The more options there are, the more likely one of them has a large upward error, so the best-looking estimate rises even though no action is truly better.

Predict first: Estimates are unbiased, so predict whether the best-looking of two equal actions is overestimated, and by roughly what fraction of the noise sd.

116
Dots for one round of noisy estimates of equal actions with the best ringed, and curves of average overestimate against the number of actions.
The maximum of unbiased noisy estimates is biased upward, more so with more options and more noise.
Number of equally good actions: 8 · Noise sd of each estimate: 1.0

With 8 equally good actions and estimate noise sd 1, the best-looking one is overestimated by 1.42 on average, though the estimates themselves are unbiased. The upward bias comes from picking the maximum of noisy numbers, and it grows with the number of options.

Average best-looking estimate
1.42
True value of the action it picks
0
Overestimate in units of the noise sd
1.42
This round's best-looking estimate
1.25
Why it matters

A learner that bases its target on the best-looking next estimate inherits this bias. The score it is given is not the cause; the maximum is.

In machines

Q-learning's target takes a maximum over estimated values, which is why the chapter notes the 2015 network sometimes overestimates action values. This picture is the arithmetic of a maximum, not a test of that network.

Show the calculation

Average of the largest of 8 standard normal draws = 1.42 (numerical integration of x n phi(x) Phi(x)^(n-1)). Times noise sd 1 = 1.42. For 2 actions the exact value is sd / sqrt(pi) = 0.564.

The equations and symbols

𝔼[maxaQ̂(a)]=σ𝔼[maxi≤nZi]≥0,Zi∼𝒩(0,1) \mathbb{E}\left[\max_a \hat Q(a)\right]=\sigma\,\mathbb{E}\left[\max_{i\le n}Z_i\right]\ \ge\ 0,\qquad Z_i\sim\mathcal N(0,1)

Q-hat(a)
a noisy but unbiased estimate of the action's value, whose true value is 0 for every action here
sigma
the standard deviation of the estimate's noise
n
the number of equally good actions
Z_i
independent standard normal draws
Where the conclusion applies

Independent, unbiased, normally distributed noise of the same size for every action, and equal true values. Real estimates are neither independent nor equally noisy.

Check your understanding: For 2 actions with noise sd 2, what is the average overestimate of the best-looking one?
2 / sqrt(pi) = 1.13.

Book source: Chapter 14, Atari: 75 percent of a human tester's gain on 29 of 49 games, 0.0 percent on Montezuma's Revenge. Illustration C14-D05. Simulation. The chapter says that trusting the best-looking estimate can overrate an action, and that van Hasselt and colleagues found the 2015 network sometimes overestimates action values in some games. This card is the companion's own constructed illustration of the statistical reason, with made-up noise and equally good actions. It is not a measurement of any network, and the size of the real overestimate is not shown here.

06 / 07

How good is a game score, counted over random play?

From Chapter 14, Atari: 75 percent of a human tester's gain on 29 of 49 games, 0.0 percent on Montezuma's Revenge

On Frostbite the 2015 network scored 328.3, random play 65.2 and a human tester 4,335. What share of the human gain is that?

The gain over random play is the part of the score that was learned. Dividing the network's gain by the human tester's gain gives the share. Frostbite is one of the weak games; Montezuma's Revenge was 0.0 percent.

Predict first: Predict, before moving the slider, whether 328.3 is above or below a quarter of the human gain on Frostbite, then find the score needed for 75 percent.

65.24335.0
Bars of Frostbite scores for random play, a chosen score and a human tester, and a line of share of the human gain against score.
Counted over random play, the network's Frostbite gain is 6.2 percent of the human gain, though it reached 75 percent on 29 of 49 games.
Frostbite score (65.2, 328.3 and 4,335 are reported; others hypothetical): 328.3

The 2015 network's Frostbite score of 328.3 is a gain of 263.1 over random play, 6.16 percent of the human tester's 4269.8. Reaching 75 percent on this game takes a score of 3268.

Gain over random play
263.1
Human gain over random play
4269.8
Share of the human gain (percent)
6.16
Reaches 75 percent
no
Why it matters

An average over games hides weak ones. Subtracting random play keeps a lucky guesser from looking skilled.

In machines

Each network used the same design and settings but trained from scratch on one game. The score is a stand-in for the goal, not the goal.

Show the calculation

Gain = 328.3 - 65.2 = 263.1. Human gain = 4335 - 65.2 = 4269.8. Share = 263.1 / 4269.8 = 0.06162. Score for 75 percent = 65.2 + 0.75 x 4269.8 = 3267.55.

The equations and symbols

share=score−randomhuman−random \text{share}=\frac{\text{score}-\text{random}}{\text{human}-\text{random}}

score
the learner's game score
random
the score of random play, 65.2 on Frostbite
human
the professional tester's score, 4,335 on Frostbite
Where the conclusion applies

Gains are counted over random play so that only what was learned beyond guessing counts. A different network was trained on each game, so this is 49 trained networks, not one network that learned 49 games. Rewards were clipped to plus 1, minus 1 or zero, which hid differences in size. The study did not show that the score stands for what a person wants.

Check your understanding: A hypothetical Frostbite score of 2,200: what share of the human gain is it?
The gain is 2200 - 65.2 = 2134.8, and 2134.8 / 4269.8 is about 0.5, or 50 percent.

Book source: Chapter 14, Atari: 75 percent of a human tester's gain on 29 of 49 games, 0.0 percent on Montezuma's Revenge. Illustration C14-D06. Reported result. Reported result from Mnih and colleagues (2015), as quoted in the chapter: Frostbite scores of 65.2, 328.3 and 4,335, and that the networks reached at least 75 percent of the tester's gain on 29 of 49 games. The companion's arithmetic gives the gains and shares. The slider's 65.2 (random play), 328.3 (the network) and 4,335 (the human tester) are the paper's scores; the other slider values are hypothetical, to show what each share would take.

07 / 07

Why would many forecasting channels have different reversal points?

From Chapter 14, Mouse dopamine cells flipped at different reward sizes, as a machine idea had predicted

If some channels weigh good news more and others bad news, where does each settle, and which reward sizes flip its sign?

A channel stops changing when the pull from rewards above its forecast balances the pull from rewards below it, each weighted by its optimism. A balanced channel (0.5) settles at the average reward. An optimistic channel needs a larger reward before the news counts as good, so its reversal point is higher.

Predict first: With seven equally likely reward sizes, predict which pair of sizes a channel with optimism 0.1 flips between, and one with optimism 0.9.

0.10.9
A channel's forecast settling at its exact reversal point, and reversal point against optimism for three constructed reward patterns.
Channels that weigh good and bad news differently settle at different forecasts, so, in this model, each flips sign at a different reward size.
Optimism of the channel (tau): 0.5 · Chances of the seven reward sizes (constructed): equal chances

A channel that weighs good news 0.5 and bad news 0.5 settles where rewards above its forecast and below it balance, at 4. Rewards larger than 4 give it a positive error and smaller ones a negative error, so its reversal point sits between sizes 4 and 5.

Reversal point (exact)
4
Falls between reward sizes
4 and 5
Average reward
4
Last 500 trials of the run
3.94
Why it matters

Together the channels trace out the spread of possible rewards, not only the average. That is the prediction machine learning made first, which the 2020 study then tested.

In brains and behavior

In the 2020 reanalysis, 40 cells from a handful of mice had substantially different reversal points, and the pattern held when the trials were split in half at random. The evidence is observational and model-dependent, and the mathematics of a brain is a model.

In machines

A distributional learner keeps many forecasts with different asymmetries. Together they describe a distribution of rewards instead of one average.

Show the calculation

The forecast stops moving when 0.5 x E[(r - v) above zero] = 0.5 x E[(v - r) above zero]. Solving gives v = 4. The seeded run averaged 3.94 over its last 500 trials. At optimism 0.5 the point is the average reward, 4. The reward sizes, their chances and the step 0.02 are constructed here, not the mice's data.

The equations and symbols

v←v+ητ(r−v)(r>v),v←v+η(1−τ)(r−v)(r<v) v\leftarrow v+\eta\,\tau\,(r-v)\ \ (r>v),\qquad v\leftarrow v+\eta\,(1-\tau)\,(r-v)\ \ (r<v)

v
the channel's forecast, which is also its reversal point
tau
the channel's optimism: the weight on good news, between 0 and 1
eta
a small step size (0.02 here, made up)
r
the reward delivered
Where the conclusion applies

Each channel follows its own error with asymmetric weights, and rewards are drawn independently from a fixed pattern. A close agreement with data would show consistency with this account, not that dopamine cells run this rule. The recordings were gathered for another purpose, the mice were few, and there are no human data.

Check your understanding: For equal chances of sizes 1 to 7, what is the reversal point of a channel with optimism 0.5?
The average reward, (1 + 2 + 3 + 4 + 5 + 6 + 7) / 7 = 4.

Book source: Chapter 14, Mouse dopamine cells flipped at different reward sizes, as a machine idea had predicted. Illustration C14-D07. Model. The structure follows the chapter's description of the distributional idea: channels that give more weight to good news drift high and those that give more weight to bad news drift low. The seven reward sizes (1 to 7) and the three chance patterns are constructed, not the mice's water amounts. The reported result is the 2020 reanalysis the chapter describes: 40 dopamine cells recorded earlier by another team from a handful of mice, which the authors say had substantially different reversal points.

Mathematical illustrations use the book's formulas with disclosed small examples. Each interactive choice shows a result computed from the same calculations as the chapter notebook. A simulation is not a measurement, and for a brain the mathematics is a model: a picture illustrates a result, and the assumptions determine its scope. Review copy, brain-computer partnership revision of 1 november 2026.