Three measurements give the points , and . The line through the first two points predicts at input , and the horizontal line through the last two misses the first point, so no straight line passes through all three. The linear system for an intercept and a slope that match all three outputs has no solution, yet some lines clearly fit better than others. This lesson fixes what “best” means, the smallest sum of squared errors, and shows that orthogonal projection both finds the best fit and proves that nothing does better.
Key terms
Vertical residuals
Definition Suppose the data points are compared with a line with intercept and slope . The residual of observation is
the measured output minus the predicted output at the same input . In the plot it is the signed vertical distance from the line to the point, positive when the point lies above the line and negative when it lies below. The inputs are treated as exact and are never moved.
In plain words A residual says how far the line misses one measurement, measured straight up or down at that measurement’s input. Its size is never smaller than the shortest distance from the point to the line, and it is larger whenever the line is sloped and misses the point.
Example For the three points above, the line predicts , and , so its residuals are , and . The horizontal line has residuals , and . At the point the vertical residual of has size , while the perpendicular distance from that point to the line is only . Least squares works with the vertical residual.
Sum of squared errors
Definition For a line with coefficients and , the sum of squared errors is
Least squares uses this number as its error criterion, with every observation weighted equally. Squaring keeps positive and negative residuals from canceling, and it counts large misses more heavily than small ones. If the outputs carry a unit, the sum carries the square of that unit.
In plain words Square each miss and add. A smaller total means a closer fit in this particular sense. It is a chosen criterion, and a problem whose measurements deserve different trust may call for weights instead.
Example For the points , and ,
The lines and both score . Equal scores do not make either line best, and the next cards find a line that does better.
Residual vector in observation space
Definition Collect the outputs in the observation vector in and the coefficients in . On this page a tuple written for a vector stands for a column, while pairs such as are points in the plot. The design matrix is the matrix whose th row is , so is the vector of predictions . The residual vector is
Its th coordinate is the vertical residual of observation , and . The space is called observation space because it has one coordinate axis for each observation. Every possible prediction vector lies in , and the system is consistent exactly when lies in .
In plain words The scatter plot has one point per observation. Observation space turns the whole data set into a single vector, and every choice of coefficients into a single prediction vector in a flat subspace. The residual vector is the arrow from the prediction to the data, and its squared length is the sum of squared errors.
Example For the three points,
The column space of is a plane through the origin in . The vector is not in it, since the first two coordinates force and , and then the third prediction is instead of .
Least-squares solution
Definition Let be a real matrix and a vector in . A vector in is a least-squares solution of if
for every in . Let be the orthogonal projection of onto . For every the vector is orthogonal to and the vector lies in it, so the Pythagorean theorem gives
Hence is a least-squares solution exactly when . Least-squares solutions always exist, the smallest value of is , and when lies in the least-squares solutions are exactly the ordinary solutions. In a fitting problem the matrix is the design matrix , the unknown is the coefficient vector and the right side is the observation vector .
In plain words When no coefficients reproduce the data, take the prediction vector closest to the data. The closest point of a subspace is the foot of the perpendicular, and any other prediction adds a second, perpendicular piece of error. This proves a global minimum without calculus and without trying values one by one. The right angle lives in observation space. In the scatter plot the fitted line need not meet its vertical residual segments at right angles.
Example For the three points, the coefficients give the predictions and residual
The residual is orthogonal to both columns of , since and . So the predictions are the projection , and is a least-squares solution. Its score is , less than the score of either earlier line. The best line is .
Normal equations
Definition For a real matrix and a vector in , the normal equations are
A vector in is a least-squares solution of if and only if it satisfies the normal equations. The reason is that the entries of are the dot products of the residual with the columns of . The equations say the residual is orthogonal to every column, hence to all of , and by the projection property that happens exactly when . The normal equations always have at least one solution, even when has none. The word normal means perpendicular.
In plain words The best residual has zero dot product with every column of the model. If it had a nonzero dot product with some vector in , moving the prediction a little in the direction of or of would shrink the error. Writing the dot products for an unknown gives a square system with one equation per coefficient.
Example For the three points,
so the normal equations are and . The coefficients and satisfy both, since and . The first equation comes from the all-ones column and says that the residuals sum to zero. A model without a constant column need not have residuals that sum to zero.
Fitted vector
Definition For a real design matrix and observation vector , the fitted vector is the orthogonal projection of onto . It equals for every least-squares solution , and it is unique, because by the identity above any prediction with the smallest error is at distance from . If is one least-squares solution, all of them are the vectors with in . So the coefficients are unique if and only if the columns of are linearly independent.
In plain words The best prediction is always one definite vector. Whether exactly one set of coefficients produces it depends on whether the model has redundant columns.
Example For the three points the columns of are independent, so is the only least-squares solution, and the fitted vector is . Now fit a constant to the two observations and . The design column is , and orthogonality says the residuals and add to zero, so . The fitted vector is , the residual is and the smallest squared error is . If the design matrix contains the all-ones column twice, the prediction depends only on the sum of the two coefficients, and every pair with sum gives the same fitted vector .
Check yourself
- For the points , and , find the residual vector and the sum of squared errors of the line .
Show the answer
The line predicts , and . Subtracting the predictions from the observations gives the residual vector , and the sum of squared errors is .
- For the same points, someone claims the least-squares line is . Check the claim without solving any equations, and find its sum of squared errors.
Show the answer
The predictions are , and , so the residual vector is . Its dot product with the all-ones column is , and with the input column it is . The residual is orthogonal to , so the predictions are the projection of the data and the claim is correct. The sum of squared errors is , smaller than the score from the first question, as it must be.
- Fit the model , a line through the origin, to the points and . Do the residuals sum to zero?
Show the answer
The design matrix is the single column and the observation vector is . The normal equation is , so . The fitted vector is and the residual is . It is orthogonal to the design column, since , but its entries sum to , not zero. Residuals are guaranteed to sum to zero only when the all-ones vector lies in , for example when the model has an intercept, and this model has none.