Maximum Likelihood with Complete Data
The three-step recipe - write the likelihood, differentiate the log, set it to zero - applied to a discrete parameter, to a network of them, and to a Gaussian, together with the small-sample failure it walks straight into.
Everything so far has taken a probability model as given: a network with its tables filled in, a Gaussian with its mean and variance. Someone had to supply those numbers. This lesson is about where they come from when every variable in the model is observed - the case that turns out to be almost embarrassingly easy, and whose ease is worth understanding before the next lesson takes it away.
The likelihood of a data set
Fix a model with parameters . The likelihood of a data set is the probability the model assigns to exactly those observations. If the examples are independent given the parameters, that probability is a product:
The maximum-likelihood estimate is the that makes this largest. Note what has been assumed away: no prior over appears, so every parameter value is treated as equally plausible before the data arrives. That is a deliberate choice, and the next lesson is about what it costs.
Three steps
A candy manufacturer sells bags with an unknown proportion of cherry candies, the rest lime. You unwrap 25 and find 18 cherry. What is ?
Step one, write the likelihood. With cherries and limes,
Step two, take logs and differentiate. The logarithm turns the product into a sum, which is what makes the derivative tractable:
Step three, set it to zero. That gives , so
The maximum-likelihood hypothesis says the proportion of cherries in the bag equals the proportion observed. A grid search over two million values of agrees to six decimal places, and the log likelihood there is , against at and at .
It is fair to feel cheated. Considerable machinery has produced the answer any sensible person would have written down immediately. The point is not the answer; it is that the same three steps keep working when no sensible answer suggests itself.
Why taking the logarithm is safe
Two justifications, and both matter. Mathematically, is strictly increasing, so it does not move the location of a maximum - only its height. Computationally, a product of thousands of probabilities underflows to zero in floating point while a sum of thousands of logarithms does not. Real implementations work in log space for the second reason long after the first has been forgotten.
Many parameters at once
Now suppose the candies also have a wrapper, red or green, chosen with a probability that depends on the flavour. The model has three parameters: for the flavour, , and . Our 25 candies break down as 12 cherry-red, 6 cherry-green, 2 lime-red, 5 lime-green.
The likelihood is a product over four kinds of candy, and its logarithm is
Expand the logarithms of the products and something useful happens: every term mentioning mentions nothing else. The three partial derivatives are therefore independent equations, and each one is the one-parameter problem solved again:
Running Nelder–Mead on the full three-parameter likelihood lands within of those values, at a log likelihood of .
This decoupling is the reason learning a Bayesian network from complete data is cheap. Each conditional probability table is fitted by counting within its own conditioning case, independently of every other table, with no search and no iteration. It is worth naming the condition that makes it true: the data must be complete, every variable observed in every example. Break that and the terms stop separating, which is precisely the difficulty the third lesson confronts.
The separation is easier to believe once it is on screen as three numbers. The figure below shows the log likelihood split into its three terms, one per parameter. Push theta1 to either extreme and the flavour term does not shift by a digit, which is why the theta that maximises the total cannot move either.
The second preset is the failure at the end of this lesson, and it breaks in two ways rather than one. After a single cherry candy the flavour estimate is 1, so lime is impossible. But theta2 is worse off than that: its term is zero for every value, so an unobserved conditioning case leaves a whole row of the table undetermined rather than badly determined. Starting every count at one repairs both, which is where the next lesson begins.
Interactive: one curve, three terms
Move a wrapper parameter and watch the flavour term ignore it.
- Flavour term
- -14.823833
- Wrapper term, cherry
- -11.457707
- Wrapper term, lime
- -4.188200
- Total log likelihood
- -30.469740
The three sliders move three terms, and the terms add. That is the whole of the decoupling: expanding the logarithms turns the likelihood into [18 log t + 7 log(1 - t)] plus a term in t1 alone plus a term in t2 alone, so the value of t that maximises the total cannot depend on the other two. Move t1 to either extreme and the flavour term does not shift by a digit. That is why fitting a whole network from complete data is counting rather than searching, and it is exactly what the next lesson takes away. Right now the counted estimate is 0.72 and the flavour term is -14.823833 at your 0.72.
The continuous case
The recipe does not care whether the variable is discrete. For observations from a Gaussian with unknown mean and standard deviation ,
and setting the two partial derivatives to zero gives
The sample mean and - note the denominator - the root mean squared deviation about it. On ten measurements summing to 51.0 with squared deviations summing to 3.98, that is and , where dividing by instead would have given . The two differ by the factor .
Which is right? Both, for different questions. Dividing by genuinely maximises the likelihood; dividing by genuinely gives an unbiased estimator of the population variance. Maximum likelihood was never asked to be unbiased, and it isn't. Being clear about which criterion is in force saves a great deal of confusion.
There is a bonus in this derivation. Fit a linear Gaussian model - normal about with fixed variance - and the only part of the log likelihood that depends on the parameters is . Maximising it is minimising the sum of squared errors. Least squares is not a convenient loss chosen by tradition; it is maximum likelihood under Gaussian noise.
The failure that is coming
Unwrap one candy, find it cherry, and the recipe reports : the bag is entirely cherry, and a lime candy is impossible. Not unlikely - impossible, with probability exactly zero, a claim no amount of subsequent evidence can be multiplied back out of.
This is not an artefact of small samples in general; it is what maximum likelihood does with any event it has not yet seen. It reads "not observed" as "cannot happen". The standard field repair is to initialise every count at one instead of zero, which sounds like a hack and turns out, in the next lesson, to be a prior in disguise.
Before the quiz
Three steps: write the likelihood, differentiate the log, set it to zero. With complete data the parameters separate, so fitting a whole network is a set of independent counting problems. The Gaussian case gives the sample mean and a standard deviation divided by , and it explains why least squares is the right loss for Gaussian noise. And the whole apparatus assigns probability zero to whatever it has not yet seen.
References & further reading
- Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
- Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, An Introduction to Statistical Learning, with Applications in R, Springer (Springer Texts in Statistics 103), 2013source ↗
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.
Unlock the full path
This first lesson is free. Enrol to take the mastery quiz, earn XP, and unlock every module, with more interactive, runnable examples throughout.