Probability II: Bayes, independence, MLE and MAP
Updating beliefs, and why minimizing a loss is maximizing a likelihood
Two ideas from this lesson run the whole path. Bayes' theorem is how evidence updates belief — it is the logic of classifiers (3.7 Naive Bayes and generative vs discriminative models), of posterior inference in VAEs (8.8 Generative models I: VAEs and GANs) and of every "given the context, what is the next token" computation (7.6 Decoder-only: GPT and autoregressive generation). Maximum likelihood is why the loss functions of Phase 4 look the way they do: squared error, binary cross-entropy and cross-entropy are each the negative log-likelihood of a distribution, and regularization is a prior (3.6 Regularization and generalization). After this lesson, no loss function will ever look arbitrary again.
You'll be able to
- Manipulate joint, marginal and conditional probabilities with the sum, product and chain rules
- Apply Bayes' theorem to a diagnostic problem and explain the base-rate fallacy
- Derive maximum-likelihood estimates for Bernoulli and Gaussian models, and explain MAP as MLE plus a prior
- Show that negative log-likelihood under Gaussian, Bernoulli and Categorical models gives MSE, BCE and cross-entropy
Key ideas
- Sum rule marginalizes, product rule conditions, and the chain rule p(x₁…xₙ) = Π p(xₜ | x₍<ₜ₎) is the language-model factorization.
- Independence: p(x, y) = p(x)p(y); conditional independence given z underlies i.i.d. data and naive Bayes.
- Bayes: posterior ∝ likelihood × prior; a 99%-sensitive, 95%-specific test at 1% prevalence gives only a 16.7% posterior — the base-rate fallacy.
Show 5 more key ideas
- Likelihood treats the data as fixed and ranks parameters; log-likelihoods of i.i.d. data are sums.
- MLE: Bernoulli p̂ = k/n; Gaussian μ̂ = mean, σ̂² = (1/n)Σ(xᵢ − x̄)² (slightly biased).
- MAP = MLE + log prior: Beta priors are pseudo-counts (Laplace smoothing); Gaussian priors are ℓ₂ weight decay, Laplace priors are ℓ₁.
- Loss = negative log-likelihood: Gaussian → MSE, Bernoulli → BCE, Categorical → cross-entropy, Laplace → MAE. Choosing a loss is choosing a noise model.
- Multivariate Gaussian: Mahalanobis distance, ellipsoidal level sets from Σ's eigen-decomposition, sampling via x = μ + Lz.
Suggested path through this lesson
Joint, marginal, conditional
For two random variables, the joint assigns probability to pairs. The sum rule recovers a marginal by summing out what you do not care about, , and the product rule defines the conditional: , so — the probability of in the world where is known. Applying the product rule repeatedly gives the chain rule of probability,
which is exactly how a language model scores a sentence: one conditional per token, each given everything before it (6.2 Classical NLP and n-gram language models, 7.6 Decoder-only: GPT and autoregressive generation).
Independence
and are independent when , equivalently : learning tells you nothing about . They are conditionally independent given when . Two assumptions of this kind underlie much of ML: that training examples are independent and identically distributed (i.i.d., 3.1 What is learning? ERM, generalization and the ML workflow), and naive Bayes' assumption that features are independent given the class (3.7 Naive Bayes and generative vs discriminative models). Independence turns products into sums in log space, which is why log-likelihoods of datasets are sums over examples.
Bayes' theorem
Writing the product rule both ways, , and dividing:
The posterior is proportional to likelihood times prior; the evidence is just the normalizer. Worked example. A disease has prevalence 1%. A test has sensitivity 99% (positive if sick) and specificity 95% (negative if healthy). You test positive. Then
Only a one-in-six chance, despite a "99% accurate" test, because the 5% false-positive rate acts on the 99% of people who are healthy. Ignoring the prior is the base-rate fallacy; the demo above lets you move all three numbers and watch the posterior respond.
Intuition. Bayes' theorem is bookkeeping for evidence: start from how plausible each hypothesis was, multiply by how well it predicts what you saw, renormalize.
Likelihood is not probability
Fix the data and read as a function of the parameters: that is the likelihood . It need not integrate to one over ; it ranks parameter values by how well they explain the data. For i.i.d. data the likelihood is a product and the log-likelihood is a sum, , which is numerically stable and easy to differentiate.
Maximum likelihood estimation
Bernoulli. Observe heads in flips. ; setting gives . The sample frequency — reassuring, and derived rather than assumed.
Gaussian. For ,
Differentiating in : , the sample mean. Differentiating in : — note the , which makes this estimator slightly biased; 1.8 Statistics essentials explains the fix.
MAP: adding a prior
Maximum a posteriori estimation maximizes the posterior instead: (the evidence does not depend on ). With a prior on the coin, : pseudo-counts that pull the estimate toward the prior when data are scarce — the same mathematics as Laplace smoothing in naive Bayes (3.7 Naive Bayes and generative vs discriminative models). With a Gaussian prior on weights, , the log-prior is : MAP adds an penalty to the loss. Weight decay is a Gaussian prior; a Laplace prior gives (3.6 Regularization and generalization). The prior's influence shrinks as grows, so MAP and MLE agree in the large-data limit.
Negative log-likelihood is the loss
Suppose a model predicts and we assume a distribution of the observed around it. Minimizing summed over the data gives:
| assumed | negative log-likelihood (per example) | the loss you know |
|---|---|---|
| mean squared error (3.2 Linear regression) | ||
| binary cross-entropy (3.3 Logistic regression and softmax classification) | ||
| cross-entropy (4.3 Loss functions catalog) | ||
| Laplace | mean absolute error |
So "choose a loss" really means "choose a noise model", and the pairing is not optional: squared error assumes Gaussian residuals, cross-entropy assumes a categorical output (1.9 Information theory: entropy, cross-entropy and KL divergence shows the same table from the information-theory side).
The multivariate Gaussian
For with mean and covariance (symmetric positive definite), the density is the one quoted in 1.6 Probability I: random variables and distributions. The quadratic form is the squared Mahalanobis distance: Euclidean distance after whitening by the covariance. Writing (1.3 Eigen-things, SVD and low rank), the level sets are ellipsoids with axes and semi-axes ; is the precision matrix, whose zeros encode conditional independences. To sample, take and set with (a Cholesky factor) — the reparameterization that VAEs rely on (8.8 Generative models I: VAEs and GANs). The MLE of is the sample covariance , the matrix PCA diagonalizes (3.12 Dimensionality reduction: PCA, t-SNE and UMAP).
Where this shows up later
- Estimator bias, variance and intervals — 1.8 Statistics essentials; cross-entropy as KL plus entropy — 1.9 Information theory: entropy, cross-entropy and KL divergence.
- Naive Bayes and generative classifiers — 3.7 Naive Bayes and generative vs discriminative models; logistic regression as Bernoulli MLE — 3.3 Logistic regression and softmax classification; weight decay as a Gaussian prior — 3.6 Regularization and generalization; the loss catalog — 4.3 Loss functions catalog.
- The chain rule of probability as the language-model objective — 6.2 Classical NLP and n-gram language models, 7.6 Decoder-only: GPT and autoregressive generation; ELBO and the reparameterization trick — 8.8 Generative models I: VAEs and GANs; Bradley–Terry preference likelihoods in RLHF — 8.3 Alignment: RLHF, PPO, DPO, GRPO and Constitutional AI.
Common pitfalls (4)
- Confusing P(positive | sick) with P(sick | positive) — the prosecutor's fallacy in medical clothing.
- Treating the likelihood as a distribution over θ; it does not integrate to 1 and only ranks parameter values.
- Forgetting that MLE's σ̂² uses 1/n and is biased; most libraries report the n − 1 version.
- Using squared error on heavy-tailed targets without noticing that it silently assumes Gaussian noise.
Recap: all 8 key points
- Sum rule marginalizes, product rule conditions, and the chain rule p(x₁…xₙ) = Π p(xₜ | x₍<ₜ₎) is the language-model factorization.
- Independence: p(x, y) = p(x)p(y); conditional independence given z underlies i.i.d. data and naive Bayes.
- Bayes: posterior ∝ likelihood × prior; a 99%-sensitive, 95%-specific test at 1% prevalence gives only a 16.7% posterior — the base-rate fallacy.
- Likelihood treats the data as fixed and ranks parameters; log-likelihoods of i.i.d. data are sums.
- MLE: Bernoulli p̂ = k/n; Gaussian μ̂ = mean, σ̂² = (1/n)Σ(xᵢ − x̄)² (slightly biased).
- MAP = MLE + log prior: Beta priors are pseudo-counts (Laplace smoothing); Gaussian priors are ℓ₂ weight decay, Laplace priors are ℓ₁.
- Loss = negative log-likelihood: Gaussian → MSE, Bernoulli → BCE, Categorical → cross-entropy, Laplace → MAE. Choosing a loss is choosing a noise model.
- Multivariate Gaussian: Mahalanobis distance, ellipsoidal level sets from Σ's eigen-decomposition, sampling via x = μ + Lz.
Watch
The area picture of prior, likelihood and posterior; the medical-test example in visual form.
Read
Try it live
Interactive demo
runs in your browserExercises
- By hand: three Bayes updates
Recompute the medical-test posterior for prevalence 0.1%, 1% and 10% with the same test. Then compute the posterior after two independent positive tests at 1% prevalence. Expected: about 1.9%, 16.7% and 68.8% for one test; about 79.9% after two positives — evidence compounds multiplicatively in the odds.
- NumPy: MLE for a Gaussian, step by step
Sample 500 points from . Evaluate the log-likelihood on a grid of values, plot it as a heatmap, and locate the maximum; compare with
x.mean()andx.std(ddof=0). Expected: the grid maximum sits on the sample mean and the biased standard deviation. - Experiment: a Beta prior meets data
For a coin with true , plot the prior and the posterior after 1, 5, 20 and 200 flips, marking the MLE and the MAP . Expected: MAP sits between prior mean and MLE early on, and the two estimates merge as grows while the posterior narrows like .
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →