Least squares, explained
6 min read
Short answer
When has no exact solution, the least-squares solution makes as small as possible. It solves the normal equations . Geometrically, is the projection of onto the column space of , so the error is perpendicular to every column.
Why the normal equations
The closest point in the column space is the one whose error is perpendicular to the column space, meaning . Rearranging gives . If the columns of are independent, is invertible and the solution is unique.
Fitting a line
To fit to data points, each point gives one equation . Stack them into with a column of ones and a column of values, then solve the normal equations.
Worked example
Fit to the points .
and . Then and .
Solve and : subtract to get , so and .
Check: the residuals are , which add to 0 and satisfy , so the error is perpendicular to both columns. ✓
Try it yourself
Fit to .
Show the answer
and . Solving , gives and , so .
Keep going with practice
Least squares ties together projections and matrices in Vidi's Linear Algebra & Probability course.