M8 · Likelihood, Bayes and entropy
Where MSE, cross-entropy, L2 regularization and the information gain of trees come from: loss functions seen as probabilities.
Mastering AI · Required Maths · Week 3 (probability and statistics) · Part 8
Goal of this step
So far we’ve taken MSE and log-loss as given. They don’t fall from the sky: they follow from a single idea, maximum likelihood. Add Bayes’ theorem and the notion of entropy, and you also get regularization and the splitting criterion of decision trees.
By the end of this article you’ll be able to:
- apply Bayes’ theorem and explain the effect of the prior;
- estimate a parameter by maximum likelihood;
- derive MSE from Gaussian noise and cross-entropy from a Bernoulli label;
- interpret regularization as a Gaussian prior (MAP);
- compute entropy, cross-entropy, KL divergence and information gain.
Key concepts
1. Bayes’ theorem
Intuition. We often know (“if the region is cold, my detector says so 95% of the time”) but we want (“the detector says cold: what’s the chance it really is?”). Bayes reverses the arrow, provided you know the base rate, the prior.
Running-project example: 70% of regions are cold. A detector says “cold” for 95% of cold regions and, by mistake, for 20% of hot ones. When it says “cold”, the probability that the region is actually hot (a false cold) is:
The same detector, on a workload where only 20% of regions are cold, gives 46% false colds. A classifier’s quality can never be read without the base rate: we’ll see it again in part 15 with precision.
2. Likelihood
Intuition. You have data and a model with a parameter . The likelihood is the probability of having observed exactly this data if the parameter were . Maximum likelihood (MLE) picks the that makes the data most probable.
For independent observations, the likelihood is a product, which the logarithm turns into a sum (numerically more stable, same maximum):
Example: access counts assumed to follow a Poisson( ) distribution. Setting the derivative of the log-likelihood to zero gives : the maximum likelihood estimator is the sample mean.
Minimizing a loss means maximizing a likelihood. In ML we write everything as a minimization: the loss is the average negative log-likelihood (NLL).
3. Gaussian noise ⇒ MSE
Assumption: with independent noise . The NLL is:
For fixed , only the second term depends on : it’s the sum of squared errors. Maximizing the Gaussian likelihood is exactly minimizing the MSE. And the estimator of is the square root of the MSE.
Corollary: MSE assumes symmetric noise without extreme values. With outliers, a Laplace distribution gives the loss (MAE), which is more robust.
4. Bernoulli label ⇒ cross-entropy
Assumption: follows a Bernoulli distribution with parameter . The probability of a label is , hence:
That’s exactly the log-loss (or binary cross-entropy) of logistic regression. It punishes a confident wrong prediction very hard: predicting for a region that will be accessed costs bits.
5. MAP: regularization as a prior
Intuition. Before seeing the data, you may already have an opinion: “the weights are probably small”. Bayes lets you combine that prior with the likelihood. You then maximize the posterior probability (MAP):
With a Gaussian prior , : you recover regularization (Ridge), with . A Laplace prior would give (Lasso). Regularization isn’t a trick: it’s a prior belief about the weights (M11, part 16).
6. Entropy, cross-entropy and KL divergence
Intuition. Entropy measures the uncertainty of a distribution, in bits: the average number of yes/no questions needed to guess the outcome. A fair coin is worth 1 bit, a rigged coin that always lands heads is worth 0.
Cross-entropy measures the average cost when the truth follows but you encode (or predict) with . The gap is the Kullback-Leibler divergence:
KL is zero if and only if , and it isn’t symmetric. Since doesn’t depend on the model, minimizing cross-entropy is the same as minimizing the KL between the true label distribution and the one the model predicts.
7. The information gain of trees
A decision tree (part 17) picks, at each node, the split that reduces the entropy of the labels the most:
A split that cleanly separates hot regions from cold ones produces “pure” children, with low entropy, hence a high gain.
Code example
Bayes and the weight of the prior
p_cold = 0.70 # prior: 70% of regions are cold (Zipf)
p_says_cold_if_cold = 0.95
p_says_cold_if_hot = 0.20
p_says_cold = p_says_cold_if_cold * p_cold + p_says_cold_if_hot * (1 - p_cold)
p_hot_if_says_cold = p_says_cold_if_hot * (1 - p_cold) / p_says_cold
print(f"P(says cold) = {p_says_cold:.3f} | P(actually hot | says cold) = {p_hot_if_says_cold:.3f}")
for prior in (0.9, 0.5, 0.2):
psc = p_says_cold_if_cold * prior + p_says_cold_if_hot * (1 - prior)
print(f" prior cold = {prior:.1f} -> P(hot | says cold) = {p_says_cold_if_hot * (1 - prior) / psc:.3f}")
| Share of cold regions | |
|---|---|
| 90% | 2.3% |
| 70% | 8.3% |
| 50% | 17.4% |
| 20% | 45.7% |
Same detector, same error rates: the share of false colds among regions declared cold goes from 2% to 46% depending on the workload.
Maximum likelihood: Poisson, Gaussian and Ridge
import numpy as np
from scipy.optimize import minimize, minimize_scalar
from scipy.special import gammaln
rng = np.random.default_rng(0)
# 1. Poisson: the MLE of λ is the mean
accesses = rng.poisson(3.2, 200)
nll_poisson = lambda lam: -np.sum(accesses * np.log(lam) - lam - gammaln(accesses + 1))
res = minimize_scalar(nll_poisson, bounds=(0.1, 10), method="bounded")
print(f"Poisson: numerical MLE of λ = {res.x:.4f} | sample mean = {accesses.mean():.4f}")
# 2. Gaussian noise => MSE
n, d = 300, 3
X = rng.normal(size=(n, d))
y = X @ np.array([2.0, -1.0, 0.5]) + rng.normal(0, 0.5, n)
def nll_gauss(params):
w, log_sigma = params[:d], params[d]
sigma2 = np.exp(2 * log_sigma)
r = y - X @ w
return 0.5 * n * np.log(2 * np.pi * sigma2) + (r @ r) / (2 * sigma2)
opt = minimize(nll_gauss, np.zeros(d + 1))
w_ls = np.linalg.lstsq(X, y, rcond=None)[0]
print(f"Gaussian MLE w : {opt.x[:d].round(4)} | MLE σ = {np.exp(opt.x[d]):.4f}")
print(f"least squares w: {w_ls.round(4)} | √(MSE) = {np.sqrt(np.mean((y - X @ w_ls) ** 2)):.4f}")
# 3. MAP with a Gaussian prior => Ridge
lam = 10.0
w_ridge = np.linalg.solve(X.T @ X + lam * np.eye(d), X.T @ y)
w_map = minimize(lambda w: np.sum((y - X @ w) ** 2) + lam * w @ w, np.zeros(d)).x
print(f"Ridge (closed form): {w_ridge.round(4)} | numerical MAP: {w_map.round(4)}")
| Method | Result |
|---|---|
| Poisson MLE (numerical optimization) | |
| Sample mean | |
| Gaussian MLE | , |
| Least squares | , |
| Ridge, closed form ( ) | |
| Numerical MAP, Gaussian prior |
Three pairs of methods, three exact matches: the Poisson MLE is the mean, the Gaussian MLE is the least squares solution, and the Gaussian MAP is Ridge regression. Notice too that Ridge shrinks every weight toward 0, as the prior predicts.
Entropy, KL and information gain
import numpy as np
def H(p):
p = np.asarray(p, float)
p = p[p > 0]
return -np.sum(p * np.log2(p))
def CE(p, q):
return -np.sum(np.asarray(p) * np.log2(q))
def KL(p, q):
return CE(p, q) - H(p)
p = np.array([0.7, 0.2, 0.1])
q = np.array([0.2, 0.3, 0.5])
print(f"H(p) = {H(p):.3f} bits | H(uniform over 3) = {H([1/3] * 3):.3f} bits")
print(f"CE(p, q) = {CE(p, q):.3f} = H(p) + KL(p||q) = {H(p):.3f} + {KL(p, q):.3f}")
print(f"KL(p||q) = {KL(p, q):.3f} | KL(q||p) = {KL(q, p):.3f}")
# information gain of a split on 100 regions (30 hot, 70 cold)
y_node = np.array([1] * 30 + [0] * 70)
left = np.array([1] * 27 + [0] * 8) # depuis_dernier < threshold
right = np.array([1] * 3 + [0] * 62)
Hb = lambda v: H([v.mean(), 1 - v.mean()])
gain = Hb(y_node) - (len(left) * Hb(left) + len(right) * Hb(right)) / len(y_node)
print(f"node entropy {Hb(y_node):.3f} | left {Hb(left):.3f} | right {Hb(right):.3f} | gain {gain:.3f} bits")
- bits, versus for the uniform distribution over 3 values (the maximum possible): is fairly predictable.
- : cross-entropy is indeed entropy plus KL.
- but : KL isn’t a symmetric distance.
- The split takes the entropy from bits (node) to (left) and (right): a gain of bits. The right branch (62 cold, 3 hot) is almost pure.
Hands-on exercise
- Bayes by hand. A detection test has 99% sensitivity and 99% specificity. If the condition affects 1 person in 1,000, what’s the probability of being affected when the test is positive? Check by simulation.
- Bernoulli MLE. For
observations in 0/1, write the log-likelihood of
and show that
is the proportion of 1s. Check with
minimize_scalar. - Laplace and MAE. Show that if the noise follows a Laplace distribution, the NLL is proportional to
. Add 5 outliers to
yand compare the weights obtained with MSE and with MAE (scipy.optimize.minimize). - KL isn’t symmetric. Find two distributions and over 2 values for which and differ by a factor of at least 3.
- Running project. On part 14’s dataset, compute the information gain of the split
depuis_dernier < sfor several thresholds . Which threshold maximizes the gain? Compare with the first split of aDecisionTreeClassifier(criterion="entropy", max_depth=1).
Hint for question 1: the answer is around 9%, not 99%.
Further resources
- Bishop, Pattern Recognition and Machine Learning, chapters 1 and 2: likelihood, MAP and Bayes applied to ML.
- Murphy, Probabilistic Machine Learning: An Introduction, chapters 4 and 6 (estimation and information theory), free online.
- 3Blue1Brown, Bayes theorem, the geometry of changing beliefs (video).
- Christopher Olah, Visual Information Theory (colah.github.io blog).
What you should be able to do before moving on
- Apply Bayes and explain the effect of the base rate
- Write a log-likelihood and find its maximum
- Derive MSE from Gaussian noise and the log-loss from a Bernoulli label
- Explain regularization as a Gaussian prior
- Compute entropy, cross-entropy and KL, and relate the three
- Compute the information gain of a split
Next part: M9 · Inferential statistics for comparing models. How to claim, with numbers and confidence intervals, that one model is better than another.