We now solve the general linear least-squares problem in Definition 3.1.1. That is, given A∈Rm×n and b∈Rm, with m>n, find the x∈Rn that minimizes ∥b−Ax∥2.
There is a concise explicit solution. In the following proof we make use of the elementary algebraic fact that for two vectors u and v,
The normal equations have a geometric interpretation, as shown in Figure 3.2.1. The vector in the range (column space) of A that lies closest to b makes the vector difference Ax−b perpendicular to the range. Thus for any z, we must have (Az)T(Ax−b)=0, which is satisfied if AT(Ax−b)=0.
If we associate the left-hand side of the normal equations as (ATA)x, we recognize (3.2.3) as a squaren×n linear system to solve for x.
Mathematically, the overdetermined least-squares problem Ax≈b has the solution x=A+b.
Computationally we can generalize the observation about Julia from Chapter 2: backslash is equivalent mathematically to left-multiplication by the inverse (in the square case) or pseudoinverse (in the rectangular case) of a matrix. One can also compute the pseudoinverse directly using pinv, but as with matrix inverses, this is rarely necessary in practice.
The matrix ATA appearing in the pseudoinverse has some important properties.
The definition of the pseudoinverse involves taking the inverse of a matrix, so it is not advisable to use the pseudoinverse computationally. Instead, we use the definition of the normal equations to set up a linear system, which we already know how to solve. In summary, the steps for solving the linear least squares problem Ax≈b are:
In the last step we can exploit the fact, proved in Theorem 3.2.2, that N is symmetric and positive definite, and use Cholesky factorization as in Exploiting matrix structure. This detail is included in Function 3.2.2.
We have already used A\b as the native way to solve the linear least-squares problem Ax≈b in Julia. The algorithm employed by the backslash does not proceed through the normal equations, because of instability.
The conditioning of the linear least-squares problem relates changes in the solution x to those in the data, A and b. A full accounting of the condition number is too messy to present here, but we can get the main idea. We start by generalizing our previous definition of the matrix condition number.
Provided that the minimum residual norm ∥b−Ax∥ is relatively small, the conditioning of the linear least-squares problem is close to κ(A).
As an algorithm, the normal equations begin by computing the data for the n×n system (ATA)x=ATb. When these equations are solved, perturbations to the data can be amplified by a factor κ(ATA).
The following can be proved using results in Chapter 7.
This squaring of the condition number in the normal equations is the cause of instability. If κ(A) is large, the squaring of it can destabilize the normal equations: while the solution of the least-squares problem is sensitive, finding it via the normal equations makes it doubly so.
✍ Use (3.2.4) to find the pseudoinverse A+ of the matrix A=[1−23]T.
✍ Prove the first statement of Theorem 3.2.2: ATA is symmetric for any m×n matrix A with m≥n.
✍ Prove that if A is an invertible square matrix, then A+=A−1.
(a) ✍ Show that for any m×nA with m>n for which ATA is nonsingular, A+A is the n×n identity.
(b) ⌨ Show using an example in Julia that AA+ is not an identity matrix. (This matrix has rank no greater than n, so it can’t be an m×m identity.)
✍ Prove that the vector AA+b is the vector in the column space (i.e., range) of A that is closest to b in the sense of the 2-norm.
✍ Show that the flop count for Function 3.2.2 is asymptotically ∼2mn2+31n3. (In finding the asymptotic count you can ignore terms like mn whose total degree is less than 3.)
⌨ Let t1,…,tm be m equally spaced points in [0,2π]. In this exercise, use m=500.
(a) Let Aβ be the matrix in (3.1.2) that corresponds to fitting data with the function c1+c2sin(t)+c3cos(βt). Using the identity (3.2.7), make a table of the condition numbers of Aβ for β=2,1.1,1.01,…,1+10−8.
(b) Repeat part (a) using the fitting function c1+c2sin2(t)+c3cos2(βt).
(c) Why does it make sense that κ(Aβ)→∞ as β→1 in part (b) but not in part (a)?
✍ ⌨ When A is m×n with rank less than n, the pseudoinverse is still defined and can be computed using pinv from LinearAlgebra. However, the behavior in this case is not always intuitive. Let