Matrix derivation of least square minimizer
Part of Math Camp: An Introduction to Mathematics and Statistics with Applications in Economics.
Main Stam4Econ site | View this section in the book | Author: Fan Wang | DOI: 10.5281/zenodo.24696926
Date: 2026-11-30 | HTML | PDF | QMD
1 Matrix derivation of least square minimizer
1.1 Minimizing the residual sum of squares
Given matrix \(X\) that is \(n\) by \(k\) where \(n\) is the number of observations and \(k\) is the number of variables to be included on the “right-hand side” of the regression, and given vector \(y\) that is \(n\) by \(1\) representing the outcome variable, we want to find the vector \(\beta\) that is \(k\) by \(1\) that minimizes the residual sum of squares (RSS). Residual sum of squares is a scalar value, so this is a scalar-valued function with a vector input.
The residual sum of squares can be expressed as, using summation notation, and not using vector notation:
\[ RSS(\beta) = \sum_{i=1}^{n} (y_i - \sum_{j=1}^k x_{ij} \beta_j )^2 \]
where \(y_i\) is the \(i\)-th observed value of the outcome variable, \(x_{ij}\) is the value of the \(j\)-th predictor variable for the \(i\)-th observation, and \(\beta_j\) is the coefficient for the \(j\)-th predictor variable.
Using vector notation, we can express the residual sum of squares as:
\[ RSS(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \]
Suppose \(n=2\) and \(k=2\), then we can write out the matrix formula out explicitly as:
\[ RSS(\boldsymbol{\beta}) = \begin{bmatrix} y_1 - (x_{11}\beta_1 + x_{12}\beta_2) & y_2 - (x_{21}\beta_1 + x_{22}\beta_2) \end{bmatrix} \begin{bmatrix} y_1 - (x_{11}\beta_1 + x_{12}\beta_2) \\ y_2 - (x_{21}\beta_1 + x_{22}\beta_2) \end{bmatrix} \]
1.2 Minimizing the residual sum of squares, partial derivative approach
For solve for the vector \(\beta\) that minimizes the residual sum of squares, we can take the derivative of the RSS function with respect to \(\beta\), set it equal to zero, and solve for \(\beta\).
We can do this with the summation based formula, and take derivative with respect to each \(\beta_j\) one by one. We have:
\[ \frac{\partial RSS(\boldsymbol{\beta})}{\partial \beta_j} = -2 \sum_{i=1}^{n} x_{ij} (y_i - \sum_{m=1}^{k} x_{im} \beta_m) = 0 \]
The equations above represents a system of linear equations with \(k\) equations and \(k\) unknowns, which can be solved using standard methods for solving systems of linear equations, which looks cumbersome, and still ends up requiring matrix algebra to solve linear system.
1.3 Minimizing the residual sum of squares, using the product rule
We know directly use the matrix differential product rule and take the derivative of the RSS function with respect to \(\beta\).
We start with the RSS function:
\[ RSS(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \]
First, we distribute the terms using the distributive property of matrix multiplication:
\[ RSS(\boldsymbol{\beta}) = \mathbf{y}^T\mathbf{y} - 2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \]
Second, we apply the matrix differential product rule to take the derivative of each term with respect to \(\boldsymbol{\beta}\). For the first term, \(\mathbf{y}^T\mathbf{y}\), it is a constant with respect to \(\boldsymbol{\beta}\), so its derivative is zero. For the second term, \(-2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y}\), we treat \(\mathbf{X}^T\mathbf{y}\) as a constant vector and apply the product rule:
\[ \frac{\partial (-2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y})}{\partial \boldsymbol{\beta}} = -2\mathbf{X}^T\mathbf{y} \]
Third, for the third term, \(\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta}\), we treat \(\mathbf{X}^T\mathbf{X}\) as a constant matrix and apply the product rule:
\[ \frac{\partial (\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} = 2\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \]
1.4 Minimizing the residual sum of squares, more steps
A less efficient way to find the derivative is to not use the product rule and work out things in more granular steps. Using the matrix formula for RSS, we can expand the expression as follows using the transpose and distributive properties of matrix multiplication:
\[ RSS(\boldsymbol{\beta}) = \mathbf{y}^T\mathbf{y} - 2\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{y} + \boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} \]
Now we can consider the \(d(RSS(\boldsymbol{\beta}))\), the differential of the RSS function with respect to \(\boldsymbol{\beta}\). Applying the distributive and inverse rules, we have:
\[\begin{align} RSS(\boldsymbol{\beta}+d\boldsymbol{\beta})-RSS(\boldsymbol{\beta}) &= (\mathbf{y} - \mathbf{X}(\boldsymbol{\beta} + d\boldsymbol{\beta}))^T (\mathbf{y} - \mathbf{X}(\boldsymbol{\beta} + d\boldsymbol{\beta})) - (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) \\ &= -2 d\boldsymbol{\beta}^T \mathbf{X}^T (\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) + d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} d\boldsymbol{\beta} \\ &= -2 d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{y} + 2 d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} + d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} d\boldsymbol{\beta} \end{align}\]
The last term \(d\boldsymbol{\beta}^T \mathbf{X}^T \mathbf{X} d\boldsymbol{\beta}\) is second order in the increment and does not contribute to the differential. We are left with the first two terms, which we can express as:
\[ d(RSS(\boldsymbol{\beta})) = \left(-2 \mathbf{X}^T \mathbf{y} + 2 \mathbf{X}^T \mathbf{X} \boldsymbol{\beta}\right)^T d\boldsymbol{\beta} \]
This means that the gradient of the RSS function with respect to \(\boldsymbol{\beta}\) is:
\[ \nabla RSS(\boldsymbol{\beta}) = -2 \mathbf{X}^T \mathbf{y} + 2 \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} \]
If \(\mathbf{X}\) has full column rank, then \(\mathbf{X}^T\mathbf{X}\) is invertible. Setting the gradient to zero and multiplying by its inverse gives:
\[\begin{align} -2 \mathbf{X}^T \mathbf{y} + 2 \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} &= 0\\ \mathbf{X}^T \mathbf{X} \boldsymbol{\beta} &= \mathbf{X}^T \mathbf{y}\\ \boldsymbol{\beta} &= (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}\\ \end{align}\]
If \(\mathbf{X}\) is rank deficient, the normal equations still characterize least-squares minimizers, but the coefficient vector need not be unique; a generalized inverse can be used to select a solution.