6.3 Least Squares: Beat the Normal Equations
Seven measurements: , , , , , , . One model: a plane . Each data point would like the coefficients to satisfy — and read that way, each measurement is a linear equation in the unknowns : seven planes in coefficient space. Chapter 1 says three independent planes already pin down a point, so seven generically share nothing. The system is overdetermined and inconsistent; no plane passes through all seven points, and the interesting question is which plane misses them least.
"Least" needs a score. The residual of point is the vertical gap , and the score is the sum of squared residuals over all seven points — squared so that misses above and below both count against you, and big misses count hardest. Your job in the first figure: tune , , by hand and drive the score as low as you can. Then find out whether you beat the machine. (You will not. The point is understanding why.)
Explore in 3D (opens in a new tab)Seven small white spheres scattered in 3D space around a tilted, semi-transparent plane through the origin. Two vertical red segments connect two of the spheres to the plane, showing how far the plane misses them vertically. A second, better-fitting plane is included but hidden; the full XYZ 3D editor can reveal it.
Explore the figure
- Orbit the data and read the two drawn residuals. Above the plane sits at while the point is at — a gap of above. At the plane is at and the point at — a gap of below.
- Judge all seven points: five sit above the starting plane, only two below. A plane that splits its misses is doing better than one that is systematically low — this one is too low, and too flat in the direction. Its score is .
- Hiding objects and editing expressions happen in the full editor: choose Open in XYZ 3D ↗ under the figure and work there from here on. Hide the two objects labeled residual at and residual at — they are drawn for the starting plane only and will not follow your edits. Then edit the plane's expression: try first, and watch the plane move as you retype. Better or worse? How can you tell from the picture alone?
- Keep tuning — treat the three coefficients like three sliders and iterate until you cannot visibly improve. Hand fits usually land near –, –, –, scoring somewhere between and . Write your three numbers down.
- Now, still in the editor, reveal the hidden object labeled least-squares plane and orbit. How close did you come — and can you find any single coefficient change that would visibly improve on it?
The normal equations
Your tuning loop was a search of a subspace. Collect the seven heights a candidate plane produces into a vector in : it equals times the column of 's, plus times the column of 's, plus times a column of ones. As range over all values, these vectors sweep out the span of three columns — a three-dimensional subspace of — and you were hunting the point of it closest to the data vector . Section 6.1 answered that hunt in general: closest means the residual vector is perpendicular to the subspace, i.e. to each of the three spanning columns. Writing out those three dot products — , , — and substituting the definition of gives the normal equations:
Where those nine coefficients come from. Stack the seven measurements into a matrix — a column of , a column of , a column of ones — and the three dot-product conditions are exactly , computed here from the measurements themselves rather than summed by hand.
And the three numbers on the right: , the same seven measurements dotted against the same three columns. Between this and the matrix above, every number in the system is now derived from the data — so a slip in one of the twelve dot-product sums would stop the build rather than quietly solve the wrong system correctly.
Three equations, three unknowns, one solution — the whole search compressed onto an index card.
The three normal equations as an augmented matrix, reduced to solved form by a computer algebra system. The final column is the winning plane in exact arithmetic: , , — the fractions that round to the , , worked out above.
The winner scores — nearly fifty times better than the starting plane's , and better than any hand fit. Its seven residuals are , , , , , , : small everywhere, mixed in sign, and summing to zero — the visible face of the perpendicularity conditions, since is literally one of them.
Feel the trade-off
The second figure replays the contest with the coefficients on actual sliders and the two residual segments now live — they stretch and shrink as you drag.
Explore in 3D (opens in a new tab)Seven small white spheres scattered in 3D space around a tilted, semi-transparent plane. Three sliders named a, b, and c set the plane's height: a times x, plus b times y, plus c. Two vertical red segments connect two of the spheres to the plane and stretch or shrink live as the sliders move. A second, better-fitting plane is included but hidden; the full XYZ 3D editor can reveal it.
- From the starting setting , drag upward and watch the residual at shrink. At first the one at shrinks too, closing completely at . Keep going: once the plane dips below that miss grows again, and by the time the gap at closes, at , it has doubled to . Every coefficient eventually helps some points at other points' expense; that tension is what the score arbitrates.
- Try to make both drawn residuals small at once, then check the rest of the data by orbiting. The best reachable setting on these sliders is about , scoring — the true optimum lies between slider clicks, and no setting the sliders can reach attains .
- Choose Open in XYZ 3D ↗ under the figure, reveal the hidden least-squares plane in the editor, and set the sliders to hug it as closely as you can. At the optimum, no single-slider move improves the score: each slider direction has been made exactly perpendicular to the remaining error. That is the normal equations, felt by hand.
An original work of XYZ Homework, built around interactive XYZ 3D figures. Chapters 1–6 follow the topic sequence of Interactive Linear Algebra (Margalit & Rabinoff, Georgia Tech, GNU FDL). Chapter 0's scope (its topics, their order, and the idea each figure shows) follows the Vectors chapter of Ximera's Linear Algebra: An Interactive Introduction. The prose, worked examples and figures were written for this book, which is not affiliated with or endorsed by the authors of either work. License: CC-BY-NC-SA-4.0.