Skip to content
mlmentorship

Linear regression

Predict a continuous target as a linear combination of features by minimizing squared error. Closed-form solution, MLE under Gaussian noise, and the foundation everything else builds on.

Published · 4 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

See ordinary least squares as projecting the observed target vector onto the column space of the design matrix, leaving a residual perpendicular to every feature direction.

Preparing the visual…

Summary

Linear regression models with . The MLE / least-squares estimator is

Linear regression is the most-analyzed model in statistics and the building block for almost everything: GLMs, kernel ridge regression, MLP last layers, factor models. Knowing its assumptions and failure modes is essential. If you don’t know when OLS is wrong, you don’t know when fancier models help.

Ordinary least squares (OLS)

Loss: (absorb into by adding a column of 1s).

Closed-form minimizer (when is invertible):

This is the normal equations solution. Derived by setting .

Learning objective

See why the least-squares residual must be perpendicular to the feature space.

Ordinary least squares as an orthogonal projection A slanted plane represents the column space of the design matrix X. From the origin, the fitted vector y-hat ends on that plane. The observed target vector y ends above it. A dashed residual vector connects y-hat to y and meets the plane at a marked right angle. This perpendicular residual satisfies X transpose r equals zero. Col(X) all possible fitted vectors Xw origin fitted y_hat = Xw_hat observed y residual r y - y_hat r is perpendicular to Col(X) X^T r = 0
Read it this way: every coefficient vector produces a fitted vector inside Col(X). OLS chooses the point y_hat closest to the observed y, so the leftover residual meets that space at a right angle. Writing that orthogonality as XT(y - Xw_hat) = 0 gives the normal equations. This is the geometry of the solution, not advice to form an inverse numerically: use QR or SVD as described below. Original schematic checked against MIT OpenCourseWare's projection notes and the NIST least-squares handbook.

Numerically, never compute it that way. Use:

  • QR decomposition: then . Stable.
  • SVD: works even when is singular (gives minimum-norm solution).
  • Gradient descent: for huge where matrix factorization doesn’t fit.

Probabilistic interpretation

OLS is MLE for . The Gaussian-noise assumption is what motivates the squared-error loss; if errors are heavy-tailed, OLS is no longer optimal (consider Huber loss or quantile regression).

Ridge and lasso

When is ill-conditioned (collinear features, ), OLS variance explodes. Add regularization:

MethodPenaltyEffect
Ridge (L2)Shrinks all coefficients toward 0; closed form:
Lasso (L1)Drives some coefficients to exactly 0 (sparsity); no closed form (use coordinate descent / proximal gradient)
Elastic netbothCombines sparsity with grouping

Ridge is the default for prediction; lasso for variable selection or interpretability.

Assumptions and diagnostics

The classical OLS assumptions:

  1. Linearity: is linear in .
  2. Independence: residuals are independent.
  3. Homoskedasticity: residual variance is constant.
  4. Normality: residuals are normally distributed (only matters for inference, not for prediction).

Check by plotting residuals: vs. predicted (linearity, homoskedasticity), vs. each feature (linearity), QQ plot (normality), Durbin-Watson (independence in time series).

Gauss–Markov theorem

Under the first three assumptions (with finite variance), OLS is the Best Linear Unbiased Estimator (BLUE). Minimum variance among all linear unbiased estimators. Note: biased estimators (ridge) can do better in MSE.

When to use vs. alternatives

  • Linear is enough: low-dimensional clean data, interpretability needed.
  • Non-linear: add basis functions (polynomial features), kernels (kernel ridge regression), or use trees / neural nets.
  • Heavy-tailed errors: Huber regression, quantile regression.
  • Many irrelevant features: lasso or elastic net.
  • Hierarchical / clustered data: mixed-effects (linear mixed model).

Common pitfalls

  • Inverting directly. Use QR / SVD; the inverse is numerically unstable and ridge-style regularization is often needed.
  • Including highly collinear features without regularization. Coefficients become unstable and uninterpretable; drop one or use ridge.
  • Reporting on training data and calling it generalization. Use cross-validated .
  • Confusing prediction intervals with confidence intervals. Confidence intervals cover the mean; prediction intervals cover individual outcomes (much wider).