A table lists three measurements, output at input , output at input and output at input . To fit a line or a parabola to it by least squares, the table has to become a matrix, and someone has to decide what each column of that matrix means. This lesson builds the design matrix from the data, solves the normal equations by elimination, and checks the answer against the original observations. It also shows what changes when two columns of the model carry the same information.
Key terms
Model linear in its coefficients
Definition A model of the form
where are known functions of the input and are unknown real coefficients, is linear in its coefficients. The functions may be nonlinear in . What matters is that each prediction is a linear combination of the unknowns with known weights, so fitting the model to data by least squares is a linear algebra problem. A model with an unknown inside a nonlinear function, such as , is usually not linear in that unknown.
In plain words The word linear refers to the unknown coefficients, not to the shape of the graph. A parabola, or a combination of sines with fixed frequencies, still leads to a linear system for its coefficients.
Example The quadratic model uses , and , so it is linear in although its graph bends whenever . The model is also linear in its coefficients. The model is not linear in , because that coefficient sits inside the exponential.
Design matrix
Definition Fit a model to the observations . The design matrix is the matrix with entries
Row belongs to observation , and column belongs to the coefficient , in the order chosen for the coefficient vector . The measured outputs form the separate observation vector , and the predictions of the model at the observed inputs are the entries of . On this page a tuple written for a vector stands for a column.
In plain words Each column records what one coefficient does to every prediction at once. The known inputs go into , and the measured outputs stay on the right side in . Swapping those roles would pose a different problem.
Example For the line with and the points , , ,
For the quadratic model at the inputs , , and , the row for input is , so
The powers are computed from the known inputs before any coefficient is found.
Fitted values
Definition Let be a least-squares solution, that is, a solution of the normal equations
The fitted values are the entries of , the predictions of the fitted model at the observed inputs, and the residuals are the entries of . The vector is the orthogonal projection of onto , so it is the same for every least-squares solution, and the smallest sum of squared errors is .
In plain words Fitted values are what the chosen model says at the inputs where you measured. The residuals tell you by how much, and in which direction, the model misses each measurement. A residual has the unit of the output, and its square has the squared unit.
Example For the line fitted to the three points above, each entry of and is a dot product of columns,
The normal equations are and . Subtracting the first from the second gives , so . Then , so . No inverse is needed. The fitted values are
and subtracting them from , , gives the residuals , and . The first and third points lie a little below the line and the middle one lies above it. The sum of squared errors is .
Residual orthogonality check
Definition For a coefficient vector with residual , compute , whose entries are the dot products of the residual with the columns of . The condition holds exactly when is a least-squares solution. A zero result therefore certifies that minimizes the sum of squared errors for this model and this criterion, and a nonzero entry shows that it does not. The check says nothing about whether the model suits the data, whether the measurements are accurate, or whether the fit holds outside the observed inputs.
In plain words A small but nonzero squared error proves nothing on its own, since another choice might be smaller still. Zero dot products with every design column prove the minimum. Keep the signs of the residuals until the check is done, because squaring them first hides mistakes.
Example For the residual above, the all-ones column gives , and the input column gives . Both vanish, so is the least-squares line. The same check can also find a fit. For the constant model with the same data, orthogonality to the all-ones column says
so , the mean of the observations. The residuals are , , , and the sum of squared errors is . That is larger than , and it could not be smaller, since every constant is a line with slope . In general the least-squares constant for equally weighted observations is their arithmetic mean.
Dependent design columns
Definition The columns of an design matrix are dependent when for some nonzero in , that is, when . Because
the matrices and have the same null space. So the matrix is invertible exactly when the columns of are independent, and then the least-squares coefficients are unique. When the columns are dependent, the normal equations still have solutions, but if is one of them, the full solution set is , which contains infinitely many vectors. These are nonunique coefficients, and all of them give the same fitted values. Having more observations than coefficients does not by itself make the columns independent.
In plain words Dependent columns mean that different coefficient choices make exactly the same predictions, so the data cannot tell them apart. The best prediction is still unique. Picking one coefficient vector needs an extra rule or a redesigned model.
Example Give the three points a design matrix with two all-ones columns, so every prediction is . Then
and both normal equations read . Every pair with is a least-squares solution, for example , and , and all of them give the fitted values . The singular matrix signals coefficients that cannot be told apart, not the absence of a best fit.
Check yourself
- Write the design matrix for the model at the inputs , and . Is the model linear in its coefficients?
Show the answer
The row for input is , so
The first model is linear in and even though is not linear in . The second is not linear in , since that coefficient appears inside the exponential, so its predictions are not a fixed matrix times .
- Find the least-squares line for the points , , and , and check the residual.
Show the answer
The columns are and , and , so
Multiply by and by , then subtract to get . So , and gives . The fitted values are , , , and the residuals are , , , . They sum to , and weighting by the inputs gives , so the residual is orthogonal to both columns. The sum of squared errors is .
- Three measurements are all taken at the input , with outputs , and . Fit the line . Are the coefficients unique?
Show the answer
Both columns of equal , so the columns are dependent even though there are more observations than coefficients. Both normal equations read , so every pair with is a least-squares solution, namely for every real . The coefficients are not unique, but the fitted values are, namely , with residual and sum of squared errors . Measurements at a single input cannot reveal a slope.