Historical Context & Motivation
The problem of fitting a line—or more generally, a linear model—to observed data is among the oldest and most consequential in the mathematical sciences. Long before the formal apparatus of statistical inference existed, astronomers and surveyors grappled with the practical reality that measurements are noisy: multiple observations of the same celestial position or geographic angle yield slightly different values. The method of least squares arose as a principled answer to the question of how to reconcile conflicting observations into a single best estimate. Its development is intertwined with the birth of modern statistics, and its mathematical elegance has ensured its place at the center of regression analysis, signal processing, and machine learning to this day.
The central question that motivates this entire topic is deceptively simple: given a set of observations and a linear model that cannot perfectly reproduce them all, how should we choose the model parameters to obtain the "best" approximation? The least squares criterion offers a compelling answer by minimizing the sum of squared deviations between observed and predicted values, and the normal equations provide the algebraic machinery that translates this optimization into a system of linear equations with a unique closed-form solution.
Core Principles & Definitions
Before diving into the algebra, it is essential to establish the conceptual pillars upon which least squares estimation rests. The framework begins with a linear model that relates an n × 1 response vector y to an n × p design matrix X and a p × 1 parameter vector β through the equation y = Xβ + ε, where ε captures random error. The overarching goal is to find the estimate β̂ that makes the model's predictions as close to the observed data as possible in the Euclidean sense.
The Linear Model
The Least Squares Criterion
The Normal Equations
Geometric Interpretation
Gauss–Markov Optimality
Geometric Visualization of Least Squares Projection
The deepest insight into least squares estimation comes from viewing it as an orthogonal projection. The diagram below represents this geometric picture in three dimensions. The column space C(X) is depicted as a two-dimensional plane (representing a p-dimensional subspace in general). The response vector y lives in ℝⁿ and typically does not lie on this plane. The OLS fitted value ŷ = Xβ̂ is the foot of the perpendicular from y to the plane, and the residual vector e = y − ŷ is orthogonal to every vector in C(X). This orthogonality condition is exactly what the normal equations XᵀXβ̂ = Xᵀy encode.
This geometric perspective clarifies several facts simultaneously. First, because ŷ is a linear combination of the columns of X, it lives in C(X) by definition. Second, the orthogonality condition Xᵀ(y − Xβ̂) = 0 guarantees that no further movement within C(X) can reduce ‖y − Xβ‖². Third, the hat matrix H = X(XᵀX)⁻¹Xᵀ is the orthogonal projection matrix onto C(X), satisfying H² = H, Hᵀ = H, and ŷ = Hy. The Pythagorean theorem in ℝⁿ gives the fundamental decomposition ‖y‖² = ‖ŷ‖² + ‖e‖², which underlies the analysis of variance (ANOVA) decomposition in regression.
Mathematical Framework — Derivation of the Normal Equations
We now derive the normal equations rigorously from the calculus-based minimization of the residual sum of squares. Consider the model y = Xβ + ε where X has full column rank p. The objective function is a scalar-valued quadratic form in the parameter vector β.
Differentiating S(β) with respect to the vector β using standard matrix calculus identities, we obtain the gradient. Recall that ∂(βᵀa)/∂β = a and ∂(βᵀAβ)/∂β = 2Aβ when A is symmetric. Setting the gradient to the zero vector yields the normal equations.
To confirm that this critical point is indeed a minimum, we examine the Hessian matrix ∂²S/∂β∂βᵀ = 2XᵀX. Since X has full column rank, XᵀX is positive definite, which means S(β) is strictly convex, and the critical point β̂ is the unique global minimizer. This is a powerful result: there is no concern about local minima or saddle points in ordinary least squares.
Detailed Breakdown — The Hat Matrix and ANOVA Decomposition
Once the OLS estimator β̂ = (XᵀX)⁻¹Xᵀy is obtained, several important derived quantities illuminate the structure of the fit. The hat matrix H = X(XᵀX)⁻¹Xᵀ maps the observed response y to the fitted values ŷ = Hy. It is idempotent (H² = H), symmetric (Hᵀ = H), and has trace equal to p, the number of parameters. Similarly, the residual-forming matrix I − H projects y onto the orthogonal complement of C(X), producing the residuals e = (I − H)y. The trace of I − H is n − p, which explains why the unbiased estimator of σ² divides by n − p rather than n.
This decomposition has a beautiful Pythagorean structure. The vectors ŷ − ȳ1 and e lie in orthogonal subspaces, so their squared norms add. The coefficient of determination R² = SSR/SST is the cosine squared of the angle between y − ȳ1 and ŷ − ȳ1 in ℝⁿ. When R² = 1, y lies entirely in the column space (perfect fit); when R² = 0, the fitted values provide no improvement over the grand mean ȳ.
| Quantity | Formula | Properties |
|---|---|---|
| Hat Matrix H | X(XᵀX)⁻¹Xᵀ | Symmetric, idempotent, tr(H) = p, eigenvalues 0 or 1 |
| Fitted Values ŷ | Hy = Xβ̂ | Orthogonal projection of y onto C(X) |
| Residuals e | (I − H)y | Xᵀe = 0; E[e] = 0; Cov(e) = σ²(I − H) |
| σ² estimator s² | eᵀe / (n − p) | Unbiased: E[s²] = σ²; denominator from tr(I − H) |
Worked Example — Simple Linear Regression via Normal Equations
Consider a simple linear regression with n = 4 observations. We model a response y as a function of a single predictor x: yᵢ = β₀ + β₁xᵢ + εᵢ. The data are (x, y) = {(1, 2), (2, 3), (3, 5), (4, 4)}. We will construct the design matrix, form the normal equations, solve for β̂, and compute the residuals.
Strengths, Limitations & Computational Considerations
Ordinary least squares via the normal equations is remarkably powerful, but no estimator is universally optimal. Understanding its strengths and weaknesses guides practitioners in selecting appropriate methods for their data.
| Strengths | Limitations |
|---|---|
| Closed-form solution β̂ = (XᵀX)⁻¹Xᵀy — no iterative optimization required. | Requires XᵀX to be invertible (full column rank). Fails when p > n or under perfect multicollinearity. |
| BLUE under Gauss–Markov conditions — optimal among all linear unbiased estimators. | Sensitive to outliers: squared loss magnifies the effect of extreme residuals. |
| Geometric transparency — projection interpretation provides deep intuition. | Forming XᵀX can worsen conditioning: cond(XᵀX) = cond(X)². QR or SVD approaches are numerically superior. |
| Under normality of ε, OLS = MLE, and exact t- and F-tests are available. | Assumes homoscedasticity and uncorrelated errors. Violations require GLS, robust, or sandwich methods. |
| Foundation for extensions: WLS, GLS, ridge, LASSO, and GLMs all build on the OLS framework. | Unbiasedness comes at the cost of potentially high variance when predictors are nearly collinear — the bias-variance tradeoff favors regularized alternatives. |
lm() function and Python's numpy.linalg.lstsq().Connection to Generalized Least Squares and GLMs
Ordinary least squares is the simplest member of a family of estimation methods. When the assumption Cov(ε) = σ²I is violated — due to heteroscedasticity or correlated errors — the generalized least squares (GLS) estimator replaces the identity with a known positive definite covariance matrix Ω, yielding β̂_GLS = (XᵀΩ⁻¹X)⁻¹XᵀΩ⁻¹y. This can be understood as OLS applied to a transformed model where both sides are premultiplied by Ω^(−1/2). Moving further from linearity, generalized linear models (GLMs) extend the framework by allowing non-Gaussian response distributions and a nonlinear link function g(μ) = Xβ. In GLMs, the parameter estimation is performed via iteratively reweighted least squares (IRLS), where each iteration solves a weighted normal equation with a weight matrix that depends on the current parameter estimates.
| Feature | OLS (Normal Equations) | GLS | GLM (IRLS) |
|---|---|---|---|
| Error distribution | ε ~ (0, σ²I) — any distribution with constant variance | ε ~ (0, σ²Ω) — known covariance structure | y ~ Exponential family (Bernoulli, Poisson, Gamma, etc.) |
| Normal equations | XᵀXβ̂ = Xᵀy | XᵀΩ⁻¹Xβ̂ = XᵀΩ⁻¹y | XᵀW⁽ᵏ⁾Xβ⁽ᵏ⁺¹⁾ = XᵀW⁽ᵏ⁾z⁽ᵏ⁾ (iterative) |
| Link function | Identity: E[y] = Xβ | Identity: E[y] = Xβ | g(E[y]) = Xβ (logit, log, inverse, etc.) |
| Solution type | Closed-form | Closed-form (if Ω known) | Iterative convergence to MLE |
| Optimality | BLUE (Gauss–Markov) | BLUE under general Ω (Aitken theorem) | Asymptotically efficient (MLE properties) |
The thread connecting all three frameworks is the idea of projection. OLS projects onto C(X) using the standard inner product. GLS uses a weighted inner product defined by Ω⁻¹. IRLS projects onto a linearized version of the mean function at each iteration, converging to the maximum likelihood solution. Mastering the OLS normal equations is therefore not merely an exercise in introductory regression—it builds the conceptual foundation for the entire generalized linear modeling framework that dominates modern applied statistics.
Practice Problems
Lesson Summary
The method of least squares estimates the parameter vector β in the linear model y = Xβ + ε by minimizing the residual sum of squares S(β) = ‖y − Xβ‖². Setting the gradient to zero produces the normal equations XᵀXβ̂ = Xᵀy, which have the unique closed-form solution β̂ = (XᵀX)⁻¹Xᵀy when X has full column rank. Geometrically, the fitted vector ŷ = Xβ̂ is the orthogonal projection of y onto the column space of X, and the residual e = y − ŷ is perpendicular to every column of X — the very condition encoded by the normal equations.
Under the Gauss–Markov assumptions (E[ε] = 0, Cov(ε) = σ²I), the OLS estimator is BLUE — the Best Linear Unbiased Estimator. Its covariance matrix is Cov(β̂) = σ²(XᵀX)⁻¹, and the unbiased estimate of σ² is s² = eᵀe/(n − p). The hat matrix H = X(XᵀX)⁻¹Xᵀ (idempotent, symmetric, with trace p) drives the ANOVA decomposition SST = SSR + SSE. This OLS framework generalizes to GLS (weighted inner products) and GLMs (iteratively reweighted least squares), making it the conceptual bedrock of modern regression analysis.