studies human height, and the word regression appears
Two centuries later it is still the first thing to try on any continuous target: fast, interpretable, and a baseline that is hard to beat.
The path of this part
Galton's puzzleregression to the mean; the slope is the correlation
The modelŷ = Xw; reading the coefficients; why not invert X
Least squaresminimize the squared residuals; the formulas by hand
Residualsresidual plots; outliers and leverage
Measuring qualityMAE, MSE, RMSE and R²
Each stage answers one question: why are the children of tall parents less tall? → what shape does a prediction take? → which line is the best one? → what did the line get wrong? → how good is the line, in one number?
Stage 1 · why are the children of tall parents less tall?
Galton's puzzle
Heights of 928 adult children and their parents.
He was looking for the strength of heredity.
Predict. A "mid-parent" (the average of the two parents) is 3 inches above the mean. How far above the mean do you expect the child to be: more than 3, exactly 3, or less?
The children come back toward the mean
Dashed line: identity (the child equals the parent). The fitted line is flatter: 3 inches above the mean in the parents → only about 2 in the child.
\(r_{xy}\): correlation between mid-parent \(x\) and child \(y\); \(s_x, s_y\): their standard deviations, in inches.
correlation \(r\)
no units; how tightly the points follow a line
slope \(\hat w_1\)
units of \(y\) per unit of \(x\); the same as \(r\) only when \(s_x = s_y\)
Why do children (2.52) vary more than mid-parents (1.80)? A mid-parent is the average of two people, and averaging shrinks spread: \(2.5/\sqrt{2} \approx 1.77\).
Simulator: regression to the mean
Both variables have the same spread here, so the slope is the correlation. Lower the correlation and watch the line flatten.
Regression to the mean is not a force
Regress parent on child: that line is flatter too. Children cannot pull their parents toward the mean.
It is arithmetic: a measurement is signal + noise. An extreme value had help from luck; the signal comes back, the luck does not.
tutoring
the worst students improve — they would have anyway
the magazine jinx
an exceptional season is followed by a normal one
the clinic
part of the drop in pressure is regression; only a control group separates it
3200: among apartments of the same age, one extra m² goes with R$ 3200 more.
It is not "bigger apartments cost 3200 more per m²": if newer buildings are larger, the raw relation mixes both effects.
Coefficients are not importances. Measure area in km² and its coefficient grows a million times, with the same model. Standardize first to compare them.
Predict. \(\hat y = Xw\) looks like a linear system. Why not just compute \(w = X^{-1}y\)?
\(y\) points somewhere the columns of \(X\) cannot reach: no line passes through the three points. That is the normal situation, and the reason regression exists.
What we do instead: the closest reachable point
Minimize \(\lVert y - Xw\rVert^2\), the sum of squared residuals. Setting its gradient to zero gives the normal equations:
\(\bar x, \bar y\): the means of \(x\) and \(y\). The numerator adds, point by point, how far \(x\) and \(y\) are from their means, multiplied; the denominator, the squared distances of \(x\).
The second formula says the fitted line always passes through \((\bar x, \bar y)\): it pivots about the center of the data.
Least squares by hand, step by step
Predict. Five points: (1, 2), (2, 4), (3, 5), (4, 4), (5, 6). Which point adds nothing to the slope? Then step through the table, the slope, the intercept and the residuals.
In scikit-learn
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(X_train, y_train)
model.coef_, model.intercept_
y_pred = model.predict(X_test)
One call does the whole table. The lab opens it: the matrix form, the failed inverse and the normal equations, with NumPy, on the same five points.
Stage 4 · what did the line get wrong?
Residuals: what the model failed to explain
The residual plot: \(e_i = y_i - \hat y_i\) against \(\hat y_i\).
A healthy one looks like nothing at all: a band around zero.
Any pattern says which assumption broke.
Simulator: diagnose before reading the verdict
Predict. For each dataset, look only at the residual plot and decide: fine, curve, funnel or a dangerous point?
Reading a residual plot
what you see
what it means
what to do
structureless band
the linear form fits
nothing
U or ∩ curve
a nonlinear relationship
polynomial terms, or transform \(x\)
funnel
heteroscedasticity: noise grows with \(\hat y\)
transform \(y\) (often log)
isolated extreme point
outlier or high leverage
investigate it; never delete it silently
waves over the index
correlated errors (time)
a time-series model
Outlier ≠ leverage. A point far along \(x\) can drag the line so well that its own residual ends up small. The worst offenders do not have big residuals.
Stage 5 · how good is the line, in one number?
Four ways to summarize the residuals
metric
formula
units
MAE
\(\frac{1}{n}\sum \lvert e_i \rvert\)
of \(y\)
MSE
\(\frac{1}{n}\sum e_i^2\)
\(y\) squared
RMSE
\(\sqrt{\text{MSE}}\)
of \(y\)
R²
\(1 - \frac{\sum e_i^2}{\sum (y_i - \bar{y})^2}\)
none
The name is the recipe, read right to left:
RMSE = Error → Squared → Mean → Root.
Mean Absolute Error, Mean Squared Error, Root Mean Squared Error, coefficient of determination.
\(\text{MSE} = \frac{0.36+0.36+0.64+1.00+0.04}{5} = \frac{2.40}{5} = \mathbf{0.48}\) in squared units of \(y\)
\(\text{RMSE} = \sqrt{0.48} = \mathbf{0.69}\)
"The model misses by about 0.7" is a sentence you can say out loud. That is why RMSE exists: it brings the error back to the scale of \(y\).
MAE or RMSE?
Predict. Model A misses by 2, 2, 2, 2, 2. Model B misses by 0, 0, 0, 0, 10. Which one is better?
errors
MAE
RMSE
Model A
2, 2, 2, 2, 2
2.0
2.0
Model B
0, 0, 0, 0, 10
2.0
4.47
RMSE squares before averaging: the single 10 contributes 100, and \(\sqrt{100/5} = 4.47\). Neither metric is right in the abstract: choose the one your problem's costs call for, before looking at the results.
SST (total sum of squares): the error of always predicting \(\bar y\). SSE (sum of squared errors): what the line still gets wrong.
Five points: \(R^2 = 1 - 2.40/8.80 = 0.727\), and \(r = 0.853\), \(0.853^2 = 0.727\).
R² can be negative on test data: worse than the flat line. Always report the constant baseline (DummyRegressor), and evaluate on held-out data.
Summary
Regression to the mean is arithmetic, not a force: the slope is \(r_{xy}\,s_y/s_x\).
A coefficient is the effect of its feature with the others fixed; it is not an importance.
\(X\) cannot be inverted; least squares takes the reachable point closest to \(y\): \(X^\top X w = X^\top y\). The line passes through \((\bar x, \bar y)\).
The residuals sum to zero and are orthogonal to the features; plot them.
MAE, RMSE, R²: different summaries of the same residuals, meaningful on held-out data.
Next, the advanced approach: when the coefficients can be trusted, what breaks the exact formula, and the gradient that replaces it.
Fundamental lab · about 30 min, in pairs
Five points, from scratch: steps 1 to 4
Only NumPy: the matrix form, the inverse that fails, the normal equations, the residuals.
The same five points as the hand calculation: every output can be checked on paper.
Notebook on Colab (in Portuguese), sections 1 to 4: nothing to download. Save a copy to your Drive.
At every step, answer the Predictbefore running.
Simulations: your instinct, the special case (step 3) and the point that turns the line (step 4).
Pace: steps 1 and 2 in 5 min; step 3 and the first simulation in 10 min; step 4 and the second in 15 min.