Real data rarely fits a model exactly. Measure four points that "should" lie on a line and they won't quite; the system Ax=b for the line's coefficients has no solution. Instead of giving up, you ask for the x that makes Ax as close to b as possible. Orthogonal projection answers that question exactly.
The least-squares problem
Definition
Least-squares solution
If A is m×n and b is in Rm, a least-squares solution of Ax=b is a vector x^ in Rn such that
∥b−Ax^∥≤∥b−Ax∥for all x in Rn.
The name comes from the fact that ∥b−Ax∥2 is a sum of squares of the entries of b−Ax. The number ∥b−Ax^∥ is the least-squares error.
The vectors Ax are exactly the vectors in ColA. So you want the point of ColA closest to b, and by the best approximation theorem that is
b^=projColAb.
Since b^ is in ColA, the equation Ax=b^ is consistent, and its solutions are the least-squares solutions.
The normal equations
You usually don't have an orthogonal basis for ColA, so computing b^ directly is awkward. Instead, use the key property of the projection: b−Ax^ is orthogonal to ColA, so it is orthogonal to every column of A. That means AT(b−Ax^)=0, or:
The normal equations
The least-squares solutions of Ax=b are exactly the solutions of
ATAx=ATb.
This system is always consistent. If the columns of A are linearly independent, then ATA is invertible and the least-squares solution is unique:
x^=(ATA)−1ATb.
If the columns of A are dependent, there are infinitely many least-squares solutions (they all give the same Ax^=b^).
Worked example: Solving with the normal equations
Find the least-squares solution of Ax=b and the least-squares error, where
A=101011,b=216.
The system is inconsistent: rows 1 and 2 force x1=2, x2=1, but then row 3 gives 3=6. Compute
ATA=[2112],ATb=[2+61+6]=[87].
Solve 2x1+x2=8, x1+2x2=7: subtracting twice the second from the first gives −3x2=−6, so x2=2 and x1=3. Thus x^=(3,2).
Then Ax^=(3,2,5) and b−Ax^=(−1,−1,1). Check it is orthogonal to both columns: (−1,−1,1)⋅(1,0,1)=0 and (−1,−1,1)⋅(0,1,1)=0. The least-squares error is 1+1+1=3.
Common mistake
Don't "solve" Ax=b by cancelling AT from ATAx=ATb, and don't write x^=A−1b: a non-square A has no inverse. The whole point is that Ax=b has no solution, while the square system ATAx=ATb does. Also remember that x^ lives in Rn while b^=Ax^ lives in Rm.
Fitting a line to data
Given data points (x1,y1),…,(xm,ym), you want the line y=β0+β1x that fits best. Asking every point to lie on the line gives m equations β0+β1xi=yi, that is, Xβ=y with
X=11⋮1x1x2⋮xm,β=[β0β1],y=y1y2⋮ym.
X is the design matrix. The least-squares solution gives the least-squares line (the regression line), which minimizes the sum of the squared vertical distances from the points to the line. The normal equations are
XTX=[m∑xi∑xi∑xi2],XTy=[∑yi∑xiyi].
Worked example: The least-squares line
Find the least-squares line for the points (0,1), (1,1), (2,2), (3,4).
Here m=4, ∑xi=6, ∑xi2=0+1+4+9=14, ∑yi=8 and ∑xiyi=0+1+4+12=17. The normal equations are
The least-squares line is y=21+x. Its predictions 0.5,1.5,2.5,3.5 miss the data by 0.5,−0.5,−0.5,0.5, and the sum of squared residuals is 4(0.25)=1. No other line does better.
The least-squares line y = 1/2 + x for the points (0, 1), (1, 1), (2, 2), (3, 4). Each point is off by 1/2 vertically.Open in grapher →
The same method fits any model that is linear in its coefficients, such as a parabola y=β0+β1x+β2x2: the design matrix just gets a column of xi2.
Shortcuts: orthogonal columns and QR
If the columns a1,…,an of A are orthogonal, ATA is diagonal, and each entry of x^ is a weight from the projection formula:
x^j=aj⋅ajb⋅aj.
More generally, if A=QR, then ATA=RTR and ATb=RTQTb, and the normal equations reduce to the triangular system
Rx^=QTb,
solved by back substitution. This is how software computes least-squares solutions: forming ATA explicitly can magnify rounding errors.
Worked example: Orthogonal columns
Find the least-squares solution of Ax=b for A=111−110 and b=(2,6,1).
The columns a1=(1,1,1) and a2=(−1,1,0) are orthogonal: −1+1+0=0. So
x^1=32+6+1=3,x^2=2−2+6+0=2,x^=(3,2).
Check: b−Ax^=(2,6,1)−(1,5,3)=(1,1,−2), which is orthogonal to both columns.
Tip
After solving, compute the residual b−Ax^ and dot it with each column of A. Every result must be 0. For a fitted line with an intercept, this says the residuals add up to 0.
Practice
Practice 1
Find the least-squares solution of Ax=b for A=101011 and b=(1,3,1).
Enter a point like (2, -3)
Practice 2
For the system in the previous problem, find the least-squares error ∥b−Ax^∥ as an exact value.
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Practice 3
Find the least-squares solution of Ax=b for A=111123 and b=(1,2,2).
Enter a point like (2, -3)
Practice 4
Find the slope of the least-squares line y=β0+β1x for the points (1,2), (2,3), (3,5).
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Practice 5
Find the least-squares line for the points (−1,0), (0,1), (1,3), and use it to predict y when x=2.
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Practice 6
The columns of A=11111−11−1 are orthogonal. Find the least-squares solution of Ax=b for b=(4,0,2,2).
Enter a point like (2, -3)
Practice 7
Let x^ be a least-squares solution of Ax=b. Which statement is always true?