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 02 of 09

02From a line to a probability

The answer is yes or no, but the model should say how likely “yes” is. Can the least squares of Episode 14 do it?

2.1A line is not a probability

Figure 1a plots the diagnosis against the mean nucleus radius: each dot is one tumor, at height 1 if malignant and 0 if benign. Large nuclei are mostly malignant. Fit a least-squares line to these zeros and ones, and read its height as a probability. The line predicts -0.34 for the smallest nuclei and 1.77 for the largest: no probability is negative or above one. The trouble is built in. A line rises forever, so for inputs far enough from the middle it always leaves [0,1]; and least squares tries to pass close to every point, so tumors with extreme radii pull the line, although they are the easiest to classify.

The 569 tumors of Street et al. (1993): diagnosis (1 malignant, 0 benign; dots shifted up or down slightly so they do not overlap) against the mean nucleus radius. (a) A least-squares line leaves [0,1] (shaded). (b) The sigmoid of a linear score, fitted by maximum likelihood (Chapter 3), stays in (0,1). Computed for this tutorial, with the same data as the companion video.
Figure 1. The 569 tumors of Street et al. (1993): diagnosis (1 malignant, 0 benign; dots shifted up or down slightly so they do not overlap) against the mean nucleus radius. (a) A least-squares line leaves [0,1] (shaded). (b) The sigmoid of a linear score, fitted by maximum likelihood (Chapter 3), stays in (0,1). Computed for this tutorial, with the same data as the companion video.
Try itFit the curve by hand

The 569 tumors of Figure 1. Choose where the curve crosses p = 0.5 and how steep it is (the slope w1). The page adds up what every tumor pays, the average cross-entropy of Chapter 3, and counts how many tumors land on the right side of 0.5.

2.2Squash a linear score

The fix keeps what is good about the line, a linear score z=w_0+w_1x, and squashes it into (0,1) with the sigmoid (logistic function)

\sigma(z)=\frac{1}{1+e^{-z}},\qquad p=P(y=1\mid x)=\sigma(w^\top x). \tag{1}

A very negative score gives p near 0, a score of 0 gives exactly \frac12, a very positive score gives p near 1 (Figure 1b), and the curve is symmetric: \sigma(-z)=1-\sigma(z). Two facts about the sigmoid do all the work in the rest of the tutorial. The first is its slope, which follows from the quotient rule:

\sigma'(z)=\frac{e^{-z}}{(1+e^{-z})^2}=\frac{1}{1+e^{-z}}\cdot\frac{e^{-z}}{1+e^{-z}}=\sigma(z)\bigl(1-\sigma(z)\bigr). \tag{2}

The slope is largest, \frac14, at p=\frac12, and close to zero wherever p is close to 0 or 1: there the curve is flat. The second fact is what the score means. Solve (1) for z: p(1+e^{-z})=1 gives e^{-z}=(1-p)/p, so

\log\frac{p}{1-p}=w^\top x=w_0+w_1x_1+\dots+w_dx_d . \tag{3}

The ratio p/(1-p) is the odds (a probability of 0.8 is odds of 4 to 1), and its logarithm, the log-odds or logit, is linear in the inputs (Murphy, 2022, eq. 10.2). That is why the method is called a regression: it is a linear regression for the log-odds. It also gives each weight a meaning. Raising x_j by one unit adds w_j to the log-odds, so it multiplies the odds by e^{w_j}, the odds ratio.

Worked example 1 (Odds of malignancy). Fitted by maximum likelihood (Chapter 3), our model is p=\sigma(-15.25+1.034\,x). Each extra unit of mean nucleus radius multiplies the odds of malignancy by e^{1.034}=2.8. The score is zero, so p=\frac12, at radius 15.25/1.034=14.75: tumors with larger nuclei are called malignant, smaller ones benign, and this simple rule is right for 87.9\% of the tumors.

Pause and think. Why not keep the least-squares line and simply clip its predictions to [0,1]?

Show the answer

The clipped line is still fitted to make the squared distances to 0 and 1 small, so tumors with extreme radii still pull it, and everywhere beyond the clip points it says “probability 0” or “1” with total certainty. The sigmoid never reaches 0 or 1, so it can always express doubt, and, as the next chapters show, it comes with a loss that has a principled meaning and a clean gradient.

The formula (1) gives the shape of the model, but not its weights. Which weights should we choose? Which loss should we minimize?