Linear Regression

Volume II, Chapter 4 — Part I. A complete theoretical treatment of linear regression: the probabilistic model, least-squares derivation, normal equations, geometric interpretation as orthogonal projection, regularization theory, and the bias–variance connection.

Beginner

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. The Linear Model and Notation
  6. Least Squares from Maximum Likelihood
  7. The Normal Equations: Derivation
  8. Geometric Interpretation: Orthogonal Projection
  9. Existence, Uniqueness, and Rank Conditions
  10. Gradient Descent for Linear Regression
  11. Regularization: Ridge and Lasso
  12. The Bias–Variance Decomposition
  13. Statistical Properties of the OLS Estimator
  14. Worked Examples
  15. Connection to the Broader Curriculum
  16. Common Pitfalls and Misconceptions
  17. Research Perspective
  18. Summary of Takeaways
  19. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. State the linear regression model in scalar, vector, and matrix form, and explain the role of the design matrix X\mathbf{X}.
  2. Derive the least-squares objective from a Gaussian noise model via maximum likelihood.
  3. Prove the normal equations XTXθ^=XTy\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\theta}} = \mathbf{X}^T\mathbf{y} and solve for θ^\hat{\boldsymbol{\theta}} when the Gram matrix is invertible.
  4. Interpret ordinary least squares (OLS) as orthogonal projection of y\mathbf{y} onto the column space of X\mathbf{X}.
  5. Characterize when the OLS solution is unique in terms of rank and linear independence of features.
  6. Derive the gradient descent update for linear regression and connect it to the condition number of XTX\mathbf{X}^T\mathbf{X}.
  7. Derive closed-form Ridge regression and explain why λI\lambda \mathbf{I} restores invertibility.
  8. State the bias–variance decomposition for squared error and identify the sources of each term.

Prerequisites

This chapter assumes familiarity with:

Optional but helpful: Positive Definite Matrices for regularization theory, and Maximum Likelihood Estimation for the probabilistic derivation.


Notation

  • XRN×(d+1)\mathbf{X} \in \mathbb{R}^{N \times (d+1)} — Design matrix
  • yRN\mathbf{y} \in \mathbb{R}^N — Target vector
  • θRd+1\boldsymbol{\theta} \in \mathbb{R}^{d+1} — Parameter vector (including bias)
  • x~i\tilde{\mathbf{x}}_i — Augmented feature vector with leading 1
  • θ^OLS\hat{\boldsymbol{\theta}}_{\mathrm{OLS}} — Ordinary least squares estimator
  • λ\lambda — Regularization strength (Ridge/Lasso)

Core Intuition

Linear regression is the simplest supervised learning model — and the most important. Every neural network layer begins as a linear map; attention computes linear combinations; diffusion models predict noise through linear projections in latent space. Understanding linear regression deeply means understanding the template that all of supervised learning follows: specify a model, define a loss, optimize.

The question is deceptively simple. Given pairs (xi,yi)(\mathbf{x}_i, y_i) for i=1,,Ni = 1, \ldots, N, find parameters θ\boldsymbol{\theta} such that predictions y^i=f(xi;θ)\hat{y}_i = f(\mathbf{x}_i; \boldsymbol{\theta}) are close to observed targets yiy_i. When ff is linear in θ\boldsymbol{\theta}, the problem admits:

  • A closed-form solution (the normal equations)
  • A geometric interpretation (projection onto a subspace)
  • A probabilistic foundation (Gaussian noise     \implies least squares)
  • A convex optimization landscape (single global minimum)

These properties make linear regression the canonical entry point to machine learning theory. Logistic regression replaces the Gaussian likelihood with Bernoulli; neural networks replace linear ff with compositions of linear maps and nonlinearities; but the loss–gradient–update pattern established here persists throughout.

Series context. This opens Chapter 4 (Supervised Learning) in Volume II: Classical Machine Learning. Subsequent articles cover Logistic Regression, Bayesian Linear Regression, and the Bias–Variance Tradeoff.

Linear Regression Fit

xy
MSE = 0.7366
Slope m
0.50
Bias b
0.00
Dataŷ = mx + bResiduals
Explore: Adjust slope and intercept to minimize MSE. Red lines show residuals (y − ŷ). A straight line cannot fit curved data well — notice the persistent error.

The Linear Model and Notation

Definition 1 (Linear Regression Model). Given feature vectors xiRd\mathbf{x}_i \in \mathbb{R}^d and scalar targets yiRy_i \in \mathbb{R} for i=1,,Ni = 1, \ldots, N, the linear regression model predicts

y^i=θTx~i=θ0+j=1dθjxij(1)\hat{y}_i = \boldsymbol{\theta}^T \tilde{\mathbf{x}}_i = \theta_0 + \sum_{j=1}^{d} \theta_j x_{ij} \tag{1}

where x~i=(1,xi1,,xid)TRd+1\tilde{\mathbf{x}}_i = (1, x_{i1}, \ldots, x_{id})^T \in \mathbb{R}^{d+1} is the augmented feature vector (bias absorbed via a leading coordinate of 1), and θ=(θ0,θ1,,θd)TRd+1\boldsymbol{\theta} = (\theta_0, \theta_1, \ldots, \theta_d)^T \in \mathbb{R}^{d+1} is the parameter vector.

Definition 2 (Design Matrix). Stack augmented features into the design matrix

X=[x~1Tx~NT]RN×(d+1),y=[y1yN]RN.(2)\mathbf{X} = \begin{bmatrix} \tilde{\mathbf{x}}_1^T \\ \vdots \\ \tilde{\mathbf{x}}_N^T \end{bmatrix} \in \mathbb{R}^{N \times (d+1)}, \quad \mathbf{y} = \begin{bmatrix} y_1 \\ \vdots \\ y_N \end{bmatrix} \in \mathbb{R}^N. \tag{2}

Then predictions for all samples simultaneously are

y^=Xθ.(3)\hat{\mathbf{y}} = \mathbf{X}\boldsymbol{\theta}. \tag{3}

Important equation. Equation (3) is the matrix form of every linear model in machine learning. The design matrix X\mathbf{X} maps parameters to predictions; its column space C(X)\mathcal{C}(\mathbf{X}) is the set of all vectors reachable by some choice of θ\boldsymbol{\theta}.

Definition 3 (Hypothesis Class). The hypothesis class for linear regression is

H={fθ:xθTx~θRd+1}.(4)\mathcal{H} = \{ f_{\boldsymbol{\theta}} : \mathbf{x} \mapsto \boldsymbol{\theta}^T \tilde{\mathbf{x}} \mid \boldsymbol{\theta} \in \mathbb{R}^{d+1} \}. \tag{4}

This is the set of all affine functions on Rd\mathbb{R}^d. It is a (d+1)(d+1)-dimensional vector space when features are non-degenerate.


Least Squares from Maximum Likelihood

We now derive why squared error is the natural loss function — not by fiat, but from a probabilistic model.

Assumption 1 (Gaussian Noise Model). Observations arise as

yi=θTx~i+εi,εii.i.d.N(0,σ2).(5)y_i = \boldsymbol{\theta}^T \tilde{\mathbf{x}}_i + \varepsilon_i, \quad \varepsilon_i \stackrel{\text{i.i.d.}}{\sim} \mathcal{N}(0, \sigma^2). \tag{5}

Equivalently, yixi,θN(θTx~i,σ2)y_i \mid \mathbf{x}_i, \boldsymbol{\theta} \sim \mathcal{N}(\boldsymbol{\theta}^T \tilde{\mathbf{x}}_i, \sigma^2).

Proposition 1. Under Assumption 1, the maximum likelihood estimator of θ\boldsymbol{\theta} is identical to the minimizer of the sum of squared residuals:

θ^MLE=argminθXθy22.(6)\hat{\boldsymbol{\theta}}_{\text{MLE}} = \arg\min_{\boldsymbol{\theta}} \|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|_2^2. \tag{6}

Proof. The log-likelihood for independent Gaussian observations is

(θ)=N2log(2πσ2)12σ2i=1N(yiθTx~i)2.(7)\ell(\boldsymbol{\theta}) = -\frac{N}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^{N}(y_i - \boldsymbol{\theta}^T \tilde{\mathbf{x}}_i)^2. \tag{7}

Since σ2\sigma^2 is constant with respect to θ\boldsymbol{\theta}, maximizing (θ)\ell(\boldsymbol{\theta}) is equivalent to minimizing i(yiθTx~i)2=Xθy22\sum_i (y_i - \boldsymbol{\theta}^T \tilde{\mathbf{x}}_i)^2 = \|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|_2^2. \blacksquare

Definition 4 (Empirical Risk / MSE Loss). The mean squared error objective is

J(θ)=12NXθy22=12Ni=1N(y^iyi)2.(8)J(\boldsymbol{\theta}) = \frac{1}{2N}\|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|_2^2 = \frac{1}{2N}\sum_{i=1}^{N}(\hat{y}_i - y_i)^2. \tag{8}

The factor 12N\frac{1}{2N} is a convention: the 12\frac{1}{2} cancels upon differentiation; the 1N\frac{1}{N} averages over samples. Minimizers are identical regardless of these constants.

Definition 5 (Ordinary Least Squares). The OLS estimator is

θ^OLS=argminθRd+1J(θ).(9)\hat{\boldsymbol{\theta}}_{\text{OLS}} = \arg\min_{\boldsymbol{\theta} \in \mathbb{R}^{d+1}} J(\boldsymbol{\theta}). \tag{9}

The Normal Equations: Derivation

We derive the closed-form solution step by step.

Theorem 1 (Normal Equations). A necessary condition for θ^\hat{\boldsymbol{\theta}} to minimize J(θ)J(\boldsymbol{\theta}) is

XTXθ^=XTy.(10)\mathbf{X}^T\mathbf{X}\,\hat{\boldsymbol{\theta}} = \mathbf{X}^T\mathbf{y}. \tag{10}

If XTX\mathbf{X}^T\mathbf{X} is invertible, the unique minimizer is

θ^=(XTX)1XTy.(11)\hat{\boldsymbol{\theta}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}. \tag{11}

Proof. Expand the objective:

J(θ)=12N(Xθy)T(Xθy)=12N(θTXTXθ2θTXTy+yTy).(12)J(\boldsymbol{\theta}) = \frac{1}{2N}(\mathbf{X}\boldsymbol{\theta} - \mathbf{y})^T(\mathbf{X}\boldsymbol{\theta} - \mathbf{y}) = \frac{1}{2N}\left(\boldsymbol{\theta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\theta} - 2\boldsymbol{\theta}^T\mathbf{X}^T\mathbf{y} + \mathbf{y}^T\mathbf{y}\right). \tag{12}

Differentiating with respect to θ\boldsymbol{\theta} (using θ(θTAθ)=2Aθ\nabla_{\boldsymbol{\theta}}(\boldsymbol{\theta}^T\mathbf{A}\boldsymbol{\theta}) = 2\mathbf{A}\boldsymbol{\theta} for symmetric A\mathbf{A}):

θJ=1N(XTXθXTy).(13)\nabla_{\boldsymbol{\theta}} J = \frac{1}{N}\left(\mathbf{X}^T\mathbf{X}\boldsymbol{\theta} - \mathbf{X}^T\mathbf{y}\right). \tag{13}

Setting θJ=0\nabla_{\boldsymbol{\theta}} J = \mathbf{0} yields (10). Since JJ is convex (as XTX\mathbf{X}^T\mathbf{X} is positive semidefinite), any critical point is a global minimum. If XTX\mathbf{X}^T\mathbf{X} is positive definite (hence invertible), the solution is unique and given by (11). \blacksquare

Definition 6 (Moore–Penrose Solution). When XTX\mathbf{X}^T\mathbf{X} is singular, (10) has infinitely many solutions. The minimum-norm least-squares solution is

θ^=X+y,(14)\hat{\boldsymbol{\theta}} = \mathbf{X}^+\mathbf{y}, \tag{14}

where X+=(XTX)+XT\mathbf{X}^+ = (\mathbf{X}^T\mathbf{X})^+\mathbf{X}^T is the pseudoinverse. This is developed fully in Singular Value Decomposition.

Important equation. Equation (11) is the normal equation (or closed-form solution). It requires inverting a (d+1)×(d+1)(d+1) \times (d+1) matrix — cost O(d3)O(d^3) — making it impractical when dd is in the millions, even though forming XTX\mathbf{X}^T\mathbf{X} costs only O(Nd2)O(Nd^2).


Geometric Interpretation: Orthogonal Projection

The least-squares problem has an elegant geometric interpretation that clarifies many properties of OLS.

Theorem 2 (Projection Characterization). Let y^=Xθ^\hat{\mathbf{y}} = \mathbf{X}\hat{\boldsymbol{\theta}} be the OLS fitted values. Then y^\hat{\mathbf{y}} is the orthogonal projection of y\mathbf{y} onto C(X)\mathcal{C}(\mathbf{X}), the column space of X\mathbf{X}. The residual r=yy^\mathbf{r} = \mathbf{y} - \hat{\mathbf{y}} is orthogonal to every column of X\mathbf{X}:

XTr=0.(15)\mathbf{X}^T\mathbf{r} = \mathbf{0}. \tag{15}

Proof. By definition, y^C(X)\hat{\mathbf{y}} \in \mathcal{C}(\mathbf{X}). The residual is r=yXθ^\mathbf{r} = \mathbf{y} - \mathbf{X}\hat{\boldsymbol{\theta}}. From the normal equations (10): XT(yXθ^)=0\mathbf{X}^T(\mathbf{y} - \mathbf{X}\hat{\boldsymbol{\theta}}) = \mathbf{0}, which is precisely (15). This is the orthogonality condition characterizing projection onto a subspace. \blacksquare

Corollary 1 (Pythagorean Decomposition). The total squared norm decomposes as

y2=y^2+r2,(16)\|\mathbf{y}\|^2 = \|\hat{\mathbf{y}}\|^2 + \|\mathbf{r}\|^2, \tag{16}

since y^r\hat{\mathbf{y}} \perp \mathbf{r}.

Interpretation. OLS finds the point in the column space of X\mathbf{X} closest to y\mathbf{y} in Euclidean distance. When N>d+1N > d+1 (overdetermined system), we cannot fit y\mathbf{y} exactly; we find the best approximation. When N<d+1N < d+1 (underdetermined), infinitely many θ\boldsymbol{\theta} yield zero training error; the pseudoinverse selects the minimum-norm parameter vector.


Existence, Uniqueness, and Rank Conditions

Theorem 3 (Uniqueness Condition). The OLS solution θ^\hat{\boldsymbol{\theta}} is unique if and only if X\mathbf{X} has full column rank, i.e., rank(X)=d+1\text{rank}(\mathbf{X}) = d+1.

Proof. XTX\mathbf{X}^T\mathbf{X} is invertible if and only if X\mathbf{X} has full column rank (see Positive Definite Matrices). Full column rank means the columns of X\mathbf{X} are linearly independent — no feature (including the bias column) is a linear combination of the others. \blacksquare

Proposition 2 (Training Error and Rank). If rank(X)=N\text{rank}(\mathbf{X}) = N (full row rank, requires Nd+1N \leq d+1), there exists θ\boldsymbol{\theta} such that Xθ=y\mathbf{X}\boldsymbol{\theta} = \mathbf{y} exactly — zero training error.

When features are collinear (multicollinearity), XTX\mathbf{X}^T\mathbf{X} is near-singular: small perturbations in y\mathbf{y} cause large changes in θ^\hat{\boldsymbol{\theta}}. The condition number κ(XTX)=λmax/λmin\kappa(\mathbf{X}^T\mathbf{X}) = \lambda_{\max}/\lambda_{\min} quantifies this sensitivity via the eigenvalue spread of the Gram matrix.


Gradient Descent for Linear Regression

When dd is large, forming and inverting XTX\mathbf{X}^T\mathbf{X} is prohibitive. Gradient descent provides an iterative alternative.

From (13), the gradient of the unscaled loss J~(θ)=12Xθy2\tilde{J}(\boldsymbol{\theta}) = \frac{1}{2}\|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|^2 is

θJ~=XT(Xθy).(17)\nabla_{\boldsymbol{\theta}} \tilde{J} = \mathbf{X}^T(\mathbf{X}\boldsymbol{\theta} - \mathbf{y}). \tag{17}

The update rule with learning rate α>0\alpha > 0:

θt+1=θtαXT(Xθty).(18)\boldsymbol{\theta}_{t+1} = \boldsymbol{\theta}_t - \alpha \,\mathbf{X}^T(\mathbf{X}\boldsymbol{\theta}_t - \mathbf{y}). \tag{18}

Proposition 3 (Convergence in the Eigenbasis). Let XTX=QΛQT\mathbf{X}^T\mathbf{X} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^T be the eigendecomposition with eigenvalues λ1λd+1>0\lambda_1 \geq \cdots \geq \lambda_{d+1} > 0. Gradient descent with step size α<2/λ1\alpha < 2/\lambda_1 converges to θ^\hat{\boldsymbol{\theta}}. The convergence rate per step in direction qj\mathbf{q}_j (eigenvector) is governed by 1αλj|1 - \alpha\lambda_j|.

When κ(XTX)\kappa(\mathbf{X}^T\mathbf{X}) is large (ill-conditioned features), convergence is slow — a theme developed in Gradient Descent and addressed by preconditioning and adaptive methods (Adam, etc.).

Proposition 4 (Stochastic Gradient Descent). Replacing the full gradient (17) with an unbiased estimate based on a mini-batch B{1,,N}\mathcal{B} \subset \{1,\ldots,N\}:

~θ=1BiBx~i(θTx~iyi)(19)\tilde{\nabla}_{\boldsymbol{\theta}} = \frac{1}{|\mathcal{B}|}\sum_{i \in \mathcal{B}} \tilde{\mathbf{x}}_i(\boldsymbol{\theta}^T\tilde{\mathbf{x}}_i - y_i) \tag{19}

yields an unbiased gradient estimator with reduced per-step cost O(Bd)O(|\mathcal{B}| \cdot d) instead of O(Nd)O(Nd).


Regularization: Ridge and Lasso

When XTX\mathbf{X}^T\mathbf{X} is singular or ill-conditioned, regularization stabilizes estimation.

Definition 7 (Ridge Regression / L2 Penalty). The Ridge objective adds an 2\ell_2 penalty:

JRidge(θ)=12NXθy2+λj=1dθj2=12NXθy2+λθ1:d22,(20)J_{\text{Ridge}}(\boldsymbol{\theta}) = \frac{1}{2N}\|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|^2 + \lambda \sum_{j=1}^{d} \theta_j^2 = \frac{1}{2N}\|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|^2 + \lambda \|\boldsymbol{\theta}_{1:d}\|_2^2, \tag{20}

where θ1:d=(θ1,,θd)T\boldsymbol{\theta}_{1:d} = (\theta_1, \ldots, \theta_d)^T excludes the bias (typically not penalized).

Theorem 4 (Ridge Closed Form). The Ridge estimator is

θ^Ridge=(XTX+2NλD)1XTy,(21)\hat{\boldsymbol{\theta}}_{\text{Ridge}} = \left(\mathbf{X}^T\mathbf{X} + 2N\lambda \,\mathbf{D}\right)^{-1}\mathbf{X}^T\mathbf{y}, \tag{21}

where D=diag(0,1,,1)R(d+1)×(d+1)\mathbf{D} = \text{diag}(0, 1, \ldots, 1) \in \mathbb{R}^{(d+1)\times(d+1)}.

Proof. Setting the gradient of (20) to zero:

1NXT(Xθy)+2λDθ=0.(22)\frac{1}{N}\mathbf{X}^T(\mathbf{X}\boldsymbol{\theta} - \mathbf{y}) + 2\lambda \mathbf{D}\boldsymbol{\theta} = \mathbf{0}. \tag{22}

Rearranging: (XTX+2NλD)θ=XTy(\mathbf{X}^T\mathbf{X} + 2N\lambda \mathbf{D})\boldsymbol{\theta} = \mathbf{X}^T\mathbf{y}. For any λ>0\lambda > 0, the matrix XTX+2NλD\mathbf{X}^T\mathbf{X} + 2N\lambda \mathbf{D} is positive definite and hence invertible. \blacksquare

Definition 8 (Lasso / L1 Penalty). The Lasso objective uses an 1\ell_1 penalty:

JLasso(θ)=12NXθy2+λθ1:d1.(23)J_{\text{Lasso}}(\boldsymbol{\theta}) = \frac{1}{2N}\|\mathbf{X}\boldsymbol{\theta} - \mathbf{y}\|^2 + \lambda \|\boldsymbol{\theta}_{1:d}\|_1. \tag{23}

Unlike Ridge, Lasso has no closed-form solution. The 1\ell_1 penalty induces sparsity: many θj\theta_j are driven exactly to zero, performing implicit feature selection. The geometry of the 1\ell_1 ball (corners on coordinate axes) explains this — the level sets of θ1\|\boldsymbol{\theta}\|_1 are non-smooth at axes, so optima often lie on sparse faces.

Proposition 5 (Bayesian Interpretation of Ridge). Ridge regression corresponds to MAP estimation under a Gaussian prior θ1:dN(0,12λI)\boldsymbol{\theta}_{1:d} \sim \mathcal{N}(\mathbf{0}, \frac{1}{2\lambda}\mathbf{I}) with the Gaussian noise model (5). See Bayesian Linear Regression.


The Bias–Variance Decomposition

Understanding generalization requires decomposing expected prediction error.

Theorem 5 (Bias–Variance Decomposition). Consider the model y=f(x)+εy = f(\mathbf{x}) + \varepsilon with E[ε]=0\mathbb{E}[\varepsilon] = 0, Var(ε)=σ2\text{Var}(\varepsilon) = \sigma^2. Let f^(x)\hat{f}(\mathbf{x}) be an estimator trained on a random dataset D\mathcal{D}. For a fixed test point x\mathbf{x}, the expected squared error decomposes as

ED,ε[(yf^(x))2]=(ED[f^(x)]f(x))2Bias2+ED[(f^(x)ED[f^(x)])2]Variance+σ2Irreducible noise.(24)\mathbb{E}_{\mathcal{D}, \varepsilon}\left[(y - \hat{f}(\mathbf{x}))^2\right] = \underbrace{\left(\mathbb{E}_{\mathcal{D}}[\hat{f}(\mathbf{x})] - f(\mathbf{x})\right)^2}_{\text{Bias}^2} + \underbrace{\mathbb{E}_{\mathcal{D}}\left[(\hat{f}(\mathbf{x}) - \mathbb{E}_{\mathcal{D}}[\hat{f}(\mathbf{x})])^2\right]}_{\text{Variance}} + \underbrace{\sigma^2}_{\text{Irreducible noise}}. \tag{24}

Proof. Add and subtract E[f^]\mathbb{E}[\hat{f}] and expand; cross terms vanish by independence of ε\varepsilon from D\mathcal{D}. See Bias–Variance Tradeoff for the full derivation. \blacksquare

Interpretation for linear regression:

  • Low complexity (few features, large λ\lambda): high bias, low variance — underfitting
  • High complexity (many features, λ=0\lambda = 0 with collinearity): low bias, high variance — overfitting
  • Ridge increases bias slightly but reduces variance substantially when XTX\mathbf{X}^T\mathbf{X} is ill-conditioned

The effective degrees of freedom of Ridge regression is tr(X(XTX+λI)1XT)\text{tr}(\mathbf{X}(\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I})^{-1}\mathbf{X}^T), a smooth measure of model complexity.


Statistical Properties of the OLS Estimator

Under standard assumptions, OLS enjoys strong statistical guarantees.

Assumption 2 (Gauss–Markov Conditions). (i) Linearity: E[yixi]=θTx~i\mathbb{E}[y_i \mid \mathbf{x}_i] = \boldsymbol{\theta}^T\tilde{\mathbf{x}}_i; (ii) Homoscedasticity: Var(εixi)=σ2\text{Var}(\varepsilon_i \mid \mathbf{x}_i) = \sigma^2; (iii) Uncorrelated errors: Cov(εi,εj)=0\text{Cov}(\varepsilon_i, \varepsilon_j) = 0 for iji \neq j; (iv) Full rank: rank(X)=d+1\text{rank}(\mathbf{X}) = d+1.

Theorem 6 (Gauss–Markov). Under Assumption 2, the OLS estimator θ^\hat{\boldsymbol{\theta}} is the Best Linear Unbiased Estimator (BLUE): among all linear unbiased estimators, it has minimum variance.

Proposition 6 (Variance of OLS). Under homoscedastic Gaussian errors:

Cov(θ^)=σ2(XTX)1.(25)\text{Cov}(\hat{\boldsymbol{\theta}}) = \sigma^2 (\mathbf{X}^T\mathbf{X})^{-1}. \tag{25}

Large entries in (XTX)1(\mathbf{X}^T\mathbf{X})^{-1} — caused by collinear features — inflate the variance of individual coefficient estimates.


Worked Examples

Example 1: Simple Linear Regression

Given N=3N = 3 points with scalar feature: (1,2),(2,3),(3,5)(1, 2), (2, 3), (3, 5). Augmented design matrix and target:

X=[111213],y=[235].(26)\mathbf{X} = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}, \quad \mathbf{y} = \begin{bmatrix} 2 \\ 3 \\ 5 \end{bmatrix}. \tag{26}

Compute:

XTX=[36614],XTy=[1023].(27)\mathbf{X}^T\mathbf{X} = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix}, \quad \mathbf{X}^T\mathbf{y} = \begin{bmatrix} 10 \\ 23 \end{bmatrix}. \tag{27} θ^=[36614]1[1023]=16[14663][1023]=[0.331.5].(28)\hat{\boldsymbol{\theta}} = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix}^{-1} \begin{bmatrix} 10 \\ 23 \end{bmatrix} = \frac{1}{6}\begin{bmatrix} 14 & -6 \\ -6 & 3 \end{bmatrix}\begin{bmatrix} 10 \\ 23 \end{bmatrix} = \begin{bmatrix} 0.33 \\ 1.5 \end{bmatrix}. \tag{28}

The fitted line is y^=0.33+1.5x\hat{y} = 0.33 + 1.5x.

Example 2: Orthogonality of Residuals

For the solution above, y^=Xθ^\hat{\mathbf{y}} = \mathbf{X}\hat{\boldsymbol{\theta}} and r=yy^\mathbf{r} = \mathbf{y} - \hat{\mathbf{y}}. Direct computation confirms XTr=0\mathbf{X}^T\mathbf{r} = \mathbf{0} — the residual is orthogonal to both the bias column and the feature column.

Example 3: Ridge Stabilization

Suppose two features are nearly collinear: x1x2\mathbf{x}_1 \approx \mathbf{x}_2. Then XTX\mathbf{X}^T\mathbf{X} has a very small eigenvalue λmin\lambda_{\min}, and (XTX)1(\mathbf{X}^T\mathbf{X})^{-1} has a very large eigenvalue 1/λmin1/\lambda_{\min}. Adding λI\lambda \mathbf{I} shifts all eigenvalues by 2Nλ2N\lambda, bounding κ(XTX+2NλI)\kappa(\mathbf{X}^T\mathbf{X} + 2N\lambda \mathbf{I}) and stabilizing θ^\hat{\boldsymbol{\theta}}.


Connection to the Broader Curriculum

Linear regression is the Rosetta Stone of machine learning:

  • Linear model Xθ\mathbf{X}\boldsymbol{\theta} — Neural network layers
  • Squared loss — Cross-entropy, Huber, contrastive losses
  • Normal equations — No closed form; backpropagation instead
  • Ridge/Lasso — Weight decay, dropout, all regularization
  • Bias–variance — Universal generalization framework
  • Design matrix rank — Representation rank, LoRA low-rank structure

Logistic Regression replaces the Gaussian likelihood with Bernoulli and the identity link with the logistic function. Backpropagation generalizes the gradient computation to arbitrary computation graphs. Self-Attention computes similarity-weighted linear combinations — regression over values indexed by queries.


Common Pitfalls and Misconceptions

Pitfall 1: Applying the normal equation when N<dN < d. When there are more parameters than samples, XTX\mathbf{X}^T\mathbf{X} is singular. The normal equation as written in (11) does not apply; use the pseudoinverse or regularization.

Pitfall 2: Regularizing the bias term. Penalizing θ0\theta_0 shifts predictions systematically and is usually undesirable. Always exclude the intercept from 1\ell_1 and 2\ell_2 penalties unless features are centered and targets are demeaned.

Pitfall 3: Confusing R2R^2 with model quality. High R2R^2 on training data does not imply good generalization. R2=1r2/yyˉ12R^2 = 1 - \|\mathbf{r}\|^2/\|\mathbf{y} - \bar{y}\mathbf{1}\|^2 measures fit on the training set only.

Pitfall 4: Ignoring feature scaling before gradient descent. Features on different scales yield an ill-conditioned XTX\mathbf{X}^T\mathbf{X}, slowing convergence. Standardization (x~j=(xjμj)/σj\tilde{x}_j = (x_j - \mu_j)/\sigma_j) improves conditioning without changing the OLS solution (if the bias is unpenalized).

Pitfall 5: Treating linear regression as "always the right first model." Linearity in parameters does not mean linearity in features. Polynomial, interaction, and basis-expansion features can capture nonlinearity while preserving the closed-form solution.

Pitfall 6: Assuming homoscedasticity. When Var(εi)\text{Var}(\varepsilon_i) depends on xi\mathbf{x}_i, OLS is still unbiased but no longer efficient. Weighted least squares or robust methods are appropriate.


Research Perspective

Linear regression dates to Gauss and Legendre's work on least squares in the early nineteenth century for astronomical orbit prediction. The probabilistic formulation through maximum likelihood connects to Fisher's foundations of statistical inference in the 1920s.

In modern machine learning, linear models remain indispensable:

  • Generalized Linear Models (GLMs) extend the linear predictor to exponential-family distributions (Nelder & Wedderburn, 1972).
  • Kernel Ridge Regression implicitly maps features to infinite-dimensional spaces while retaining a closed-form solution via the representer theorem.
  • Random Features (Rahimi & Recht, 2007) approximate kernel methods with explicit finite-dimensional linear models.
  • Scaling laws for language models show that linear trends in log-loss vs. compute persist across orders of magnitude — the simplest model class still governs the largest systems.

The resurgence of interest in interpretability has renewed focus on linear probes and linear concept directions in embedding spaces, demonstrating that linear algebra remains central even in the era of billion-parameter models.


Summary of Takeaways

  • Linear modely^=θTx~\hat{y} = \boldsymbol{\theta}^T\tilde{\mathbf{x}} — Simplest supervised hypothesis class
  • MSE loss12N —Xθy 2\frac{1}{2N}\ — \mathbf{X}\boldsymbol{\theta} - \mathbf{y}\ — ^2 — MLE under Gaussian noise
  • Normal equationsXTXθ^=XTy\mathbf{X}^T\mathbf{X}\hat{\boldsymbol{\theta}} = \mathbf{X}^T\mathbf{y} — Closed-form OLS
  • Projectiony^=projC(X)(y)\hat{\mathbf{y}} = \text{proj}_{\mathcal{C}(\mathbf{X})}(\mathbf{y}) — Geometric interpretation
  • Uniquenessrank(X)=d+1\text{rank}(\mathbf{X}) = d+1 — Full column rank required
  • Ridge(XTX+2NλD)1XTy(\mathbf{X}^T\mathbf{X} + 2N\lambda \mathbf{D})^{-1}\mathbf{X}^T\mathbf{y} — Stabilizes ill-conditioned problems
  • Bias–variance — Error = Bias2^2 + Variance + σ2\sigma^2 — Generalization decomposition

Next article: Logistic Regression → — where the linear predictor is composed with the logistic function and Bernoulli likelihood replaces the Gaussian.


Exercises

Exercise 1 (Derivation). Starting from the Gaussian log-likelihood (7), verify that σ^MLE2=1NXθ^y2\hat{\sigma}^2_{\text{MLE}} = \frac{1}{N}\|\mathbf{X}\hat{\boldsymbol{\theta}} - \mathbf{y}\|^2 after substituting the MLE for θ\boldsymbol{\theta}.

Exercise 2 (Projection). Prove that among all vC(X)\mathbf{v} \in \mathcal{C}(\mathbf{X}), the vector y^=Xθ^\hat{\mathbf{y}} = \mathbf{X}\hat{\boldsymbol{\theta}} uniquely minimizes yv2\|\mathbf{y} - \mathbf{v}\|_2. Use the Pythagorean theorem with the orthogonal decomposition y=y^+r\mathbf{y} = \hat{\mathbf{y}} + \mathbf{r}.

Exercise 3 (Rank). Construct a design matrix XR4×3\mathbf{X} \in \mathbb{R}^{4 \times 3} with rank 2. Show that the normal equations have infinitely many solutions and characterize the solution set as an affine subspace of R3\mathbb{R}^3.

Exercise 4 (Ridge). Prove that the eigenvalues of XTX+λI\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I} are λj+λ\lambda_j + \lambda where λj\lambda_j are eigenvalues of XTX\mathbf{X}^T\mathbf{X}. Conclude that Ridge always produces a unique solution for λ>0\lambda > 0.

Exercise 5 (Bias–variance). For the simple model y=θx+εy = \theta x + \varepsilon with a single observation (x1,y1)(x_1, y_1) and known x10x_1 \neq 0, compute E[θ^]\mathbb{E}[\hat{\theta}] and Var(θ^)\text{Var}(\hat{\theta}) where θ^=y1/x1\hat{\theta} = y_1/x_1. Verify the bias–variance decomposition at a test point xx_*.

Exercise 6 (Condition number). Let X\mathbf{X} have columns x1,x2\mathbf{x}_1, \mathbf{x}_2 with x1=x2=1\|\mathbf{x}_1\| = \|\mathbf{x}_2\| = 1 and x1Tx2=ρ\mathbf{x}_1^T\mathbf{x}_2 = \rho. Compute XTX\mathbf{X}^T\mathbf{X} and its condition number as a function of ρ\rho. What happens as ρ1\rho \to 1?

Exercise 7 (Conceptual). Explain why centering features (x~ij=xijxˉj\tilde{x}_{ij} = x_{ij} - \bar{x}_j) does not change the fitted values y^\hat{\mathbf{y}}, but does change the interpretation of θ0\theta_0. How does centering affect the Ridge penalty if the bias is not excluded?

Exercise 8 (Connection). Show that the gradient descent update (18) can be written as θt+1=(IαXTX)θt+αXTy\boldsymbol{\theta}_{t+1} = (\mathbf{I} - \alpha \mathbf{X}^T\mathbf{X})\boldsymbol{\theta}_t + \alpha \mathbf{X}^T\mathbf{y}. Analyze convergence via the spectral radius of (IαXTX)(\mathbf{I} - \alpha \mathbf{X}^T\mathbf{X}).