Classical ML

Normal Equation vs Gradient Descent

Comparing closed form analytical matrix inversion against iterative gradient descent for linear regression.

🟡 intermediate4 min readoptimization
The Normal Equation provides an exact closed form analytical solution for Linear Regression: w = (X^T X)^-1 X^T y. While the Normal Equation computes exact optimal weights in a single matrix operation without tuning a learning rate, it requires inverting an n x n feature matrix with cubic complexity O(n^3). Gradient Descent scales far better to high dimensional datasets with millions of features or samples.

Solving Linear Regression

In Ordinary Least Squares Linear Regression, we want to find weight vector $w$ that minimizes Mean Squared Error:

$$J(w) = \frac{1}{2m} | Xw - y |^2$$

We can solve this problem in two completely different ways:

┌──────────────────────────┬──────────────────────────┐
│ 1. NORMAL EQUATION       │ 2. GRADIENT DESCENT      │
├──────────────────────────┼──────────────────────────┤
│ Closed-form analytical   │ Iterative optimization   │
│ matrix math solution.    │ step by step.            │
│ Exact answer in 1 step!  │ Requires tuning rate η.  │
│ Slow for large features. │ Scales to millions!      │
└──────────────────────────┴──────────────────────────┘

1. The Normal Equation (Analytical Solution)

Setting the derivative $\nabla_w J(w) = 0$ yields an exact closed form equation:

$$w = (X^T X)^{-1} X^T y$$

Advantages of Normal Equation

  1. Zero Iterations: Computes the exact global minimum in a single step.
  2. Zero Hyperparameters: You do not need to pick or tune a learning rate $\eta$.
  3. No Feature Scaling Required: Works identically whether features are normalized or un-normalized.

Disadvantages of Normal Equation

  1. High Computational Complexity: Computing $(X^T X)^{-1}$ requires matrix inversion of shape $[n \times n]$. Matrix inversion takes $\mathcal{O}(n^3)$ computational time.
  2. Singular Matrix Risk: If features are linearly dependent or $n > m$, $X^T X$ is singular and cannot be inverted directly (requires Moore-Penrose Pseudo-Inverse via SVD).

2. Gradient Descent (Iterative Solution)

Updates weights iteratively:

$$w_{\text{new}} = w_{\text{old}} - \eta \frac{1}{m} X^T (X w - y)$$

Computational complexity per iteration: $\mathcal{O}(m \cdot n)$.

Detailed Comparison Matrix

DimensionNormal EquationGradient Descent
Solution TypeClosed Form AnalyticalIterative Approximation
Learning Rate $\eta$Not RequiredMandatory to tune
Iterations1 StepHundreds to Thousands
Feature Count Scaling ($n$)Slow $\mathcal{O}(n^3)$ (Fails if $n > 10,000$)Fast $\mathcal{O}(n)$ (Scales to Millions)
Sample Count Scaling ($m$)Linear $\mathcal{O}(m)$Scales via Mini Batches
Feature ScalingNot NeededMandatory for fast convergence

Say this out loud

The Normal Equation solves linear regression analytically in one step using w = (X^T X)^-1 X^T y without tuning a learning rate. However, matrix inversion has O(n^3) cubic complexity, making it impractically slow for large feature counts. Gradient Descent uses iterative O(n) steps, scaling smoothly to high dimensional datasets.

Followups to expect

  1. How does Ridge L2 regularization change the Normal Equation? L2 regularization adds $\lambda I$ to $X^T X$: $w = (X^T X + \lambda I)^{-1} X^T y$. This guarantees $(X^T X + \lambda I)$ is invertible even when $n > m$ or features are collinear!
  2. Why is pseudo-inverse used in practice for Normal Equation? np.linalg.pinv uses Singular Value Decomposition (SVD) to compute the pseudo-inverse, handling non invertible singular matrices cleanly.

Check yourself

Question 1 of 3

What is the exact closed form Normal Equation formula for Ordinary Least Squares Linear Regression?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min