Lecture 9: Orthogonal Projection and the Least Squares Method
These notes are interactive: click a section title to open it, click any dotted term to unfold its exact definition or statement in place, click ▸ Proof to expand a proof, and try the exercises — answers are checked instantly.
9.1 When has no solution
So far an inconsistent system has been a dead end: row reduction produces a row with , and we stop. In applications this is where the work begins. Measurements carry noise, a model is only approximately right, and numbers stored in a computer are rounded. A system built from such data typically has more equations than unknowns, and then it almost never has an exact solution. Yet the data still contain information, and we want the that fits them best. This lecture makes โbestโ precise: we measure the error vector by its length and make it as short as possible. The answer is geometric, the closest point of a subspace, and it leads to three practical methods of computing it.
A length is measured three times, with results , and . The โsystemโ is and it is plainly inconsistent. Nobody would answer ``there is no length''; the natural answer is a compromise, such as the average . We will see that the average is exactly what the least-squares method produces (Section 9.6).
Suppose is a quantity that changes over time , and we want to know whether there is a linear trend in in terms of . An experiment gives four data points : We want a line that fits the data as closely as possible. If the line passed through all four points, each point would give one linear equation in the unknowns : The first two equations force and , and then the third equation reads . The points are not on one line, so there is no exact solution. We still want the line that comes closest to the data; it is found in Section 9.6.
Let be with , more equations than unknowns. The system is consistent exactly when , by Theorem 6.25, and , by the rank theorem (Theorem 7.9). So is a proper subspace of , a line or a plane or a thinner set inside a bigger space. Even if the true data lie in , a small error in the measurements, or the rounding of each entry to the digits a computer keeps, moves off this thin subspace in almost every case. Rounding the entries of has the same effect, since it changes the subspace itself. So with real data we must expect to have no solution.
For each , the vector is the error, also called the residual. We will choose to make the residual as short as possible. As varies, runs through all of , so the task is: find the vector of the subspace that is closest to . This needs a notion of length (Section 9.2), of perpendicularity and the closest point of a subspace (Sections 9.3 and 9.4), and good bases for computing it (Section 9.5). Section 9.6 returns to .
9.2 Inner product, length, and orthogonality
Lecture 1 defined the rowโcolumn product, which multiplies a row by a column and gives a number. Pairing a column vector with another column vector in the same way, by turning the first one into a row, gives the inner product. In the plane and in space it is the familiar dot product, and it carries all the geometry we need: the length of a vector comes from Pythagoras, the distance between two points is the length of their difference, and two vectors are perpendicular when their inner product is . All of this makes sense in for every , where we can no longer draw, and the algebra lets us reason there as if we could.
The inner product (or dot product) of is the number the rowโcolumn product (Definition 1.8) of the row vector with the column vector . (For it appeared in Remark 5.15, on the cross product.)
For all and every scalar :
- ;
- and ; by (a) the same holds in the second factor;
- , and if and only if .
(a) and (b) are statements about the sums and follow from the rules of arithmetic: , and . (c) is a sum of squares, so it is , and it is only if every , that is, every .
□The length (or Euclidean norm) of is which makes sense by part (c) of Proposition 9.6. The distance between and is . A vector of length is a unit vector. For , the vector is a unit vector in the direction of ; replacing by it is called normalizing .
For in the plane, the arrow from to is the hypotenuse of a right triangle with legs and , so its length is by Pythagoras; in space, Pythagoras applied twice gives . Definition 9.7 uses the same formula in every dimension. Since the -th entry of is , scaling a vector by scales its length by . In particular , as claimed in Definition 9.7.
Let and . Then , and since , . The unit vector in the direction of is .
Two vectors are orthogonal (or perpendicular), written , if . The zero vector is orthogonal to every vector.
For , Hence if and only if . The same holds for the sum: if and only if .
By the properties of the inner product (Proposition 9.6), So holds exactly when , that is, when . For the sum, apply this to in place of : , , and , so , and exactly when .
□The vectors and , drawn from , and the vector , drawn from the tip of to the tip of , form a triangle; let be its angle at . By Theorem 9.11, the squared length of the side differs from the sum of the squared lengths of the other two sides by , and this term is exactly when is a right angle. This is the theorem of Pythagoras from plane geometry, now valid in for every . (The remark on angles below shows that , so the identity in the theorem is the law of cosines.)
For all ,
If both sides of the first inequality are . Otherwise let and . Then , so , and by Pythagoras (Theorem 9.11) applied to , Multiplying by and taking square roots gives the first inequality. For the second, by Pythagoras and the first inequality,
□The proof of Theorem 9.12 splits into two perpendicular parts, (9.1) as in the figure that follows. The vector lies on the line and is the foot of the perpendicular dropped from to that line: it is the projection of onto the line through , and is the distance from to the line. This is the first instance of the main construction of this lecture.
The second inequality is the triangle inequality. Draw from and then from the tip of ; the arrow from to the tip of is . The three arrows form a triangle, and the inequality says that the side is never longer than the other two sides together: going straight is never longer than going around.
By CauchyโSchwarz (Theorem 9.12), for nonzero we have the inequality of the ratio: The number measures how strongly and are correlated. It equals when is a positive multiple of and when is a negative multiple of (in the proof of Theorem 9.12, equality holds exactly when , that is, when ), and it is when . If the entries of and are two series of measurements, each with its mean subtracted, then is the correlation coefficient of statistics. In machine learning is called the cosine similarity and is used everywhere: words, documents, images or users are represented by vectors, and two of them are considered similar when the cosine similarity of their vectors is close to ; search engines and recommendation systems rank their results this way.
Since , there is exactly one angle with ; it is called the angle between and . In the plane this agrees with the angle of elementary geometry, by the law of cosines. For the vectors of Example 9.9, . Orthogonal means , that is, . For the decomposition in (9.1): when is acute, and .
Let be vectors in .
- The vectors form an orthogonal set if for all , and an orthonormal set if moreover each is a unit vector.
- An matrix is column orthogonal if its columns form an orthogonal set, and column orthonormal if its columns form an orthonormal set. A square column orthonormal matrix is called an orthogonal matrix.
- A basis of a vector space (in this lecture, of or of a subspace of ) is an orthogonal basis if it is an orthogonal set, and an orthonormal basis if it is an orthonormal set.
Let and . Then is the matrix whose entry is . In particular, is the matrix whose entry is ; consequently is column orthogonal if and only if is diagonal, and column orthonormal if and only if .
Row of is and column of is , so by the entry formula for products (Proposition 4.12) the entry of is . Taking , the off-diagonal entries of are the inner products of different columns of , and the diagonal entries are . So all off-diagonal entries are exactly when the columns are orthogonal, and in addition all diagonal entries are exactly when the columns are unit vectors.
□Suppose that all columns of and of are unit vectors. Then the entry of is the correlation of Remark 9.13, so is a table showing how strongly each column of is correlated with each column of : an entry near means that the two vectors point in nearly the same direction, an entry near that they are unrelated, and an entry near that they point in nearly opposite directions. All of these inner products are computed at once, by a single matrix product, which computers do very fast.
This is the core computation of the attention mechanism in transformers, the neural networks behind today's large language models. Machine learning stores data in rows: a text of words (tokens) is an matrix whose -th row is the vector representing word , and the columns of are the features. From the network computes two matrices and (the weight matrices and are learned in training). Row of is the query of word (โwhat is this word looking for?โ), and row of is the key of word (``what does this word offer?''). The table of all scores is the matrix which is Lemma 9.15 with and , whose columns are the queries and the keys. The entry scores how relevant word is to word , and these scores decide how much each word draws on every other word. In practice the vectors are not normalized, the scores are divided by , and each row of the table is turned into weights by the softmax function. (The letters and follow the machine learning literature and are used only in this remark; elsewhere in these notes denotes a column orthonormal matrix, as in the QR factorization.)
Let Any two different columns agree in exactly two positions and disagree in the other two, so their inner product is , and each column has . By Lemma 9.15, So is column orthogonal, and is column orthonormal: . In particular , without any row reduction. (Matrices of entries with orthogonal columns are called Hadamard matrices; they are used in coding and signal processing.) Two columns alone also give a column orthonormal matrix: but : a non-square column orthonormal matrix has a left inverse but no inverse.
Let be an orthogonal set of nonzero vectors in , and let . Then:
- are linearly independent, so they form an orthogonal basis of ;
- every has a unique representation
First we prove (b). Let . Since is the span of the , we can write for some numbers . Fix with and take the inner product of both sides with . By linearity (Proposition 9.6), because all other terms vanish by orthogonality. Since , and we can divide: . So the coefficients of any representation of are determined by through this formula, and the representation is unique. This proves (b).
Now (a). Take in (b): any relation has for every . So the are independent; they span , so they are a basis of , and it is an orthogonal basis by part (c) of Definition 9.14.
□The columns of in Example 9.17 are four orthogonal nonzero vectors in , hence a basis of (by Theorem 9.18 and the basis test (Theorem 6.48)). To write in this basis, compute , , , and divide each by : Check the first entry: .
Let be an column orthonormal matrix, that is, . Then for all , Consequently , , and if and only if : the map keeps lengths, distances, angles and right angles. Conversely, a matrix with for all is column orthonormal.
By the definition of the inner product, and the transpose rule and associativity of the product (Theorem 4.17), Taking gives . Distances follow since , and angles since they are defined through inner products and lengths. For the converse, take and : is column of , so , which is if and otherwise.
□Let with from Example 9.17, and let , . Then , and Also . For itself, which is column orthogonal but not orthonormal, and the same computation gives : keeps right angles but doubles every length. The rotation matrices of Example 3.17 are orthogonal matrices, which is why rotations keep lengths.
What is the length of ?
.
For which number are and orthogonal?
The inner product is .
Let be a matrix with . Which statement is true?
Proposition 9.20 applies. A matrix has no inverse, and has rank at most .
9.3 The orthogonal complement and the projection theorem
Here is the problem left open in Section 9.1. We are given a subspace of (for least squares, ) and a vector , usually not in , and we want the vector of that is closest to . The figure below shows the answer when is a plane in : drop the perpendicular from to the plane. Its foot is , and splits into two pieces, Why is the closest point? For any other point of , the points , , form a right triangle with the right angle at , so the segment from to is a hypotenuse and is longer than . In we cannot draw, so we prove both facts. In this section we name the set of all vectors perpendicular to , describe it as a null space, and prove that every splits uniquely as with and ; the proof also computes by solving a small square system. Section 9.4 then shows, with Pythagoras, that is the closest point.
Let be a subspace of . A vector is orthogonal to if for every . The set of all such vectors is the orthogonal complement of , (read โ perpโ).
Let be a subspace of , and let be the matrix with these columns. Then:
- if and only if for ;
- ; in particular is a subspace of ;
- .
(a) If , then because each . Conversely, suppose for all . Every has the form , and by linearity (Proposition 9.6) .
(b) Row of is , so the -th entry of is . Hence if and only if for all , which by (a) means . A null space is a subspace by Theorem 6.21.
(c) If , then is orthogonal to every vector of , in particular to itself: , so by part (c) of Proposition 9.6.
□For every subspace of ,
If , then and (the only vector orthogonal to all of is orthogonal to itself), so both statements hold. Otherwise let be a basis of , so , and let . The columns of are independent, so , and by the rank theorem (Theorem 7.9). The matrix is , so the rank theorem gives , and by Proposition 9.23. Hence .
For the second statement, every is orthogonal to every (this is Definition 9.22, read from the other side), so . Applying the first statement to the subspace , . A subspace of with the same dimension is all of it, by part (3) of Theorem 6.47, so .
□For an matrix , Proposition 9.23 applied to the columns of (the rows of , transposed) gives The first identity says something we have used since Lecture 1 without the words: solves exactly when is orthogonal to every row of . The dimension count is the rank theorem (Theorem 7.9) again.
Let in with and , a plane through . By Proposition 9.23, is the solution set of The first equation gives , and then the second gives . So with , a line, and indeed . The vector is a normal vector of the plane, which is the solution set of the single equation , that is, , in agreement with .
With from Example 9.17, let . The vectors are orthogonal to and , so they lie in by part (a) of Proposition 9.23; they are independent, and , so by the basis test (Theorem 6.48).
Let be an matrix whose columns are linearly independent. Then the matrix is invertible.
is a matrix, so by the invertibility theorem (Theorem 7.13) it suffices to show that has only the solution . Let . Multiplying on the left by and using the transpose rule , so by part (c) of Proposition 9.6. Since the columns of are independent, forces .
□Let be a subspace of and . Then can be written uniquely as (9.2) Moreover, for any basis of , we have that is, is the unique solution of the system (9.3)
If , then and the only decomposition is . So assume . We first show that there is at most one decomposition, and then that every basis of produces one, given by the formula in the statement.
Step 1: uniqueness. Suppose are two decompositions as in (9.2). Then The left side lies in and the right side lies in , since both are subspaces. So this vector lies in (part (c) of Proposition 9.23), and , .
Step 2: decompositions in terms of a basis. Let be any basis of (a nonzero subspace has a basis). A vector lies in exactly when for some (the column view of , Theorem 1.12). For such , the vector lies in if and only if , by part (b) of Proposition 9.23, that is, if and only if satisfies (9.3).
Step 3: existence and the formula. The columns of are independent, so is invertible by Lemma 9.28, and (9.3) has exactly one solution, . By Step 2, and form a decomposition (9.2), so a decomposition exists. By Step 1 it is the only one; hence, whichever basis of is used, the vector of the decomposition equals with .
□In Theorem 9.29, is called the orthogonal projection of onto and is denoted ; the vector is the component of orthogonal to . By the uniqueness in Theorem 9.29, depends only on and , not on the basis used to compute it.
Let be the plane of Example 9.26, and use the basis , of (note , and ). Project by solving (9.3). With , The system , gives , so and . Hence As a check, and , and is a multiple of the normal vector . This is the situation drawn in the figure at the beginning of this section.
Let be the plane in . Which vector spans ?
with , so and by Proposition 9.24. The other two vectors lie in .
9.4 Distance to a subspace and the projection formula
The picture of the projection theorem promised more than a decomposition: the foot of the perpendicular is the point of closest to . This is Pythagoras once more, and it is the fact that will solve the least-squares problem. The section then collects the algebraic properties of and gives the formula that makes projections easy to compute: when the basis of is orthogonal, the system (9.3) is diagonal and can be solved by inspection.
Let be a subspace of , and . Then for every , so whenever . In words, is the unique point of closest to .
Write . The first vector is , and the second lies in , being a difference of two vectors of . So they are orthogonal, and Pythagoras (Theorem 9.11) gives the identity. If , the last term is positive.
□The distance from to the subspace is the minimum exists and is attained only at , by Theorem 9.32. It is the length of the component of orthogonal to , and it is exactly when .
Let be a subspace of and write .
- is a linear map.
- if and only if , and if and only if . So the image of is and its kernel is .
- : projecting twice is the same as projecting once.
- .
- , so .
- If is a matrix whose columns form a basis of , the standard matrix of is and it satisfies and .
Every statement comes from the uniqueness in the projection theorem (Theorem 9.29): whenever we exhibit with and , we may conclude .
(a) Let and be the decompositions, and scalars. Then where the first bracket lies in and the second in , both being subspaces. So .
(b) If , then is a decomposition, so ; conversely always, so forces . If , then is a decomposition, so ; conversely, means .
(c) , so by (b).
(d) By Proposition 9.24, . So has and , which is the decomposition of relative to the subspace ; hence .
(e) This is Pythagoras (Theorem 9.11) for .
(f) By the projection theorem (Theorem 9.29), for every , so is the standard matrix of . Then is (c), or directly: For the transpose, , hence , and by the transpose rule for products.
□In the language of Lecture 8 (Theorem 8.30), part (b) of Theorem 9.34 says that every nonzero vector of is an eigenvector of with eigenvalue and every nonzero vector of is an eigenvector with eigenvalue . Since , a basis of together with a basis of is a basis of consisting of eigenvectors. (The combined list is independent: a relation among its vectors has the form with , , so , and since each part is a combination of a basis equal to , all its coefficients are . There are vectors, so the basis test (Theorem 6.48) applies.) Hence is diagonalizable with . The map of Lecture 8 is the projection onto the -axis, and the reflection across a line in Lecture 7 is , with the projection onto : it keeps the part of a vector in and reverses the part orthogonal to .
Let be an orthogonal basis of the subspace of . Then for every , (9.4) If the basis is orthonormal and , this reads (9.5) so the standard matrix of is .
Call the right side . It lies in , so by the uniqueness in the projection theorem (Theorem 9.29) it suffices to show , that is, for each (part (a) of Proposition 9.23). By orthogonality only the -th term of contributes to : For an orthonormal basis, , and . By the column view of a product (Theorem 1.12), , since is the column of the numbers . (Equivalently: with an orthogonal basis, the matrix in (9.3) is diagonal by Lemma 9.15, and solving (9.3) gives .)
□For , formula (9.4) is the projection onto the line through , which is the vector of (9.1) in Remark 9.13. Theorem 9.36 says that projecting onto is the sum of the projections onto the lines , provided the are orthogonal. For a basis that is not orthogonal the sum is wrong: with and of Example 9.31,
The basis , of in Example 9.26 is orthogonal: . For , the same vector as in Example 9.31, with no system to solve. The distance from to is By part (d) of Theorem 9.34, the same number comes from projecting onto the line : . This also gives the matrix of quickly: for all , so and indeed .
Let with the columns of from Example 9.17, and . From Example 9.19, and , so and . The orthogonal component is the part of the expansion of that lies in : with an orthogonal basis of all of whose first vectors span , projecting means keeping the first terms of the expansion. In matrix form, with , this projection replaces the first two entries of by their average, and likewise the last two.
The projection of onto the line through is . What is ?
.
What is the distance from to the line through ?
By the preceding self-check the projection is itself, and .
9.5 The GramโSchmidt process and the QR factorization
The projection formula needs an orthogonal basis, and the bases we meet in practice, such as the columns of a matrix, are seldom orthogonal. The GramโSchmidt process repairs this one vector at a time: keep the first vector; from the second subtract its projection onto the first; from the third subtract its projection onto the span of the first two, which we can now compute because those two are already orthogonal; and so on. What is subtracted at each step is a combination of earlier vectors, so the spans do not change. Recording the subtracted coefficients turns the process into a factorization , which is to orthogonality what row reduction is to solving equations.
Let be linearly independent vectors in . Define and, for , (9.6) Then, for each , the vectors are nonzero and pairwise orthogonal, and
Induction on . For : , since independent vectors are nonzero, and the spans agree.
Let and assume the statement for : the vectors are nonzero, pairwise orthogonal, and span . So they are an orthogonal basis of , and by the projection formula (9.4) the sum subtracted in (9.6) is : (9.7) Orthogonality. is the component of orthogonal to , so for .
. Otherwise would be a combination of , contradicting independence.
Spans. By the induction hypothesis, (both are spanned by together with ). The vector is plus a linear combination of , so by Proposition 6.17, replacing by does not change the span.
□- Written out, the first steps of (9.6) are and so on: by (9.7) in the proof, each is minus its projection onto .
- Taking in Theorem 9.40, the vectors form an orthogonal basis of the span of , and the unit vectors form an orthonormal basis.
- The new vectors in terms of the old. Each with lies in , so (9.6) shows that is plus a linear combination of . In the examples we first write in this form and only then substitute the vectors.
- The old vectors in terms of the new. Moving the sum in (9.6) to the other side gives (9.8) for instance , and . The numbers are computed anyway during the process. In matrix form these relations become the QR factorization later in this section.
(a) Each may be replaced by any nonzero multiple before it is used in later steps: the term equals . Clearing denominators this way keeps hand computations in integers. The relations (9.8) then hold with the rescaled vectors and correspondingly rescaled coefficients. (b) Check each new by computing its inner products with the earlier ones; they must be . (c) If the are dependent, some comes out , exactly when is a combination of the earlier vectors; dropping it and continuing produces an orthogonal basis of the span. So every subspace of has an orthogonal, and hence an orthonormal, basis.
Let , , .
Step 1. , with .
Step 2. Since , the relation (9.6) is . Plugging in the vectors, To avoid fractions we use instead (point (a) of Remark 9.42), with . In terms of the relation reads , that is, .
Step 3. Since and , the relation is . Plugging in, and we use , with ; then .
Check: , , . Normalizing, is an orthonormal basis of . The relations found in the three steps, collected, are all that is needed for the QR factorization of , computed after the QR theorem below.
Let Step 1. , with .
Step 2. Since , the relation (9.6) is . Plugging in the vectors, and we use , with . In terms of the relation reads , that is, .
Step 3. Since and , the relation is . Plugging in, and we use , with ; then .
Check: , , . So , , is an orthogonal basis of , and is an orthonormal basis of it. The relations of the three steps, collected, express each column of through the new vectors:
Let be an matrix with linearly independent columns. Then where is an column orthonormal matrix whose columns form an orthonormal basis of , and is an upper triangular matrix with positive diagonal entries; in particular is invertible. Explicitly, let be the GramโSchmidt vectors of the columns of , and write the relations (9.8) as , where and is the upper triangular matrix with entries above the diagonal and on it. With , (9.9) that is, for and . Moreover .
By Theorem 9.40, the vectors are nonzero and orthogonal and span , so the vectors form an orthonormal basis of ; their matrix is (column of divided by ), and by Lemma 9.15. Substituting into the relations (9.8) gives Let be the upper triangular matrix whose column holds these coefficients, as in (9.9). By the column view (Theorem 1.12), column of is , so ; and , since multiplying on the left by multiplies row by . The diagonal entries are positive, so (Lemma 5.16) and is invertible. Finally .
□No extra work is needed to find : the GramโSchmidt process already produces the relations (9.8), that is, column of , and (9.9) turns them into column of by multiplying the coefficient of by . This works just as well with rescaled vectors (point (a) of Remark 9.42): write each in terms of the and multiply the coefficient of by , because as well. The formula , whose entry is , is a good check; its entries below the diagonal come out . For a square , the matrix is an orthogonal matrix, and writes the map as a triangular map followed by a length-preserving one. One can also show that the factorization with positive diagonal in is unique.
Let , the vectors of Example 9.43. The relations found there, say with . The lengths are , , , so by (9.9) (in the form of Remark 9.46) we multiply the rows of by these lengths: and with the of Example 9.43. As a check with : the entry is , and the entry is indeed .
For of Example 9.44, the relations found there are so with . The lengths are , , ; multiplying the rows of by them (Remark 9.46) gives , and dividing the columns of by them gives : For instance, since and , the relation for becomes which is the second column of . As a check with : , and .
Apply GramโSchmidt to , . What is the second entry of ?
.
has first column , and is its QR factorization. What is the entry of ?
.
9.6 Least-squares solutions
We return to an inconsistent system . As varies, runs over the subspace , and the residual is shortest when is the point of closest to , which is the projection . So the โbestโ solves the consistent system . The three ways of computing projections in Sections 9.3โ9.5 become three methods: the normal equations , the GramโSchmidt data, and the QR factorization.
Let be and . A least-squares solution of is a vector such that The number is the least-squares error. The name refers to : a least-squares solution makes the sum of the squares of the errors as small as possible. If is consistent, its least-squares solutions are its ordinary solutions, with error .
Let be , , and . For the following are equivalent:
- is a least-squares solution of ;
- ;
- is orthogonal to every column of ;
- solves the normal equations (9.10)
Let ; the vectors , for , are exactly the vectors of .
(a) (b). By Theorem 9.32, for is smallest exactly at . So minimizes if and only if .
(b) (c). If , then . Conversely, if (by part (a) of Proposition 9.23 this is (c)), then is a decomposition with parts in and , and by uniqueness in the projection theorem (Theorem 9.29), .
(c) (d). The -th entry of is the inner product of column of with . So (c) says , which is (d).
Finally, , so is consistent, and its solutions are least-squares solutions by (b). The error is .
□The argument in the proof of Lemma 9.28 works for every matrix , not only for one with independent columns: implies , so ; and clearly implies . Hence the second by the rank theorem (Theorem 7.9), since and have the same number of columns. In particular, is invertible if and only if the columns of are independent, which is the converse of Lemma 9.28. This is used in the next theorem.
For an matrix the following are equivalent:
- the columns of are linearly independent;
- is invertible;
- for every , the system has exactly one least-squares solution.
(a) (b) is Lemma 9.28. (b) (a): by Remark 9.51, , which is when is invertible, so the columns of are independent. (b) (c): the normal equations (9.10) have the unique solution , and they characterize the least-squares solutions by Theorem 9.50. (c) (a): take , whose least-squares solutions are the solutions of ; if they are unique, the columns are independent.
For the QR statement, gives and . So the normal equations (9.10) read , and since is invertible (its determinant is ), this is equivalent to (9.12). The formula for is (9.5), because the columns of are an orthonormal basis of .
□Let have independent columns, and let be the GramโSchmidt vectors (possibly rescaled), . The GramโSchmidt relations (9.8), which write each column as , give with upper triangular and invertible, as in Theorem 9.45. Then:
- with ;
- the least-squares solution is the solution of , where .
(a) is the projection formula (9.4) for the orthogonal basis of . For (b), is upper triangular with diagonal entries (as in the proof of Theorem 9.45), so it is invertible. By the characterization of least-squares solutions (Theorem 9.50), is the solution of , that is, . The columns of are independent, so this holds exactly when .
□For a matrix with independent columns we now have three ways to the least-squares solution: (a) the GramโSchmidt data, with Proposition 9.53; (b) the QR factorization, solving (9.12); (c) the normal equations (9.10). By hand, (c) is usually the shortest for small matrices. In floating-point arithmetic, (b) is the standard choice: forming squares the sensitivity of the problem to rounding errors, while preserves lengths (Proposition 9.20) and so does not magnify errors. Library routines for least squares (such as the backslash operator of MATLAB, or numpy.linalg.lstsq) are based on orthogonal factorizations, not on the normal equations.
For the system , , of Example 9.1, and . The normal equation (9.10) is , that is, , so : the average of the measurements. The residual is whose entries sum to (it is orthogonal to the column ), and the least-squares error is . In general, for measurements of one quantity, the least-squares estimate is their mean .
We fit the line to the data of Example 9.2, , by the method of the normal equations (9.10). The system has one equation for each data point, so The columns of are and . As in Lemma 9.15, the entries of are the inner products of the columns of , and the entries of are the inner products of the columns with . Each inner product is a sum over the data points: since , , and . Solving the normal equations (9.10), here by (9.11) with , we get The least-squares line is , drawn in the figure. Its values at are , where ; the residual is , and the least-squares error is . No other line has a smaller sum of squared vertical errors.
Let The system is inconsistent: row reduction of gives four pivots, one of them in the last column. We find its least-squares solution in three ways.
Method 1: GramโSchmidt (9.6). . Since and , the relation is , that is, . Plugging in, Next and , with , so the relation is , that is, . Plugging in, The GramโSchmidt vectors are columns of the matrix of Example 9.17: , , , each with . The relations , , say with Project onto : , , , so and By Proposition 9.53, solve by back substitution:
Method 2: QR. All three lengths are , so by (9.9), and : (One checks ; for instance its entry is .) Back substitution in (9.12): , so ; , so ; , so .
Method 3: the normal equations (9.10). Row reduction of the augmented matrix gives and back substitution gives , , again.
Conclusion. All three methods give and the least-squares error is . As the characterization of least-squares solutions (Theorem 9.50) requires, the residual is orthogonal to each column of : , , .
Take the matrix of Example 9.44 and . The system is and the last two equations contradict each other. Using Example 9.48, and back substitution in (9.12) gives , then , so , and , so . Thus : the first two equations hold exactly, and splits the difference between and . The residual is , and the error is .
If the columns of are dependent, is still unique, but has infinitely many solutions, all of the form with (the structure theorem, Theorem 3.3). For example, for and , the normal equations (9.10) are and , a single equation : every point of this line is a least-squares solution, and each gives , the projection of onto the line . In such cases one usually asks in addition for the least-squares solution of smallest length. It is the one orthogonal to , that is, the one in (Remark 9.25): with , so , of length .
Four measurements of a quantity give , , , . What is the least-squares solution of , , , ?
Example 9.55: the normal equation is .
Find the least-squares line for the data , , . What is the slope ?
The columns and are orthogonal, so and .
Let be a matrix with independent columns and the least-squares solution of . Which statement is always true?
See the characterization of least-squares solutions (Theorem 9.50), parts (c) and (d). A matrix has no inverse, and may be inconsistent.
Least squares fits not only lines: any model that is linear in its unknown coefficients, such as a parabola or a combination of sines and cosines, leads to an inconsistent system and to the same normal equations.
When the columns of are dependent, the least-squares solutions form the set (Remark 9.59), and the one of smallest length is the unique least-squares solution lying in , the orthogonal complement of . It is written , where is the pseudoinverse of ; when the columns are independent, and is the of (9.11). Both and the most reliable numerical least-squares methods come from the singular value decomposition (SVD) , which exists for every matrix: () and () are orthogonal matrices, and is an matrix whose only nonzero entries are the singular values in positions , with . Then , where the matrix has in the same positions. The SVD also shows how close is to having dependent columns, which is what matters when the data are rounded.
The matrices of this lecture also carry a structural message. A projection matrix satisfies and is diagonalizable with an orthogonal basis of eigenvectors, and is symmetric as well. The spectral theorem says that this is true of every symmetric matrix: it can be diagonalized as with orthogonal, which removes both obstacles to diagonalization met in Lecture 8. Applied to the symmetric matrix , whose eigenvalues are and , it gives the matrix and the singular values of the SVD.
Index of statements
- Example 9.1 (Measuring one quantity three times)
- Example 9.2 (Fitting a line to data)
- Remark 9.3 (Why exact solutions are rare)
- Remark 9.4 (The plan)
- Definition 9.5 (Inner product)
- Proposition 9.6 (Properties of the inner product)
- Definition 9.7 (Length, distance, unit vector)
- Remark 9.8 (Why this is the right length)
- Example 9.9 (Computing lengths)
- Definition 9.10 (Orthogonal vectors)
- Theorem 9.11 (Pythagoras)
- Theorem 9.12 (CauchyโSchwarz and triangle inequalities)
- Remark 9.13 (Correlation and angles)
- Definition 9.14 (Orthogonal sets, matrices and bases)
- Lemma 9.15 (The matrix records inner products)
- Remark 9.16 (Correlation tables and attention in transformers)
- Example 9.17 (A column orthogonal matrix)
- Theorem 9.18 (Orthogonal sets are independent)
- Example 9.19 (Coordinates without row reduction)
- Proposition 9.20 (Column orthonormal matrices preserve inner products)
- Example 9.21 (Checking the proposition)
- Definition 9.22 (Orthogonal complement)
- Proposition 9.23 (Computing )
- Proposition 9.24 (The dimension of )
- Remark 9.25 (The four subspaces of a matrix)
- Example 9.26 (A plane and its normal line)
- Example 9.27 (Complements in )
- Lemma 9.28 ( is invertible)
- Theorem 9.29 (The orthogonal projection theorem)
- Definition 9.30 (Orthogonal projection)
- Example 9.31 (Projecting onto a plane)
- Theorem 9.32 (Best approximation)
- Definition 9.33 (Distance to a subspace)
- Theorem 9.34 (Properties of the projection)
- Remark 9.35 (The projection as a diagonalizable map)
- Theorem 9.36 (The projection formula for an orthogonal basis)
- Remark 9.37 (Projection onto a line; a warning)
- Example 9.38 (The plane again, with an orthogonal basis)
- Example 9.39 (A projection in )
- Theorem 9.40 (The GramโSchmidt process)
- Remark 9.41 (The GramโSchmidt relations)
- Remark 9.42 (Practical points)
- Example 9.43 (GramโSchmidt in )
- Example 9.44 (GramโSchmidt on the columns of a matrix)
- Theorem 9.45 (The QR factorization)
- Remark 9.46 (Computing )
- Example 9.47 (QR of the matrix)
- Example 9.48 (QR of the matrix)
- Definition 9.49 (Least-squares solution)
- Theorem 9.50 (Characterization of least-squares solutions)
- Remark 9.51 ( in general)
- Theorem 9.52 (When the least-squares solution is unique)
- Proposition 9.53 (Least squares from the GramโSchmidt data)
- Remark 9.54 (Three methods)
- Example 9.55 (The three measurements)
- Example 9.56 (The least-squares line)
- Example 9.57 (One problem, three methods)
- Example 9.58 (Two conflicting equations)
- Remark 9.59 (Dependent columns)
- Remark 9.60 (Looking ahead)