MoonSay /Tutorials/Machine Learning Interview #15
Contents
  1. 01Introduction
  2. 02From a line to a probability
  3. 03Maximum likelihood
  4. 04The push
  5. 05One bowl
  6. 06Interview follow-ups
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Logistic Regression/Chapter 03 of 09

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:

P(y_i\mid x_i;w)=p_i^{\,y_i}\,(1-p_i)^{1-y_i}. \tag{4}

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

\begin{aligned}J(w)&=-\log\prod_{i=1}^{n}p_i^{\,y_i}(1-p_i)^{1-y_i}\\&=-\sum_{i=1}^{n}\Bigl[y_i\log p_i+(1-y_i)\log(1-p_i)\Bigr]=\sum_{i=1}^{n}H_{\mathrm{ce}}(y_i,p_i),\end{aligned} \tag{5}

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.

Try itStep through the derivation: from likelihood to cross-entropy

Press Next step to go from the chance of the observed diagnoses to cross-entropy, one line at a time.

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

-\log\bigl(\text{probability given to the truth}\bigr)=\begin{cases}-\log p_i & \text{if malignant},\\ -\log(1-p_i) & \text{if benign}.\end{cases} \tag{6}

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.

(a) The loss of one tumor, -\log of the probability given to the true label, for a malignant (solid) and a benign tumor (dashed). (b) Two malignant tumors of Street et al. (1993) on the fitted curve of Figure 1b: a large one, to which the model gives p=0.99, and a small one, to which it gives p=0.02. Computed for this tutorial, with the same data as the companion video.
Figure 2. (a) The loss of one tumor, -\log of the probability given to the true label, for a malignant (solid) and a benign tumor (dashed). (b) Two malignant tumors of Street et al. (1993) on the fitted curve of Figure 1b: a large one, to which the model gives p=0.99, and a small one, to which it gives p=0.02. Computed for this tutorial, with the same data as the companion video.

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?