Linear algebraLesson 6 of 6
Fit the weights by least squares
Least squares and normal equations
Return to all three records and both features:
No weights fit these scores exactly. Instead, choose weights that minimize the sum of squared residuals:
Here is the vector of observed minus predicted scores. Squaring its length adds the three squared errors. Argmin asks for the weights achieving the smallest value; it does not ask for the value of that error. The hat marks fitted weights.
From perpendicular error to equations
Section titled “From perpendicular error to equations”Every possible prediction lies in the column space of . Least squares chooses the point in that space closest to . The residual must therefore be perpendicular to each column, not merely to one of them:
Each entry of this product is a column dotted with the residual. Rearranging gives the normal equations:
“Normal” here means perpendicular. Calculate the products:
For example, the top-left entry is , and the top-right is . On the right, .
Multiply the first equation by 2 and the second by 5. Subtracting gives . Substitute back to obtain
Check the fit
Section titled “Check the fit”The predictions and residuals are
The residual dotted with the hours column is . With the sets column it is . The squared errors sum to . Dividing by three records gives training mean squared error ; this positive constant factor does not change the fitted weights.
The earlier weights fit the first two records exactly but missed the third by 1, giving squared error 1. Least squares accepts smaller errors on all three records to reduce their total. It does not insist on preserving any individual exact fit.
Our independent columns make these weights unique. Dependent columns can give several weight vectors with the same closest prediction. Least squares itself does not require normally distributed noise; additional assumptions enter when interpreting uncertainty or making claims about the data-generating process.
This model has no intercept. Adding a column of ones would add one, changing the allowed predictions. A small training error also says nothing by itself about new records: that requires evaluation on held-out data.
Try it
Section titled “Try it”What does the fitted model predict for a new input ?
Work through the answer
. This is a prediction from the fitted rule, not a known outcome or a causal estimate.
For more examples, see Georgia Tech’s least-squares chapter.