Probabilistic Machine Learning
Models that say how sure they are.
A spam filter that says "spam" is useful. One that says "92% spam" is more useful, because it tells you how far to trust it. Probabilistic machine learning builds models that answer with probabilities, fits them to data with the rules of probability (Bayes' rule above all), and keeps track of what it does not yet know.
What you’ll learn
- Explain what it means for a model to output a probability distribution, and why a stated uncertainty is useful.
- Estimate a coin's probability of heads by maximum likelihood, and explain why that estimate is overconfident on little data.
- Update a belief with Bayes' rule, and relate the prior, the posterior, the MAP estimate and the maximum likelihood estimate.
- Describe how latent variables and mixture models explain data through hidden causes, and what the EM algorithm does.
- Explain why exact inference is often impossible, and what sampling (MCMC) and approximation do instead.
Answers with error bars
Most models you meet first give one answer: a price, a class, a cluster. A probabilistic model gives a whole distribution over answers. Two loan models can both predict "will repay", one with 51% confidence and one with 99%; only the second deserves to be trusted without a human check. A demand forecast of "120 units, give or take 15" lets a shop decide how much spare stock to hold, which "120 units" alone cannot.
The language for this is probability. It describes two kinds of uncertainty with the same rules: randomness in the world (a coin flip, noise in a sensor), which no amount of data removes, and ignorance about the model itself (which parameter values are right), which shrinks as data arrive.
| Task | A plain model outputs | A probabilistic model outputs |
|---|---|---|
| Classification | one label | a probability for every class |
| Regression | one number | a distribution: a mean and a spread |
| Clustering | one cluster per point | a probability for each cluster, per point |
| Fitting parameters | one best value | a distribution over plausible values |
A weather model says "30% chance of rain" on 200 different days. If its probabilities mean what they say, on about how many of those days should it rain?
Show the answer
60 days
, give or take about 6 from chance. A model whose stated probabilities match long-run frequencies like this is calibrated (calibration). Probabilities are only useful if they are honest, which is why later lessons check them against data.
The coin: maximum likelihood
Take a coin that lands heads with an unknown probability . You flip it 10 times and see 7 heads and 3 tails. Treat the flips as independent, each heads with probability (a Bernoulli trial). The probability of exactly this sequence, read as a function of , is the likelihood:
How many times more probable is this exact sequence if than if the coin is fair ()?
Show the answer
2.28 ×
and , a ratio of about . Both numbers are tiny, because any particular sequence of 10 flips is unlikely. Only the comparison between values of matters.
The value of that makes the observed data most probable is the maximum likelihood estimate (MLE). For a coin it is simply the fraction of heads, : here . (The Descent lesson derives it with one line of calculus.) Try it on a coin whose true probability is hidden from you.
Bayes' rule: updating a belief
Bayes' rule says how a belief in a hypothesis should change after seeing evidence :
is the belief before the evidence, is how likely the evidence is if is true, and is the overall chance of the evidence, which makes the answer sum to one. In words: posterior ∝ likelihood × prior.
A classic case: a disease affects 1% of people. A test detects 90% of real cases, and it also comes back positive for 9% of healthy people.
You test positive. Roughly how likely is it that you have the disease?
- About 90%
- About 50%
- About 9%
- About 1%, the same as before the test
Show the answer
C. About 9%
Picture 1,000 people: 10 are sick and 9 of them test positive; of the 990 healthy people, about 89 also test positive. Only 9 of the 98 positives are sick, about 9.2%. The disease is so rare that false alarms among the many healthy people outnumber the true cases.
A prior over the coin
Now apply Bayes' rule to the coin's unknown itself. The prior is a curve over saying which values you find plausible before flipping. The posterior is that curve multiplied by the likelihood and rescaled to area one. A convenient family of priors is the Beta distribution , which behaves like imaginary heads and imaginary tails seen before you start. After real heads and real tails you simply add the counts:
Two single numbers summarize the posterior: its mean, and its peak, the MAP estimate (maximum a posteriori). With a flat prior the MAP equals the MLE; a prior pulls both toward the values it favors. The posterior also gives a range: a 95% credible interval contains with probability 95%, given the data and the prior.
Think first Two people start from very different priors, "Heads" and "Tails", and watch the same 1,000 flips. Do they end up agreeing?
Almost exactly. "Heads" is and "Tails" is , each worth 40 imaginary flips, which 1,000 real flips swamp: whatever the count of heads , the posterior means and differ by only , and the gap keeps shrinking as flips accumulate. There is one exception: a prior that gives some value of zero probability can never be argued back into it, whatever the data say. Never rule out what you cannot exclude.
Hidden causes: latent variables and mixtures
Many datasets are mixtures of groups whose labels nobody recorded: flower measurements from several species, purchases from different kinds of customers, sensor readings from a machine that is sometimes healthy and sometimes not. The unrecorded group is a latent variable, a hidden cause behind each data point.
A Gaussian mixture model says each point was made in two moves: pick one of clusters at random (cluster with probability ), then draw the point from that cluster's bell-shaped Gaussian distribution. Fitting it means finding the clusters without knowing which point came from which. Bayes' rule does the inverse step: given the current clusters, the probability that a point came from cluster is its responsibility for that cluster.
The EM algorithm alternates two steps:
- E-step: compute every point's responsibilities from the current clusters.
- M-step: refit each cluster's weight, center and shape to all the points, each counted in proportion to its responsibility.
Each round never lowers the likelihood of the data, so EM always settles down, though not always on the best fit. It is a soft version of k-means, which forces every point to belong to exactly one cluster.
After EM has converged, a point has responsibilities 0.55 for cluster 1, 0.40 for cluster 2 and 0.05 for cluster 3. What does this mean?
- 55% of the point belongs to cluster 1 and 40% to cluster 2
- Cluster 1 contains 55% of all the data
- Under the fitted model, the point came from cluster 1 with probability 0.55 and from cluster 2 with probability 0.40
- The point is an outlier that fits no cluster
Show the answer
C. Under the fitted model, the point came from cluster 1 with probability 0.55 and from cluster 2 with probability 0.40
Responsibilities are posterior probabilities of the hidden label, computed by Bayes' rule from the cluster weights and how well each Gaussian explains the point. The point came from exactly one cluster; the model is simply unsure which.
When exact answers run out: sampling
For the coin, the posterior has a formula. For most models it does not. Bayes' rule needs the normalizing constant , an integral of likelihood × prior over every possible parameter setting. A model with 1,000 parameters would need that integral over a 1,000-dimensional space: even a crude grid of 10 values per parameter has points.
Markov chain Monte Carlo (MCMC) sidesteps the integral. It runs a random walk through parameter space, a Markov chain, built so that in the long run the share of time it spends in each region matches that region's posterior probability. Each move is accepted or rejected using only a ratio of posterior values, and in a ratio the unknown constant cancels. Averages over the visited points then estimate anything you want, such as the posterior mean or a credible interval: that is the Monte Carlo part.
Where to go next
The four ideas of this lesson, likelihoods, priors and posteriors, hidden variables, and approximate inference, run through modern machine learning. Training a classifier with cross-entropy is maximum likelihood; weight decay is a prior in disguise; and many generative models, such as variational autoencoders and diffusion models, are latent-variable models trained with the tools of approximate inference.
| Descent lesson | What you will do there |
|---|---|
| Maximum Likelihood & MAP | Derive maximum likelihood estimates for coins and Gaussians, see that least squares and cross-entropy are likelihoods in disguise, and recover ridge and lasso as MAP estimates. |
| Bayesian Inference | Work with full posteriors, make predictions that average over parameters, use conjugate priors, and tell credible intervals from confidence intervals. |
| Latent Variables & EM | Fit Gaussian mixtures, compute responsibilities, and prove that EM never decreases the likelihood. |
| Approximate Inference | Estimate integrals by Monte Carlo, build Metropolis–Hastings and Gibbs samplers, and turn inference into optimization with variational methods. |
Key takeaways
- Probabilistic models output distributions, not single answers, and so say how sure they are. Calibrated probabilities can be acted on: a 30% forecast should come true 30% of the time.
- The likelihood scores each parameter value by how probable it makes the data. Maximizing it gives the MLE (the fraction of heads for a coin), which can be badly overconfident on small samples.
- Bayes' rule turns a prior into a posterior, posterior ∝ likelihood × prior. A prior on a coin becomes ; as data grow the posterior narrows and the prior washes out.
- Latent-variable models explain data through hidden causes. EM alternates soft assignments (responsibilities, by Bayes' rule) with refitting, and never lowers the likelihood.
- Exact posteriors are rare. MCMC samples from a posterior using only ratios of densities; variational inference approximates it by optimization, trading exactness for speed.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
You flip a new coin 4 times and get 4 heads. What is the maximum likelihood estimate of , and what is wrong with it?
- , because most coins are fair
- , which declares tails impossible after only 4 flips
- , because 4 of 5 imaginary flips are heads
- It does not exist until both outcomes have been seen
Show the answer
B. , which declares tails impossible after only 4 flips
The MLE is the fraction of heads, . It trusts a tiny sample completely. A flat prior gives the posterior mean instead, which still leans toward heads but leaves room for tails.
A condition affects 1 person in 1,000. A test catches 99% of cases and gives a false positive for 1% of healthy people. After one positive result, the probability of having the condition is closest to:
- 99%
- 50%
- 9%
- 0.1%
Show the answer
C. 9%
Out of 100,000 people, 100 have it and 99 test positive; of the 99,900 healthy, 999 test positive. So . A rare condition needs a test with a far smaller false-positive rate, or a second test.
Two analysts with different (but not dogmatic) Beta priors observe the same 10,000 flips of a coin. What do their posteriors look like?
- Nearly identical and narrow, centered close to the observed fraction of heads
- Still far apart, because priors never wash out
- Identical to their priors, because data cannot change a belief
- Both flat, because more data means more uncertainty
Show the answer
A. Nearly identical and narrow, centered close to the observed fraction of heads
Each Beta prior is worth a fixed number of imaginary flips, which 10,000 real flips swamp. Both posteriors concentrate within about a percentage point of the observed frequency. Only a prior that assigns zero probability to the truth resists any amount of data.
Which describes one iteration of EM for a Gaussian mixture?
- Assign every point permanently to its nearest center, then delete empty clusters
- Add one more cluster, then keep it if the likelihood rises
- Draw random cluster labels and average the results over many draws
- Compute each point's responsibilities from the current clusters, then refit every cluster using those responsibilities as weights
Show the answer
D. Compute each point's responsibilities from the current clusters, then refit every cluster using those responsibilities as weights
The E-step uses Bayes' rule to get soft assignments; the M-step refits weights, means and covariances to the weighted points. Repeating the pair never lowers the likelihood, so EM settles down, usually at a local optimum that need not be the best fit.
Why can MCMC sample from a posterior without ever computing its normalizing constant ?
- Because is always equal to 1
- Because each accept-or-reject decision uses a ratio of posterior densities, in which cancels
- Because MCMC first computes by numerical integration on a grid
- Because MCMC only works with flat priors
Show the answer
B. Because each accept-or-reject decision uses a ratio of posterior densities, in which cancels
The posterior is likelihood × prior divided by . A ratio of two posterior values divides by itself, so the sampler needs only likelihood × prior, which is easy to evaluate.
Go deeper
- Maximum Likelihood & MAP · Choosing parameters that make the data most probable.
- Bayesian Inference · Priors, posteriors and predictions that average over uncertainty.
- Latent Variables & EM · Hidden structure, and how to fit models of it.
- Approximate Inference · When the posterior is intractable: sampling and variational methods.
Further reading
- K. P. Murphy, Probabilistic Machine Learning: An Introduction (MIT Press, 2022), chapter 4.
- D. J. C. MacKay, Information Theory, Inference, and Learning Algorithms (Cambridge University Press, 2003).