Contents
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.
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)
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:
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
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?