Skip to content
Kudos AI

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.

IntermediateModule 125 min · 100 XP
The likelihood curve for a bag of candies drawn as the exponent moves, its peak sliding to the observed proportion, and then the same curve flattened by the logarithm into a sum whose derivative is a single line.

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 θ\theta. The likelihood of a data set d=d1,…,dN\mathbf{d} = d_1, \ldots, d_N is the probability the model assigns to exactly those observations. If the examples are independent given the parameters, that probability is a product:

P(d∣hθ)=∏j=1NP(dj∣hθ).P(\mathbf{d} \mid h_\theta) = \prod_{j=1}^{N} P(d_j \mid h_\theta).

The maximum-likelihood estimate is the θ\theta that makes this largest. Note what has been assumed away: no prior over θ\theta 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 θ\theta of cherry candies, the rest lime. You unwrap 25 and find 18 cherry. What is θ\theta?

Step one, write the likelihood. With c=18c = 18 cherries and ℓ=7\ell = 7 limes,

P(d∣hθ)=θc (1−θ)ℓ=θ18(1−θ)7.P(\mathbf{d} \mid h_\theta) = \theta^{c} \, (1 - \theta)^{\ell} = \theta^{18} (1 - \theta)^{7}.

Step two, take logs and differentiate. The logarithm turns the product into a sum, which is what makes the derivative tractable:

L(d∣hθ)=clog⁡θ+ℓlog⁡(1−θ),dLdθ=cθ−ℓ1−θ.L(\mathbf{d} \mid h_\theta) = c \log \theta + \ell \log(1 - \theta), \qquad \frac{dL}{d\theta} = \frac{c}{\theta} - \frac{\ell}{1 - \theta}.

Step three, set it to zero. That gives c(1−θ)=ℓθc(1-\theta) = \ell\theta, so

θ=cc+ℓ=cN=1825=0.72.\theta = \frac{c}{c + \ell} = \frac{c}{N} = \frac{18}{25} = 0.72 .

The maximum-likelihood hypothesis says the proportion of cherries in the bag equals the proportion observed. A grid search over two million values of θ\theta agrees to six decimal places, and the log likelihood there is −14.823833-14.823833, against −14.847959-14.847959 at θ=0.70\theta = 0.70 and −14.849407-14.849407 at θ=0.74\theta = 0.74.

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, log⁡\log 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: θ\theta for the flavour, θ1=P(red∣cherry)\theta_1 = P(\text{red} \mid \text{cherry}), and θ2=P(red∣lime)\theta_2 = P(\text{red} \mid \text{lime}). 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

L=12log⁡(θθ1)+6log⁡(θ(1−θ1))+2log⁡((1−θ)θ2)+5log⁡((1−θ)(1−θ2)).L = 12 \log(\theta\theta_1) + 6 \log(\theta(1 - \theta_1)) + 2 \log((1 - \theta)\theta_2) + 5 \log((1 - \theta)(1 - \theta_2)).

Expand the logarithms of the products and something useful happens: every term mentioning θ1\theta_1 mentions nothing else. The three partial derivatives are therefore independent equations, and each one is the one-parameter problem solved again:

θ=1825=0.72,θ1=1218=0.666667,θ2=27=0.285714.\theta = \frac{18}{25} = 0.72, \qquad \theta_1 = \frac{12}{18} = 0.666667, \qquad \theta_2 = \frac{2}{7} = 0.285714 .

Running Nelder–Mead on the full three-parameter likelihood lands within 1.4×10−81.4 \times 10^{-8} of those values, at a log likelihood of −30.468975-30.468975.

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 NN observations from a Gaussian with unknown mean μ\mu and standard deviation σ\sigma,

L=N(−log⁡2π−log⁡σ)−∑j=1N(xj−μ)22σ2,L = N\left(-\log\sqrt{2\pi} - \log\sigma\right) - \sum_{j=1}^{N} \frac{(x_j - \mu)^2}{2\sigma^2},

and setting the two partial derivatives to zero gives

μ=∑jxjN,σ=∑j(xj−μ)2N.\mu = \frac{\sum_j x_j}{N}, \qquad \sigma = \sqrt{\frac{\sum_j (x_j - \mu)^2}{N}} .

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 μ=5.1\mu = 5.1 and σ=0.630872\sigma = 0.630872, where dividing by N−1N - 1 instead would have given 0.6649980.664998. The two differ by the factor N/(N−1)=1.054093\sqrt{N/(N-1)} = 1.054093.

Which is right? Both, for different questions. Dividing by NN genuinely maximises the likelihood; dividing by N−1N-1 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 - yy normal about θ1x+θ2\theta_1 x + \theta_2 with fixed variance - and the only part of the log likelihood that depends on the parameters is −∑j(yj−θ1xj−θ2)2-\sum_j (y_j - \theta_1 x_j - \theta_2)^2. 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 θ=1/1=1\theta = 1/1 = 1: 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 NN, 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.