The Geometry of Least Squares and Gradient Descent

Least squares projects the label vector onto the subspace spanned by the feature rows, so the residual is orthogonal to every feature. When inverting XXᵀ is too costly, gradient descent walks downhill on the squared error instead; stochastic gradient descent uses one point or a small batch per step, trading noisy steps for cheap ones.

Machine Learning Techniques

The least-squares formula w∗=(XX⊤)−1Xyw^* = (XX^\top)^{-1}Xy came out of setting a gradient to zero. This chapter looks at it twice more: once as geometry, which explains what least squares is doing, and once as an algorithm, which explains how to compute it when the formula is too expensive.

Least squares is a projection

Change the point of view. Instead of nn points in dd-dimensional feature space, think of nn-dimensional vectors, one entry per training example.

  • The labels form one vector y∈Rny \in \mathbb{R}^n.
  • Each feature also forms a vector in Rn\mathbb{R}^n: its values across the nn examples. These are the rows of XX, or the columns of X⊤X^\top.
  • Any prediction vector X⊤w=∑jwj (feature j)X^\top w = \sum_j w_j\,(\text{feature } j) is a linear combination of the feature vectors. As ww varies, the predictions sweep out the column space of X⊤X^\top, a subspace of Rn\mathbb{R}^n of dimension at most dd.

Least squares picks the point of that subspace closest to yy:

min⁡w∥X⊤w−y∥2.\min_w \lVert X^\top w - y \rVert^2 .

The closest point of a subspace to a vector is its orthogonal projection, the same idea as finding a proxy in PCA, now in Rn\mathbb{R}^n. At the projection, the residual r=y−X⊤w∗r = y - X^\top w^* is perpendicular to the whole subspace, which means perpendicular to every feature vector:

X(y−X⊤w∗)=0⟺XX⊤w∗=Xy.X\big(y - X^\top w^*\big) = 0 \quad\Longleftrightarrow\quad XX^\top w^* = Xy .

These are exactly the normal equations. The algebra and the geometry agree: least squares projects the labels onto the span of the features, and whatever part of yy cannot be expressed through the features is left over as a residual orthogonal to all of them.

A plane spanned by two feature vectors f1 and f2, a label vector y rising out of the plane, its orthogonal projection onto the plane labelled X transpose w star, and the residual from the projection up to y, perpendicular to the plane
Least squares as projection in ℝⁿ. Predictions live in the plane spanned by the feature vectors; the best prediction is the projection of y, and the residual is perpendicular to every feature.

Two useful consequences follow.

  • If a feature is the constant 1 (the intercept), the residual is orthogonal to the all-ones vector, so the residuals sum to zero: the fitted line passes through the mean of the data.
  • Adding a feature can only enlarge the subspace, so the training error can only fall or stay the same. That is why training error alone can never tell you to stop adding features.

When the formula is too expensive

Computing w∗w^* directly means forming the d×dd \times d matrix XX⊤XX^\top (about nd2nd^2 operations) and solving a d×dd \times d system (about d3d^3). With millions of features, or data too large to hold in memory, that is impractical. The alternative is to approach the minimum step by step.

Gradient descent

The gradient of the loss points in the direction of steepest increase. Take a small step the other way, and repeat:

w(t+1)=w(t)−η ∇L(w(t))=w(t)−2η X(X⊤w(t)−y),w^{(t+1)} = w^{(t)} - \eta\, \nabla L(w^{(t)}) = w^{(t)} - 2\eta\, X\big(X^\top w^{(t)} - y\big),

where η>0\eta > 0 is the learning rate (step size). Each step costs one pass over the data, about ndnd operations, and needs no matrix inverse. Because the squared-error loss is a convex bowl, gradient descent with a small enough η\eta converges to the global minimum.

The step size matters.

  • Too small: progress is slow; thousands of steps to cross a gentle slope.
  • Too large: the steps overshoot the bottom of the bowl and can grow without limit (for least squares, divergence starts once η\eta exceeds 1/λmax⁡(XX⊤)1/\lambda_{\max}(XX^\top), where λmax⁡\lambda_{\max} is the largest eigenvalue).
  • A long, narrow bowl (features on very different scales) makes gradient descent zigzag. Standardising the features (zero mean, unit variance) rounds the bowl and speeds it up.

Stochastic gradient descent

The full gradient is a sum over all nn points. Stochastic gradient descent (SGD) estimates it from a single randomly chosen point, or a small random mini-batch BB:

w(t+1)=w(t)−η n∣B∣∑i∈B2 xi(xi⊤w(t)−yi).w^{(t+1)} = w^{(t)} - \eta\, \frac{n}{|B|}\sum_{i \in B} 2\,x_i\big(x_i^\top w^{(t)} - y_i\big).

The estimate is noisy but correct on average (unbiased), and each step is cheap: it touches ∣B∣|B| points instead of nn. In the time one full-gradient step takes, SGD makes n/∣B∣n/|B| steps, which usually wins by a wide margin on large data. The noise means SGD does not settle exactly at the minimum with a fixed step size; it hovers around it. Shrinking η\eta over time, or averaging the recent iterates, brings it home. This is the same algorithm that trains neural networks, where no closed form exists at all.

Try it yourself
Deep Learning Lab: optimizer playground →
Watch plain gradient descent and its variants walk down an error surface; try a learning rate that is too small and one that is too large.
MediumLeast squaresGeometry

Show that with an intercept feature, the least-squares residuals sum to zero.

MediumGradient descent

Gradient descent on least squares diverges. Name two likely causes and their fixes.

EasySGD

Why does SGD use far less computation per step than gradient descent, and what does it give up?