Building an orthonormal basis
Orthonormal columns are the best-behaved columns a matrix can have: QTQ=I, so Q keeps lengths and angles and never amplifies errors. Gram-Schmidt turns any independent columns a1,…,an into orthonormal ones: take q1=a1/∥a1∥, then for each next column subtract its projections onto the q's found so far and normalise what remains.
A=QR,Rij=qi⋅aj (i≤j),qj=Rjjaj−∑i<jRijqi(1) Recording the numbers you used gives the QR decomposition: Q has the orthonormal columns and R is upper triangular, with Rij=qi⋅aj and the lengths Rjj on its diagonal. For least squares, ∥Ax−b∥=∥QRx−b∥ is minimised by Rx^=QTb, a triangular system solved by back substitution, without ever forming ATA. This is why κ(R)=κ(A) instead of κ(A)2.
Gram-Schmidt on two columnsTake
A=(3412), with columns
a1=(3,4) and
a2=(1,2). Then
R11=∥a1∥=5 and
q1=(0.6,0.8). Next
R12=q1⋅a2=2.2, and
a2−2.2q1=(−0.32,0.24) has length
R22=0.4, so
q2=(−0.8,0.6). Thus
Q=(0.60.8−0.80.6) and
R=(502.20.4). Check:
detA=2=detR.
- Q: orthonormal columns, QTQ=I, no error amplification.
- R: upper triangular; its diagonal holds the lengths left after removing earlier directions.
- Modified Gram-Schmidt (or Householder reflections in libraries) keeps Q orthogonal in floating point; the textbook version loses orthogonality on nearly dependent columns.
Why libraries use QR for regressionRegressors in finance are often nearly collinear (many correlated factors, polynomial features). The normal equations would square that condition number and silently return garbage, while QR (or the SVD) copes. That is the algorithm behind the least squares routines of NumPy and statsmodels, and the one you will build in the capstone.
Lab · what's graded
- The first diagonal entry10 pts
- The matrix Q20 pts
- The matrix R20 pts
- A determinant from R15 pts
- Least squares with QR15 pts
- Why QR is more accurate20 pts