Ax=b with A∈Rm×n, m>n, has no exact solution in general. E.g. fitting m points with fewer than m basis functions instead of interpolating (to avoid overfitting),
Some nonlinear fits become linear after a transformation, minimizing the residuals of logy rather than of y:
g(t)=a1ea2t: logy≈loga1+a2t=c1+c2t,
g(t)=a1ta2: logy≈loga1+a2logt, so fit z=logy against s=logt.
Normal Equations
x solves the linear least squares problem iff
AT(Ax−b)=0⟺ATAx=ATb
(expand ∥A(x+y)−b∥22=∥Ax−b∥22+2yTAT(Ax−b)+∥Ay∥22). This turns the m×n problem into an n×n one. ATA is symmetric, and if ATAz=0 then 0=zTATAz=∥Az∥22, so ATA is singular only if the columns of A are linearly dependent, and otherwise it is positive definite. Then
x=A+b,A+=(ATA)−1AT
AlgorithmN=ATA, z=ATb, solve Nx=z by Cholesky.
With κ(A)=∥A∥⋅∥A+∥ (∞ if rankA<n), κ(ATA)=κ(A)2 in the 2-norm, so the normal equations square the condition number and are very sensitive for ill-conditioned A.
Orthogonality
u,v are orthogonal if uTv=0, and q1,…,qk are orthonormal if also qiTqi=1. For orthogonal q1,q2,
∥q1−q2∥2=(q1−q2)T(q1−q2)=∥q1∥2+∥q2∥2
so adding and subtracting them is well-conditioned. For a matrix Q, QTQ is diagonal iff its columns are orthogonal, and QTQ=I iff they are orthonormal (an ONC matrix). For Q∈Rn×k ONC and x∈Rk,
QTQ=I,∥Qx∥2=∥x∥2,∥Q∥2=1
A square ONC matrix is orthogonal: Q−1=QT, QT is orthogonal, κ2(Q)=1, ∥AQ∥2=∥A∥2, and QU is orthogonal for U orthogonal.
QR Factorization
A=QR with Q∈Rm×m orthogonal and R∈Rm×n upper triangular. Since the last m−n rows of R are zero, keeping only the first n columns of Q gives the thin QR,
A=Q^R^,Q^∈Rm×n ONC,R^∈Rn×n upper triangular
If A is not rank deficient, the normal equations become
ATAx=ATb⟹R^TR^x=R^TQ^Tb⟹R^x=Q^Tb
an n×n triangular system, stable since it does not square κ. Gram-Schmidt computes Q^ but is not stable.
Householder Reflections
Like LU, introduce zeros below the diagonal column by column, but with orthogonal transformations. For a vector z, find P with Pz nonzero only in its first entry,
Pz=±∥z∥2e1,v=∥z∥2e1−z,P=I−2vTvvvT
(P=I if v=0). P is symmetric and orthogonal.
Algorithm Take z as the first column of A, find P, and set A←PA. Then ignore the first row and column, take z as the first column of the remaining submatrix, and repeat. At step k,
Qk=[Ik−100Pk],Pk∈R(m−k+1)×(m−k+1)
is orthogonal, and QnQn−1⋯Q1A=R, so Q=(Qn⋯Q1)T. QT=Qn(Qn−1(⋯(Q1I))) is built by applying the same steps to I, without forming the P's. About 2mn2−32n3 flops.