Advanced Mathematics

Least Squares

The best approximate solution of a system with too many equations: the one whose errors have the smallest sum of squares.

Needs a step first: 11

Data rarely fit a model exactly, so Ax=bAx=b usually has no solution. Least squares picks the x^\hat x that brings Ax^A\hat x as close to bb as possible, which is the projection of bb onto the column space of AA. The leftover error is perpendicular to every column, and that gives the normal equations ATAx^=ATbA^{\mathsf T}A\hat x=A^{\mathsf T}b. It is the oldest algorithm that learns from data, and linear regression in machine learning is exactly this.

Where it shows up: Linear regression is least squares, and when the noise is normal it is also the maximum-likelihood fit.

How to hold it: In your head

See the least-squares fit as the projection of bb onto the column space of AA, which is why the residual is perpendicular to every column and the normal equations follow.

What knowing it looks like

  • Set up the matrix AA for fitting a line or a parabola to data points, and solve the normal equations.
  • Check a fit by confirming that the residuals are perpendicular to the columns of AA.
  • Explain why minimising the sum of squared errors is a projection.

Test yourself

Find the least-squares line y=c+mxy=c+mx for the points (0,1)(0,1), (1,3)(1,3), (2,4)(2,4).
Show the worked answer
Here A=(101112)A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix} and b=(1,3,4)b=(1,3,4), so ATA=(3335)A^{\mathsf T}A=\begin{pmatrix}3&3\\3&5\end{pmatrix} and ATb=(8,11)A^{\mathsf T}b=(8,11). Solve 3c+3m=83c+3m=8, 3c+5m=113c+5m=11: 2m=32m=3, so m=32m=\tfrac32 and c=76c=\tfrac76. The line is y=76+32xy=\tfrac76+\tfrac32x. The residuals are −16,13,−16-\tfrac16,\tfrac13,-\tfrac16: they sum to 00, and 0⋅(−16)+1⋅13+2⋅(−16)=00\cdot(-\tfrac16)+1\cdot\tfrac13+2\cdot(-\tfrac16)=0, so they are perpendicular to both columns.

Needs a step first: 11

Learn it with an AI

Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.

Be my maths tutor. I'm learning "Least Squares" (Undergraduate I tier): The best approximate solution of a system with too many equations: the one whose errors have the smallest sum of squares.
I already know: Orthogonality and Projections, Gaussian Elimination and Inverses.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: Find the least-squares line $y=c+mx$ for the points $(0,1)$, $(1,3)$, $(2,4)$.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.

On the way to

Recommended