Least-Squares Line
Interactive lab
Try it: Least-Squares Line
How least squares fits a line: the slope and intercept that minimise the mean squared error come from solving the normal equations, and no hand-drawn line can do better.
How it works
- Each point's residual is y − ŷ, the vertical gap to the line; its square is the squared error.
- Add up n, Σx, Σy, Σx² and Σxy.
- Write the normal equations (XᵀX)w = Xᵀy for w = (slope a, intercept b) and solve the 2×2 system.
- Measure every residual of the fitted line and average the squares (MSE).
- Compare with your own line: its MSE is always at least the least-squares MSE.
Default run (13 steps): 8 points (x, y). Your line ŷ = 0.2x + 4 has mean squared error 1.78. Least squares finds the line with the smallest possible MSE. … MSE = 1.744 / 8 = 0.218. Your line: 1.78 — 1.562 worse. No other line can beat least squares on this data.
Simplified: One input feature and at most 12 points; the 2×2 system is solved in closed form. If every x is equal the system is singular and the lab stops.
Educational simulation
Loading the simulation…