How do you derive the normal equations for least-squares linear regression? When is X⊤X 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 X⊤X to the rank of the data matrix.

Key idea

Minimising J(w)=∥y−Xw∥2 gives ∇J=−2X⊤(y−Xw)=0, i.e. X⊤Xw=X⊤y. X⊤X is invertible exactly when the columns of X 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

  1. Set up the notation: X∈Rn×d holds n samples as rows and d features as columns (including a column of ones for the intercept), y∈Rn holds the targets, and w∈Rd the weights. Expand J(w)=(y−Xw)⊤(y−Xw)=y⊤y−2w⊤X⊤y+w⊤X⊤Xw.
  2. Differentiate, because at a minimum of a smooth function the gradient is zero: ∇wJ=−2X⊤y+2X⊤Xw. Setting it to zero gives the normal equations X⊤Xw=X⊤y.
  3. Show this is a minimum. The Hessian (matrix of second derivatives) is 2X⊤X, which is positive semidefinite since v⊤X⊤Xv=∥Xv∥2≥0. So J is convex and any solution is a global minimum. If X⊤X is invertible, the unique solution is w^=(X⊤X)−1X⊤y.
  4. Singularity: X⊤X has the same rank as X, the number of linearly independent columns. It is singular (not invertible) when rank⁡(X)<d: either n<d, or some columns are collinear.
  5. Example: columns [1,x,2x], 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 w fit equally well.
  6. Fixes: drop the redundant columns; or use the pseudoinverse, w^=X+y, which picks the solution with the smallest ∥w∥; or add ridge regularisation, (X⊤X+λI)−1X⊤y, which is invertible for every λ>0.

Common mistakes

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 2∥w∥, where w is the weight vector?
Next in Supervised learning: What is the closed-form solution of ridge regression, min⁡w∥y−Xw∥2+λ∥w∥2, and, using the singular value decomposition X=UDV⊤, which directions does ridge shrink the most?