Contents
03Maximum likelihood
Chapter 2 left us without a way to choose the weights. Maximum likelihood (Episode 5) answers: pick the weights that make the diagnoses we actually observed most likely.
3.1Each tumor is a coin flip
The model says tumor i is malignant with probability p_i=\sigma(w^\top x_i), like a coin that lands “malignant” with probability p_i. The chance of the label we saw is p_i if the tumor is malignant and 1-p_i if it is benign. Both cases fit in one formula, because a power of 0 is 1:
For y_i=1 this is p_i; for y_i=0 it is 1-p_i. Assuming the tumors are independent, the likelihood of all the labels is the product, and maximizing it is the same as minimizing minus its logarithm.
Proposition 1 (Maximum likelihood is cross-entropy, Murphy, 2022, eq. 10.6–10.10). The negative log-likelihood of logistic regression is
where H_{\mathrm{ce}}(y,p)=-[y\log p+(1-y)\log(1-p)] is the binary cross-entropy between the true label and the predicted probability.
The logarithm turns the product into a sum: \log\prod_ia_i=\sum_i\log a_i. The logarithm of each factor is \log\bigl(p_i^{y_i}(1-p_i)^{1-y_i}\bigr)=y_i\log p_i+(1-y_i)\log(1-p_i). Change the sign. Each bracket is the cross-entropy of Episode 8 between the distribution (y_i,1-y_i), which puts all its mass on the true label, and the model’s distribution (p_i,1-p_i).
Since the logarithm is increasing, the weights that minimize J are exactly the weights that maximize the likelihood. Dividing by n changes nothing but the scale; the average, J/n, is the number usually reported (“log loss”).
3.2What each tumor pays
Only one of the two terms in each bracket is ever switched on, so each tumor pays
Figure 2a draws both cases. A correct and confident prediction costs almost nothing; an unsure one, p=\frac12, costs \log2=0.69 whatever the truth; and a confident mistake costs without limit, since -\log p\to\infty as p\to0.
Worked example 2 (A cheap and an expensive tumor). Both tumors in Figure 2b are malignant. The large one, radius 19.19, gets p=0.99 and pays -\log0.99=0.01. The small one, radius 10.95 (the smallest malignant tumor in the data), gets p=0.02 and pays -\log0.02=3.9, almost 400 times more. Averaged over all 569 tumors, the fitted model pays 0.29.
Unlike least squares, setting the gradient of (5) to zero does not give a closed form: the equations X^\top(\sigma(Xw)-y)=0 (derived in Chapter 4) are nonlinear in w. The weights are found iteratively, by gradient descent or by Newton’s method (Chapter 5).
Pause and think. A model that ignores the input and predicts p=\frac12 for every tumor: what average cross-entropy does it pay? What does a model pay that predicts the share of malignant tumors, 212/569, for every tumor?
Show the answer
The first pays \log2=0.69 per tumor, whatever the labels. The second pays the entropy of the labels, -[0.373\log0.373+0.627\log0.627]=0.66. Using the radius brings it down to 0.29; this comparison is the basis of the “pseudo-R^2” measures for logistic regression.
So cross-entropy is not a convention: it is the likelihood. But squared error, (p_i-y_i)^2, also punishes mistakes. Why not keep it?