Least Squares and QR

Summary

When Ax=b is inconsistent, least squares finds x^ minimizing Axb . The residual is orthogonal to Col(A) . QR factorization provides a stable way to compute x^ without forming ATA explicitly in unstable ways.

Prerequisites

Orthogonality and Projections, Matrices and Row Reduction, Systems of Linear Equations. Hub: Linear Algebra.

Definition / Statement

Given ARm×n and bRm , a least-squares solution is any x^Rn minimizing

Axb2=(Axb)(Axb).

If the columns of A are linearly independent, x^ is unique and satisfies the normal equations

ATAx^=ATb.

A QR factorization of full-column-rank A writes

A=QR,

where QRm×n has orthonormal columns ( QTQ=In ) and RRn×n is upper triangular and invertible.

Objects and Dimensions

Object Meaning Dimensions
A data / design matrix m×n , typically mn
b observations m×1
x^ least-squares coefficient vector n×1
Q,R thin QR factors m×n , n×n
r=bAx^ residual m×1

Notation

Symbol Meaning
||2 Euclidean norm
ATAx^=ATb normal equations
A=QR thin QR factorization

Conditions / Assumptions

Matrix / Vector Form

Normal equations

ATAx^=ATb,x^=(ATA)1ATb(rankA=n).

Geometry: Ax^=projCol(A)b and AT(bAx^)=0 .

Via QR ( A=QR ):

ATA=RTQTQR=RTR, RTRx^=RTQTbRx^=QTb

(solve by forward/back substitution on the triangular system).

Procedure

  1. Check whether Ax=b is consistent; if not, use least squares.
  2. If using normal equations: form ATA and ATb , solve the n×n system (watch conditioning).
  3. If using QR: factor A=QR (Gram–Schmidt, Householder, or library routine), solve Rx^=QTb .
  4. Report residual norm bAx^ as a fit diagnostic.

Worked Example

Fit a line y=c0+c1x through points (0,1) , (1,1) , (2,3) in the least-squares sense.

A=(101112),b=(113),c=(c0c1). ATA=(3335),ATb=(57).

Solve (3335)(c0c1)=(57) :
subtract first equation from second after scaling gives 2c1=2 , so c1=1 , then 3c0+3=5 , so c0=23 .

Model: y=23+x .

Common Mistakes

Connections

References

Least squares and QR as in MIT 18.06.[1]


  1. MIT OpenCourseWare, 18.06 Linear Algebra, https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/ ↩︎