Classical MLLinear Regression

Linear Regression

Background
  • Galton (1886) coined regression studying hereditary height; Legendre formalized least squares in 1805
  • Foundational supervised learning algorithm for continuous outputs
  • The closed-form solution (normal equations) makes it unique among ML algorithms
The Concept

Linear regression fits the line (or hyperplane) through data that minimizes the sum of squared vertical distances from each point to the line. It learns one weight per input feature plus a bias term.

How it Links
  • Foundation for logistic regression, regularized regression (ridge/lasso), and GLMs
  • Normal equations are a special case of the projection theorem in linear algebra
  • The bias-variance trade-off is most clearly illustrated here via ridge and lasso regularization
Concrete Example
Predicting a house price from square footage: the model learns that each extra square foot adds $200, plus a base price of $50,000. Residuals are the difference between predicted and actual prices - you minimize the sum of those squared.
Learning outcomes
  • Derive the OLS normal equations from the mean-squared-error objective.
  • State the five classical assumptions and the Gauss-Markov theorem.
  • Recognize OLS as MLE under Gaussian noise, and ridge as the MAP estimate under a Gaussian prior.
  • Compare ridge, lasso, and elastic net by objective, closed form, sparsity, and behaviour under multicollinearity.
  • Implement OLS and ridge from scratch in NumPy, with sklearn, and as a PyTorch autograd loop.

TL;DR

TL;DR

Linear regression fits the hyperplane y^=Xβ\hat{y} = X\beta that minimizes the squared residual norm. The closed-form OLS solution β^=(X⊤X)−1X⊤y\hat{\beta} = (X^{\top}X)^{-1} X^{\top} y is the orthogonal projection of yy onto the column space of XX, and Gauss-Markov says it is the best linear unbiased estimator (BLUE) when errors are uncorrelated and homoskedastic [1]. Ridge [4], lasso [3], and elastic net [5] add penalties that trade a little bias for a lot of variance reduction, with ridge giving a closed form, lasso giving sparsity, and elastic net combining both.

Intuition

A line through a cloud of points is the simplest predictive model that is still useful. Linear regression chooses the line (or hyperplane in higher dimensions) that minimizes the sum of squared vertical residuals. Two equivalent stories explain why squared error is the right loss:

  • Probabilistic. If we assume the noise is i.i.d. Gaussian, yi=xi⊤β+εiy_i = x_i^{\top}\beta + \varepsilon_i with εi∼N(0,σ2)\varepsilon_i \sim \mathcal{N}(0, \sigma^2), then maximum-likelihood estimation of β\beta is exactly minimization of the squared residual norm [2].
  • Geometric. Squared error is the squared Euclidean distance, so OLS finds the orthogonal projection of yy onto the column space of XX. The residual vector is the component of yy that lies orthogonal to every feature column.
The right picture

OLS is just orthogonal projection. The fitted vector y^\hat{y} lives in col⁡(X)\operatorname{col}(X) and the residual r=y−y^r = y - \hat{y} is perpendicular to every column of XX. That single fact gives you the normal equations, Gauss-Markov, the geometry of multicollinearity, and why ridge “shrinks” along the small-singular-value directions.

OLS as orthogonal projection of y onto col(X)

col(X) - hyperplane spanned by columns of X

x_1x_2yy_hat = X beta_hatr = y - y_hat

Normal equations: X^T r = 0 ⇔ X^T (y - X beta_hat) = 0 ⇔ beta_hat = (X^T X)^(-1) X^T y

Figure 1. The OLS fit y_hat is the orthogonal projection of y onto col(X). The residual r is perpendicular to every column of X, which is exactly the normal equations X^T r = 0.

Math

Math: derivation of the normal equations and the OLS estimator

Setup

Stack nn observations in X∈Rn×dX \in \mathbb{R}^{n \times d} and y∈Rny \in \mathbb{R}^{n}. We fit y^=Xβ\hat{y} = X\beta with β∈Rd\beta \in \mathbb{R}^{d} (an intercept is absorbed by adding a column of ones to XX).

Squared-error objective

Define the residual sum of squares

L(β)=12 ∥y−Xβ∥22=12 (y−Xβ)⊤(y−Xβ).\mathcal{L}(\beta) = \tfrac{1}{2}\,\lVert y - X\beta \rVert_{2}^{2} = \tfrac{1}{2}\,(y - X\beta)^{\top}(y - X\beta).

Expanding,

L(β)=12 y⊤y−β⊤X⊤y+12 β⊤X⊤Xβ.\mathcal{L}(\beta) = \tfrac{1}{2}\,y^{\top}y - \beta^{\top} X^{\top} y + \tfrac{1}{2}\,\beta^{\top} X^{\top} X \beta.

Setting the gradient to zero

Differentiate with respect to β\beta:

∇βL(β)=− X⊤y+X⊤Xβ.\nabla_{\beta} \mathcal{L}(\beta) = -\,X^{\top} y + X^{\top} X \beta.

The first-order condition ∇βL(β)=0\nabla_{\beta} \mathcal{L}(\beta) = 0 gives the normal equations

X⊤X β^=X⊤y.X^{\top} X \, \hat{\beta} = X^{\top} y.

If XX has full column rank, X⊤XX^{\top} X is invertible and

 β^=(X⊤X)−1X⊤y. \boxed{\,\hat{\beta} = (X^{\top} X)^{-1} X^{\top} y.\,}

The Hessian is ∇β2L(β)=X⊤X⪰0\nabla^{2}_{\beta} \mathcal{L}(\beta) = X^{\top} X \succeq 0, so L\mathcal{L} is convex and β^\hat{\beta} is a global minimum.

Geometric interpretation

The fitted vector y^=Xβ^=X(X⊤X)−1X⊤y=PX y\hat{y} = X\hat{\beta} = X (X^{\top}X)^{-1} X^{\top} y = P_{X}\, y, where PXP_X is the orthogonal projection matrix onto col⁡(X)\operatorname{col}(X). The residual

r=y−y^=(I−PX) yr = y - \hat{y} = (I - P_X)\, y

lies in col⁡(X)⊥\operatorname{col}(X)^{\perp}, so X⊤r=0X^{\top} r = 0. That is exactly the normal equations: residuals are orthogonal to every feature column.

Connection to MLE

If yi=xi⊤β+εiy_i = x_i^{\top}\beta + \varepsilon_i with εi∼N(0,σ2)\varepsilon_i \sim \mathcal{N}(0, \sigma^2) i.i.d., the log-likelihood is

log⁡p(y∣X,β,σ2)=−n2log⁡(2πσ2)−12σ2 ∥y−Xβ∥22.\log p(y \mid X, \beta, \sigma^2) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\,\lVert y - X\beta \rVert_{2}^{2}.

Maximizing in β\beta is identical to minimizing ∥y−Xβ∥22\lVert y - X\beta \rVert_{2}^{2}, so OLS is the MLE under Gaussian noise [2].

Gradient descent variant

When X⊤XX^{\top}X is too large to invert, run gradient descent with step size η\eta:

βt+1=βt−η (X⊤Xβt−X⊤y)=βt−η X⊤(Xβt−y).\beta_{t+1} = \beta_{t} - \eta\, \bigl(X^{\top} X \beta_{t} - X^{\top} y\bigr) = \beta_{t} - \eta\, X^{\top}(X\beta_{t} - y).

Convergence requires η<2/λmax⁡(X⊤X)\eta < 2 / \lambda_{\max}(X^{\top}X). Stochastic gradient descent uses a single example or mini-batch per step.

Gauss-Markov theorem: OLS is BLUE

Statement

Assume:

  1. Linearity: y=Xβ+εy = X\beta + \varepsilon with XX non-random and full column rank.
  2. Zero-mean errors: E[ε]=0\mathbb{E}[\varepsilon] = 0.
  3. Spherical errors: Cov⁡(ε)=σ2I\operatorname{Cov}(\varepsilon) = \sigma^{2} I (uncorrelated and homoskedastic).

Then among all linear unbiased estimators of β\beta, the OLS estimator β^=(X⊤X)−1X⊤y\hat{\beta} = (X^{\top}X)^{-1} X^{\top} y has the smallest variance: it is the Best Linear Unbiased Estimator (BLUE).

Proof sketch

Any linear estimator can be written β~=Cy\tilde{\beta} = C y for some matrix C∈Rd×nC \in \mathbb{R}^{d \times n}. Write C=(X⊤X)−1X⊤+DC = (X^{\top}X)^{-1} X^{\top} + D, where DD is the deviation from the OLS row-operator.

Unbiasedness E[β~]=β\mathbb{E}[\tilde{\beta}] = \beta requires CX=IC X = I, which forces DX=0D X = 0.

Compute the covariance:

Cov⁡(β~)=σ2CC⊤=σ2[(X⊤X)−1+DD⊤],\operatorname{Cov}(\tilde{\beta}) = \sigma^{2} C C^{\top} = \sigma^{2}\Bigl[(X^{\top}X)^{-1} + D D^{\top}\Bigr],

using DX=0D X = 0 to kill the cross terms. Since DD⊤⪰0D D^{\top} \succeq 0,

Cov⁡(β~)−Cov⁡(β^OLS)=σ2DD⊤⪰0,\operatorname{Cov}(\tilde{\beta}) - \operatorname{Cov}(\hat{\beta}_{\mathrm{OLS}}) = \sigma^{2} D D^{\top} \succeq 0,

so β^OLS\hat{\beta}_{\mathrm{OLS}} has the smallest variance among linear unbiased estimators. Equality requires D=0D = 0, i.e. uniqueness up to multiplicity. Note that biased estimators (ridge!) can do strictly better in MSE; Gauss-Markov is a statement about the unbiased class only [1].

The classical assumptions

OLS is consistent and well-behaved when these five assumptions hold. Real data often violates one or more, and each violation has a textbook fix.

AssumptionWhat it meansCommon diagnosticStandard fix
LinearityE[y | x] is linear in beta.Residuals-vs-fitted plot shows no curvature.Add polynomial features, splines, or use a non-linear model.
IndependenceObservations (or errors) are independent.Durbin-Watson on residuals; check for time-series or cluster structure.GLS, mixed-effects, or block-resampling.
HomoskedasticityVar(epsilon | x) is constant.Breusch-Pagan test; funnel-shaped residual plot.Weighted least squares or robust (sandwich) standard errors.
Normality of residualsepsilon is Gaussian. Needed for finite-sample inference, not consistency.QQ-plot of residuals; Shapiro-Wilk on small n.Bootstrap confidence intervals; do not bother for large n by CLT.
No multicollinearityX has full column rank; columns are not near-collinear.VIF (variance inflation factor) above 5-10; condition number of X above 30.Drop a redundant feature, ridge regression, or PCA pre-processing.
Inference vs prediction

Strict normality matters for finite-sample t and F statistics. For prediction under a large nn, the central limit theorem rescues you and you can ignore non-Gaussian residuals. Always know which job your model is doing.

Regularization

Adding a penalty to the OLS objective trades a bit of bias for a lot of variance reduction, especially when features are correlated or when dd is close to nn.

Ridge regression: closed-form derivation

The ridge objective is

Lridge(β)=12 ∥y−Xβ∥22+λ2 ∥β∥22,λ>0.\mathcal{L}_{\mathrm{ridge}}(\beta) = \tfrac{1}{2}\,\lVert y - X\beta \rVert_{2}^{2} + \tfrac{\lambda}{2}\,\lVert \beta \rVert_{2}^{2}, \qquad \lambda > 0.

Setting ∇βLridge(β)=−X⊤y+X⊤Xβ+λβ=0\nabla_{\beta}\mathcal{L}_{\mathrm{ridge}}(\beta) = -X^{\top}y + X^{\top}X\beta + \lambda \beta = 0,

 β^ridge=(X⊤X+λI)−1X⊤y. \boxed{\,\hat{\beta}_{\mathrm{ridge}} = (X^{\top}X + \lambda I)^{-1} X^{\top} y.\,}

The penalty term makes X⊤X+λIX^{\top}X + \lambda I strictly positive definite, so the inverse always exists, even when X⊤XX^{\top}X is singular (e.g. d>nd > n or perfectly collinear columns). This is exactly why ridge dominates OLS under multicollinearity [4].

MAP interpretation

Place a Gaussian prior β∼N(0,τ2I)\beta \sim \mathcal{N}(0, \tau^{2} I). The posterior mode under the Gaussian likelihood is

arg⁡max⁡β[log⁡p(y∣X,β)+log⁡p(β)]=arg⁡min⁡β [12σ2∥y−Xβ∥2+12τ2∥β∥2],\arg\max_{\beta}\bigl[\log p(y \mid X, \beta) + \log p(\beta)\bigr] = \arg\min_{\beta}\,\Bigl[\tfrac{1}{2\sigma^{2}}\lVert y - X\beta \rVert^{2} + \tfrac{1}{2\tau^{2}}\lVert \beta \rVert^{2}\Bigr],

with λ=σ2/τ2\lambda = \sigma^{2}/\tau^{2}. Ridge is MAP under a Gaussian prior on the weights. Smaller τ2\tau^{2} pulls β^\hat{\beta} harder towards zero [2].

SVD view

Let X=UΣV⊤X = U \Sigma V^{\top} be the thin SVD with singular values s1≥s2≥⋯≥sd≥0s_1 \ge s_2 \ge \dots \ge s_d \ge 0. Then

β^ridge=V diag⁡ ⁣(sjsj2+λ)U⊤y,β^OLS=V diag⁡ ⁣(1sj)U⊤y.\hat{\beta}_{\mathrm{ridge}} = V\,\operatorname{diag}\!\left(\frac{s_j}{s_j^{2} + \lambda}\right) U^{\top} y, \qquad \hat{\beta}_{\mathrm{OLS}} = V\,\operatorname{diag}\!\left(\frac{1}{s_j}\right) U^{\top} y.

Ridge applies the multiplicative shrinkage sj2/(sj2+λ)<1s_j^{2}/(s_j^{2}+\lambda) < 1 to the OLS coefficients, with the heaviest shrinkage on directions with small singular values. Those are exactly the directions where OLS has highest variance, so ridge buys variance reduction precisely where OLS is least stable [1].

Lasso and elastic net

The lasso replaces the squared penalty with an absolute-value penalty:

Llasso(β)=12 ∥y−Xβ∥22+λ ∥β∥1.\mathcal{L}_{\mathrm{lasso}}(\beta) = \tfrac{1}{2}\,\lVert y - X\beta \rVert_{2}^{2} + \lambda \,\lVert \beta \rVert_{1}.

There is no closed form, but the objective is still convex and is solved by coordinate descent or proximal gradient. Each coordinate update reduces to the soft-thresholding operator sign⁡(z) max⁡(∣z∣−λ,0)\operatorname{sign}(z)\,\max(|z| - \lambda, 0), which sets coefficients to zero exactly when their unpenalized residual signal is below λ\lambda. That is why lasso produces sparse solutions: it is the penalty geometry, not a heuristic [3].

The elastic net is the convex combination

Lenet(β)=12 ∥y−Xβ∥22+λ ⁣[(1−α) 12∥β∥22+α ∥β∥1],\mathcal{L}_{\mathrm{enet}}(\beta) = \tfrac{1}{2}\,\lVert y - X\beta \rVert_{2}^{2} + \lambda\!\Bigl[(1-\alpha)\,\tfrac{1}{2}\lVert \beta \rVert_{2}^{2} + \alpha\,\lVert \beta \rVert_{1}\Bigr],

with α∈[0,1]\alpha \in [0, 1]. It inherits lasso’s sparsity but, unlike lasso, can keep correlated groups of features together rather than arbitrarily picking one of them [5].

MethodObjectiveClosed form?SparsityMulticollinearityHyperparameter
OLS||y - X beta||^2yes (X^T X must be invertible)nounstable, exploding variancenone
Ridge||y - X beta||^2 + lambda * ||beta||^2yes: (X^T X + lambda I)^(-1) X^T yno (shrinks but does not zero)stable, shrinks correlated features togetherlambda
Lasso||y - X beta||^2 + lambda * ||beta||_1no (coordinate descent / proximal)yes (sets coefficients to 0)arbitrarily picks 1 of a correlated grouplambda
Elastic net||y - X beta||^2 + lambda * [(1 - alpha) * ||beta||^2 / 2 + alpha * ||beta||_1]no (coordinate descent)yeskeeps correlated groups togetherlambda, alpha

Bias-variance trade-off in regularization

OLS is unbiased; any non-zero λ\lambda introduces bias by pulling coefficients toward zero. The win is variance reduction: the shrinkage factor sj2/(sj2+λ)s_j^{2}/(s_j^{2}+\lambda) is strictly below 1 on every direction, and tiny on small-singular-value directions. The optimal λ\lambda is the one that balances the squared bias against the variance, and it is almost always strictly positive in finite samples [1].

Why a biased estimator can win

Gauss-Markov says OLS is best among unbiased linear estimators. Ridge is biased, so the theorem does not apply, and ridge often has lower MSE precisely because the bias-variance trade-off pays off when X⊤XX^{\top}X has small eigenvalues. This is the cleanest example in classical statistics of “controlled bias is good”.

Algorithm

StepOLS via normal equationsOLS via gradient descent
1Stack X (with intercept column) and y.Stack X (with intercept column) and y.
2Compute G = X^T X.Initialise beta = 0.
3Compute c = X^T y.Choose step size eta below 2 / lambda_max(X^T X).
4Solve G beta = c (e.g. Cholesky if G is PD).Repeat: beta <- beta - eta * X^T (X beta - y) until ||grad|| is small.
5Return beta_hat.Return beta.
CostO(n d^2 + d^3) one-shotO(n d) per step, no matrix inverse
# OLS via normal equations (pseudocode)
G = X.T @ X
c = X.T @ y
beta = solve(G, c)        # Cholesky / LU; do not call inv() in production
 
# OLS via gradient descent (pseudocode)
beta = zeros(d)
for step in range(max_steps):
    grad = X.T @ (X @ beta - y)
    beta = beta - eta * grad
    if norm(grad) < tol:
        break

Implementations

Three idiomatic implementations of the same model. The NumPy version solves the normal equations directly, the sklearn version is the production one-liner, and the PyTorch version uses autograd on the squared-error loss so the tensor view lines up with the math above.

python
import numpy as np

def ols_fit(X, y):
  """Closed-form OLS via the normal equations. Adds an intercept column."""
  n = X.shape[0]
  X1 = np.hstack([np.ones((n, 1)), X])
  G = X1.T @ X1
  c = X1.T @ y
  beta = np.linalg.solve(G, c)        # do not call inv() in production
  return beta[0], beta[1:]            # intercept, slopes

def ridge_fit(X, y, lam=1.0):
  """Closed-form ridge via (X^T X + lambda I)^{-1} X^T y. Penalty on slopes only."""
  n, d = X.shape
  X1 = np.hstack([np.ones((n, 1)), X])
  A = X1.T @ X1
  A[1:, 1:] += lam * np.eye(d)        # do not regularize the intercept
  beta = np.linalg.solve(A, X1.T @ y)
  return beta[0], beta[1:]

def ols_gd_fit(X, y, eta=1e-3, steps=2000):
  """OLS via batch gradient descent (no closed form)."""
  n, d = X.shape
  X1 = np.hstack([np.ones((n, 1)), X])
  beta = np.zeros(d + 1)
  for _ in range(steps):
      beta -= eta * (X1.T @ (X1 @ beta - y))
  return beta[0], beta[1:]

# small synthetic example
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 3))
beta_true = np.array([1.5, -2.0, 0.5])
y = 0.7 + X @ beta_true + 0.3 * rng.normal(size=200)

print("OLS  :", ols_fit(X, y))
print("Ridge:", ridge_fit(X, y, lam=1.0))
print("GD   :", ols_gd_fit(X, y))
From-scratch reference. solve() is preferred over inv() for numerical stability.

Run the from-scratch NumPy normal-equations solver live in the browser:

Idle (packages: numpy)
(no output yet)

Real-world uses

DomainSystemWhy linear regression
Real estateZillow Zestimate-style price modelsInterpretable per-feature coefficients (sqft, beds, neighborhood) make legal and explainability requirements easier to meet.
ExperimentationA/B test impact estimation with covariates (CUPED-style)Closed-form variance estimator and well-understood standard errors; covariate adjustment shrinks the variance of the treatment effect.
FinanceFama-French and other factor modelsLinear in factor exposures; OLS gives BLUE estimates of betas under the Gauss-Markov assumptions.
Pharma / clinicalDose-response modelsRegulatory pipelines require model forms whose assumptions can be checked, audited, and reproduced.
Causal inferenceDifference-in-differences, instrumental variables, fixed effectsMost causal estimators are linear regressions in disguise, with closed-form standard errors.

Pitfalls

Multicollinearity inflates variance

If two columns of XX are nearly collinear, X⊤XX^{\top}X has a near-zero eigenvalue, and the OLS coefficient variance blows up along that direction. The point estimates are still unbiased but useless for inference. Diagnose with VIF; fix with ridge or by dropping a redundant feature.

Heteroskedasticity breaks standard errors

OLS point estimates remain unbiased under heteroskedasticity, but the textbook standard errors are wrong, so confidence intervals and p-values lie. Use heteroskedasticity-robust (sandwich, HC1, HC3) standard errors, or weighted least squares if you can model the variance structure.

Outliers dominate squared error

Squared error penalizes a residual of size rr by r2r^{2}, so a single bad point can swing β^\hat{\beta} noticeably. Use Huber regression for moderate contamination, RANSAC for adversarial outliers, or an explicit robust loss.

Standardize before regularizing

Ridge, lasso, and elastic net are not scale-invariant: a feature in millimetres gets penalized far more than the same feature in kilometres. Always standardize features (zero mean, unit variance) before running a regularized model, and never regularize the intercept.

Interview Q&A

Derive the OLS normal equations from the squared-error objective.research

Stack nn examples in X∈Rn×dX \in \mathbb{R}^{n \times d} and y∈Rny \in \mathbb{R}^{n}, and define

L(β)=12 ∥y−Xβ∥22=12 (y−Xβ)⊤(y−Xβ).\mathcal{L}(\beta) = \tfrac{1}{2}\,\lVert y - X\beta \rVert_{2}^{2} = \tfrac{1}{2}\,(y - X\beta)^{\top}(y - X\beta).

Expanding,

L(β)=12 y⊤y−β⊤X⊤y+12 β⊤X⊤Xβ.\mathcal{L}(\beta) = \tfrac{1}{2}\, y^{\top} y - \beta^{\top} X^{\top} y + \tfrac{1}{2}\,\beta^{\top} X^{\top} X \beta.

Differentiate with respect to β\beta using ∇β(β⊤Aβ)=(A+A⊤)β\nabla_{\beta}(\beta^{\top} A \beta) = (A + A^{\top})\beta and ∇β(c⊤β)=c\nabla_{\beta}(c^{\top}\beta) = c:

∇βL(β)=− X⊤y+X⊤Xβ.\nabla_{\beta}\mathcal{L}(\beta) = -\,X^{\top} y + X^{\top} X\beta.

Set ∇βL(β)=0\nabla_{\beta}\mathcal{L}(\beta) = 0 to obtain X⊤Xβ^=X⊤yX^{\top} X \hat{\beta} = X^{\top} y, and if XX has full column rank, β^=(X⊤X)−1X⊤y\hat{\beta} = (X^{\top}X)^{-1} X^{\top} y. The Hessian is X⊤X⪰0X^{\top}X \succeq 0, so L\mathcal{L} is convex and the stationary point is the global minimum. Geometrically, y^=Xβ^=PXy\hat{y} = X\hat{\beta} = P_X y is the orthogonal projection of yy onto col⁡(X)\operatorname{col}(X), and the residual r=y−y^r = y - \hat{y} satisfies X⊤r=0X^{\top} r = 0 - which is the normal equations [1].

State and prove the Gauss-Markov theorem.research

Statement. If y=Xβ+εy = X\beta + \varepsilon with XX non-random and full column rank, E[ε]=0\mathbb{E}[\varepsilon] = 0, and Cov⁡(ε)=σ2I\operatorname{Cov}(\varepsilon) = \sigma^{2} I, then among all linear unbiased estimators of β\beta, the OLS estimator β^=(X⊤X)−1X⊤y\hat{\beta} = (X^{\top}X)^{-1}X^{\top}y has the smallest variance (it is BLUE).

Proof sketch. Any linear estimator can be written β~=Cy\tilde{\beta} = C y for a fixed C∈Rd×nC \in \mathbb{R}^{d \times n}. Decompose C=(X⊤X)−1X⊤+DC = (X^{\top}X)^{-1}X^{\top} + D.

Unbiasedness. E[β~]=CXβ\mathbb{E}[\tilde{\beta}] = C X \beta, so unbiasedness for all β\beta requires CX=IC X = I. Substituting, DX=0D X = 0.

Covariance. Using Cov⁡(ε)=σ2I\operatorname{Cov}(\varepsilon) = \sigma^{2} I,

Cov⁡(β~)=σ2 CC⊤=σ2[(X⊤X)−1+(X⊤X)−1X⊤D⊤+DX(X⊤X)−1+DD⊤].\operatorname{Cov}(\tilde{\beta}) = \sigma^{2}\, C C^{\top} = \sigma^{2}\bigl[(X^{\top}X)^{-1} + (X^{\top}X)^{-1}X^{\top}D^{\top} + D X (X^{\top}X)^{-1} + D D^{\top}\bigr].

The cross terms vanish because DX=0D X = 0, leaving σ2[(X⊤X)−1+DD⊤]\sigma^{2}\bigl[(X^{\top}X)^{-1} + D D^{\top}\bigr]. Since DD⊤⪰0D D^{\top} \succeq 0,

Cov⁡(β~)−Cov⁡(β^OLS)=σ2 DD⊤⪰0,\operatorname{Cov}(\tilde{\beta}) - \operatorname{Cov}(\hat{\beta}_{\mathrm{OLS}}) = \sigma^{2}\, D D^{\top} \succeq 0,

so β^OLS\hat{\beta}_{\mathrm{OLS}} has the smallest variance in the Loewner order. Equality requires D=0D = 0. ∎

The theorem says nothing about biased estimators, which is why ridge can have strictly lower MSE than OLS in finite samples [1].

Why is minimizing MSE equivalent to MLE under Gaussian noise?onsite

Assume yi=xi⊤β+εiy_i = x_i^{\top}\beta + \varepsilon_i with εi∼N(0,σ2)\varepsilon_i \sim \mathcal{N}(0, \sigma^2) i.i.d. Then

log⁡p(y∣X,β,σ2)=∑i=1nlog⁡N(yi∣xi⊤β,σ2)=−n2log⁡(2πσ2)−12σ2∑i=1n(yi−xi⊤β)2.\log p(y \mid X, \beta, \sigma^2) = \sum_{i=1}^{n} \log \mathcal{N}(y_i \mid x_i^{\top}\beta, \sigma^2) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^{n}(y_i - x_i^{\top}\beta)^2.

The first term does not depend on β\beta, and the second is a strictly decreasing function of the squared residual norm. Therefore

arg⁡max⁡βlog⁡p(y∣X,β,σ2)=arg⁡min⁡β ∥y−Xβ∥22.\arg\max_{\beta} \log p(y \mid X, \beta, \sigma^2) = \arg\min_{\beta}\,\lVert y - X\beta \rVert_{2}^{2}.

OLS is the MLE. The same calculation explains why ridge corresponds to MAP estimation under a Gaussian prior on β\beta, and why Laplace priors give lasso [2].

Derive the ridge closed form and explain why it works when X is rank-deficient.research

The ridge objective is

L(β)=12 ∥y−Xβ∥22+λ2 ∥β∥22.\mathcal{L}(\beta) = \tfrac{1}{2}\,\lVert y - X\beta \rVert_{2}^{2} + \tfrac{\lambda}{2}\,\lVert \beta \rVert_{2}^{2}.

Take the gradient and set to zero:

∇βL(β)=−X⊤y+X⊤Xβ+λβ=0  ⟹  (X⊤X+λI)β^ridge=X⊤y.\nabla_{\beta}\mathcal{L}(\beta) = -X^{\top}y + X^{\top}X\beta + \lambda \beta = 0 \implies (X^{\top}X + \lambda I)\hat{\beta}_{\mathrm{ridge}} = X^{\top} y.

So β^ridge=(X⊤X+λI)−1X⊤y\hat{\beta}_{\mathrm{ridge}} = (X^{\top}X + \lambda I)^{-1} X^{\top} y.

Even if X⊤XX^{\top}X is singular (rank-deficient XX, or d>nd > n), the matrix X⊤X+λIX^{\top}X + \lambda I has all eigenvalues at least λ>0\lambda > 0, so it is invertible and the solution is unique. This is why ridge is the standard fix for multicollinearity and high-dimensional underdetermined regression. The SVD view X=UΣV⊤X = U\Sigma V^{\top} makes the shrinkage explicit: ridge replaces 1/sj1/s_j with sj/(sj2+λ)s_j / (s_j^{2} + \lambda), attenuating the small-singular-value directions where OLS variance is largest [4][1].

Check yourself
Score: 0 / 5(0 answered)
  1. 1. Which expression is the OLS estimator?
  2. 2. Under which assumption(s) is OLS the BLUE (Gauss-Markov)?
  3. 3. Why does ridge regression always have a unique closed-form solution?
  4. 4. Which property does lasso have that ridge does not?
  5. 5. What does it mean that OLS minimizing MSE equals MLE under Gaussian noise?
Build it yourself

A short evening project:

  1. Generate synthetic data with a known β⋆\beta^{\star} and Gaussian noise.
  2. Fit OLS, ridge, lasso, and elastic net on the same training set across a grid of λ\lambda values.
  3. Plot validation MSE vs λ\lambda and report the optimum for each model.
  4. Add a few highly correlated features and re-run; watch ridge keep them all and lasso pick one.
  5. Stretch: add deliberate heteroskedasticity, then refit OLS with HC3 robust standard errors and confirm the confidence intervals widen on high-variance regions.

Interactive Demo

Regression Fit Demo
005510101515
Fitted line
y = 1.652x + 1.447
R² = 0.9005
What to try: Drag a point far from the cluster to see how one outlier pulls the regression line.
Regression line
Residuals

References

  1. Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning, 2nd ed.. Springer. https://hastie.su.domains/ElemStatLearn/
  2. Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. ISBN 978-0-387-31073-2
  3. Tibshirani, R. (1996). Regression Shrinkage and Selection via the Lasso. Journal of the Royal Statistical Society, Series B 58(1), 267-288. doi:10.1111/j.2517-6161.1996.tb02080.x
  4. Hoerl, A. E. and Kennard, R. W. (1970). Ridge Regression: Biased Estimation for Nonorthogonal Problems. Technometrics 12(1), 55-67. doi:10.1080/00401706.1970.10488634
  5. Zou, H. and Hastie, T. (2005). Regularization and variable selection via the elastic net. Journal of the Royal Statistical Society, Series B 67(2), 301-320. doi:10.1111/j.1467-9868.2005.00503.x