Mehmet Emre Tiryaki
Back to Notes

Least Squares and QR

Overdetermined Systems

Ax=bAx = b with A∈Rm×nA \in \mathbb{R}^{m \times n}, m>nm > n, has no exact solution in general. E.g. fitting mm points with fewer than mm basis functions instead of interpolating (to avoid overfitting),

f(t)=c1f1(t)+⋯+cnfn(t),V=[f1(t1)⋯fn(t1)⋮⋮f1(tm)⋯fn(tm)]∈Rm×n,Vc≈yf(t) = c_1 f_1(t) + \cdots + c_n f_n(t), \qquad V = \begin{bmatrix} f_1(t_1) & \cdots & f_n(t_1) \\ \vdots & & \vdots \\ f_1(t_m) & \cdots & f_n(t_m) \end{bmatrix} \in \mathbb{R}^{m \times n}, \qquad Vc \approx y

where the fit depends linearly on cc. Least squares minimizes the sum of squares of the residuals yi−f(ti)y_i - f(t_i),

R(c1,…,cn)=∑i=1m[yi−f(ti)]2=rTr=∥r∥22,r=y−VcR(c_1, \ldots, c_n) = \sum_{i=1}^m [y_i - f(t_i)]^2 = r^T r = \|r\|_2^2, \qquad r = y - Vc

i.e. in general argminx∈Rn∥b−Ax∥22\mathrm{argmin}_{x \in \mathbb{R}^n} \|b - Ax\|_2^2.

Some nonlinear fits become linear after a transformation, minimizing the residuals of log⁡y\log y rather than of yy:

  • g(t)=a1ea2tg(t) = a_1 e^{a_2 t}: log⁡y≈log⁡a1+a2t=c1+c2t\log y \approx \log a_1 + a_2 t = c_1 + c_2 t,
  • g(t)=a1ta2g(t) = a_1 t^{a_2}: log⁡y≈log⁡a1+a2log⁡t\log y \approx \log a_1 + a_2 \log t, so fit z=log⁡yz = \log y against s=log⁡ts = \log t.

Normal Equations

xx solves the linear least squares problem iff

AT(Ax−b)=0  ⟺  ATAx=ATbA^T(Ax - b) = 0 \iff A^T A x = A^T b

(expand ∥A(x+y)−b∥22=∥Ax−b∥22+2yTAT(Ax−b)+∥Ay∥22\|A(x + y) - b\|_2^2 = \|Ax - b\|_2^2 + 2y^T A^T(Ax - b) + \|Ay\|_2^2). This turns the m×nm \times n problem into an n×nn \times n one. ATAA^T A is symmetric, and if ATAz=0A^T A z = 0 then 0=zTATAz=∥Az∥220 = z^T A^T A z = \|Az\|_2^2, so ATAA^T A is singular only if the columns of AA are linearly dependent, and otherwise it is positive definite. Then

x=A+b,A+=(ATA)−1ATx = A^+ b, \qquad A^+ = (A^T A)^{-1} A^T

Algorithm N=ATAN = A^T A, z=ATbz = A^T b, solve Nx=zNx = z by Cholesky.

With κ(A)=∥A∥⋅∥A+∥\kappa(A) = \|A\| \cdot \|A^+\| (∞\infty if rank A<n\mathrm{rank}\, A < n), κ(ATA)=κ(A)2\kappa(A^T A) = \kappa(A)^2 in the 2-norm, so the normal equations square the condition number and are very sensitive for ill-conditioned AA.

Orthogonality

u,vu, v are orthogonal if uTv=0u^T v = 0, and q1,…,qkq_1, \ldots, q_k are orthonormal if also qiTqi=1q_i^T q_i = 1. For orthogonal q1,q2q_1, q_2,

∥q1−q2∥2=(q1−q2)T(q1−q2)=∥q1∥2+∥q2∥2\|q_1 - q_2\|^2 = (q_1 - q_2)^T(q_1 - q_2) = \|q_1\|^2 + \|q_2\|^2

so adding and subtracting them is well-conditioned. For a matrix QQ, QTQQ^T Q is diagonal iff its columns are orthogonal, and QTQ=IQ^T Q = I iff they are orthonormal (an ONC matrix). For Q∈Rn×kQ \in \mathbb{R}^{n \times k} ONC and x∈Rkx \in \mathbb{R}^k,

QTQ=I,∥Qx∥2=∥x∥2,∥Q∥2=1Q^T Q = I, \qquad \|Qx\|_2 = \|x\|_2, \qquad \|Q\|_2 = 1

A square ONC matrix is orthogonal: Q−1=QTQ^{-1} = Q^T, QTQ^T is orthogonal, κ2(Q)=1\kappa_2(Q) = 1, ∥AQ∥2=∥A∥2\|AQ\|_2 = \|A\|_2, and QUQU is orthogonal for UU orthogonal.

QR Factorization

A=QRA = QR with Q∈Rm×mQ \in \mathbb{R}^{m \times m} orthogonal and R∈Rm×nR \in \mathbb{R}^{m \times n} upper triangular. Since the last m−nm - n rows of RR are zero, keeping only the first nn columns of QQ gives the thin QR,

A=Q^R^,Q^∈Rm×n ONC,R^∈Rn×n upper triangularA = \hat{Q}\hat{R}, \qquad \hat{Q} \in \mathbb{R}^{m \times n} \text{ ONC}, \quad \hat{R} \in \mathbb{R}^{n \times n} \text{ upper triangular}

If AA is not rank deficient, the normal equations become

ATAx=ATb  ⟹  R^TR^x=R^TQ^Tb  ⟹  R^x=Q^TbA^T A x = A^T b \implies \hat{R}^T \hat{R} x = \hat{R}^T \hat{Q}^T b \implies \hat{R} x = \hat{Q}^T b

an n×nn \times n triangular system, stable since it does not square κ\kappa. Gram-Schmidt computes Q^\hat{Q} but is not stable.

Householder Reflections

Like LU, introduce zeros below the diagonal column by column, but with orthogonal transformations. For a vector zz, find PP with PzPz nonzero only in its first entry,

Pz=±∥z∥2e1,v=∥z∥2e1−z,P=I−2vvTvTvPz = \pm \|z\|_2 e_1, \qquad v = \|z\|_2 e_1 - z, \qquad P = I - 2\frac{vv^T}{v^T v}

(P=IP = I if v=0v = 0). PP is symmetric and orthogonal.

Algorithm Take zz as the first column of AA, find PP, and set A←PAA \leftarrow PA. Then ignore the first row and column, take zz as the first column of the remaining submatrix, and repeat. At step kk,

Qk=[Ik−100Pk],Pk∈R(m−k+1)×(m−k+1)Q_k = \begin{bmatrix} I_{k-1} & 0 \\ 0 & P_k \end{bmatrix}, \qquad P_k \in \mathbb{R}^{(m-k+1) \times (m-k+1)}

is orthogonal, and QnQn−1⋯Q1A=RQ_n Q_{n-1} \cdots Q_1 A = R, so Q=(Qn⋯Q1)TQ = (Q_n \cdots Q_1)^T. QT=Qn(Qn−1(⋯(Q1I)))Q^T = Q_n(Q_{n-1}(\cdots(Q_1 I))) is built by applying the same steps to II, without forming the PP's. About 2mn2−23n32mn^2 - \frac{2}{3}n^3 flops.