How to use this pageClick any colored symbol, in an equation or in a sentence, and it explains itself. Press the buttons, drag the sliders and click the charts. Some answers are hidden until you guess.
θ, x, μ, σ true values and data θ̂, μ̂, σ̂, pmodel the model and estimates log, arg max, likelihood likelihood and its operations KL, NLL, MSE divergence and error m, λ sample size and constants
Section 1
The maximum likelihood principle
Section 5.4 defined estimators by guessing a formula and then checking its bias and variance. A learning system needs the reverse: a recipe that produces a good estimator for any model we write down. The most common recipe is the maximum likelihood principle.
The idea is the one a detective uses. Among the explanations the model family allows, prefer the one under which the evidence we saw would have been least surprising.
Predict first. A coin lands heads 9 times in 10 flips. Which heads probability makes these flips most probable?
We have examples drawn independently from the true but unknown distribution . We pick a family indexed by the parameter . The maximum likelihood estimator is the parameter that makes the whole dataset most probable:
Click any coloured symbol to see what it means.
The symbol returns the value of , not the value of the function. The expression , viewed as a function of , is the likelihood. Equation 5.57 makes it a product because the examples are independent.
Choose the Gaussian that explains the data best
model densitydensity at each data pointdata point (drag to move)
Challenge +8 XPUsing the sliders by hand, bring the log-likelihood to within 0.1 of its maximum. The button does not count. The readout tells you how far you are.
The model is a Gaussian with mean and standard deviation . Each stem shows how probable the model finds one data point, and the likelihood is the product of the stem heights. Slide the mean away from the data and the stems shrink, so the likelihood collapses. Make the standard deviation very small and the stems far from the mean vanish. The lower chart shows the log-likelihood against the mean alone: one smooth peak.
Predict first. For the Gaussian family, the best mean is the sample mean. Which quantity is the best variance?
For a Gaussian family the maximum has a closed form, visible in the chart: the best mean is the sample mean, and the best variance is the average squared distance from it. That second quantity is the biased sample variance of Equation 5.36, so maximum likelihood can return an estimator that is biased in small samples. Section 5 of this module explains why it is usually accepted.
Section 2
Logarithms and the empirical expectation
The product in Equation 5.57 is awkward. Each factor is a probability below 1, so the product shrinks fast and a computer rounds it to zero: this is numerical underflow. The logarithm is strictly increasing, so the value of that maximizes a quantity also maximizes its logarithm, and it turns the product into a sum:
Click any coloured symbol to see what it means.
The demo shows both effects. The upper chart rescales the likelihood and the log-likelihood to one range. The lower chart multiplies densities in double precision next to the sum of their logarithms.
Why logarithms
likelihood, rescaledlog-likelihood, rescaledsample mean
naive running productsum of logarithmssmallest positive number a double can hold
Challenge +8 XPOn the lower chart, raise the number of examples until the naive product underflows to exactly 0. The sum of logarithms keeps going.
With enough examples the naive product reaches the smallest number the computer can represent and becomes exactly 0. From then on every parameter value looks equally terrible, and an optimizer cannot tell them apart. The sum of logarithms keeps going, because it grows only linearly with . That is the practical reason for Equation 5.58. Sums are also easier to differentiate than products, and learning algorithms differentiate.
Predict first. The naive product of 2000 densities, each near 0.3, is computed in double precision. What happens?
The empirical expectation
Dividing the sum in Equation 5.58 by does not change the arg max, and it turns the sum into an average. That average is an expectation with respect to the empirical distribution , which puts probability 1/ on each training example:
Click any coloured symbol to see what it means.
This is a change of notation. It lets us compare two distributions, the one defined by the data and the one defined by the model.
Two examples you can compute by hand
The closed forms below are added here and not printed in the book.
Going deeper: the two closed forms (added here, not printed in the book)
Take coin flips with heads and the Bernoulli family of Section 5.4. The condition that the derivative of the log-likelihood is zero gives
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
The estimate of the heads probability is the fraction of heads, the sample mean of Equation 5.22, which Section 5.4 showed to be unbiased. For the Gaussian family the same calculation gives
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
The mean is the sample mean. The variance divides by : it is the sample variance of Equation 5.36, which Section 5.4 showed to be biased by −/. Two of the book's earlier estimators therefore come from this one principle.
Likelihood of a coin
likelihoodlog-likelihoodmaximum at k/m
Challenge +8 XPSet the number of flips to at least 40 and the fraction of heads to 0.3. The likelihood then has a narrow peak at the observed fraction.
Predict first. You want a narrower likelihood peak for the coin. Which change does that?
The peak always sits at the observed fraction of heads. With few flips the likelihood is wide: many coins explain the data about equally well. With many flips it narrows, the same fact as the shrinking standard error of Section 5.4.3. If every flip is a head, the maximum sits at : maximum likelihood would claim that a coin cannot land tails after seeing three heads. Section 5.6 returns to this weakness.
Section 3
Maximum likelihood as minimizing KL divergence
One way to see what maximum likelihood does is to ask how far the model is from the empirical distribution . The book measures the gap with the Kullback–Leibler (KL) divergence, which compares two distributions by the average log-ratio of their probabilities:
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
The divergence is never negative, and it is 0 exactly when the two distributions are equal. Its first term involves only the data, so training can ignore it. What remains to minimize over is
Click any coloured symbol to see what it means.
which is exactly the maximization of Equation 5.59 with a minus sign.
Predict first. Suppose the model equals the empirical distribution exactly. What is the KL divergence between them?
The book states the equivalence in words. The split of the divergence into a data term and a cross-entropy term is added to the book’s text, and it is in the deeper block below. The demo uses a six-sided die: the data are simulated rolls, and you set six weights that are normalized into probabilities.
Shape the model to match the data
empirical distributionmodel q (weights normalized)
Challenge +8 XPReshape the model with the sliders until the KL divergence is below 0.02. The button does not count. The readout gives the value.
Start with the uniform model and reshape it by hand. Each change that moves the bars towards the data lowers the cross-entropy and the KL divergence together. The KL divergence reaches 0 only when the model copies the empirical distribution. The button builds that model, which is a rough sketch with 6 rolls and close to the truth with 1000.
Going deeper: the split into entropy and cross-entropy (added here, not printed in the book)
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
The split of the KL divergence into a data term and a cross-entropy term is an algebra step, shown here. The entropy of the data is written with a minus sign, so the first term is the negative entropy and does not depend on the parameters. The same two-part split appears as Equation 5.60 expanded and as the display above.
The book makes the same point: the ideal would be to match the true , which we cannot access directly.
Predict first. Can the average negative log-likelihood of a continuous model be below zero?
Naming and a warning about sign
Many authors use cross-entropy only for a Bernoulli or softmax negative log-likelihood. The book calls that a misnomer: any negative log-likelihood is a cross-entropy. Minimizing the negative log-likelihood (NLL) is the software name for maximum likelihood. Unlike the KL divergence, the NLL has no floor of zero.
The next demo shows why. A probability density can exceed 1, because it only needs to integrate to 1 over its range, and the log of a density above 1 is positive.
A negative negative log-likelihood
model densitydensity = 1average NLL
Challenge +8 XPLower the model standard deviation until the average NLL is below zero. The readout says when this happens.
Squeeze the data into a narrow cluster and the best Gaussian becomes tall and narrow. Its peak is far above 1, the average NLL dips below 0, and nothing is wrong. Negative values are legitimate for continuous data, whereas the KL divergence stays at or above zero.
Section 4, book 5.5.1
Conditional log-likelihood and mean squared error
Most of supervised learning predicts a target from an input , so the object to estimate is . The conditional maximum likelihood estimator is
Click any coloured symbol to see what it means.
For i.i.d. examples the probability factorizes, and after taking logs:
Click any coloured symbol to see what it means.
Linear regression as maximum likelihood
Section 5.1.4 chose the weights by minimizing the mean squared error, which the book calls arbitrary. Let the model output a Gaussian for with the prediction as its mean:
Click any coloured symbol to see what it means.
The variance is fixed in advance. The log of one Gaussian density is a constant plus a squared error:
Click any coloured symbol to see what it means.
Predict first. A model with Gaussian noise of fixed standard deviation is fitted by maximum likelihood. Which loss does it minimize?
The sum over the independent examples gives the conditional log-likelihood of Equation 5.64, which the book writes as
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
Going deeper: from one density to the sum (added here, not printed in the book)
Each factor in the sum of Equation 5.64 is the log of one Gaussian density. The expansion gives a constant, minus the log of the standard deviation, minus half the squared standardized error. The sum over the examples gives the first line of Equation 5.65. This derivation is added here and is not printed in the book.
Compare it with the training mean squared error:
Click any coloured symbol to see what it means.
The weights appear only in the last term of Equation 5.65, which is times the MSE. The first two terms do not depend on . So maximizing the log-likelihood over the weights is the same as minimizing the training MSE, for any . Drag the points and move the line to see both curves.
Same optimum, different numbers
your lineleast-squares lineLaplace-noise fitMSE against the slopeNLL against the slope
Challenge +8 XPAdd an outlier, tick Laplace-noise fit, and make its slope differ from the least-squares slope by at least 0.1.
Predict first. You change the assumed noise level . Where does the lowest NLL move along the slope?
Drag the slope slider. The two lower curves have different heights, but both have their lowest point at the same slope, marked by the dashed line. Change and the NLL curve changes in height and sharpness while its lowest point stays still. The least-squares line is therefore the maximum likelihood line under Gaussian noise. The button labeled Best sets to its own maximum likelihood value.
Going deeper: the best noise level and the cross-entropy (added here, not printed in the book)
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
Click any coloured symbol to see what it means.
The same algebra shows that the MSE is, up to a constant and a factor, the cross-entropy between the empirical distribution and a Gaussian model:
Click any coloured symbol to see what it means.
So minimizing the cross-entropy and minimizing the MSE are the same job.
The likelihood chooses the loss
The justification works in both directions. MSE is the right loss if the noise is Gaussian. A different noise model gives a different loss.
Predict first. One data point lies far from the line. Under which noise model does this single outlier pull the fitted line less?
The check box Laplace-noise fit shows the best line under a Laplace (double-exponential) noise model, whose negative log-likelihood is proportional to the sum of absolute errors. This extra is not part of the book’s text. Add the outlier and compare the two lines: squared error is pulled hard towards the outlier, while absolute error barely moves.
Section 5, book 5.5.2
Properties of maximum likelihood
The book names two asymptotic properties: consistency and efficiency.
Consistency and its two conditions
Under suitable conditions the maximum likelihood estimator is consistent, as defined in Section 5.4.5: with more examples the estimate converges to the true parameter. The conditions are:
- The truth lies inside the model family. The true distribution must belong to the family, or no estimator can recover it.
- The truth corresponds to exactly one parameter value. Otherwise maximum likelihood can recover the right distribution but cannot tell which parameter produced it.
Predict first. The truth is a mixture of two Gaussians, and the fitted family is a single Gaussian. As grows, does the KL divergence go to 0?
The first demo breaks the first condition. The model is always a Gaussian. The data are either Gaussian, so the truth is inside the family, or an even mix of two well separated Gaussians, so the truth is outside it. After fitting by maximum likelihood, the demo measures the KL divergence from the true distribution to the fitted one, averaged over 40 simulated experiments for each dataset size.
When the truth is inside and outside the family
true densityfitted Gaussiandata (histogram)
truth inside the familytruth outside the familylimit for the outside case
Challenge +8 XPSwitch to the truth outside the family, and reach a size of at least 500 at which the fitted KL divergence is within 5 percent of the limit.
With the truth inside the family, the divergence falls roughly in proportion to 1/m towards 0. With the truth outside, it stops falling and settles at a positive level. Every model in practice is somewhat wrong, so this second case is the normal one.
Going deeper: where the divergence settles (added here, not printed in the book)
Maximum likelihood still converges, but to the member of the family that is closest to the truth in KL divergence. For this mixture that member is the Gaussian with mean 0 and variance 2.74, a value computed here and not printed in the book. The limit line in the chart is that divergence.
The second demo breaks the other condition. The model is a Gaussian with unit variance whose mean is the sum of two parameters, . Only the sum affects the data, so many pairs describe the same distribution.
Predict first. Only the sum of the two parameters affects the data. Which parameter pairs fit equally well?
A ridge of equally good parameters
likelihood (darker is higher)ridge: sum equals the sample meangradient ascent paths
More data make the ridge thinner, so the sum is pinned down more accurately. Six optimizers all reach the maximum, at different parameters. Maximum likelihood recovers the distribution, but the parameters are not identifiable.
Statistical efficiency and the Cramér–Rao bound
Other inductive principles besides maximum likelihood are also consistent, and consistent estimators can still differ in statistical efficiency: one may reach a lower generalization error with a fixed number of samples , or may need fewer examples for a fixed error.
Predict first. On Gaussian data, which estimator of the center sits closer to the Cramér–Rao bound?
Efficiency is measured by the expected squared error between estimated and true parameter, over training samples. For large the Cramér–Rao lower bound (Rao 1945, Cramér 1946) says that no consistent estimator has a lower mean squared error than maximum likelihood. The final demo estimates the center of a symmetric distribution with the sample mean and the sample median, both consistent and unbiased. The vertical axis is the MSE divided by the bound, so an efficient estimator sits at 1.
Efficiency: whose MSE is closer to the bound?
sample meansample medianCramér–Rao bound = 1
On Gaussian data the mean sits on the bound, and the median needs about 57% more data for the same error. The ratio tends to π/2, about 1.57. On Laplace data the roles reverse.
Going deeper: the Laplace comparison (added here, not printed in the book)
The comparison of the two estimators, including which one is the maximum likelihood estimator for each family, is added here and is not printed in the book. The mean of Gaussian data is the maximum likelihood estimator, and the median of Laplace data is the maximum likelihood estimator. Each curve here comes from 3000 simulated datasets, so wiggles of a few percent are simulation noise. The book states the bound for large only, which is why its statement carries the words “for large”.
When data are scarce: regularization
For these reasons, consistency and efficiency, maximum likelihood is often the preferred estimator in machine learning. When examples are few and overfitting appears, the book says that regularization strategies such as weight decay may be used to obtain a biased version of maximum likelihood with less variance.
Predict first. With only 8 noisy examples, can a moderate weight decay ever give a lower total error than plain maximum likelihood?
The last demo estimates six regression weights from few noisy examples. For each penalty strength it repeats the experiment 300 times and splits the squared error of the weight estimate into bias and variance. A penalty of 0 is plain maximum likelihood.
Weight decay trades variance for bias
bias² (dotted)variance (dashed)total squared errorchosen λ
Going deeper: bias, variance and the Bayesian reading (added here)
At , the maximum likelihood solution, the bias is 0 and the variance is large when the examples are few. A moderate penalty adds a little bias and removes much of the variance, so the total error falls. A strong penalty drives the estimate towards zero, and the bias dominates. As grows or the noise falls, the best penalty shrinks towards 0. Section 5.6 shows that weight decay is also what a Bayesian obtains from a Gaussian prior. The demo is a toy with six features and a fixed true weight vector, drawn from seeded random numbers.
- The maximum likelihood estimator maximizes the probability of the data (Eqs. 5.56 and 5.57), and logs avoid underflow (Eq. 5.58).
- It is the same as minimizing the KL divergence, the cross-entropy or the NLL (Eqs. 5.60 and 5.61). A Gaussian with fixed variance turns the conditional version into the MSE (Eqs. 5.65 and 5.66).
- It is consistent and efficient for large , provided the truth lies in the family.
- With few examples, weight decay gives a biased estimate with less variance.
Final quizCheck yourself
Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.
Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 5.5 (pages 131–135), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.