Sub topic 2
Why a straight line is the wrong tool for labels
This chapter assumes you know ordinary least squares linear regression, the idea of a loss function, and basic NumPy. It also assumes you know that a probability lies between 0 and 1. The goal is to see exactly where linear regression breaks when the target is a class label. That gives you the requirements for a better model.
The tempting shortcut
Suppose you want to predict a binary label. Encode the classes as numbers: 0 for "negative" and 1 for "positive". Then fit a straight line by least squares and predict the positive class when the output is at least 0.5. Applying linear regression this way is called the linear probability model (LPM).
The shortcut is not absurd. For a 0/1 target, the true conditional mean is a probability:
E[y | x] = P(y = 1 | x)
Linear regression estimates a conditional mean, so the line is trying to estimate a probability. The trouble is that a line has no reason to respect what a probability is. It breaks in four ways.
Failure 1: outputs outside [0, 1]
A line keeps going. If the fitted slope is positive, then a large enough x gives a prediction above 1, and a small enough x gives one below 0. A "probability" of 1.4 or -0.2 has no meaning.
You can still threshold at 0.5 and get labels. But you lose any use of the output as a probability. That matters for ranking, for risk estimates, and for choosing thresholds based on cost.
Failure 2: the boundary moves for the wrong reasons
Least squares treats every point as pulling the line toward it. A point that is clearly in the positive class, far from the boundary, still pulls the line. The squared error on that point is large, because the line's value there is well below 1 or well above it. That pull drags the line, and the 0.5 crossing moves with it, even though the point was already classified correctly.
The code below fits a line to one-dimensional data, then adds a few extreme positive points. Those points are easy to classify, yet the boundary shifts.
import numpy as np
rng = np.random.default_rng(0)
x = np.concatenate([rng.normal(-1, 1, 50), rng.normal(1, 1, 50)])
y = np.array([0] * 50 + [1] * 50)
def fit_line(x, y):
slope, intercept = np.polyfit(x, y, 1)
return slope, intercept
def boundary(slope, intercept):
# x where the fitted line equals 0.5
return (0.5 - intercept) / slope
slope, intercept = fit_line(x, y)
print("range of fitted values:",
(slope * x + intercept).min().round(2),
(slope * x + intercept).max().round(2))
print("boundary before:", round(boundary(slope, intercept), 3))
# Ten extra positive examples, far from the boundary
x_extra = np.full(10, 15.0)
y_extra = np.ones(10, dtype=int)
x2 = np.concatenate([x, x_extra])
y2 = np.concatenate([y, y_extra])
slope2, intercept2 = fit_line(x2, y2)
print("boundary after: ", round(boundary(slope2, intercept2), 3))The first print line shows fitted values that can fall outside 0 to 1 even on the training data. The two boundary lines show the shift. Every added point sits at x = 15 and is labelled 1, so a sensible classifier would still label all of them 1. The line does not care about that. It only cares about squared distance to 1.
A model that predicts probabilities should flatten out for extreme inputs. Once a point is confidently positive, pushing it further out should change little.
Failure 3: the error assumptions do not hold
Linear regression's usual inference assumes the errors have roughly constant variance and are roughly normal. With a binary target, neither holds.
For a binary outcome with probability p = P(y = 1 | x), the variance of y given x is:
Var(y | x) = p(1 - p)
This depends on x, so the errors are heteroscedastic: their spread changes with the input. The variance is largest where p is near 0.5 and shrinks near 0 and 1.
The residual at each point can take only two values. If the line predicts ŷ, the residual is either 1 - ŷ or -ŷ. That is two clusters, not a bell curve. Standard errors and p-values from ordinary least squares are therefore unreliable here.
The predictions themselves can still be useful. It is the inference that suffers, and the fitting is also statistically inefficient compared with a model that accounts for the variance.
Failure 4: the wrong loss for the job
Squared error measures distance on a number line. A class label is not a position on a number line. It is an outcome with a probability attached.
Consider a positive example where the model gives 0.99, and another where it gives 0.01. Squared error penalizes the second heavily, but only up to a cap of 1. A model that is confidently wrong deserves a much harsher penalty than that, and the log loss introduced later in this tutorial supplies one: it grows without bound as a confident prediction goes wrong.
| Property | Linear regression on 0/1 labels | What a classifier needs |
|---|---|---|
| Output range | Any real number | Between 0 and 1 |
| Effect of far, easy points | Shift the boundary | Little or no effect |
| Error variance | Depends on x | Modelled explicitly as p(1 - p) |
| Loss | Squared error | A loss based on probabilities |
When the line is still acceptable
The linear probability model is not always wrong. Applied researchers, especially in economics, use it deliberately when:
- Predictions stay well inside (0, 1), because the features have a narrow range.
- The goal is an average effect of a feature on the probability, and a constant slope is an acceptable summary.
- Robust standard errors are used to deal with the heteroscedasticity.
If your goal is prediction, calibrated probabilities, or ranking, these arguments do not help. You want a model that guarantees valid probabilities by construction.
What a better model must do
The failures give us a list of requirements:
- Map any real-valued score to a number strictly between 0 and 1.
- Flatten out for very large or very small scores, so easy points stop pulling on the fit.
- Keep the score linear in the features, so the model stays simple and interpretable.
- Train with a loss that comes from the probabilities themselves.
The next chapters build exactly this. First we change what the linear score represents, from a probability to the log-odds of the positive class. Then we pass the score through the sigmoid function to get a valid probability.
Exercise
A line fitted to 0/1 labels has the form ŷ = 0.2 + 0.1x. For what values of x does it predict a "probability" outside [0, 1]? Also, why does adding a correctly classified positive example at a very large x still change the fitted line?
Show answer
ŷ exceeds 1 when 0.2 + 0.1x > 1, so x > 8. It drops below 0 when 0.2 + 0.1x < 0, so x < -2. Adding a positive example at a very large x changes the line because least squares minimizes squared distance to the label 1. The line's value at that x is far above 1, so the point has a large squared error even though it is correctly classified, and the fit moves to reduce that error. This shifts the 0.5 crossing, and with it the decision boundary.