Linear Algebra · Least squares

Least squares

Least squares fits a line to data by making the sum of the squared vertical errors as small as possible. Geometrically, the fitted values are the projection of the data vector onto the column space, and the errors are at right angles to it.

Least squares in this visualization

Three points rarely lie on one line, so any line misses some of them. On the left are the points, the best line and its residuals, the vertical misses whose squares add up to SSE. On the right the same three heights form one vector in space, and finding the best line means projecting that vector onto a plane. You can move the points, try a line of your own and compare two simpler models.

The sum of squared residuals
SSE=∑i=13(yi−c−mxi)2\mathrm{SSE} = \sum_{i=1}^{3} (y_i - c - m x_i)^2

Why is the data a single point? Write the three heights as one vector b⃗=(y1,y2,y3)\vec b = (y_1, y_2, y_3). That is the blue arrow on the right, and its axes are named y1,y2,y3y_1, y_2, y_3 because each one measures a height. A line takes the values c+mxic + m x_i at the three xix_i, which is the vector c 1+m x⃗c\,\mathbf 1 + m\,\vec x with 1=(1,1,1)\mathbf 1 = (1, 1, 1). In the general model y=c+mxy = c + mx, with the xix_i not all equal, these vectors fill the glass plane through 1\mathbf 1 and x⃗\vec x as cc and mm vary. So every line you could draw on the left is one point of that plane.

Pick any line. Its residuals are the coordinates of b⃗\vec b minus those of its point in the plane, so its SSE is the squared distance between the two. The best line therefore sits at the point p⃗\vec p of the plane nearest to b⃗\vec b, the orthogonal projection. The residual vector r⃗=b⃗−p⃗\vec r = \vec b - \vec p meets the plane at a right angle, so it is perpendicular to both 1\mathbf 1 and x⃗\vec x. Written out, the two right angles are the normal equations:

The normal equations
r⃗⋅1=∑ri=0,r⃗⋅x⃗=∑xiri=0\vec r\cdot\mathbf 1 = \sum r_i = 0, \qquad \vec r\cdot\vec x = \sum x_i r_i = 0

With Right angles and balance on, the residuals become equally stiff vertical springs. Their net force is proportional to ∑ri\sum r_i, and their torque about the origin to ∑xiri\sum x_i r_i. The general fit makes both zero. The constant model needs only the net force to vanish, and the line through the origin only the torque.

Your line, drawn in red, is always a line of the current model, so its point q⃗\vec q lies in the same plane or line as p⃗\vec p. The step from p⃗\vec p to q⃗\vec q lies there too, while r⃗\vec r is perpendicular to it, so Pythagoras splits your error exactly. Your SSE reaches the minimum exactly when q⃗=p⃗\vec q = \vec p.

Your line against the best one
SSE(q⃗)=SSEmin⁡+∣p⃗−q⃗∣2\mathrm{SSE}(\vec q) = \mathrm{SSE}_{\min} + |\vec p - \vec q|^2

The two smaller models keep one column each. Fitting y=cy = c projects b⃗\vec b onto the line through 1\mathbf 1, and cc comes out as the mean of the heights. Fitting y=mxy = mx projects onto the line through x⃗\vec x, or just onto the origin when every xix_i is 0. When the xix_i are not all equal, these are two different lines in the plane. Neither model contains the other, and the general line does at least as well as both. The same projection is at the heart of linear regression.

Things to notice

  • The residuals are vertical. Least squares measures the miss in yy at each xx, not the shortest distance from a point to the line.
  • In the general model, when all three xix_i are equal, the plane shrinks to the line through 1\mathbf 1. Every line through the hollow ring fits equally well, so the best line is not unique, although p⃗\vec p still is.
  • Parameter plane shows the general model's SSE as a function of cc and mm. Each data point gives a line of pairs (c,m)(c, m) that fit it exactly, and blue marks the current model's best parameters.

Controls

  • The right picture only turns and zooms. Drag the points up and down on the left, and type an xx in the panel to move a point sideways.

Related