Learning the Numbers in a Probability Model
Where the numbers in a Bayesian network or a Gaussian actually come from: the three-step maximum-likelihood recipe worked through on discrete and continuous parameters, the Beta prior that repairs what it does to an unseen event, naive Bayes and the single zero count that destroys it, and the EM algorithm for the case where the counts cannot be taken at all - with every figure computed rather than asserted.
Prerequisites: Bayesian Networks and Probabilistic Inference
Every probability model discussed so far arrived with its numbers already in place. A Bayesian network came with its conditional probability tables filled in; a Gaussian came with a mean and a variance. Somebody had to put them there. This article is about how, and it splits cleanly in two: the case where every variable is observed, which is almost trivially easy, and the case where one is not, which requires a genuinely different algorithm.
A. The likelihood, and the three steps
Fix a model with parameters . The likelihood of a data set is the probability the model assigns to exactly the observations you got, which for independent examples is a product:
Maximum-likelihood estimation picks the maximising this. The recipe is always the same three steps: write the likelihood, differentiate its logarithm, set the derivative to zero.
Unwrap 25 candies from a bag with unknown cherry proportion and find 18 cherry. Then , so
A grid over two million values of agrees to six decimals: the log likelihood is at , against at and at .
The answer is the observed proportion, which is what anyone would have guessed. The value of the derivation is that it proves the guess is the maximiser, and that the same three steps keep working where no guess suggests itself.
Taking the logarithm is worth defending twice. Mathematically it cannot move a maximum, since is strictly increasing. Computationally, a product of thousands of probabilities underflows to zero in floating point and a sum of logarithms does not - which is why production code lives in log space.
B. Why complete data makes a network cheap
Give the candies a wrapper too, red or green, with a probability depending on flavour. Three parameters now: , , . With counts 12 cherry-red, 6 cherry-green, 2 lime-red, 5 lime-green,
Expand the logarithms and every term containing contains nothing else. The partial derivatives are independent equations, each the one-parameter problem again:
Nelder–Mead on the full three-parameter likelihood lands within of these, at a log likelihood of .
That decoupling is the whole reason learning a Bayesian network from complete data is fast. Each conditional probability table is fitted by counting within its own conditioning case, with no search and no iteration - a set of independent counting problems rather than one joint optimisation.
The figure opens with the two wrapper sliders on the rounded values 0.67 and 0.29, so its total log likelihood reads . Press snap to the counted estimates and the sliders move to the maximum-likelihood values, and the total to the quoted above.
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.
C. The continuous case, and why least squares
For draws from a Gaussian, setting the two partial derivatives to zero gives
Note the denominator. On ten measurements summing to with squared deviations summing to , that is and , where dividing by gives - a factor of apart. Both are correct answers to different questions: one maximises the likelihood, the other is unbiased. Maximum likelihood was never asked to be unbiased.
A useful corollary falls out. Fit as normal about with fixed variance, and the only parameter-dependent part of the log likelihood is . Minimising squared error is maximum likelihood under Gaussian noise, which is the reason least squares is the default and not merely a convenient tradition.
D. What it does to an event it has not seen
Unwrap one candy, find it cherry, and the recipe reports : lime is impossible, probability exactly zero, a claim no later evidence can multiply its way out of. Maximum likelihood reads "not yet observed" as "cannot happen".
The Bayesian repair is a prior. For the natural choice is a Beta distribution, whose useful property is conjugacy: a prior with cherries and limes gives a posterior. Updating is addition, and the posterior stays in the same family, so belief can be updated forever without the representation growing.
behaves like imaginary observations already counted. Watch - one imaginary candy of each flavour - handle the pathological case:
| observations | posterior | posterior mean | MAP | maximum likelihood |
|---|---|---|---|---|
| , | ||||
| , | ||||
| , | ||||
| , |
The prior does its work early and then gets out of the way: it is the difference between and a claim of certainty at one observation, and worth three and a half thousandths at 250. Note also that maximum likelihood is exactly MAP under a uniform prior, and that adding one to every count - the standard field hack - is the posterior mean under . It was a prior all along.
E. Naive Bayes, and one zero
A naive Bayes model assumes the attributes are conditionally independent given the class, so : for Boolean attributes, parameters, all fitted by counting.
Take 14 papers labelled theory or applied, described by whether each contains a proof, uses a dataset, and reports a benchmark. Six are theory, eight applied, so the priors are and :
| proof | dataset | benchmark | |
|---|---|---|---|
| theory (6) | |||
| applied (8) |
A paper with a proof and nothing else scores against , a posterior of . Sensible.
Now look at the zero. No theory paper reports a benchmark, so . Classify a paper with a proof, no dataset, and a benchmark: the theory score is a product containing that zero, so it is zero exactly. The posterior is regardless of the proof, regardless of the prior. One attribute value absent from the training data holds a veto over the entire prediction - and in text classification, where most words are missing from most classes, that is the normal condition rather than an edge case.
Add-one smoothing turns into , and the same paper scores against : a posterior of against . An honest near-tie instead of a certainty, and the smoothed classifier still labels all 14 training papers correctly.
Interactive: one zero, and the classifier stops working
14 papers, 6 theory and 8 applied.
| has a proof | uses a dataset | reports a benchmark | |
|---|---|---|---|
| theory (6) | 5/60.833 | 2/60.333 | 0/60.000 |
| applied (8) | 2/80.250 | 7/80.875 | 7/80.875 |
- P(theory | evidence)
- 0.990712
- Verdict
- theory
- Training papers right
- 14 / 14
With this evidence the classifier is confident and both settings agree. The interesting case is the one the corpus cannot represent: ask for a benchmark and watch the theory column collapse. Note that the zero is visible in the fitted table before any prediction is made - the marked cell is where the veto lives.
The independence assumption is of course false - papers with benchmarks tend to have datasets - and wherever attributes are dependent within a class the shared evidence is counted twice, so the probabilities are not to be trusted. The rankings survive anyway, because the decision is an and double-counting tends to inflate both scores together. Trust the class, not the number.
F. When the counts cannot be taken
Everything above needed complete data. Hide a variable - which cluster a point came from, which topic wrote a document - and the likelihood of an observed example becomes a sum over what the hidden variable might have been:
The logarithm of a sum does not split, so the parameters no longer decouple and there is no closed form to set to zero.
Expectation–maximization substitutes expected counts for the counts it cannot take:
The E step computes the posterior over the hidden variables under the current parameters - the fractional membership a point gets is its responsibility. The M step applies the complete-data formulas with every count replaced by a sum of responsibilities. For a mixture of Gaussians that is
with . These are the Gaussian formulas from section C, weighted.
G. A measured run, and where k-means loses
Twenty points on a line: eight from a narrow component near zero () and twelve from a much broader one near five (), with the labels withheld.
From a crude start - means 0 and 5, both standard deviations 1 - the log likelihood rises from to over 82 iterations, never once falling, and settles on
The larger responsibility per point recovers all twenty withheld labels.
The figure stops at iteration 81, one short of the 82 above, because its stopping tolerance differs; the log likelihood it starts and ends on, and , agrees with the run described here.
Interactive: the point that changes hands
Iteration 0 of 81.
- Log likelihood
- -56.616239
- EM: labels recovered
- 19 / 20
- Optimal k-means
- 19 / 20
- x = 1.7 is claimed by
- the narrow component
Early on both components are unit-width and sit where they were placed, so the fit is still deciding by something close to distance and 1.7 belongs to the narrow side. Keep stepping: once width is allowed to matter, it changes hands. The k-means split beneath is the proven optimum over all 19 contiguous partitions, so its mistake is a property of hard assignment, not of a bad start.
The monotone rise is guaranteed: each iteration maximises a lower bound touching the log likelihood at the current parameters, so a decrease means a bug. The global maximum is not guaranteed - over 400 random restarts, 385 reached and 15 stopped at , a worse fixed point no further iteration escapes.
Now compare k-means on the same points. Its optimal split - verified by enumerating all 19 contiguous partitions, so this is not a local minimum - has centres and with a within-cluster sum of squares of , and it puts in the narrow cluster. That is one point away from EM's answer and one point away from the truth.
The reason is instructive. lies from the fitted narrow mean and from the broad one, so distance alone hands it over, and distance is all k-means has. EM compares densities:
giving responsibilities and . The broad component is 3.24 times wider and carries the larger weight, and both count. The point is further from that component and still more likely to have come from it.
Responsibilities elsewhere are mostly decisive but not everywhere: splits , and splits . EM fits component widths and mixing weights, which k-means does not represent at all.
Key takeaways
- With complete data, maximum likelihood is three steps and the answer is a ratio of counts; the parameters of a network decouple, so fitting it is a set of independent counting problems.
- The maximum-likelihood Gaussian divides by , not , and it explains why least squares is the right loss under Gaussian noise.
- Maximum likelihood assigns probability zero to anything unobserved, which destroys a naive Bayes prediction through a single zero factor. A Beta prior is the principled repair, and add-one smoothing is a special case of it.
- Hidden variables put a sum inside the logarithm and break the decoupling. EM replaces counts with expected counts, never decreases the likelihood, and reaches only a local maximum - so restart it.
- Because EM models width and weight rather than distance alone, it recovers structures that hard assignment cannot.
What's next
Every EM instance here fitted a static model. The same two steps fit a model that changes over time - the E step becomes forward–backward smoothing, and the reason it must be smoothing rather than filtering is that estimating a transition needs evidence from after it happened.
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.