Toward the gradienta sum weighted by the residuals
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? → how good is the line, and when can we trust it? → what if the exact formula is out of reach?
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.
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
Least squares gives the same answer: \((X^\top X)^{-1}X^\top = X^{-1}\).
\(X^{+} = (X^\top X)^{-1}X^\top\) is the Moore–Penrose pseudo-inverse: what "inverse" becomes when the matrix is not square. Libraries never form it; they solve by QR or SVD.
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.
\(\sum e_i = 0\) and \(\sum e_i x_i = -0.6+1.2+2.4-4.0+1.0 = 0\). True for every OLS fit with an intercept.
They are the normal equations, one per parameter: the residual vector is perpendicular to every column of \(X\). Least squares is a right angle in disguise.
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)
The closed form costs \(O(d^3)\) and fails when two features are perfectly collinear. For very wide problems we switch to the iterative route (end of the lesson).
Stage 4 · how good is the line, and when can we trust it?
Residuals: what the model failed to explain
The residual plot: \(e_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.
Measuring quality: 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.
On the five points
Residuals: −0.6, 0.6, 0.8, −1.0, 0.2
MAE
0.64
3.2 / 5
MSE
0.48
2.40 / 5, in squared units
RMSE
0.69
\(\sqrt{0.48}\)
"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. Neither metric is right in the abstract: choose the one your problem's costs call for, before looking at the results.
The gradient is a sum weighted by the residuals: move each parameter in the direction the mistakes point, as much as they are big. Still true for a 100-layer network.
Simulator: walking down the loss surface
Predict. Run it, then switch center x on and run it again from the same kind of start. What changes in the path?
Why scaling comes before fitting
uncentered \(x\)
A long narrow valley. Changing the slope of a line whose \(x\) values are all far from zero also swings its height, so \(w_0\) and \(w_1\) are coupled. The gradient points across the valley: the path zigzags and creeps.
centered \(x\)
A round bowl. The gradient points at the bottom, and the same steps go almost straight to the answer.
Schematic: contour lines of \(J(w_0, w_1)\), the optimum (green) and the steps of gradient descent (orange).
The closed form gives the same line either way. The iterative route cares enormously: standardizing reshapes the surface the optimizer walks.
Summary
Regression to the mean is arithmetic, not a force.
Least squares = the reachable point closest to \(y\): \(X^\top X w = X^\top y\).
The residuals are orthogonal to the features; plot them.
MAE, RMSE, R²: different summaries of the same residuals; R² against \(\bar y\).
The gradient is a sum weighted by the residuals.
Next: Gradient Descent & Regularization, the step size, batches and penalties.