จะคล้าย ๆ กับ 7 – Interpolation แต่ไม่เหมือนเสียทีเดียว เพราะในกรณีนี้เรามีการ “จำกัดรูปแบบของสมการ” ให้เป็นหน้าตาที่กำหนดไว้ล่วงหน้า เช่น อยากให้เป็นสมการกำลังสอง แต่ใน Interpolation จะเลือกสมการที่ “ง่ายที่สุด” ซึ่งสามารถผ่านทุกจุดได้
ต้องทำเป็น
Classic Application: Data Fitting
Example: Fitting a Quadratic Function
Problem : Fit a quadratic function through five given data points ( t i , y i ) (t_i, y_i) ( t i , y i ) , i = 1 , 2 , … , 5 i = 1, 2, \ldots, 5 i = 1 , 2 , … , 5
Unknown quadratic : y = c t 2 + d t + e y = ct^2 + dt + e y = c t 2 + d t + e
Goal : We want the quadratic to pass through all five data points (if possible)
This means we need to satisfy:
y 1 = c t 1 2 + d t 1 + e y 2 = c t 2 2 + d t 2 + e y 3 = c t 3 2 + d t 3 + e y 4 = c t 4 2 + d t 4 + e y 5 = c t 5 2 + d t 5 + e \begin{align}
y_1 &= ct_1^2 + dt_1 + e \\
y_2 &= ct_2^2 + dt_2 + e \\
y_3 &= ct_3^2 + dt_3 + e \\
y_4 &= ct_4^2 + dt_4 + e \\
y_5 &= ct_5^2 + dt_5 + e
\end{align} y 1 y 2 y 3 y 4 y 5 = c t 1 2 + d t 1 + e = c t 2 2 + d t 2 + e = c t 3 2 + d t 3 + e = c t 4 2 + d t 4 + e = c t 5 2 + d t 5 + e
This is equivalent to solving:
[ t 1 2 t 1 1 t 2 2 t 2 1 t 3 2 t 3 1 t 4 2 t 4 1 t 5 2 t 5 1 ] [ c d e ] = [ y 1 y 2 y 3 y 4 y 5 ] \begin{bmatrix}
t_1^2 & t_1 & 1 \\
t_2^2 & t_2 & 1 \\
t_3^2 & t_3 & 1 \\
t_4^2 & t_4 & 1 \\
t_5^2 & t_5 & 1
\end{bmatrix}
\begin{bmatrix}
c \\
d \\
e
\end{bmatrix}
=
\begin{bmatrix}
y_1 \\
y_2 \\
y_3 \\
y_4 \\
y_5
\end{bmatrix} t 1 2 t 2 2 t 3 2 t 4 2 t 5 2 t 1 t 2 t 3 t 4 t 5 1 1 1 1 1 c d e = y 1 y 2 y 3 y 4 y 5
for c , d , e c, d, e c , d , e .
This is a linear system: A x = b \boxed{Ax = b} A x = b
More rows than columns ⇒ The system is overdetermined
In general, there is no solution
That is, no quadratic function that passes through all five data points
Analogy : Imagine trying to draw a smooth parabola (curve) through 5 random dots on paper. Unless the dots happen to lie perfectly on a parabola already, you can't make a single curve pass through all of them exactly!
Alternative Approach: Best Fit
Since we can't find x x x such that A x = b Ax = b A x = b exactly:
How about finding x x x such that A x Ax A x is as close to b b b as possible?
That is, find x x x that minimizes ∥ A x − b ∥ 2 \|Ax - b\|_2 ∥ A x − b ∥ 2
Analogy : If you can't hit the bullseye exactly, try to get as close as possible!
Linear Least-Squares Problem
Given : A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n and b ∈ R m b \in \mathbb{R}^m b ∈ R m
Find : x ∈ R n x \in \mathbb{R}^n x ∈ R n that minimizes ∥ A x − b ∥ 2 \boxed{\|Ax - b\|_2} ∥ A x − b ∥ 2
Sometimes written as A x ≅ b \boxed{Ax \cong b} A x ≅ b
Why Use the 2-Norm ?
Reason 1: Computational Ease
2-norm problem (min ∥ A x − b ∥ 2 \min \|Ax - b\|_2 min ∥ A x − b ∥ 2 ): Can be solved using calculus and linear algebra
1-norm problem (min ∥ A x − b ∥ 1 \min \|Ax - b\|_1 min ∥ A x − b ∥ 1 ) and ∞-norm problem (min ∥ A x − b ∥ ∞ \min \|Ax - b\|_\infty min ∥ A x − b ∥ ∞ ): These are linear programming (linear optimization) problems
3-norm problem (min ∥ A x − b ∥ 3 \min \|Ax - b\|_3 min ∥ A x − b ∥ 3 ): This is convex nonlinear programming
The 2-norm is mathematically "smooth" and differentiable, making it much easier to work with analytically.
Reason 2: Statistical Reasons
The 2-norm (squared) has strong statistical foundations
Related to maximum likelihood estimation under Gaussian noise assumptions
Related to minimizing variance in statistical models
Assumptions
Throughout this chapter, we assume :
m > n m > n m > n (more equations than unknowns - overdetermined system)
rank ( A ) = n \text{rank}(A) = n rank ( A ) = n (full column rank - columns are linearly independent)
Note : The cases where rank ( A ) < n \text{rank}(A) < n rank ( A ) < n or m < n m < n m < n are more difficult and require different techniques to solve (covered later).
Analogy : We assume we have more data points than parameters to fit (overdetermined), and that all our parameters are actually meaningful and independent (full rank).
Solving Least-Squares: The Method of Normal Equations
วิธีการ Solve แรก
Goal
We want to minimize ∥ A x − b ∥ 2 \|Ax - b\|_2 ∥ A x − b ∥ 2
Key Insight
This is the same as minimizing ∥ A x − b ∥ 2 2 \|Ax - b\|_2^2 ∥ A x − b ∥ 2 2 since ∥ A x − b ∥ 2 ≥ 0 \|Ax - b\|_2 \geq 0 ∥ A x − b ∥ 2 ≥ 0 by definition of norm.
Why square it? Because the square root function is monotonic, the minimum of f ( x ) f(x) f ( x ) occurs at the same point as the minimum of [ f ( x ) ] 2 [f(x)]^2 [ f ( x ) ] 2 . Squaring eliminates the square root and makes the math easier!
Mathematical Setup
Recall :
∥ w ∥ 2 2 = w T w \|w\|_2^2 = w^T w ∥ w ∥ 2 2 = w T w
( B C ) T = C T B T (BC)^T = C^T B^T ( B C ) T = C T B T
We can expand:
∥ A x − b ∥ 2 2 = ( A x − b ) T ( A x − b ) = ( x T A T − b T ) ( A x − b ) = x T A T A x − x T A T b − b T A x + b T b \begin{align}
\|Ax - b\|_2^2 &= (Ax - b)^T(Ax - b) \\
&= (x^T A^T - b^T)(Ax - b) \\
&= x^T A^T Ax - x^T A^T b - b^T Ax + b^T b
\end{align} ∥ A x − b ∥ 2 2 = ( A x − b ) T ( A x − b ) = ( x T A T − b T ) ( A x − b ) = x T A T A x − x T A T b − b T A x + b T b
Important observation : b T A x b^T Ax b T A x is a scalar
So its transpose equals itself: ( b T A x ) T = x T A T b = b T A x (b^T Ax)^T = x^T A^T b = b^T Ax ( b T A x ) T = x T A T b = b T A x
Therefore:
∥ A x − b ∥ 2 2 = x T A T A x − 2 b T A x + b T b (Equation 1) \boxed{\|Ax - b\|_2^2 = x^T A^T Ax - 2b^T Ax + b^T b} \quad \text{(Equation 1)} ∥ A x − b ∥ 2 2 = x T A T A x − 2 b T A x + b T b (Equation 1)
Lemma 1: Properties of A T A A^T A A T A
Lemma 1
Suppose A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n has rank n n n . Then A T A A^T A A T A is symmetric positive definite .
Proof:
Part 1 - Symmetry :
( A T A ) T = A T ( A T ) T = A T A (A^T A)^T = A^T (A^T)^T = A^T A ( A T A ) T = A T ( A T ) T = A T A ✓
Part 2 - Positive Definiteness :
Suppose x ∈ R n x \in \mathbb{R}^n x ∈ R n is nonzero
Then:
x T A T A x = ( A x ) T A x = ∥ A x ∥ 2 2 ≥ 0 x^T A^T Ax = (Ax)^T Ax = \|Ax\|_2^2 \geq 0 x T A T A x = ( A x ) T A x = ∥ A x ∥ 2 2 ≥ 0
But A x Ax A x is a nontrivial linear combination of columns of A A A (since x ≠ 0 x \neq 0 x = 0 )
Since columns of A A A are linearly independent (because rank of A A A is n n n ), we have A x ≠ 0 Ax \neq 0 A x = 0
So ∥ A x ∥ 2 ≠ 0 \|Ax\|_2 \neq 0 ∥ A x ∥ 2 = 0
Therefore: x T A T A x = ∥ A x ∥ 2 2 > 0 x^T A^T Ax = \|Ax\|_2^2 > 0 x T A T A x = ∥ A x ∥ 2 2 > 0 ✓
Why this matters : A T A A^T A A T A being symmetric positive definite means it's invertible, which is crucial for finding a unique solution to our least-squares problem!
Lemma 2
Suppose f ( x ) = x T C x − 2 h T x + d f(x) = x^T Cx - 2h^T x + d f ( x ) = x T C x − 2 h T x + d , where C C C is symmetric positive definite. Then f f f has a unique minimizer at x ∗ = C − 1 h \boxed{x^* = C^{-1}h} x ∗ = C − 1 h .
Proof:
Step 1 : Evaluate f ( x ∗ ) f(x^*) f ( x ∗ )
First:
f ( x ∗ ) = f ( C − 1 h ) = ( C − 1 h ) T C C − 1 h − 2 h T C − 1 h + d f(x^*) = f(C^{-1}h) = (C^{-1}h)^T CC^{-1}h - 2h^T C^{-1}h + d f ( x ∗ ) = f ( C − 1 h ) = ( C − 1 h ) T C C − 1 h − 2 h T C − 1 h + d
= h T C − T h − 2 h T C − 1 h + d = h^T C^{-T}h - 2h^T C^{-1}h + d = h T C − T h − 2 h T C − 1 h + d
But C C C is symmetric, so:
C − T = ( C T ) − 1 = C − 1 C^{-T} = (C^T)^{-1} = C^{-1} C − T = ( C T ) − 1 = C − 1
Therefore:
f ( x ∗ ) = h T C − 1 h − 2 h T C − 1 h + d = − h T C − 1 h + d f(x^*) = h^T C^{-1}h - 2h^T C^{-1}h + d = -h^T C^{-1}h + d f ( x ∗ ) = h T C − 1 h − 2 h T C − 1 h + d = − h T C − 1 h + d
Step 2 : Evaluate f ( x ∗ + y ) f(x^* + y) f ( x ∗ + y ) for any nonzero vector y y y (one way to show x ∗ x^* x ∗ is unique minimizer)
Let y ∈ R n y \in \mathbb{R}^n y ∈ R n be any arbitrary nonzero vector.
We have:
f ( x ∗ + y ) = ( C − 1 h + y ) T C ( C − 1 h + y ) − 2 h T ( C − 1 h + y ) + d f(x^* + y) = (C^{-1}h + y)^T C(C^{-1}h + y) - 2h^T(C^{-1}h + y) + d f ( x ∗ + y ) = ( C − 1 h + y ) T C ( C − 1 h + y ) − 2 h T ( C − 1 h + y ) + d
= ( h T C − 1 + y T ) ( h + C y ) − 2 h T C − 1 h − 2 h T y + d = (h^T C^{-1} + y^T)(h + Cy) - 2h^T C^{-1}h - 2h^T y + d = ( h T C − 1 + y T ) ( h + C y ) − 2 h T C − 1 h − 2 h T y + d
= h T C − 1 h + h T C − 1 C y + y T h + y T C y − 2 h T C − 1 h − 2 h T y + d = h^T C^{-1}h + h^T C^{-1}Cy + y^T h + y^T Cy - 2h^T C^{-1}h - 2h^T y + d = h T C − 1 h + h T C − 1 C y + y T h + y T C y − 2 h T C − 1 h − 2 h T y + d
= h T C − 1 h + h T y + y T h + y T C y − 2 h T C − 1 h − 2 h T y + d = h^T C^{-1}h + h^T y + y^T h + y^T Cy - 2h^T C^{-1}h - 2h^T y + d = h T C − 1 h + h T y + y T h + y T C y − 2 h T C − 1 h − 2 h T y + d
But h T y = ∑ i h i y i = y T h h^T y = \sum_i h_i y_i = y^T h h T y = ∑ i h i y i = y T h (scalars are symmetric).
So:
f ( x ∗ + y ) = h T C − 1 h + 2 h T y + y T C y − 2 h T C − 1 h − 2 h T y + d f(x^* + y) = h^T C^{-1}h + 2h^T y + y^T Cy - 2h^T C^{-1}h - 2h^T y + d f ( x ∗ + y ) = h T C − 1 h + 2 h T y + y T C y − 2 h T C − 1 h − 2 h T y + d
= − h T C − 1 h + d + y T C y = f ( x ∗ ) + y T C y = -h^T C^{-1}h + d + y^T Cy = f(x^*) + y^T Cy = − h T C − 1 h + d + y T C y = f ( x ∗ ) + y T C y
Step 3 : Show x ∗ x^* x ∗ is the unique minimizer
But C C C is symmetric positive definite, so:
y T C y > 0 y^T Cy > 0 y T C y > 0
Therefore:
f ( x ∗ + y ) = f ( x ∗ ) + y T C y > f ( x ∗ ) f(x^* + y) = f(x^*) + y^T Cy > f(x^*) f ( x ∗ + y ) = f ( x ∗ ) + y T C y > f ( x ∗ )
So x ∗ x^* x ∗ is the unique minimizer . ✓
The Solution: Normal Equations
Recall that we want to minimize equation (1):
∥ A x − b ∥ 2 2 = x T A T A x − 2 b T A x + b T b \|Ax - b\|_2^2 = x^T A^T Ax - 2b^T Ax + b^T b ∥ A x − b ∥ 2 2 = x T A T A x − 2 b T A x + b T b
Compare this with Lemma 2:
f ( x ) = x T C x − 2 h T x + d f(x) = x^T Cx - 2h^T x + d f ( x ) = x T C x − 2 h T x + d
Setting :
C = A T A C = A^T A C = A T A
h = A T b h = A^T b h = A T b
d = b T b d = b^T b d = b T b
We have that ∥ A x − b ∥ 2 2 \|Ax - b\|_2^2 ∥ A x − b ∥ 2 2 is minimized at:
x ∗ = C − 1 h = ( A T A ) − 1 A T b \boxed{x^* = C^{-1}h = (A^T A)^{-1}A^T b} x ∗ = C − 1 h = ( A T A ) − 1 A T b
Main Theorem
Theorem
x = ( A T A ) − 1 A T b \boxed{x = (A^T A)^{-1}A^T b} x = ( A T A ) − 1 A T b
is the unique solution to the linear least squares problem when A A A has rank n n n .
Method of Normal Equations
To solve min x ∥ A x − b ∥ 2 \min_x \|Ax - b\|_2 min x ∥ A x − b ∥ 2 :
Form products A T A A^T A A T A and A T b A^T b A T b
Solve แ using Cholesky factorization
(Assuming rank ( A ) = n \text{rank}(A) = n rank ( A ) = n , Lemma 1 tells us that A T A A^T A A T A is symmetric positive definite)
The equation A T A x = A T b \boxed{A^T Ax = A^T b} A T A x = A T b is called the normal equations .
ไม่ควรใช้ GEPP เนอะ
rank ( A ) = n \text{rank}(A) = n rank ( A ) = n ถึงจะใช้ Method ที่ว่ามาได้
Example 1: Fitting a Line
(2)
Problem : Given data points ( x , y ) (x, y) ( x , y ) : ( 0 , 1 ) , ( 1 , − 1 ) , ( 2 , 4 ) , ( 3 , 2 ) (0, 1), (1, -1), (2, 4), (3, 2) ( 0 , 1 ) , ( 1 , − 1 ) , ( 2 , 4 ) , ( 3 , 2 ) . We want to fit these 4 points with the line y = m x + c y = mx + c y = m x + c . Find the line that best fits the data points in least-squares sense using the method of normal equations.
Solution:
We want m m m and c c c satisfying:
m x 1 + c ≅ y 1 mx_1 + c \cong y_1 m x 1 + c ≅ y 1
m x 2 + c ≅ y 2 mx_2 + c \cong y_2 m x 2 + c ≅ y 2
m x 3 + c ≅ y 3 mx_3 + c \cong y_3 m x 3 + c ≅ y 3
m x 4 + c ≅ y 4 mx_4 + c \cong y_4 m x 4 + c ≅ y 4
In matrix form :
[ x 1 1 x 2 1 x 3 1 x 4 1 ] [ m c ] ≅ [ y 1 y 2 y 3 y 4 ] \begin{bmatrix} x_1 & 1 \\ x_2 & 1 \\ x_3 & 1 \\ x_4 & 1 \end{bmatrix} \begin{bmatrix} m \\ c \end{bmatrix} \cong \begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \end{bmatrix} x 1 x 2 x 3 x 4 1 1 1 1 [ m c ] ≅ y 1 y 2 y 3 y 4
Substituting the data points :
[ 0 1 1 1 2 1 3 1 ] [ m c ] ≅ [ 1 − 1 4 2 ] \begin{bmatrix} 0 & 1 \\ 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{bmatrix} \begin{bmatrix} m \\ c \end{bmatrix} \cong \begin{bmatrix} 1 \\ -1 \\ 4 \\ 2 \end{bmatrix} 0 1 2 3 1 1 1 1 [ m c ] ≅ 1 − 1 4 2
Let's call the matrix A A A and the right-hand side vector b b b .
By the method of normal equations :
A T A = [ 0 1 2 3 1 1 1 1 ] [ 0 1 1 1 2 1 3 1 ] = [ 14 6 6 4 ] A^T A = \begin{bmatrix} 0 & 1 & 2 & 3 \\ 1 & 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} 0 & 1 \\ 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{bmatrix} = \begin{bmatrix} 14 & 6 \\ 6 & 4 \end{bmatrix} A T A = [ 0 1 1 1 2 1 3 1 ] 0 1 2 3 1 1 1 1 = [ 14 6 6 4 ]
5555 คือเนื่องจากมันเป็น Symmetric เนอะ ไม่ต้อง Compute ทั้งหมด ก็ก็อบมาจากข้างบนเลย
And:
A T b = [ 0 1 2 3 1 1 1 1 ] [ 1 − 1 4 2 ] = [ 13 6 ] A^T b = \begin{bmatrix} 0 & 1 & 2 & 3 \\ 1 & 1 & 1 & 1 \end{bmatrix} \begin{bmatrix} 1 \\ -1 \\ 4 \\ 2 \end{bmatrix} = \begin{bmatrix} 13 \\ 6 \end{bmatrix} A T b = [ 0 1 1 1 2 1 3 1 ] 1 − 1 4 2 = [ 13 6 ]
Solve A T A x = A T b A^T Ax = A^T b A T A x = A T b by Cholesky factorization:
[ 14 6 6 4 ] [ m c ] = [ 13 6 ] \begin{bmatrix} 14 & 6 \\ 6 & 4 \end{bmatrix} \begin{bmatrix} m \\ c \end{bmatrix} = \begin{bmatrix} 13 \\ 6 \end{bmatrix} [ 14 6 6 4 ] [ m c ] = [ 13 6 ]
The solution is m = 0.8 m = 0.8 m = 0.8 , c = 0.3 c = 0.3 c = 0.3 .
So the best-fit line in least-squares sense is:
y = 0.8 x + 0.3 \boxed{y = 0.8x + 0.3} y = 0.8 x + 0.3
📄 CSS322_Doable_L10_Ex1.pdf
Example 2: Fitting an Exponential Function
Problem : Fit the same data points ( 0 , 1 ) , ( 1 , − 1 ) , ( 2 , 4 ) , ( 3 , 2 ) (0, 1), (1, -1), (2, 4), (3, 2) ( 0 , 1 ) , ( 1 , − 1 ) , ( 2 , 4 ) , ( 3 , 2 ) with the function y = c 1 x + c 2 e x y = c_1 x + c_2 e^x y = c 1 x + c 2 e x in least-squares sense using the method of normal equations.
Solution:
We want c 1 c_1 c 1 and c 2 c_2 c 2 satisfying:
c 1 x 1 + c 2 e x 1 ≅ y 1 c_1 x_1 + c_2 e^{x_1} \cong y_1 c 1 x 1 + c 2 e x 1 ≅ y 1
c 1 x 2 + c 2 e x 2 ≅ y 2 c_1 x_2 + c_2 e^{x_2} \cong y_2 c 1 x 2 + c 2 e x 2 ≅ y 2
c 1 x 3 + c 2 e x 3 ≅ y 3 c_1 x_3 + c_2 e^{x_3} \cong y_3 c 1 x 3 + c 2 e x 3 ≅ y 3
c 1 x 4 + c 2 e x 4 ≅ y 4 c_1 x_4 + c_2 e^{x_4} \cong y_4 c 1 x 4 + c 2 e x 4 ≅ y 4
In matrix form :
[ x 1 e x 1 x 2 e x 2 x 3 e x 3 x 4 e x 4 ] [ c 1 c 2 ] ≅ [ y 1 y 2 y 3 y 4 ] \begin{bmatrix} x_1 & e^{x_1} \\ x_2 & e^{x_2} \\ x_3 & e^{x_3} \\ x_4 & e^{x_4} \end{bmatrix} \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} \cong \begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \end{bmatrix} x 1 x 2 x 3 x 4 e x 1 e x 2 e x 3 e x 4 [ c 1 c 2 ] ≅ y 1 y 2 y 3 y 4
Substituting the data points :
[ 0 e 0 1 e 1 2 e 2 3 e 3 ] [ c 1 c 2 ] ≅ [ 1 − 1 4 2 ] \begin{bmatrix} 0 & e^0 \\ 1 & e^1 \\ 2 & e^2 \\ 3 & e^3 \end{bmatrix} \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} \cong \begin{bmatrix} 1 \\ -1 \\ 4 \\ 2 \end{bmatrix} 0 1 2 3 e 0 e 1 e 2 e 3 [ c 1 c 2 ] ≅ 1 − 1 4 2
The method of normal equations :
A T A = [ 14 77.7530 77.7530 466.4160 ] A^T A = \begin{bmatrix} 14 & 77.7530 \\ 77.7530 & 466.4160 \end{bmatrix} A T A = [ 14 77.7530 77.7530 466.4160 ]
And:
A T b = [ 13 68.0090 ] A^T b = \begin{bmatrix} 13 \\ 68.0090 \end{bmatrix} A T b = [ 13 68.0090 ]
Solve A T A [ c 1 c 2 ] = A T b A^T A \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} = A^T b A T A [ c 1 c 2 ] = A T b by Cholesky factorization.
The solution is c 1 = 1.6013 c_1 = 1.6013 c 1 = 1.6013 , c 2 = − 0.1211 c_2 = -0.1211 c 2 = − 0.1211 .
So the best-fit function is:
y = 1.6013 x − 0.1211 e x \boxed{y = 1.6013x - 0.1211e^x} y = 1.6013 x − 0.1211 e x
Running Time of Method of Normal Equations
Matrix multiplication complexity :
A ∈ R p × q A \in \mathbb{R}^{p \times q} A ∈ R p × q , B ∈ R q × r B \in \mathbb{R}^{q \times r} B ∈ R q × r
Multiplying A B AB A B requires 2 p q r 2pqr 2 pq r arithmetic operations
The matrix C = A B C = AB C = A B is p p p -by-r r r matrix
To find c i j = ∑ k = 1 q a i k b k j c_{ij} = \sum_{k=1}^q a_{ik} b_{kj} c ij = ∑ k = 1 q a ik b k j , need 2 q 2q 2 q operations
C C C has p r pr p r entries, so 2 p q r 2pqr 2 pq r operations total
For normal equations :
But A T A A^T A A T A is symmetric, so only need to compute the upper triangular portion
Therefore, Step 1 needs m n 2 + O ( m n ) mn^2 + O(mn) m n 2 + O ( mn ) operations
Step 2 requires n 3 / 3 + O ( n 2 ) n^3/3 + O(n^2) n 3 /3 + O ( n 2 ) operations (running time of Cholesky factorization)
Total arithmetic operations :
m n 2 + n 3 / 3 + O ( m n ) = m n 2 + O ( n 3 + m n ) \boxed{mn^2 + n^3/3 + O(mn) = mn^2 + O(n^3 + mn)} m n 2 + n 3 /3 + O ( mn ) = m n 2 + O ( n 3 + mn ) (assuming m ≥ n m \geq n m ≥ n )
Advantages :
The method of normal equations is fast
A T A A^T A A T A is smaller than A A A and is symmetric
So solving A T A x = A T b A^T Ax = A^T b A T A x = A T b is fast
Stability :
The method is conditionally stable but not backward stable
This means numerical errors can accumulate in certain ill-conditioned problems
QR Factorization
อีก Method นึง
QR Factorization
Given A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n , m ≥ n m \geq n m ≥ n , a QR factorization of A A A is:
A = Q R \boxed{A = QR} A = QR
where:
Q Q Q is an m m m -by-m m m orthogonal matrix (meaning Q T Q = I Q^T Q = I Q T Q = I )
R R R is an m m m -by-n n n upper triangular matrix
Structure of R R R :
R = [ R 1 0 ] R = \begin{bmatrix} R_1 \\ 0 \end{bmatrix} R = [ R 1 0 ]
where:
R 1 R_1 R 1 is an n n n -by-n n n upper triangular matrix
0 0 0 is an ( m − n ) (m-n) ( m − n ) -by-n n n zero matrix
Examples of R R R :
[ 1 3 0 2 0 0 ] , [ 4 1 2 0 2 5 0 0 − 1 0 0 0 0 0 0 ] \begin{bmatrix} 1 & 3 \\ 0 & 2 \\ 0 & 0 \end{bmatrix}, \quad \begin{bmatrix} 4 & 1 & 2 \\ 0 & 2 & 5 \\ 0 & 0 & -1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{bmatrix} 1 0 0 3 2 0 , 4 0 0 0 0 1 2 0 0 0 2 5 − 1 0 0
ก็คือ R R R มันไม่ใช่ Upper triangular matrix แบบธรรมดานะ มันต้องมี extra row of 0s อยู่ข้างล่างด้วย
Solving Least-Squares with QR Factorization
Suppose we have a QR factorization of A A A :
A = Q R A = QR A = QR
We want to minimize:
∥ A x − b ∥ 2 = ∥ Q R x − b ∥ 2 \|Ax - b\|_2 = \|QRx - b\|_2 ∥ A x − b ∥ 2 = ∥ QR x − b ∥ 2
Key step : Let c = Q T b c = Q^T b c = Q T b . That is, b = Q c b = Qc b = Q c .
So:
∥ A x − b ∥ 2 = ∥ Q R x − Q c ∥ 2 \|Ax - b\|_2 = \|QRx - Qc\|_2 ∥ A x − b ∥ 2 = ∥ QR x − Q c ∥ 2
= ∥ Q ( R x − c ) ∥ 2 = \|Q(Rx - c)\|_2 = ∥ Q ( R x − c ) ∥ 2
= ∥ R x − c ∥ 2 (since ∥ Q x ∥ 2 = ∥ x ∥ 2 for orthogonal Q ) = \|Rx - c\|_2 \quad \text{(since } \|Qx\|_2 = \|x\|_2 \text{ for orthogonal } Q) = ∥ R x − c ∥ 2 (since ∥ Q x ∥ 2 = ∥ x ∥ 2 for orthogonal Q )
= ∥ [ R 1 0 ] x − [ c 1 c 2 ] ∥ 2 = \left\|\begin{bmatrix} R_1 \\ 0 \end{bmatrix} x - \begin{bmatrix} c_1 \\ c_2 \end{bmatrix}\right\|_2 = [ R 1 0 ] x − [ c 1 c 2 ] 2
where c = [ c 1 c 2 ] c = \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} c = [ c 1 c 2 ] , c 1 ∈ R n c_1 \in \mathbb{R}^n c 1 ∈ R n , and c 2 ∈ R m − n c_2 \in \mathbb{R}^{m-n} c 2 ∈ R m − n .
Simplifying :
∥ A x − b ∥ 2 = ∥ [ R 1 x − c 1 − c 2 ] ∥ 2 = ∥ R 1 x − c 1 ∥ 2 2 + ∥ c 2 ∥ 2 2 \|Ax - b\|_2 = \left\|\begin{bmatrix} R_1 x - c_1 \\ -c_2 \end{bmatrix}\right\|_2 = \sqrt{\|R_1 x - c_1\|_2^2 + \|c_2\|_2^2} ∥ A x − b ∥ 2 = [ R 1 x − c 1 − c 2 ] 2 = ∥ R 1 x − c 1 ∥ 2 2 + ∥ c 2 ∥ 2 2
Key observation : c 2 c_2 c 2 is a constant (it does not depend on x x x since c = Q T b c = Q^T b c = Q T b ).
So the x x x that minimizes ∥ A x − b ∥ 2 \|Ax - b\|_2 ∥ A x − b ∥ 2 is the one that minimizes ∥ R 1 x − c 1 ∥ 2 \|R_1 x - c_1\|_2 ∥ R 1 x − c 1 ∥ 2 .
If R 1 R_1 R 1 is nonsingular , then:
R 1 x = c 1 R_1 x = c_1 R 1 x = c 1
has a solution. This solution x x x satisfies:
R 1 x − c 1 = 0 R_1 x - c_1 = 0 R 1 x − c 1 = 0
∥ R 1 x − c 1 ∥ 2 = 0 \|R_1 x - c_1\|_2 = 0 ∥ R 1 x − c 1 ∥ 2 = 0
In other words, this solution x x x minimizes ∥ R 1 x − c 1 ∥ 2 \|R_1 x - c_1\|_2 ∥ R 1 x − c 1 ∥ 2 and therefore minimizes ∥ A x − b ∥ 2 \|Ax - b\|_2 ∥ A x − b ∥ 2 .
Theorems for QR Factorization
Theorem 1
Any A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n (m ≥ n m \geq n m ≥ n ) can be factored A = Q R A = QR A = QR where Q Q Q is an m × m m \times m m × m orthogonal matrix and R R R is an m × n m \times n m × n upper triangular matrix.
Theorem 2
If A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n , m ≥ n m \geq n m ≥ n , has rank n n n and A = Q R A = QR A = QR is the QR factorization of A A A , then R R R has rank n n n and R 1 R_1 R 1 is nonsingular.
Conclusion : If rank ( A ) = n \text{rank}(A) = n rank ( A ) = n , the least-squares problem can always be solved using QR factorization.
Algorithm: Solving Least-Squares with QR Factorization
Main Theorem
Let A = Q R A = QR A = QR be the QR factorization of A A A .
Assume A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n , m ≥ n m \geq n m ≥ n , rank ( A ) = n \text{rank}(A) = n rank ( A ) = n .
Let c = Q T b c = Q^T b c = Q T b . Let c 1 c_1 c 1 be the top n n n entries of c c c :
c = [ c 1 c 2 ] c = \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} c = [ c 1 c 2 ]
where c 1 ∈ R n c_1 \in \mathbb{R}^n c 1 ∈ R n and c 2 ∈ R m − n c_2 \in \mathbb{R}^{m-n} c 2 ∈ R m − n .
Then x x x minimizes ∥ A x − b ∥ 2 \|Ax - b\|_2 ∥ A x − b ∥ 2 if:
R 1 x = c 1 \boxed{R_1 x = c_1} R 1 x = c 1
Steps to solve min x ∥ A x − b ∥ 2 \min_x \|Ax - b\|_2 min x ∥ A x − b ∥ 2 :
Factor A = Q R A = QR A = QR
Compute c = Q T b c = Q^T b c = Q T b
Solve R 1 x = c 1 R_1 x = c_1 R 1 x = c 1 for x x x by back substitution
Backward stable and fast than GEPP อีกนะ เลยใช้เลยล่ะ
Example 3: Using QR Factorization
Problem : Solve the least-squares problem:
[ 1 2 0 − 2.4 0 1.8 ] x ≅ [ − 1 0 1 ] \begin{bmatrix} 1 & 2 \\ 0 & -2.4 \\ 0 & 1.8 \end{bmatrix} x \cong \begin{bmatrix} -1 \\ 0 \\ 1 \end{bmatrix} 1 0 0 2 − 2.4 1.8 x ≅ − 1 0 1
Given QR factorization :
[ 1 2 0 − 2.4 0 1.8 ] = [ 1 0 0 0 0.8 0.6 0 − 0.6 0.8 ] [ 1 2 0 − 3 0 0 ] \begin{bmatrix} 1 & 2 \\ 0 & -2.4 \\ 0 & 1.8 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0.8 & 0.6 \\ 0 & -0.6 & 0.8 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 0 & -3 \\ 0 & 0 \end{bmatrix} 1 0 0 2 − 2.4 1.8 = 1 0 0 0 0.8 − 0.6 0 0.6 0.8 1 0 0 2 − 3 0
Solution:
Step 1 : Let
c = Q T b = [ 1 0 0 0 0.8 − 0.6 0 0.6 0.8 ] [ − 1 0 1 ] = [ − 1 − 0.6 0.8 ] c = Q^T b = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0.8 & -0.6 \\ 0 & 0.6 & 0.8 \end{bmatrix} \begin{bmatrix} -1 \\ 0 \\ 1 \end{bmatrix} = \begin{bmatrix} -1 \\ -0.6 \\ 0.8 \end{bmatrix} c = Q T b = 1 0 0 0 0.8 0.6 0 − 0.6 0.8 − 1 0 1 = − 1 − 0.6 0.8
Step 2 : Since A A A is 3-by-2:
c 1 = [ − 1 − 0.6 ] c_1 = \begin{bmatrix} -1 \\ -0.6 \end{bmatrix} c 1 = [ − 1 − 0.6 ]
Step 3 : Solve R 1 x = c 1 R_1 x = c_1 R 1 x = c 1 :
[ 1 2 0 − 3 ] x = [ − 1 − 0.6 ] \begin{bmatrix} 1 & 2 \\ 0 & -3 \end{bmatrix} x = \begin{bmatrix} -1 \\ -0.6 \end{bmatrix} [ 1 0 2 − 3 ] x = [ − 1 − 0.6 ]
by back substitution.
The solution is:
x = [ − 1.4 0.2 ] \boxed{x = \begin{bmatrix} -1.4 \\ 0.2 \end{bmatrix}} x = [ − 1.4 0.2 ]
which is also the solution to the least squares problem.
📄 CSS322_Doable_L10_Ex3.pdf
Summary
Two main methods for solving least-squares problems :
Method 1: Normal Equations
Solve : A T A x = A T b A^T A x = A^T b A T A x = A T b
Advantages : Fast, simple
Disadvantages : Conditionally stable (can have numerical issues)
Running time : m n 2 + O ( n 3 + m n ) mn^2 + O(n^3 + mn) m n 2 + O ( n 3 + mn )
Method 2: QR Factorization
Solve : Factor A = Q R A = QR A = QR , then solve R 1 x = c 1 R_1 x = c_1 R 1 x = c 1 where c 1 c_1 c 1 are top n n n entries of Q T b Q^T b Q T b
Advantages : More numerically stable, backward stable
Disadvantages : Slower than normal equations
Running time : 2 m n 2 − 2 n 3 3 + O ( m n ) 2mn^2 - \frac{2n^3}{3} + O(mn) 2 m n 2 − 3 2 n 3 + O ( mn )
When to use which?
Use normal equations when speed is critical and A A A is well-conditioned
Use QR factorization when numerical stability is important or A A A is ill-conditioned
QR Factorization Algorithms
Three main algorithms for computing QR factorization:
Householder transformations (most commonly used)
Givens rotations (useful for sparse matrices)
Gram-Schmidt algorithm (simple but less stable)
Definition
Let v ∈ R m v \in \mathbb{R}^m v ∈ R m be a nonzero vector. H = I − 2 v v T v T v \boxed{H = I - 2\frac{vv^T}{v^T v}} H = I − 2 v T v v v T
is called a Householder transform/reflection/matrix .
Theorem
H H H is symmetric and orthogonal.
Proof of Properties
Part 1 - Symmetry : (เวลาโชว์ทำไงนะ ก็โชว์ว่า H T = H H^T = H H T = H ไง)
H T = ( I − 2 v v T v T v ) T = I T − 2 ( v v T ) T v T v = I − 2 v v T v T v = H H^T = \left(I - 2\frac{vv^T}{v^T v}\right)^T = I^T - 2\frac{(vv^T)^T}{v^T v} = I - 2\frac{vv^T}{v^T v} = H H T = ( I − 2 v T v v v T ) T = I T − 2 v T v ( v v T ) T = I − 2 v T v v v T = H
Part 2 - Orthogonality : (นี่ล่ะ H H T = I HH^T=I H H T = I )
H H T = H H = ( I − 2 v v T v T v ) ( I − 2 v v T v T v ) HH^T = HH = \left(I - 2\frac{vv^T}{v^T v}\right)\left(I - 2\frac{vv^T}{v^T v}\right) H H T = H H = ( I − 2 v T v v v T ) ( I − 2 v T v v v T )
= I − 4 v v T v T v + 4 v v T v v T ( v T v ) 2 = I - 4\frac{vv^T}{v^T v} + 4\frac{vv^T vv^T}{(v^T v)^2} = I − 4 v T v v v T + 4 ( v T v ) 2 v v T v v T
= I − 4 v v T v T v + 4 v ( v T v ) v T ( v T v ) 2 = I - 4\frac{vv^T}{v^T v} + 4\frac{v(v^T v)v^T}{(v^T v)^2} = I − 4 v T v v v T + 4 ( v T v ) 2 v ( v T v ) v T
= I − 4 v v T v T v + 4 v v T v T v = I = I - 4\frac{vv^T}{v^T v} + 4\frac{vv^T}{v^T v} = I = I − 4 v T v v v T + 4 v T v v v T = I
Using Householder to Create Zeros
Goal : Given a vector a a a (say, the first column of A A A ), we want a Householder transform such that:
H a = ( I − 2 v v T v T v ) a = [ α 0 ⋮ 0 ] = α e 1 Ha = \left(I - 2\frac{vv^T}{v^T v}\right)a = \begin{bmatrix} \alpha \\ 0 \\ \vdots \\ 0 \end{bmatrix} = \alpha e_1 H a = ( I − 2 v T v v v T ) a = α 0 ⋮ 0 = α e 1
for some scalar α \alpha α (which will be the first column of R R R ).
Solution : This is satisfied by taking:
v : = a − α e 1 v := a - \alpha e_1 v := a − α e 1
α = ± ∥ a ∥ 2 \alpha = \pm \|a\|_2 α = ± ∥ a ∥ 2
To avoid cancellation : Choose α \alpha α to have the opposite sign from a 1 a_1 a 1 : α = − sign ( a 1 ) ∥ a ∥ 2 \boxed{\alpha = -\text{sign}(a_1) \|a\|_2} α = − sign ( a 1 ) ∥ a ∥ 2
This ensures numerical stability by avoiding subtraction of nearly equal numbers.
Verification
We need to verify that H a = α e 1 Ha = \alpha e_1 H a = α e 1 .
Starting with:
H a = ( I − 2 v v T v T v ) a = a − 2 v v T v T v a Ha = \left(I - 2\frac{vv^T}{v^T v}\right)a = a - 2\frac{vv^T}{v^T v}a H a = ( I − 2 v T v v v T ) a = a − 2 v T v v v T a
After substituting v = a − α e 1 v = a - \alpha e_1 v = a − α e 1 and simplifying (detailed algebra in the slides):
H a = α e 1 Ha = \alpha e_1 H a = α e 1 ✓
Algorithm: QR Factorization via Householder
Given : A ∈ R m × n A \in \mathbb{R}^{m \times n} A ∈ R m × n
Process :
Write A = [ a B ] A = [a \quad B] A = [ a B ] where a ∈ R m a \in \mathbb{R}^m a ∈ R m is the first column
Choose H 1 H_1 H 1 to annihilate first column:
Set v 1 : = a − α 1 e 1 v_1 := a - \alpha_1 e_1 v 1 := a − α 1 e 1 where α 1 = − sign ( a 1 ) ∥ a ∥ 2 \alpha_1 = -\text{sign}(a_1) \|a\|_2 α 1 = − sign ( a 1 ) ∥ a ∥ 2
Then H 1 = I − 2 v 1 v 1 T v 1 T v 1 H_1 = I - 2\frac{v_1 v_1^T}{v_1^T v_1} H 1 = I − 2 v 1 T v 1 v 1 v 1 T
After applying H 1 H_1 H 1 :
H 1 A = [ α 1 w T 0 A 2 ] H_1 A = \begin{bmatrix} \alpha_1 & w^T \\ 0 & A_2 \end{bmatrix} H 1 A = [ α 1 0 w T A 2 ]
Recursively apply to submatrix A 2 A_2 A 2
Continue until upper triangular
Result : After n n n steps: H n H n − 1 ⋯ H 1 A = R H_n H_{n-1} \cdots H_1 A = R H n H n − 1 ⋯ H 1 A = R
Since H i H_i H i are orthogonal and symmetric (H i T = H i H_i^T = H_i H i T = H i and H i T H i = I H_i^T H_i = I H i T H i = I ):
A = H 1 H 2 ⋯ H n R = Q R A = H_1 H_2 \cdots H_n R = QR A = H 1 H 2 ⋯ H n R = QR
where Q = H 1 H 2 ⋯ H n Q = H_1 H_2 \cdots H_n Q = H 1 H 2 ⋯ H n is orthogonal.
Lemma
If Q 1 , … , Q n Q_1, \ldots, Q_n Q 1 , … , Q n are orthogonal, so is Q 1 Q 2 ⋯ Q n Q_1 Q_2 \cdots Q_n Q 1 Q 2 ⋯ Q n .
Efficient Implementation
For solving least-squares, we only need c = Q T b c = Q^T b c = Q T b :
c = Q T b = H n H n − 1 ⋯ H 2 H 1 b c = Q^T b = H_n H_{n-1} \cdots H_2 H_1 b c = Q T b = H n H n − 1 ⋯ H 2 H 1 b
Solution : Apply H 1 H_1 H 1 to b b b , then H 2 H_2 H 2 to result, and so on.
Key Optimization 2: Work with Submatrices
For k > 1 k > 1 k > 1 :
H k = [ I k − 1 0 0 H k ′ ] H_k = \begin{bmatrix} I_{k-1} & 0 \\ 0 & H_k' \end{bmatrix} H k = [ I k − 1 0 0 H k ′ ]
This means:
H k w = [ I k − 1 0 0 H k ′ ] [ w 1 w 2 ] = [ w 1 H k ′ w 2 ] H_k w = \begin{bmatrix} I_{k-1} & 0 \\ 0 & H_k' \end{bmatrix} \begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} w_1 \\ H_k' w_2 \end{bmatrix} H k w = [ I k − 1 0 0 H k ′ ] [ w 1 w 2 ] = [ w 1 H k ′ w 2 ]
Benefit : Only apply H k ′ H_k' H k ′ to relevant portion of vector!
Key Optimization 3: Efficient Multiplication
Computing H u Hu H u directly from definition:
H u = ( I − 2 v v T v T v ) u = u − ( 2 v T u v T v ) v Hu = \left(I - 2\frac{vv^T}{v^T v}\right)u = u - \left(2\frac{v^T u}{v^T v}\right)v H u = ( I − 2 v T v v v T ) u = u − ( 2 v T v v T u ) v
This is O ( m ) O(m) O ( m ) operations
Much cheaper than forming H H H explicitly and multiplying (O ( m 2 ) O(m^2) O ( m 2 ) operations)
Similarly, for matrix A A A :
H A = H [ a ∙ 1 a ∙ 2 ⋯ a ∙ n ] = [ H a ∙ 1 H a ∙ 2 ⋯ H a ∙ n ] HA = H[a_{\bullet 1} \quad a_{\bullet 2} \quad \cdots \quad a_{\bullet n}] = [Ha_{\bullet 1} \quad Ha_{\bullet 2} \quad \cdots \quad Ha_{\bullet n}] H A = H [ a ∙ 1 a ∙ 2 ⋯ a ∙ n ] = [ H a ∙ 1 H a ∙ 2 ⋯ H a ∙ n ]
Apply the efficient formula to each column.
Example 4: Complete Householder QR Solution
Problem : Use Householder QR factorization to solve:
[ 1 5 3 2 − 1 3 − 2 2 1 2 0 3 ] x ≅ [ 2 0 1 0 ] \begin{bmatrix} 1 & 5 & 3 \\ 2 & -1 & 3 \\ -2 & 2 & 1 \\ 2 & 0 & 3 \end{bmatrix} x \cong \begin{bmatrix} 2 \\ 0 \\ 1 \\ 0 \end{bmatrix} 1 2 − 2 2 5 − 1 2 0 3 3 1 3 x ≅ 2 0 1 0
Solution:
Step 1 : Annihilate first column
Set a = [ 1 2 − 2 2 ] a = \begin{bmatrix} 1 \\ 2 \\ -2 \\ 2 \end{bmatrix} a = 1 2 − 2 2 (first column of A A A )
Since a 1 = 1 > 0 a_1 = 1 > 0 a 1 = 1 > 0 , set:
α = − ∥ a ∥ 2 = − 3.6056 \alpha = -\|a\|_2 = -3.6056 α = − ∥ a ∥ 2 = − 3.6056
v 1 = a − α e 1 = [ 1 2 − 2 2 ] − ( − 3.6056 ) [ 1 0 0 0 ] = [ 4.6056 2 − 2 2 ] v_1 = a - \alpha e_1 = \begin{bmatrix} 1 \\ 2 \\ -2 \\ 2 \end{bmatrix} - (-3.6056)\begin{bmatrix} 1 \\ 0 \\ 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 4.6056 \\ 2 \\ -2 \\ 2 \end{bmatrix} v 1 = a − α e 1 = 1 2 − 2 2 − ( − 3.6056 ) 1 0 0 0 = 4.6056 2 − 2 2
Apply to get: H u = u − ( 2 v v T v T v ) v \boxed{Hu = u - (2\frac{vv^T}{v^T v})v} H u = u − ( 2 v T v v v T ) v
สำหรับ First Column; u u u ให้ใช้เป็น first column of A A A แล้ว v v v ก็เป็นอันข้างบนที่ได้มา v 1 v_1 v 1
Second column; คล้าย ๆ กัน แต่ u u u ก็ใช้เป็น second column of A A A
…
H 1 A = [ − 3.6056 0.2774 − 3.6056 0 − 3.0509 0.1315 0 4.0509 3.8685 0 − 2.0509 0.1315 ] H_1 A = \begin{bmatrix} -3.6056 & 0.2774 & -3.6056 \\ 0 & -3.0509 & 0.1315 \\ 0 & 4.0509 & 3.8685 \\ 0 & -2.0509 & 0.1315 \end{bmatrix} H 1 A = − 3.6056 0 0 0 0.2774 − 3.0509 4.0509 − 2.0509 − 3.6056 0.1315 3.8685 0.1315
Step 2 : Annihilate second column of submatrix (เอาแค่ส่วนล่างขวามาแค่นั้น)
Remove first row and column to get:
A 2 = [ − 3.0509 0.1315 4.0509 3.8685 − 2.0509 0.1315 ] A_2 = \begin{bmatrix} -3.0509 & 0.1315 \\ 4.0509 & 3.8685 \\ -2.0509 & 0.1315 \end{bmatrix} A 2 = − 3.0509 4.0509 − 2.0509 0.1315 3.8685 0.1315
Set a = [ − 3.0509 4.0509 − 2.0509 ] a = \begin{bmatrix} -3.0509 \\ 4.0509 \\ -2.0509 \end{bmatrix} a = − 3.0509 4.0509 − 2.0509 (first column of A 2 A_2 A 2 )
Since a 1 < 0 a_1 < 0 a 1 < 0 , set α = ∥ a ∥ 2 = 5.4702 \alpha = \|a\|_2 = 5.4702 α = ∥ a ∥ 2 = 5.4702
v 2 = a − α e 1 = [ − 8.5211 4.0509 − 2.0509 ] v_2 = a - \alpha e_1 = \begin{bmatrix} -8.5211 \\ 4.0509 \\ -2.0509 \end{bmatrix} v 2 = a − α e 1 = − 8.5211 4.0509 − 2.0509
Apply H 2 ′ H'_2 H 2 ′ to A 2 A_2 A 2 to get
H 2 ′ A 2 = [ 5.4702 2.7421 0 2.6274 0 0.7598 ] H'_2A_2=\begin{bmatrix} 5.4702 & 2.7421 \\ 0 & 2.6274 \\ 0 & 0.7598 \end{bmatrix} H 2 ′ A 2 = 5.4702 0 0 2.7421 2.6274 0.7598
Apply to get:
H 2 H 1 A = [ − 3.6056 0.2774 − 3.6056 0 5.4702 2.7421 0 0 2.6274 0 0 0.7598 ] H_2 H_1 A = \begin{bmatrix} -3.6056 & 0.2774 & -3.6056 \\ 0 & 5.4702 & 2.7421 \\ 0 & 0 & 2.6274 \\ 0 & 0 & 0.7598 \end{bmatrix} H 2 H 1 A = − 3.6056 0 0 0 0.2774 5.4702 0 0 − 3.6056 2.7421 2.6274 0.7598
Step 3 : Annihilate third column of submatrix
Remove first two rows and columns:
A 3 = [ 2.6274 0.7598 ] A_3 = \begin{bmatrix} 2.6274 \\ 0.7598 \end{bmatrix} A 3 = [ 2.6274 0.7598 ]
Since first entry > 0, set α = − ∥ a ∥ 2 = − 2.7351 \alpha = -\|a\|_2 = -2.7351 α = − ∥ a ∥ 2 = − 2.7351
v 3 = [ 5.3625 0.7598 ] v_3 = \begin{bmatrix} 5.3625 \\ 0.7598 \end{bmatrix} v 3 = [ 5.3625 0.7598 ]
Apply to get:
H 3 H 2 H 1 A = [ − 3.6056 0.2774 − 3.6056 0 5.4702 2.7421 0 0 − 2.7351 0 0 0 ] = R H_3 H_2 H_1 A = \begin{bmatrix} -3.6056 & 0.2774 & -3.6056 \\ 0 & 5.4702 & 2.7421 \\ 0 & 0 & -2.7351 \\ 0 & 0 & 0 \end{bmatrix} = R H 3 H 2 H 1 A = − 3.6056 0 0 0 0.2774 5.4702 0 0 − 3.6056 2.7421 − 2.7351 0 = R
Therefore:
R 1 = [ − 3.6056 0.2774 − 3.6056 0 5.4702 2.7421 0 0 − 2.7351 ] R_1 = \begin{bmatrix} -3.6056 & 0.2774 & -3.6056 \\ 0 & 5.4702 & 2.7421 \\ 0 & 0 & -2.7351 \end{bmatrix} R 1 = − 3.6056 0 0 0.2774 5.4702 0 − 3.6056 2.7421 − 2.7351 (Top square portion of R R R )
Step 4 : Form c = Q T b c = Q^T b c = Q T b
Apply H 1 H_1 H 1 to b b b :
H 1 b = [ 0 − 0.8685 1.8685 − 0.8685 ] H_1 b = \begin{bmatrix} 0 \\ -0.8685 \\ 1.8685 \\ -0.8685 \end{bmatrix} H 1 b = 0 − 0.8685 1.8685 − 0.8685
Apply H 2 H_2 H 2 to second through fourth entries:
H 2 H 1 b = [ 0 2.1937 0.4128 − 0.1315 ] H_2 H_1 b = \begin{bmatrix} 0 \\ 2.1937 \\ 0.4128 \\ -0.1315 \end{bmatrix} H 2 H 1 b = 0 2.1937 0.4128 − 0.1315
Apply H 3 H_3 H 3 to third and fourth entries:
c = H 3 H 2 H 1 b = [ 0 2.1937 − 0.36 − 0.2410 ] c = H_3 H_2 H_1 b = \begin{bmatrix} 0 \\ 2.1937 \\ -0.36 \\ -0.2410 \end{bmatrix} c = H 3 H 2 H 1 b = 0 2.1937 − 0.36 − 0.2410
Step 5 : Solve R 1 x = c 1 R_1 x = c_1 R 1 x = c 1 by back substitution
[ − 3.6056 0.2774 − 3.6056 0 5.4702 2.7421 0 0 − 2.7351 ] x = [ 0 2.1937 − 0.36 ] \begin{bmatrix} -3.6056 & 0.2774 & -3.6056 \\ 0 & 5.4702 & 2.7421 \\ 0 & 0 & -2.7351 \end{bmatrix} x = \begin{bmatrix} 0 \\ 2.1937 \\ -0.36 \end{bmatrix} − 3.6056 0 0 0.2774 5.4702 0 − 3.6056 2.7421 − 2.7351 x = 0 2.1937 − 0.36
Solution :
x = [ − 0.1058 0.3351 0.1316 ] \boxed{x = \begin{bmatrix} -0.1058 \\ 0.3351 \\ 0.1316 \end{bmatrix}} x = − 0.1058 0.3351 0.1316
📄 CSS322_Doable_L10_Ex4.pdf
Running Time Comparison
For solving least-squares problems :
Householder (HH) : 2 m n 2 − 2 n 3 3 + O ( m n ) 2mn^2 - \frac{2n^3}{3} + O(mn) 2 m n 2 − 3 2 n 3 + O ( mn )
Normal equations : m n 2 + n 3 3 + O ( m n ) mn^2 + \frac{n^3}{3} + O(mn) m n 2 + 3 n 3 + O ( mn )
Analysis :
If m ≈ n m \approx n m ≈ n : The two are about equal
If m ≫ n m \gg n m ≫ n : HH is about twice as slow
But HH is backward stable !
Backward Stability
Householder exactly solves: min x ∥ ( A + E ) x − b ∥ 2 \min_x \|(A + E)x - b\|_2 min x ∥ ( A + E ) x − b ∥ 2
where ∥ E ∥ / ∥ A ∥ ≈ ϵ m a c h \|E\| / \|A\| \approx \epsilon_{mach} ∥ E ∥/∥ A ∥ ≈ ϵ ma c h
This means the computed solution is the exact solution to a slightly perturbed problem, which is excellent for numerical stability!
Givens Rotations
When to Use Givens Rotations
Key difference from Householder :
Householder transformations introduce many zeros in a column at once
If matrix already has many zeros below main diagonal, Householder cannot take advantage of existing zeros
Householder requires the same operations regardless of sparsity
Therefore : If entries of A A A below the main diagonal already have many zeros, use Givens rotations , which:
Introduce zeros one at a time
Do not unnecessarily operate on entries that are already zero
Are more efficient for sparse matrices
Plane Rotations (Givens Rotations)
Definition
A Givens rotation has the form:
G = [ c s − s c ] G = \begin{bmatrix} c & s \\ -s & c \end{bmatrix} G = [ c − s s c ]
where c = cos θ c = \cos\theta c = cos θ and s = sin θ s = \sin\theta s = sin θ , with θ \theta θ being the angle of rotation.
มันก็คือ Rotation Matrix จากวิชา Mathematica Summary
Orthogonality
Check that G G G is orthogonal:
G G T = [ c s − s c ] [ c − s s c ] = [ c 2 + s 2 0 0 c 2 + s 2 ] = [ 1 0 0 1 ] GG^T = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}\begin{bmatrix} c & -s \\ s & c \end{bmatrix} = \begin{bmatrix} c^2 + s^2 & 0 \\ 0 & c^2 + s^2 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} G G T = [ c − s s c ] [ c s − s c ] = [ c 2 + s 2 0 0 c 2 + s 2 ] = [ 1 0 0 1 ]
This works because c 2 + s 2 = cos 2 θ + sin 2 θ = 1 c^2 + s^2 = \cos^2\theta + \sin^2\theta = 1 c 2 + s 2 = cos 2 θ + sin 2 θ = 1 ✓
Finding the Rotation Parameters
Goal : Given a 2-vector a = [ a 1 a 2 ] a = \begin{bmatrix} a_1 \\ a_2 \end{bmatrix} a = [ a 1 a 2 ] , choose c c c and s s s so that:
G a = [ c s − s c ] [ a 1 a 2 ] = [ α 0 ] Ga = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}\begin{bmatrix} a_1 \\ a_2 \end{bmatrix} = \begin{bmatrix} \alpha \\ 0 \end{bmatrix} G a = [ c − s s c ] [ a 1 a 2 ] = [ α 0 ]
Derivation : Rewrite as a linear system:
[ a 1 a 2 a 2 − a 1 ] [ c s ] = [ α 0 ] \begin{bmatrix} a_1 & a_2 \\ a_2 & -a_1 \end{bmatrix}\begin{bmatrix} c \\ s \end{bmatrix} = \begin{bmatrix} \alpha \\ 0 \end{bmatrix} [ a 1 a 2 a 2 − a 1 ] [ c s ] = [ α 0 ]
After Gaussian elimination and back-substitution:
[ a 1 a 2 0 − a 1 − a 2 2 / a 1 ] [ c s ] = [ α − α a 2 / a 1 ] \begin{bmatrix} a_1 & a_2 \\ 0 & -a1-a_2^2/a_1 \end{bmatrix}\begin{bmatrix} c \\ s \end{bmatrix} = \begin{bmatrix} \alpha \\ -\alpha a_2/a_1 \end{bmatrix} [ a 1 0 a 2 − a 1 − a 2 2 / a 1 ] [ c s ] = [ α − α a 2 / a 1 ]
s = α a 2 a 1 2 + a 2 2 , c = α a 1 a 1 2 + a 2 2 s = \frac{\alpha a_2}{a_1^2 + a_2^2}, \quad c = \frac{\alpha a_1}{a_1^2 + a_2^2} s = a 1 2 + a 2 2 α a 2 , c = a 1 2 + a 2 2 α a 1
Using c 2 + s 2 = 1 c^2 + s^2 = 1 c 2 + s 2 = 1 , we get:
α = a 1 2 + a 2 2 \boxed{\alpha = \sqrt{a_1^2 + a_2^2}} α = a 1 2 + a 2 2
Therefore:
c = a 1 a 1 2 + a 2 2 , s = a 2 a 1 2 + a 2 2 \boxed{c = \frac{a_1}{\sqrt{a_1^2 + a_2^2}}, \quad s = \frac{a_2}{\sqrt{a_1^2 + a_2^2}}} c = a 1 2 + a 2 2 a 1 , s = a 1 2 + a 2 2 a 2
Avoiding Overflow/Underflow
Problem : Computing a 1 2 a_1^2 a 1 2 or a 2 2 a_2^2 a 2 2 can cause unnecessary overflow or underflow.
ใน ALU มี Component อะไรป้องกันพวกนี้อยู่…
Solution 1: Use Tangent (if ∣ a 1 ∣ > ∣ a 2 ∣ |a_1| > |a_2| ∣ a 1 ∣ > ∣ a 2 ∣ )
Work with t = tan θ t = \tan\theta t = tan θ instead:
t = s c = a 2 a 1 t = \frac{s}{c} = \frac{a_2}{a_1} t = c s = a 1 a 2
Then:
c = 1 1 + t 2 , s = c t \boxed{c = \frac{1}{\sqrt{1 + t^2}}, \quad s = ct} c = 1 + t 2 1 , s = c t
Solution 2: Use Cotangent (if ∣ a 2 ∣ > ∣ a 1 ∣ |a_2| > |a_1| ∣ a 2 ∣ > ∣ a 1 ∣ )
Work with τ = cot θ \tau = \cot\theta τ = cot θ instead:
τ = c s = a 1 a 2 \tau = \frac{c}{s} = \frac{a_1}{a_2} τ = s c = a 2 a 1
Then:
s = 1 1 + τ 2 , c = s τ \boxed{s = \frac{1}{\sqrt{1 + \tau^2}}, \quad c = s\tau} s = 1 + τ 2 1 , c = s τ
Benefit : In either case, we avoid squaring any number with magnitude larger than 1, preventing overflow!
Note : We don't need to know the angle θ \theta θ explicitly. Only its sine and cosine are needed.
Example 5: Computing a Givens Rotation
Problem : Determine a Givens rotation that annihilates the second component of:
a = [ 4 3 ] a = \begin{bmatrix} 4 \\ 3 \end{bmatrix} a = [ 4 3 ]
Solution:
Since magnitudes are reasonable, compute directly:
c = a 1 a 1 2 + a 2 2 = 4 5 = 0.8 c = \frac{a_1}{\sqrt{a_1^2 + a_2^2}} = \frac{4}{5} = 0.8 c = a 1 2 + a 2 2 a 1 = 5 4 = 0.8
s = a 2 a 1 2 + a 2 2 = 3 5 = 0.6 s = \frac{a_2}{\sqrt{a_1^2 + a_2^2}} = \frac{3}{5} = 0.6 s = a 1 2 + a 2 2 a 2 = 5 3 = 0.6
Alternative using tangent :
t = a 2 a 1 = 3 4 = 0.75 t = \frac{a_2}{a_1} = \frac{3}{4} = 0.75 t = a 1 a 2 = 4 3 = 0.75
c = 1 1 + t 2 = 1 1 + ( 0.75 ) 2 = 0.8 c = \frac{1}{\sqrt{1 + t^2}} = \frac{1}{\sqrt{1 + (0.75)^2}} = 0.8 c = 1 + t 2 1 = 1 + ( 0.75 ) 2 1 = 0.8
s = c t = ( 0.8 ) ( 0.75 ) = 0.6 s = ct = (0.8)(0.75) = 0.6 s = c t = ( 0.8 ) ( 0.75 ) = 0.6
Rotation matrix :
G = [ 0.8 0.6 − 0.6 0.8 ] G = \begin{bmatrix} 0.8 & 0.6 \\ -0.6 & 0.8 \end{bmatrix} G = [ 0.8 − 0.6 0.6 0.8 ]
Verification :
G a = [ 0.8 0.6 − 0.6 0.8 ] [ 4 3 ] = [ 5 0 ] Ga = \begin{bmatrix} 0.8 & 0.6 \\ -0.6 & 0.8 \end{bmatrix}\begin{bmatrix} 4 \\ 3 \end{bmatrix} = \begin{bmatrix} 5 \\ 0 \end{bmatrix} G a = [ 0.8 − 0.6 0.6 0.8 ] [ 4 3 ] = [ 5 0 ] ✓
Generalizing to m-Vectors
ชีวิตจริง Matrix ไม่ได้มีแค่ 2 rows เหมือนตัวอย่างข้างบน
Goal : To annihilate any desired component of an m m m -vector.
Method : Apply the same technique by rotating the target component (say j j j ) with another component (say i i i ).
Process
Use the two selected components as before to determine the appropriate 2 × 2 2 \times 2 2 × 2 rotation matrix
Find the Givens rotation G G G such that:
G a = [ c s − s c ] [ a i a j ] = [ α 0 ] Ga = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}\begin{bmatrix} a_i \\ a_j \end{bmatrix} = \begin{bmatrix} \alpha \\ 0 \end{bmatrix} G a = [ c − s s c ] [ a i a j ] = [ α 0 ]
Embed G G G as a 2 × 2 2 \times 2 2 × 2 submatrix in rows and columns i i i and j j j of the m m m -dimensional identity matrix I m I_m I m
Example: Embedded Givens Matrix
For the case m = 5 m = 5 m = 5 , i = 2 i = 2 i = 2 , j = 4 j = 4 j = 4 :
[ 1 0 0 0 0 0 c 0 s 0 0 0 1 0 0 0 − s 0 c 0 0 0 0 0 1 ] [ a 1 a 2 a 3 a 4 a 5 ] = [ a 1 α a 3 0 a 5 ] \begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ 0 & c & 0 & s & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & -s & 0 & c & 0 \\ 0 & 0 & 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} a_1 \\ a_2 \\ a_3 \\ a_4 \\ a_5 \end{bmatrix} = \begin{bmatrix} a_1 \\ \alpha \\ a_3 \\ 0 \\ a_5 \end{bmatrix} 1 0 0 0 0 0 c 0 − s 0 0 0 1 0 0 0 s 0 c 0 0 0 0 0 1 a 1 a 2 a 3 a 4 a 5 = a 1 α a 3 0 a 5
ก็คือ
g i i = c g_{ii}=c g ii = c
g i j = s g_{ij}=s g ij = s
g j i = − s g_{ji}=-s g j i = − s
g j j = c g_{jj}=c g j j = c
ซึ่งจะทำให้ a 4 = 0 a_4=0 a 4 = 0 ตามใจอยาก
Algorithm: QR via Givens Rotations
Process :
Use a sequence of Givens rotations to annihilate all entries of A A A below the main diagonal one entry at a time
Transform A A A to upper triangular form, which is R R R
Like Householder, annihilate one column at a time from left to right
Otherwise, some entries in previously annihilated columns may become nonzero later
Important Notes
⚠️ Caution when selecting rotation pairs :
Do NOT use an entry that is already zero as a i a_i a i (it will change to nonzero, i.e., to α \alpha α )
Do NOT use an entry above the main diagonal as a i a_i a i (it will mess up previously finished columns)
✅ Safe strategy : Always rotate the target entry with the diagonal entry
Just like Householder:
The product of all Givens rotation matrices used in reverse order is Q Q Q
We never need to explicitly compute Q Q Q to solve least-squares problems
Example : If we need three Givens rotations:
G 3 G 2 G 1 A = R G_3 G_2 G_1 A = R G 3 G 2 G 1 A = R
Then:
Q = G 1 T G 2 T G 3 T Q = G_1^T G_2^T G_3^T Q = G 1 T G 2 T G 3 T
To get c = Q T b c = Q^T b c = Q T b , simply compute:
c = G 3 ( G 2 ( G 1 b ) ) c = G_3(G_2(G_1 b)) c = G 3 ( G 2 ( G 1 b ))
Example 6: Complete Givens Rotation Solution
Problem : Solve the following linear least-squares problem using Givens rotations:
A x = [ 1 0 0 0 1 0 0 0 1 − 1 1 0 − 1 0 1 0 − 1 1 ] x ≅ [ 1237 1941 2417 711 1177 475 ] = b Ax = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ -1 & 1 & 0 \\ -1 & 0 & 1 \\ 0 & -1 & 1 \end{bmatrix} x \cong \begin{bmatrix} 1237 \\ 1941 \\ 2417 \\ 711 \\ 1177 \\ 475 \end{bmatrix} = b A x = 1 0 0 − 1 − 1 0 0 1 0 1 0 − 1 0 0 1 0 1 1 x ≅ 1237 1941 2417 711 1177 475 = b
Solution:
Observation : A A A has only six nonzero entries below the main diagonal, making Givens rotations efficient!
Strategy : Start from the first column, work from the bottom upward .
Step 1: Annihilate a 51 a_{51} a 51
The first nonzero entry to annihilate is a 51 a_{51} a 51 .
Rotate using entries a 11 a_{11} a 11 (diagonal) and a 51 a_{51} a 51 :
a 1 = a 11 = 1 a_1 = a_{11} = 1 a 1 = a 11 = 1
a 2 = a 51 = − 1 a_2 = a_{51} = -1 a 2 = a 51 = − 1
c = a 1 a 1 2 + a 2 2 = 1 1 2 + ( − 1 ) 2 = 1 2 ≈ 0.7071 c = \frac{a_1}{\sqrt{a_1^2 + a_2^2}} = \frac{1}{\sqrt{1^2 + (-1)^2}} = \frac{1}{\sqrt{2}} \approx 0.7071 c = a 1 2 + a 2 2 a 1 = 1 2 + ( − 1 ) 2 1 = 2 1 ≈ 0.7071
s = a 2 a 1 2 + a 2 2 = − 1 1 2 + ( − 1 ) 2 = − 1 2 ≈ − 0.7071 s = \frac{a_2}{\sqrt{a_1^2 + a_2^2}} = \frac{-1}{\sqrt{1^2 + (-1)^2}} = \frac{-1}{\sqrt{2}} \approx -0.7071 s = a 1 2 + a 2 2 a 2 = 1 2 + ( − 1 ) 2 − 1 = 2 − 1 ≈ − 0.7071
First Givens rotation matrix :
G 1 = [ 0.7071 0 0 0 − 0.7071 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0.7071 0 0 0 0.7071 0 0 0 0 0 0 1 ] G_1 = \begin{bmatrix} 0.7071 & 0 & 0 & 0 & -0.7071 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0.7071 & 0 & 0 & 0 & 0.7071 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix} G 1 = 0.7071 0 0 0 0.7071 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 − 0.7071 0 0 0 0.7071 0 0 0 0 0 0 1
Apply to A A A and b b b :
G 1 A = [ 1.4142 0 − 0.7071 0 1 0 0 0 1 − 1 1 0 0 0 0.7071 0 − 1 1 ] , G 1 b = [ 42 1941 2417 711 1707 475 ] G_1 A = \begin{bmatrix} 1.4142 & 0 & -0.7071 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ -1 & 1 & 0 \\ 0 & 0 & 0.7071 \\ 0 & -1 & 1 \end{bmatrix}, \quad G_1 b = \begin{bmatrix} 42 \\ 1941 \\ 2417 \\ 711 \\ 1707 \\ 475 \end{bmatrix} G 1 A = 1.4142 0 0 − 1 0 0 0 1 0 1 0 − 1 − 0.7071 0 1 0 0.7071 1 , G 1 b = 42 1941 2417 711 1707 475
Step 2: Annihilate a 41 a_{41} a 41
Rotate using entries from first and fourth positions:
a 1 = 1.4142 a_1 = 1.4142 a 1 = 1.4142
a 2 = − 1 a_2 = -1 a 2 = − 1
c = 1.4142 ( 1.4142 ) 2 + ( − 1 ) 2 = 0.8165 c = \frac{1.4142}{\sqrt{(1.4142)^2 + (-1)^2}} = 0.8165 c = ( 1.4142 ) 2 + ( − 1 ) 2 1.4142 = 0.8165
s = − 1 ( 1.4142 ) 2 + ( − 1 ) 2 = − 0.5774 s = \frac{-1}{\sqrt{(1.4142)^2 + (-1)^2}} = -0.5774 s = ( 1.4142 ) 2 + ( − 1 ) 2 − 1 = − 0.5774
Second Givens rotation matrix :
G 2 = [ 0.8165 0 0 − 0.5774 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0.5774 0 0 0.8165 0 0 0 0 0 0 1 0 0 0 0 0 0 1 ] G_2 = \begin{bmatrix} 0.8165 & 0 & 0 & -0.5774 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0.5774 & 0 & 0 & 0.8165 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix} G 2 = 0.8165 0 0 0.5774 0 0 0 1 0 0 0 0 0 0 1 0 0 0 − 0.5774 0 0 0.8165 0 0 0 0 0 0 1 0 0 0 0 0 0 1
Apply :
G 2 G 1 A = [ 1.7321 − 0.5744 − 0.5744 0 1 0 0 0 1 0 0.8165 − 0.4082 0 0 0.7071 0 − 1 1 ] , G 2 G 1 b = [ − 376 1941 2417 605 1707 475 ] G_2 G_1 A = \begin{bmatrix} 1.7321 & -0.5744 & -0.5744 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 0.8165 & -0.4082 \\ 0 & 0 & 0.7071 \\ 0 & -1 & 1 \end{bmatrix}, \quad G_2 G_1 b = \begin{bmatrix} -376 \\ 1941 \\ 2417 \\ 605 \\ 1707 \\ 475 \end{bmatrix} G 2 G 1 A = 1.7321 0 0 0 0 0 − 0.5744 1 0 0.8165 0 − 1 − 0.5744 0 1 − 0.4082 0.7071 1 , G 2 G 1 b = − 376 1941 2417 605 1707 475
✅ First column complete!
Step 3: Annihilate a 62 a_{62} a 62
For the second column, annihilate a 62 a_{62} a 62 using diagonal and sixth entries:
a 1 = 1 a_1 = 1 a 1 = 1
a 2 = − 1 a_2 = -1 a 2 = − 1
c = 1 2 ≈ 0.7071 , s = − 1 2 ≈ − 0.7071 c = \frac{1}{\sqrt{2}} \approx 0.7071, \quad s = \frac{-1}{\sqrt{2}} \approx -0.7071 c = 2 1 ≈ 0.7071 , s = 2 − 1 ≈ − 0.7071
Third Givens rotation matrix :
G 3 = [ 1 0 0 0 0 0 0 0.7071 0 0 0 − 0.7071 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0.7071 0 0 0 0.7071 ] G_3 = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0.7071 & 0 & 0 & 0 & -0.7071 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0.7071 & 0 & 0 & 0 & 0.7071 \end{bmatrix} G 3 = 1 0 0 0 0 0 0 0.7071 0 0 0 0.7071 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 − 0.7071 0 0 0 0.7071
Apply :
G 3 G 2 G 1 A = [ 1.7321 − 0.5744 − 0.5744 0 1.4142 − 0.7071 0 0 1 0 0.8165 − 0.4082 0 0 0.7071 0 0 0.7071 ] G_3 G_2 G_1 A = \begin{bmatrix} 1.7321 & -0.5744 & -0.5744 \\ 0 & 1.4142 & -0.7071 \\ 0 & 0 & 1 \\ 0 & 0.8165 & -0.4082 \\ 0 & 0 & 0.7071 \\ 0 & 0 & 0.7071 \end{bmatrix} G 3 G 2 G 1 A = 1.7321 0 0 0 0 0 − 0.5744 1.4142 0 0.8165 0 0 − 0.5744 − 0.7071 1 − 0.4082 0.7071 0.7071
G 3 G 2 G 1 b = [ − 376 1036.6 2417 605 1707 1708.4 ] G_3 G_2 G_1 b = \begin{bmatrix} -376 \\ 1036.6 \\ 2417 \\ 605 \\ 1707 \\ 1708.4 \end{bmatrix} G 3 G 2 G 1 b = − 376 1036.6 2417 605 1707 1708.4
Completing the Process
Continue to annihilate:
Entry ( 4 , 2 ) (4,2) ( 4 , 2 )
Entries below the main diagonal of the third column
(The complete process follows the same pattern)
Comparison of QR Factorization Methods
Givens Rotation
Running time : 3 m n 2 − n 3 + O ( m n ) 3mn^2 - n^3 + O(mn) 3 m n 2 − n 3 + O ( mn )
This is 50% more than Householder for dense matrices
BUT if A A A already has many zeros below the main diagonal, Givens becomes much faster than Householder
Stability : Backward stable ✓
Best use case : Sparse matrices with existing zeros
Gram-Schmidt Algorithm
Running time : 2 m n 2 + O ( m n ) 2mn^2 + O(mn) 2 m n 2 + O ( mn )
Worse than Householder
Requires more storage than Householder
Stability :
Backward stable after some modifications
BUT: Columns of computed Q Q Q may not be orthonormal to each other due to cancellations (unlike Householder)
Advantage :
Gram-Schmidt produces one additional column of Q Q Q after each iteration (unlike Householder)
Useful when you need Q Q Q explicitly during the process
Best use case : Educational purposes or when columns of Q Q Q are needed incrementally
Final Comparison Table
Method Running Time Stability Best For Key Feature Normal Equations m n 2 + n 3 3 mn^2 + \frac{n^3}{3} m n 2 + 3 n 3 Conditionally stable Well-conditioned, dense Fastest Householder 2 m n 2 − 2 n 3 3 2mn^2 - \frac{2n^3}{3} 2 m n 2 − 3 2 n 3 Backward stable General dense matrices Most reliable Givens 3 m n 2 − n 3 3mn^2 - n^3 3 m n 2 − n 3 Backward stable Sparse matrices Exploits sparsity Gram-Schmidt 2 m n 2 2mn^2 2 m n 2 Conditionally stable Need Q Q Q columns Incremental Q Q Q
Practical Recommendations
Choose Normal Equations when:
Speed is critical
Matrix is well-conditioned (κ ( A ) \kappa(A) κ ( A ) is small)
Stability is not a major concern
Choose Householder QR when:
Stability is important
Matrix is general/dense
This is the default choice for most applications
Choose Givens QR when:
Matrix is sparse (many zeros below diagonal)
Need to preserve sparsity structure
Parallel computation is desired
Choose Gram-Schmidt when:
Educational/theoretical purposes
Need to build Q Q Q column-by-column
Not recommended for production code
Key Takeaways
Least-squares problems arise when we have more equations than unknowns
Two main approaches :
Solve normal equations: A T A x = A T b A^T A x = A^T b A T A x = A T b
Use QR factorization: A = Q R A = QR A = QR , solve R 1 x = c 1 R_1 x = c_1 R 1 x = c 1
QR is more stable but slower than normal equations
Three methods for QR factorization :
Householder (best general-purpose)
Givens (best for sparse)
Gram-Schmidt (educational)
Backward stability is crucial for numerical reliability
Always consider matrix conditioning when choosing a method
Golden Rule : When in doubt, use Householder QR factorization for least-squares problems. It provides excellent numerical stability at reasonable computational cost.