What is the Normal Equation in matrix form, and how does its cost compare with gradient descent?

Quick Overview

Derive the Normal Equation for ordinary least squares in matrix form and compare its computational cost with gradient descent. Tests linear algebra behind linear regression, complexity in rows and features, and numerical stability when features are collinear.

What is the Normal Equation in matrix form, and how does its cost compare with gradient descent?

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Consider ordinary least squares linear regression with a design matrix $X$ of $n$ rows and $p$ features and a target vector $y$. What is the Normal Equation in matrix form, and what are the computational trade-offs of using it versus an iterative solver like Gradient Descent? ```hint Derive it Write the squared-error loss in matrix form, take its gradient with respect to $\beta$, and set it to zero. ``` ```hint Count the work Look at the shapes of the matrices you have to build and solve with, then compare that with the cost of one pass over the data and the number of passes an iterative method needs. ``` ### What a Strong Answer Covers - The formula and where it comes from, including the condition needed for it to have a unique solution. - The cost of forming and solving the system in terms of $n$ and $p$, compared with the per-iteration cost and iteration count of gradient descent. - What happens when $X^\top X$ is singular or ill-conditioned, and how practical solvers handle it. - What gradient descent needs in return (learning rate, feature scaling, a stopping rule) and when each method is the better fit. ### Follow-up Questions - Why do numerical libraries solve least squares with QR or SVD instead of computing $(X^\top X)^{-1}$? - How does an L2 penalty change the closed form, and why does it fix the singular case? - With 10 million rows and 200 features that do not fit in memory, which approach would you pick and how would you compute it? - Why does logistic regression have no normal equation, and what solvers are used instead?

Overview: Derive the Normal Equation for ordinary least squares in matrix form and compare its computational cost with gradient descent. Tests linear algebra behind linear regression, complexity in rows and features, and numerical stability when features are collinear.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Consider ordinary least squares linear regression with a design matrix XX of nn rows and pp features and a target vector yy. What is the Normal Equation in matrix form, and what are the computational trade-offs of using it versus an iterative solver like Gradient Descent?

What a Strong Answer Covers Guidance

  • The formula and where it comes from, including the condition needed for it to have a unique solution.
  • The cost of forming and solving the system in terms of nn and pp , compared with the per-iteration cost and iteration count of gradient descent.
  • What happens when X⊤XX^\top X is singular or ill-conditioned, and how practical solvers handle it.
  • What gradient descent needs in return (learning rate, feature scaling, a stopping rule) and when each method is the better fit.

Follow-up Questions Guidance

  • Why do numerical libraries solve least squares with QR or SVD instead of computing (X⊤X)−1(X^\top X)^{-1} ?
  • How does an L2 penalty change the closed form, and why does it fix the singular case?
  • With 10 million rows and 200 features that do not fit in memory, which approach would you pick and how would you compute it?
  • Why does logistic regression have no normal equation, and what solvers are used instead?