How do you derive the normal equations for least-squares linear regression? When is singular, what do you do then, and what is a concrete example of a singular design?
Supervised learning, Linear regression
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.
What's being asked
Whether you can differentiate a quadratic in matrix form, and link the invertibility of to the rank of the data matrix.
Key idea
Minimising gives , i.e. . is invertible exactly when the columns of are linearly independent (full column rank). So it is singular when there are fewer samples than parameters, or when one column is a combination of others.
Steps
- Set up the notation: holds samples as rows and features as columns (including a column of ones for the intercept), holds the targets, and the weights. Expand .
- Differentiate, because at a minimum of a smooth function the gradient is zero: . Setting it to zero gives the normal equations .
- Show this is a minimum. The Hessian (matrix of second derivatives) is , which is positive semidefinite since . So is convex and any solution is a global minimum. If is invertible, the unique solution is .
- Singularity: has the same rank as , the number of linearly independent columns. It is singular (not invertible) when : either , or some columns are collinear.
- Example: columns , such as one length recorded in both metres and half-metres. Another is the dummy-variable trap: one-hot columns for every category plus an intercept, since the one-hot columns add up to the intercept column. Then infinitely many fit equally well.
- Fixes: drop the redundant columns; or use the pseudoinverse, , which picks the solution with the smallest ; or add ridge regularisation, , which is invertible for every .
Common mistakes
- Writing or putting the transposes in the wrong place. Check the shapes: must be .
- Saying “singular means no solution”. The normal equations always have a solution; it is just not unique.
- Recommending explicit matrix inversion in practice. QR or SVD is more stable numerically.
How long your answer should be
Half a page; 4–5 minutes.
Last checked on 4 December 2026.
Read this
Previous in Supervised learning: How do you derive the hard-margin SVM objective, and why is the margin , where is the weight vector?
Next in Supervised learning: What is the closed-form solution of ridge regression, , and, using the singular value decomposition , which directions does ridge shrink the most?