QR decomposition is an alternative to the normal equations for solving overdetermined systems. It factors the coefficient matrix A into an orthonormal matrix Q and an upper triangular matrix R, then reduces the least squares problem to a simple triangular system. QR decomposition tends to be numerically more stable than forming ATA explicitly.
The Factorization A=QR
Any m×n matrix A with m≥n and linearly independent columns can be written as:
A=QR
where:
Q is an m×n matrix whose columns q1,q2,…,qn form an orthonormal set.
R is an n×n upper triangular matrix with positive diagonal entries.
The columns of Q are computed from the columns of A using the Gram-Schmidt process.
The Gram-Schmidt Process
Let u1,u2,…,un be the columns of A. The Gram-Schmidt process converts them into orthonormal vectors q1,q2,…,qn using two formulas applied for k=1,2,…,n:
pk=uk−i=1∑k−1(ukTqi)qiqk=∣pk∣pk
At each step, pk removes from uk all components in the directions already spanned by q1,…,qk−1. Dividing by ∣pk∣ then normalizes the result to unit length.
For k=1: The sum is empty (lower limit exceeds upper limit), so p1=u1. We just normalize:
p1=u1,q1=∣u1∣u1
For k=2: Subtract the projection onto q1:
p2=u2−(u2Tq1)q1,q2=∣p2∣p2
For k=3: Subtract projections onto both q1 and q2:
p3=u3−(u3Tq1)q1−(u3Tq2)q2,q3=∣p3∣p3
Assembling R
Once Q is known, the entries of R are the dot products of the original columns of A with the orthonormal columns of Q. Because of the upper triangular structure of the Gram-Schmidt construction:
The second factor is exactly the upper triangular matrix R defined earlier, confirming that the Gram-Schmidt construction gives a valid QR factorization.