Multiple Linear Regression
Normal Equations Derivation
Read aloud is unavailable in this browser.
The Path to the Closed-Form Solution
With the matrix formulation established, we now face the central optimization problem: find the coefficient vector $\hat{\boldsymbol{\beta}}$ that minimizes the sum of squared errors.
Unlike many optimization problems in machine learning that require iterative algorithms, linear regression admits an exact, closed-form solution. This solution, derived from the normal equations, is one of the most elegant results in statistical learning theory.
The derivation we present here uses matrix calculus—specifically, derivatives of scalar functions with respect to vectors. This approach is not merely an exercise in mathematical sophistication; it's the foundation for understanding optimization throughout machine learning.
By the end of this page, you will derive the normal equations from first principles, understand why X⊤X must be invertible for a unique solution, and appreciate the mathematical elegance underlying ordinary least squares estimation.
The Optimization Problem
Recall from the previous page that we seek to minimize the sum of squared errors:
$$\hat{\boldsymbol{\beta}} = \underset{\boldsymbol{\beta}}{\arg\min} ; \text{SSE}(\boldsymbol{\beta}) = \underset{\boldsymbol{\beta}}{\arg\min} ; |\mathbf{y} - \mathbf{X}\boldsymbol{\beta}|^2$$
Expanding the squared norm:
$$\text{SSE}(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^\top(\mathbf{y} - \mathbf{X}\boldsymbol{\beta})$$
This is a quadratic function of $\boldsymbol{\beta}$. Quadratic functions with positive semi-definite Hessian matrices have a unique global minimum (or a set of minima if the Hessian is singular). Our task is to find this minimum analytically.
A closed-form solution means we can directly compute the answer in a finite number of operations—no iterations, no convergence criteria, no hyperparameter tuning. For linear regression, this means exact answers subject only to numerical precision limits.
Matrix Calculus Prerequisites
Before deriving the normal equations, we need key results from matrix calculus. Let $\mathbf{x}$ be a column vector and $f(\mathbf{x})$ a scalar function. The gradient $ abla_{\mathbf{x}} f$ is a vector of partial derivatives:
$$ abla_{\mathbf{x}} f = \begin{bmatrix} \frac{\partial f}{\partial x_1} \ \frac{\partial f}{\partial x_2} \ \vdots \ \frac{\partial f}{\partial x_n} \end{bmatrix}$$
Here are the essential identities we'll use:
| Function | Gradient | Notes |
|---|---|---|
| $f(\mathbf{x}) = \mathbf{a}^\top\mathbf{x}$ | $ abla_\mathbf{x} f = \mathbf{a}$ | Linear function |
| $f(\mathbf{x}) = \mathbf{x}^\top\mathbf{a}$ | $ abla_\mathbf{x} f = \mathbf{a}$ | Same as above (scalar) |
| $f(\mathbf{x}) = \mathbf{x}^\top\mathbf{A}\mathbf{x}$ | $ abla_\mathbf{x} f = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x}$ | Quadratic form |
| $f(\mathbf{x}) = \mathbf{x}^\top\mathbf{A}\mathbf{x}$ (A symmetric) | $ abla_\mathbf{x} f = 2\mathbf{A}\mathbf{x}$ | Symmetric case |
The Gram matrix X⊤X is always symmetric. This means we can use the simpler gradient formula for quadratic forms: ∇(x⊤Ax) = 2Ax when A is symmetric.
Proof sketch for the quadratic form gradient:
For $f(\mathbf{x}) = \mathbf{x}^\top\mathbf{A}\mathbf{x}$, we can expand: $$f(\mathbf{x}) = \sum_{i}\sum_{j} a_{ij} x_i x_j$$
Taking the partial derivative with respect to $x_k$: $$\frac{\partial f}{\partial x_k} = \sum_{j} a_{kj} x_j + \sum_{i} a_{ik} x_i = (\mathbf{A}\mathbf{x})_k + (\mathbf{A}^\top\mathbf{x})_k$$
Collecting terms: $ abla_\mathbf{x} f = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x}$. When $\mathbf{A} = \mathbf{A}^\top$, this simplifies to $2\mathbf{A}\mathbf{x}$.
Expanding the Objective Function
Let's expand the SSE objective to identify its structure:
$$\begin{aligned} \text{SSE}(\boldsymbol{\beta}) &= (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^\top(\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) &= \mathbf{y}^\top\mathbf{y} - \mathbf{y}^\top\mathbf{X}\boldsymbol{\beta} - \boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y} + \boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta} \end{aligned}$$
Now, $\mathbf{y}^\top\mathbf{X}\boldsymbol{\beta}$ is a scalar. The transpose of a scalar equals itself, so: $$\mathbf{y}^\top\mathbf{X}\boldsymbol{\beta} = (\mathbf{y}^\top\mathbf{X}\boldsymbol{\beta})^\top = \boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y}$$
Therefore, we can combine the middle terms: $$\text{SSE}(\boldsymbol{\beta}) = \mathbf{y}^\top\mathbf{y} - 2\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y} + \boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta}$$
Identifying the components:
- $\mathbf{y}^\top\mathbf{y}$ — A constant (doesn't depend on $\boldsymbol{\beta}$)
- $-2\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y}$ — Linear in $\boldsymbol{\beta}$
- $\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta}$ — Quadratic in $\boldsymbol{\beta}$
This is a quadratic function of $\boldsymbol{\beta}$ with:
- Linear coefficient: $-2\mathbf{X}^\top\mathbf{y}$
- Quadratic coefficient: $\mathbf{X}^\top\mathbf{X}$ (the Gram matrix)
The Gram matrix $\mathbf{X}^\top\mathbf{X}$ is symmetric and positive semi-definite, ensuring the objective is convex (bowl-shaped upward).
SSE(β) is a convex function of β because the Hessian matrix (2X⊤X) is positive semi-definite. Any local minimum is a global minimum. If X⊤X is positive definite (full column rank), the minimum is unique.
Taking the Gradient
To find the minimum, we take the gradient with respect to $\boldsymbol{\beta}$ and set it to zero.
$$ abla_{\boldsymbol{\beta}} \text{SSE} = abla_{\boldsymbol{\beta}} \left[ \mathbf{y}^\top\mathbf{y} - 2\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y} + \boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta} \right]$$
Applying our matrix calculus identities term by term:
-
$ abla_{\boldsymbol{\beta}}(\mathbf{y}^\top\mathbf{y}) = \mathbf{0}$ (constant term)
-
$ abla_{\boldsymbol{\beta}}(-2\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{y}) = -2\mathbf{X}^\top\mathbf{y}$ (linear term)
-
$ abla_{\boldsymbol{\beta}}(\boldsymbol{\beta}^\top\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta}) = 2\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta}$ (quadratic term, using symmetry of $\mathbf{X}^\top\mathbf{X}$)
Combining: $$ abla_{\boldsymbol{\beta}} \text{SSE} = -2\mathbf{X}^\top\mathbf{y} + 2\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta}$$
∇SSE = 2X⊤Xβ - 2X⊤y. This gradient tells us the direction of steepest ascent at any point β. We seek where this gradient is zero—the bottom of the bowl.
Deriving the Normal Equations
Setting the gradient to zero gives the first-order optimality condition:
$$ abla_{\boldsymbol{\beta}} \text{SSE} = \mathbf{0}$$
$$-2\mathbf{X}^\top\mathbf{y} + 2\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta} = \mathbf{0}$$
Dividing by 2 and rearranging:
$$\boxed{\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta} = \mathbf{X}^\top\mathbf{y}}$$
These are the normal equations.
The term 'normal' comes from the fact that these equations ensure the residual vector e = y - Xβ̂ is orthogonal (normal) to the column space of X. We'll explore this geometric interpretation in detail in a later page.
Solving for $\hat{\boldsymbol{\beta}}$:
If $\mathbf{X}^\top\mathbf{X}$ is invertible (i.e., has full rank), we can multiply both sides by $(\mathbf{X}^\top\mathbf{X})^{-1}$:
$$\boldsymbol{\beta} = (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}$$
This gives us the OLS estimator:
$$\boxed{\hat{\boldsymbol{\beta}} = (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{y}}$$
This is one of the most important formulas in statistics and machine learning. It expresses the optimal coefficients directly in terms of the data matrices.
Verifying This is a Minimum
Setting the gradient to zero finds stationary points—which could be minima, maxima, or saddle points. We must verify we have a minimum by examining the Hessian matrix (the matrix of second derivatives).
$$\mathbf{H} = abla^2_{\boldsymbol{\beta}} \text{SSE} = abla_{\boldsymbol{\beta}} \left( 2\mathbf{X}^\top\mathbf{X}\boldsymbol{\beta} - 2\mathbf{X}^\top\mathbf{y} \right) = 2\mathbf{X}^\top\mathbf{X}$$
The Hessian is constant (independent of $\boldsymbol{\beta}$) and equals $2\mathbf{X}^\top\mathbf{X}$.
Second-order sufficiency conditions:
For a stationary point to be a minimum, the Hessian must be positive semi-definite (for weak minimum) or positive definite (for strict, unique minimum).
Recall that $\mathbf{X}^\top\mathbf{X}$ is always positive semi-definite: $$\mathbf{v}^\top(\mathbf{X}^\top\mathbf{X})\mathbf{v} = (\mathbf{X}\mathbf{v})^\top(\mathbf{X}\mathbf{v}) = |\mathbf{X}\mathbf{v}|^2 \geq 0$$
Furthermore, $\mathbf{X}^\top\mathbf{X}$ is positive definite if and only if $\mathbf{X}$ has full column rank (i.e., no two columns are linearly dependent).
Conclusion: The stationary point $\hat{\boldsymbol{\beta}}$ is indeed a global minimum. If $\mathbf{X}^\top\mathbf{X}$ is invertible, it's the unique global minimum.
The solution β̂ = (X⊤X)⁻¹X⊤y minimizes the sum of squared errors globally. There is no other set of coefficients that achieves lower SSE.
When X⊤X Is Not Invertible
The OLS formula requires $(\mathbf{X}^\top\mathbf{X})^{-1}$ to exist, which happens when $\mathbf{X}^\top\mathbf{X}$ has full rank. This fails in several important scenarios:
- •More features than samples (p > n): The design matrix is wide, columns must be linearly dependent.
- •Perfect multicollinearity: One feature is an exact linear combination of others (e.g., total_sales = online_sales + store_sales).
- •Duplicate features: The same feature included twice under different names.
- •Constant features: A feature with zero variance (all values identical).
- •Encoding issues: One-hot encoding all categories without dropping a reference level.
Consequences:
When $\mathbf{X}^\top\mathbf{X}$ is singular:
- Infinitely many solutions — The normal equations have a solution but it's not unique
- Unstable estimates — Small data changes cause wild coefficient swings
- Numerical issues — Even near-singularity causes precision problems
Solutions:
- Regularization: Ridge regression adds $\lambda\mathbf{I}$ to $\mathbf{X}^\top\mathbf{X}$, ensuring invertibility
- Feature selection: Remove redundant or perfectly correlated features
- Dimensionality reduction: Apply PCA before regression
- Moore-Penrose pseudoinverse: Provides a minimum-norm solution even when singular
Modern statistical software (scikit-learn, R, statsmodels) usually detects rank deficiency and either warns or uses automatic regularization. However, near-singularity (high condition number) can go undetected and cause subtle problems. Check the condition number of X⊤X when coefficients seem unreasonable.
KEEP LEARNING
There’s more to understand.
You’re reading the introduction. Create a free account to read the complete lesson, save notes and keep track of your progress.