Least squares
Linear regression as a global model with one weight per feature — the normal equations, the fit as a projection, Gaussian noise and the standard errors of the weights, collinearity, R² and its traps, robust losses.
One formula for all the data
The topic Nearest neighbours predicts from nearby examples. Least squares instead fits one rule for the whole sample. Its prediction is ; a column of ones supplies an intercept. The design matrix contains the features, and contains the targets.
“Linear” means linear in the unknown weights. Columns may contain , or on its domain. Their values must be computable before choosing the weights. A weight inside does not meet this condition.
The objective is the average squared residual,
Multiplying it by a positive constant leaves its minimizer unchanged. Squared error has convenient geometry, but it also gives large residuals a large influence. We will connect the fitting rule, its assumptions and its validation.
The normal equations
Recall the normal equations from Mathematics for ML. Differentiating the residual sum of squares gives . Setting this to zero gives
If the columns are independent, is positive definite and the solution is unique. If columns are dependent, several weight vectors give the same fitted values. Having at least as many rows as columns is necessary for full column rank, but is not sufficient.
For four points , , fit an intercept and slope. Then
Solving gives . Residuals are , with sum of squares . These are a small explicit calculation, not a measured benchmark. The formula explains the result; a numerical implementation should normally factorize with QR or SVD.
The fit as a projection
The normal equations also say : the residual is orthogonal to every feature column. Predictions lie in the column space of , and the fit is the closest point in that space to . This orthogonality is in the space of all targets, not an angle between a residual segment and a line in an ordinary scatter plot.
For any alternative weights , Pythagoras gives
For the four-point data, the alternative has squared error . The extra term measures the distance between its predictions and the fitted predictions.
With independent columns, the projection matrix satisfies and . Its trace is , counting the intercept if present. With an intercept, the residuals sum to zero because they are orthogonal to the column of ones. These identities concern the fitting data, not arbitrary future observations.
Gaussian noise and standard errors
The topic Maximum likelihood supplies a statistical interpretation. If and the errors are independent , minimizing squared error maximizes the likelihood. Gaussian noise is not required just to solve least squares.
For a fixed full-rank design, makes the coefficients unbiased. The additional condition gives
Gaussian errors additionally give an exactly Gaussian coefficient distribution. Unequal or correlated errors invalidate this covariance formula, even if the conditional mean remains zero.
With , estimate by . The standard error of a coefficient is the square root of the corresponding diagonal of . For our four points, and the slope standard error is . This is uncertainty under repeated noise draws and the stated model, not a guarantee of future accuracy.
Collinearity and conditioning
If two columns are nearly equal, their sum is well observed but their difference is not. Large opposite weights can almost cancel in predictions. A small change in targets can then move individual coefficients substantially while predictions on similar inputs barely change. Such coefficients do not identify separate causal effects.
With exact duplicates, weights are not unique. SVD can choose a minimum-norm solution; this is a convention, not information supplied by the data. Ridge will add an explicit preference in Regularization.
There is a separate numerical issue. For full column rank,
Forming normal equations therefore worsens conditioning. Use a QR/SVD solver on rather than explicitly computing an inverse. Scaling can help when units differ, but it cannot remove exact dependence. Neither a more accurate solver nor more training iterations create information about a missing direction.
R² and its traps
For a nonconstant target, , where uses the mean on the evaluated sample. Negative means worse squared error than that constant. Unconstrained least squares fitted with an intercept on these same data has . This guarantee does not extend to a validation sample or every other fitted model.
Our original synthetic fixture has 240 rows and eight features. Independent standard-normal draws define , , for the remaining features, and
The noise is independent standard normal. Four auxiliary features have no true effect. Seed 20261113 fixes the dataset. It contains no observations of people or equipment.
Least squares reaches training and mean five-fold validation . On the same folds, standardized nearest neighbours reaches at the best tried . The generator deliberately favours an additive rule; this is not a universal model ranking. Folds are shuffled with seed 17 and preprocessing is fitted inside each training fold. Selecting on those folds still requires a separate test or nested evaluation for a final estimate.
Outliers and robust losses
Squared error chooses the conditional mean; absolute error chooses a conditional median. For a constant fitted to , these are and . A large target has a growing pull under squared error.
Huber's loss is quadratic near zero and linear outside a threshold:
Its derivative with respect to the residual is capped at in magnitude. But the weight gradient also multiplies this derivative by the feature vector. An extreme input can still have substantial influence. Robustness to unusual target values does not imply protection from high-leverage inputs.
Absolute and Huber objectives generally need numerical optimization. Their predictions estimate a different target or trade efficiency for robustness. Inspect residuals and the data-generating process before interpreting a better loss value as a better scientific explanation.
What changed and what comes next
We have a global prediction rule, a fitting equation and clear limits on uncertainty and validation. The closed form is useful for derivations, but forming costs and solving the dense system costs . A very large feature space makes this route expensive.
The next topic, Gradient descent, reaches a fit through steps. It changes how we optimize, not what the training data can establish. The optional notebook reproduces the four-point geometry and the original synthetic comparison; it does not count toward mastery.
This lesson is derived from the reviewed note, which retains primary references for regression, coefficient uncertainty and robust loss. The synthetic data, refreshed calculations and new checks were prepared for the Fedor Pakhurov project with Codex assistance.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Least squares: projection and validationoptional
Check normal equations and residual orthogonality, then compare training and validation on original synthetic data.
~20 min · easier on a computer · Least squares topic read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.