Least Squares Approximation

Least Squares Approximation
Normal Equation
Given a linear systemAx=b, we can write the normal equation:
(ATA)z=ATb
Why is this useful?
The solution, z, of the normal equation is a best possible approximation to a solution of Ax=b.
That is, z is the vector such that Az is as close as possible to b.
Note: this result is usually used when the system Ax=b is inconsistent, because otherwise we can find an exact solution!
Polynomials of Best Fit
Suppose we want to find a degree m polynomial that "best fits" a set of points (x1,y1),(x2,y2),...,(xn,yn).
We'll define this polynomial of best fit to be:
f(x)=c0+c1x+c2x2+...+cmxm
Let y=y1⋮yn.
Form the following matrix, just as we did when finding an interpolating polynomial:
M=11⋮1x1x2⋮xnx12x22⋮xn2.........x1mx2m⋮xnm
Then, the coefficients of the polynomial c=c0⋮cm are found by solving the normal equation:
MTMc=MTy
Note: If the degree of the polynomial is smaller than the number of unique x-coordinates (of the given points), then the coefficients are uniquely determined.

Example: Least Squares for Linear Functions
Find the equation of the line that comes as close as possible to passing through the points: (0,1),(2,3),(3,4),(4,7).
First, define the vector y (look at each of the y-values):
y=1347
Now fill in the matrix M according to: M=11⋮1x1x2⋮xnx12x22⋮xn2.........x1mx2m⋮xnm
Note: Since we are looking for a line to approximate, we want a degree 1 polynomial, so m=1.
Thus we have just 2 columns:
M=11110234
Now look at the normal equation MTMc=MTy to see what else we need to calculate:
MTM=[49929]MTy=[1546]
Row reduce the augmented matrix for the system of normal equations:
[499291546]RREF[10013/57/5]
⟹[c1c2]=[3/57/5]⟹f(x)=(3/5)+(7/5)x←line of best fit
Example: Least Squares for Polynomials
Find the equation of a quadratic polynomial that comes as close as possible to passing through the points: (1,2),(1,3),(2,3).
Note that there are only 2 distinct x-coordinates among the set of points, so a quadratic (degree 2) does NOT have degree strictly less than the number of unique x-coordinates. Therefore, we expect the answer NOT to be uniquely determined.
First, we look at all the y-coordinates to write: y=233
Then we can form the matrix M and calculate the pieces of the normal equation MTMc=MTy:
M=111112114MTM=346461061018MTy=81117
Therefore, the normal equation can be solved by row reducing:
34646106101881117RREF100010−23021/20
Here we have a free variable for the third column, representing c2:
⎩⎨⎧c0−2c2=2c1+3c2=1/2c2=t⟹⎩⎨⎧c0=2+2tc1=1/2−3tc2=t
So for any choice of the parameter t, the function f(x)=(2+2t)+(1/2−3t)x+tx2 would work.
For example, with t=1 we have: f(x)=4−(5/2)x+x2.
Interestingly, the case t=0 gives a linear function which approximates exactly as well (in terms of minimizing the squared distance from the given points): f(x)=2+21x