10 - Linear Least Squares

Updated 4 Oct 2026

จะคล้าย ๆ กับ 7 – Interpolation แต่ไม่เหมือนเสียทีเดียว เพราะในกรณีนี้เรามีการ “จำกัดรูปแบบของสมการ” ให้เป็นหน้าตาที่กำหนดไว้ล่วงหน้า เช่น อยากให้เป็นสมการกำลังสอง แต่ใน Interpolation จะเลือกสมการที่ “ง่ายที่สุด” ซึ่งสามารถผ่านทุกจุดได้

ต้องทำเป็น

Classic Application: Data Fitting

Example: Fitting a Quadratic Function

  • Problem: Fit a quadratic function through five given data points (ti,yi)(t_i, y_i), i=1,2,…,5i = 1, 2, \ldots, 5
  • Unknown quadratic: y=ct2+dt+ey = ct^2 + dt + e
  • Goal: We want the quadratic to pass through all five data points (if possible)

This means we need to satisfy:

y1=ct12+dt1+ey2=ct22+dt2+ey3=ct32+dt3+ey4=ct42+dt4+ey5=ct52+dt5+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}

Matrix Formulation

This is equivalent to solving:

[t12t11t22t21t32t31t42t41t52t51][cde]=[y1y2y3y4y5]\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}

for c,d,ec, d, e.

  • This is a linear system: Ax=b\boxed{Ax = 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 xx such that Ax=bAx = b exactly:

Analogy: If you can't hit the bullseye exactly, try to get as close as possible!


Linear Least-Squares Problem

Given: A∈Rm×nA \in \mathbb{R}^{m \times n} and b∈Rmb \in \mathbb{R}^m

Find: x∈Rnx \in \mathbb{R}^n that minimizes ∥Ax−b∥2\boxed{\|Ax - b\|_2}

  • Sometimes written as Ax≅b\boxed{Ax \cong b}

Why Use the 2-Norm?

Reason 1: Computational Ease

  • 2-norm problem (min⁡∥Ax−b∥2\min \|Ax - b\|_2): Can be solved using calculus and linear algebra
  • 1-norm problem (min⁡∥Ax−b∥1\min \|Ax - b\|_1) and ∞-norm problem (min⁡∥Ax−b∥∞\min \|Ax - b\|_\infty): These are linear programming (linear optimization) problems
  • 3-norm problem (min⁡∥Ax−b∥3\min \|Ax - 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>nm > n (more equations than unknowns - overdetermined system)
  • rank(A)=n\text{rank}(A) = n (full column rank - columns are linearly independent)

Note: The cases where rank(A)<n\text{rank}(A) < n or m<nm < 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 ∥Ax−b∥2\|Ax - b\|_2

Key Insight

This is the same as minimizing ∥Ax−b∥22\|Ax - b\|_2^2 since ∥Ax−b∥2≥0\|Ax - b\|_2 \geq 0 by definition of norm.

Why square it? Because the square root function is monotonic, the minimum of f(x)f(x) occurs at the same point as the minimum of [f(x)]2[f(x)]^2. Squaring eliminates the square root and makes the math easier!

Mathematical Setup

Recall:

  • ∥w∥22=wTw\|w\|_2^2 = w^T w
  • (BC)T=CTBT(BC)^T = C^T B^T

We can expand:

∥Ax−b∥22=(Ax−b)T(Ax−b)=(xTAT−bT)(Ax−b)=xTATAx−xTATb−bTAx+bTb\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}

Important observation: bTAxb^T Ax is a scalar

  • So its transpose equals itself: (bTAx)T=xTATb=bTAx(b^T Ax)^T = x^T A^T b = b^T Ax

Therefore:

∥Ax−b∥22=xTATAx−2bTAx+bTb(Equation 1)\boxed{\|Ax - b\|_2^2 = x^T A^T Ax - 2b^T Ax + b^T b} \quad \text{(Equation 1)}

Lemma 1: Properties of ATAA^T A

Lemma 1


Suppose A∈Rm×nA \in \mathbb{R}^{m \times n} has rank nn. Then ATAA^T A is symmetric positive definite.

Proof:

Part 1 - Symmetry:

  • (ATA)T=AT(AT)T=ATA(A^T A)^T = A^T (A^T)^T = A^T A ✓

Part 2 - Positive Definiteness:

  • Suppose x∈Rnx \in \mathbb{R}^n is nonzero

  • Then:
    xTATAx=(Ax)TAx=∥Ax∥22≥0x^T A^T Ax = (Ax)^T Ax = \|Ax\|_2^2 \geq 0

  • But AxAx is a nontrivial linear combination of columns of AA (since x≠0x \neq 0)

  • Since columns of AA are linearly independent (because rank of AA is nn), we have Ax≠0Ax \neq 0

  • So ∥Ax∥2≠0\|Ax\|_2 \neq 0

  • Therefore: xTATAx=∥Ax∥22>0x^T A^T Ax = \|Ax\|_2^2 > 0 ✓

Why this matters: ATAA^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: Minimizer of Quadratic Forms

Lemma 2


Suppose f(x)=xTCx−2hTx+df(x) = x^T Cx - 2h^T x + d, where CC is symmetric positive definite. Then ff has a unique minimizer at x∗=C−1h\boxed{x^* = C^{-1}h}.

Proof:

Step 1: Evaluate f(x∗)f(x^*)

First:
f(x∗)=f(C−1h)=(C−1h)TCC−1h−2hTC−1h+df(x^*) = f(C^{-1}h) = (C^{-1}h)^T CC^{-1}h - 2h^T C^{-1}h + d
=hTC−Th−2hTC−1h+d= h^T C^{-T}h - 2h^T C^{-1}h + d

But CC is symmetric, so:
C−T=(CT)−1=C−1C^{-T} = (C^T)^{-1} = C^{-1}

Therefore:
f(x∗)=hTC−1h−2hTC−1h+d=−hTC−1h+df(x^*) = h^T C^{-1}h - 2h^T C^{-1}h + d = -h^T C^{-1}h + d

Step 2: Evaluate f(x∗+y)f(x^* + y) for any nonzero vector yy (one way to show x∗x^* is unique minimizer)

Let y∈Rny \in \mathbb{R}^n be any arbitrary nonzero vector.

We have:
f(x∗+y)=(C−1h+y)TC(C−1h+y)−2hT(C−1h+y)+df(x^* + y) = (C^{-1}h + y)^T C(C^{-1}h + y) - 2h^T(C^{-1}h + y) + d
=(hTC−1+yT)(h+Cy)−2hTC−1h−2hTy+d= (h^T C^{-1} + y^T)(h + Cy) - 2h^T C^{-1}h - 2h^T y + d
=hTC−1h+hTC−1Cy+yTh+yTCy−2hTC−1h−2hTy+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
=hTC−1h+hTy+yTh+yTCy−2hTC−1h−2hTy+d= h^T C^{-1}h + h^T y + y^T h + y^T Cy - 2h^T C^{-1}h - 2h^T y + d

But hTy=∑ihiyi=yThh^T y = \sum_i h_i y_i = y^T h (scalars are symmetric).

So:
f(x∗+y)=hTC−1h+2hTy+yTCy−2hTC−1h−2hTy+df(x^* + y) = h^T C^{-1}h + 2h^T y + y^T Cy - 2h^T C^{-1}h - 2h^T y + d
=−hTC−1h+d+yTCy=f(x∗)+yTCy= -h^T C^{-1}h + d + y^T Cy = f(x^*) + y^T Cy

Step 3: Show x∗x^* is the unique minimizer

But CC is symmetric positive definite, so:
yTCy>0y^T Cy > 0

Therefore:
f(x∗+y)=f(x∗)+yTCy>f(x∗)f(x^* + y) = f(x^*) + y^T Cy > f(x^*)

So x∗x^* is the unique minimizer. ✓

The Solution: Normal Equations

Recall that we want to minimize equation (1):
∥Ax−b∥22=xTATAx−2bTAx+bTb\|Ax - b\|_2^2 = x^T A^T Ax - 2b^T Ax + b^T b

Compare this with Lemma 2:
f(x)=xTCx−2hTx+df(x) = x^T Cx - 2h^T x + d

Setting:

  • C=ATAC = A^T A
  • h=ATbh = A^T b
  • d=bTbd = b^T b

We have that ∥Ax−b∥22\|Ax - b\|_2^2 is minimized at:
x∗=C−1h=(ATA)−1ATb\boxed{x^* = C^{-1}h = (A^T A)^{-1}A^T b}

Main Theorem

Theorem


x=(ATA)−1ATb\boxed{x = (A^T A)^{-1}A^T b}
is the unique solution to the linear least squares problem when AA has rank nn.

Method of Normal Equations

To solve min⁡x∥Ax−b∥2\min_x \|Ax - b\|_2:

  1. Form products ATAA^T A and ATbA^T b
  2. Solve แ using Cholesky factorization
    • (Assuming rank(A)=n\text{rank}(A) = n, Lemma 1 tells us that ATAA^T A is symmetric positive definite)

The equation ATAx=ATb\boxed{A^T Ax = A^T b} is called the normal equations.

  • ไม่ควรใช้ GEPP เนอะ
  • rank(A)=n\text{rank}(A) = n ถึงจะใช้ Method ที่ว่ามาได้

Example 1: Fitting a Line

(2)
Problem: Given data points (x,y)(x, y): (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=mx+cy = mx + c. Find the line that best fits the data points in least-squares sense using the method of normal equations.

Solution:

We want mm and cc satisfying:
mx1+c≅y1mx_1 + c \cong y_1
mx2+c≅y2mx_2 + c \cong y_2
mx3+c≅y3mx_3 + c \cong y_3
mx4+c≅y4mx_4 + c \cong y_4
In matrix form:
[x11x21x31x41][mc]≅[y1y2y3y4]\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}

Substituting the data points:
[01112131][mc]≅[1−142]\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}

Let's call the matrix AA and the right-hand side vector bb.

By the method of normal equations:

ATA=[01231111][01112131]=[14664]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}

5555 คือเนื่องจากมันเป็น Symmetric เนอะ ไม่ต้อง Compute ทั้งหมด ก็ก็อบมาจากข้างบนเลย

And:
ATb=[01231111][1−142]=[136]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}

Solve ATAx=ATbA^T Ax = A^T b by Cholesky factorization:
[14664][mc]=[136]\begin{bmatrix} 14 & 6 \\ 6 & 4 \end{bmatrix} \begin{bmatrix} m \\ c \end{bmatrix} = \begin{bmatrix} 13 \\ 6 \end{bmatrix}

The solution is m=0.8m = 0.8, c=0.3c = 0.3.

So the best-fit line in least-squares sense is:
y=0.8x+0.3\boxed{y = 0.8x + 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) with the function y=c1x+c2exy = c_1 x + c_2 e^x in least-squares sense using the method of normal equations.

Solution:

We want c1c_1 and c2c_2 satisfying:
c1x1+c2ex1≅y1c_1 x_1 + c_2 e^{x_1} \cong y_1
c1x2+c2ex2≅y2c_1 x_2 + c_2 e^{x_2} \cong y_2
c1x3+c2ex3≅y3c_1 x_3 + c_2 e^{x_3} \cong y_3
c1x4+c2ex4≅y4c_1 x_4 + c_2 e^{x_4} \cong y_4

In matrix form:
[x1ex1x2ex2x3ex3x4ex4][c1c2]≅[y1y2y3y4]\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}

Substituting the data points:
[0e01e12e23e3][c1c2]≅[1−142]\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}

The method of normal equations:
ATA=[1477.753077.7530466.4160]A^T A = \begin{bmatrix} 14 & 77.7530 \\ 77.7530 & 466.4160 \end{bmatrix}

And:
ATb=[1368.0090]A^T b = \begin{bmatrix} 13 \\ 68.0090 \end{bmatrix}

Solve ATA[c1c2]=ATbA^T A \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} = A^T b by Cholesky factorization.

The solution is c1=1.6013c_1 = 1.6013, c2=−0.1211c_2 = -0.1211.

So the best-fit function is:
y=1.6013x−0.1211ex\boxed{y = 1.6013x - 0.1211e^x}

Running Time of Method of Normal Equations

Matrix multiplication complexity:

  • A∈Rp×qA \in \mathbb{R}^{p \times q}, B∈Rq×rB \in \mathbb{R}^{q \times r}
  • Multiplying ABAB requires 2pqr2pqr arithmetic operations
  • The matrix C=ABC = AB is pp-by-rr matrix
  • To find cij=∑k=1qaikbkjc_{ij} = \sum_{k=1}^q a_{ik} b_{kj}, need 2q2q operations
  • CC has prpr entries, so 2pqr2pqr operations total

For normal equations:

  • But ATAA^T A is symmetric, so only need to compute the upper triangular portion
  • Therefore, Step 1 needs mn2+O(mn)mn^2 + O(mn) operations
  • Step 2 requires n3/3+O(n2)n^3/3 + O(n^2) operations (running time of Cholesky factorization)

Total arithmetic operations:
mn2+n3/3+O(mn)=mn2+O(n3+mn)\boxed{mn^2 + n^3/3 + O(mn) = mn^2 + O(n^3 + mn)} (assuming m≥nm \geq n)

Comment on the Method of Normal Equations

Advantages:

  • The method of normal equations is fast
  • ATAA^T A is smaller than AA and is symmetric
  • So solving ATAx=ATbA^T Ax = 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∈Rm×nA \in \mathbb{R}^{m \times n}, m≥nm \geq n, a QR factorization of AA is:
A=QR\boxed{A = QR}
where:

  • QQ is an mm-by-mm orthogonal matrix (meaning QTQ=IQ^T Q = I)
  • RR is an mm-by-nn upper triangular matrix

Structure of RR:
R=[R10]R = \begin{bmatrix} R_1 \\ 0 \end{bmatrix}

where:

  • R1R_1 is an nn-by-nn upper triangular matrix
  • 00 is an (m−n)(m-n)-by-nn zero matrix

Examples of RR:
[130200],[41202500−1000000]\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}

ก็คือ RR มันไม่ใช่ Upper triangular matrix แบบธรรมดานะ มันต้องมี extra row of 0s อยู่ข้างล่างด้วย

Solving Least-Squares with QR Factorization

Suppose we have a QR factorization of AA:
A=QRA = QR

We want to minimize:
∥Ax−b∥2=∥QRx−b∥2\|Ax - b\|_2 = \|QRx - b\|_2

Key step: Let c=QTbc = Q^T b. That is, b=Qcb = Qc.

So:
∥Ax−b∥2=∥QRx−Qc∥2\|Ax - b\|_2 = \|QRx - Qc\|_2
=∥Q(Rx−c)∥2= \|Q(Rx - c)\|_2
=∥Rx−c∥2(since ∥Qx∥2=∥x∥2 for orthogonal Q)= \|Rx - c\|_2 \quad \text{(since } \|Qx\|_2 = \|x\|_2 \text{ for orthogonal } Q)
=∥[R10]x−[c1c2]∥2= \left\|\begin{bmatrix} R_1 \\ 0 \end{bmatrix} x - \begin{bmatrix} c_1 \\ c_2 \end{bmatrix}\right\|_2

where c=[c1c2]c = \begin{bmatrix} c_1 \\ c_2 \end{bmatrix}, c1∈Rnc_1 \in \mathbb{R}^n, and c2∈Rm−nc_2 \in \mathbb{R}^{m-n}.

Simplifying:
∥Ax−b∥2=∥[R1x−c1−c2]∥2=∥R1x−c1∥22+∥c2∥22\|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}

Key observation: c2c_2 is a constant (it does not depend on xx since c=QTbc = Q^T b).

So the xx that minimizes ∥Ax−b∥2\|Ax - b\|_2 is the one that minimizes ∥R1x−c1∥2\|R_1 x - c_1\|_2.

If R1R_1 is nonsingular, then:
R1x=c1R_1 x = c_1

has a solution. This solution xx satisfies:
R1x−c1=0R_1 x - c_1 = 0
∥R1x−c1∥2=0\|R_1 x - c_1\|_2 = 0

In other words, this solution xx minimizes ∥R1x−c1∥2\|R_1 x - c_1\|_2 and therefore minimizes ∥Ax−b∥2\|Ax - b\|_2.

Theorems for QR Factorization

Theorem 1


Any A∈Rm×nA \in \mathbb{R}^{m \times n} (m≥nm \geq n) can be factored A=QRA = QR where QQ is an m×mm \times m orthogonal matrix and RR is an m×nm \times n upper triangular matrix.

Theorem 2


If A∈Rm×nA \in \mathbb{R}^{m \times n}, m≥nm \geq n, has rank nn and A=QRA = QR is the QR factorization of AA, then RR has rank nn and R1R_1 is nonsingular.

Conclusion: If rank(A)=n\text{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=QRA = QR be the QR factorization of AA.

Assume A∈Rm×nA \in \mathbb{R}^{m \times n}, m≥nm \geq n, rank(A)=n\text{rank}(A) = n.

Let c=QTbc = Q^T b. Let c1c_1 be the top nn entries of cc:
c=[c1c2]c = \begin{bmatrix} c_1 \\ c_2 \end{bmatrix}
where c1∈Rnc_1 \in \mathbb{R}^n and c2∈Rm−nc_2 \in \mathbb{R}^{m-n}.

Then xx minimizes ∥Ax−b∥2\|Ax - b\|_2 if:
R1x=c1\boxed{R_1 x = c_1}

Steps to solve min⁡x∥Ax−b∥2\min_x \|Ax - b\|_2:

  1. Factor A=QRA = QR
  2. Compute c=QTbc = Q^T b
  3. Solve R1x=c1R_1 x = c_1 for xx by back substitution
    • Backward stable and fast than GEPP อีกนะ เลยใช้เลยล่ะ

Example 3: Using QR Factorization

Problem: Solve the least-squares problem:
[120−2.401.8]x≅[−101]\begin{bmatrix} 1 & 2 \\ 0 & -2.4 \\ 0 & 1.8 \end{bmatrix} x \cong \begin{bmatrix} -1 \\ 0 \\ 1 \end{bmatrix}

Given QR factorization:
[120−2.401.8]=[10000.80.60−0.60.8][120−300]\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}

Solution:

Step 1: Let
c=QTb=[10000.8−0.600.60.8][−101]=[−1−0.60.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}

Step 2: Since AA is 3-by-2:
c1=[−1−0.6]c_1 = \begin{bmatrix} -1 \\ -0.6 \end{bmatrix}

Step 3: Solve R1x=c1R_1 x = c_1:
[120−3]x=[−1−0.6]\begin{bmatrix} 1 & 2 \\ 0 & -3 \end{bmatrix} x = \begin{bmatrix} -1 \\ -0.6 \end{bmatrix}

by back substitution.

The solution is:
x=[−1.40.2]\boxed{x = \begin{bmatrix} -1.4 \\ 0.2 \end{bmatrix}}
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: ATAx=ATbA^T A x = A^T b
  • Advantages: Fast, simple
  • Disadvantages: Conditionally stable (can have numerical issues)
  • Running time: mn2+O(n3+mn)mn^2 + O(n^3 + mn)

Method 2: QR Factorization

  • Solve: Factor A=QRA = QR, then solve R1x=c1R_1 x = c_1 where c1c_1 are top nn entries of QTbQ^T b
  • Advantages: More numerically stable, backward stable
  • Disadvantages: Slower than normal equations
  • Running time: 2mn2−2n33+O(mn)2mn^2 - \frac{2n^3}{3} + O(mn)

When to use which?

  • Use normal equations when speed is critical and AA is well-conditioned
  • Use QR factorization when numerical stability is important or AA is ill-conditioned

QR Factorization Algorithms

Three main algorithms for computing QR factorization:

  1. Householder transformations (most commonly used)
  2. Givens rotations (useful for sparse matrices)
  3. Gram-Schmidt algorithm (simple but less stable)

Householder Transformations

Definition

Let v∈Rmv \in \mathbb{R}^m be a nonzero vector. H=I−2vvTvTv\boxed{H = I - 2\frac{vv^T}{v^T v}}

is called a Householder transform/reflection/matrix.

Theorem


HH is symmetric and orthogonal.

Proof of Properties

Part 1 - Symmetry: (เวลาโชว์ทำไงนะ ก็โชว์ว่า HT=HH^T = H ไง)

HT=(I−2vvTvTv)T=IT−2(vvT)TvTv=I−2vvTvTv=HH^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

Part 2 - Orthogonality: (นี่ล่ะ HHT=IHH^T=I)

HHT=HH=(I−2vvTvTv)(I−2vvTvTv)HH^T = HH = \left(I - 2\frac{vv^T}{v^T v}\right)\left(I - 2\frac{vv^T}{v^T v}\right)

=I−4vvTvTv+4vvTvvT(vTv)2= I - 4\frac{vv^T}{v^T v} + 4\frac{vv^T vv^T}{(v^T v)^2}

=I−4vvTvTv+4v(vTv)vT(vTv)2= I - 4\frac{vv^T}{v^T v} + 4\frac{v(v^T v)v^T}{(v^T v)^2}

=I−4vvTvTv+4vvTvTv=I= I - 4\frac{vv^T}{v^T v} + 4\frac{vv^T}{v^T v} = I

Using Householder to Create Zeros

Goal: Given a vector aa (say, the first column of AA), we want a Householder transform such that:

Ha=(I−2vvTvTv)a=[α0⋮0]=αe1Ha = \left(I - 2\frac{vv^T}{v^T v}\right)a = \begin{bmatrix} \alpha \\ 0 \\ \vdots \\ 0 \end{bmatrix} = \alpha e_1

for some scalar α\alpha (which will be the first column of RR).

Solution: This is satisfied by taking:

  • v:=a−αe1v := a - \alpha e_1
  • α=±∥a∥2\alpha = \pm \|a\|_2

To avoid cancellation: Choose α\alpha to have the opposite sign from a1a_1: α=−sign(a1)∥a∥2\boxed{\alpha = -\text{sign}(a_1) \|a\|_2}

This ensures numerical stability by avoiding subtraction of nearly equal numbers.

Verification

We need to verify that Ha=αe1Ha = \alpha e_1.

Starting with:
Ha=(I−2vvTvTv)a=a−2vvTvTvaHa = \left(I - 2\frac{vv^T}{v^T v}\right)a = a - 2\frac{vv^T}{v^T v}a

After substituting v=a−αe1v = a - \alpha e_1 and simplifying (detailed algebra in the slides):

Ha=αe1Ha = \alpha e_1 ✓

Algorithm: QR Factorization via Householder

Given: A∈Rm×nA \in \mathbb{R}^{m \times n}

Process:

  1. Write A=[aB]A = [a \quad B] where a∈Rma \in \mathbb{R}^m is the first column
  2. Choose H1H_1 to annihilate first column:
    • Set v1:=a−α1e1v_1 := a - \alpha_1 e_1 where α1=−sign(a1)∥a∥2\alpha_1 = -\text{sign}(a_1) \|a\|_2
    • Then H1=I−2v1v1Tv1Tv1H_1 = I - 2\frac{v_1 v_1^T}{v_1^T v_1}
  3. After applying H1H_1:
    H1A=[α1wT0A2]H_1 A = \begin{bmatrix} \alpha_1 & w^T \\ 0 & A_2 \end{bmatrix}
  4. Recursively apply to submatrix A2A_2
  5. Continue until upper triangular

Result: After nn steps: HnHn−1⋯H1A=RH_n H_{n-1} \cdots H_1 A = R

Since HiH_i are orthogonal and symmetric (HiT=HiH_i^T = H_i and HiTHi=IH_i^T H_i = I):
A=H1H2⋯HnR=QRA = H_1 H_2 \cdots H_n R = QR

where Q=H1H2⋯HnQ = H_1 H_2 \cdots H_n is orthogonal.

Lemma


If Q1,…,QnQ_1, \ldots, Q_n are orthogonal, so is Q1Q2⋯QnQ_1 Q_2 \cdots Q_n.

Efficient Implementation

Key Optimization 1: Don't Form Q Explicitly

For solving least-squares, we only need c=QTbc = Q^T b:

c=QTb=HnHn−1⋯H2H1bc = Q^T b = H_n H_{n-1} \cdots H_2 H_1 b

Solution: Apply H1H_1 to bb, then H2H_2 to result, and so on.

Key Optimization 2: Work with Submatrices

For k>1k > 1:
Hk=[Ik−100Hk′]H_k = \begin{bmatrix} I_{k-1} & 0 \\ 0 & H_k' \end{bmatrix}

This means:
Hkw=[Ik−100Hk′][w1w2]=[w1Hk′w2]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}

Benefit: Only apply Hk′H_k' to relevant portion of vector!

Key Optimization 3: Efficient Multiplication

Computing HuHu directly from definition:
Hu=(I−2vvTvTv)u=u−(2vTuvTv)vHu = \left(I - 2\frac{vv^T}{v^T v}\right)u = u - \left(2\frac{v^T u}{v^T v}\right)v

  • This is O(m)O(m) operations
  • Much cheaper than forming HH explicitly and multiplying (O(m2)O(m^2) operations)

Similarly, for matrix AA:
HA=H[a∙1a∙2⋯a∙n]=[Ha∙1Ha∙2⋯Ha∙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}]

Apply the efficient formula to each column.

Example 4: Complete Householder QR Solution

Problem: Use Householder QR factorization to solve:

[1532−13−221203]x≅[2010]\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}

Solution:

Step 1: Annihilate first column

Set a=[12−22]a = \begin{bmatrix} 1 \\ 2 \\ -2 \\ 2 \end{bmatrix} (first column of AA)

Since a1=1>0a_1 = 1 > 0, set:
α=−∥a∥2=−3.6056\alpha = -\|a\|_2 = -3.6056

v1=a−αe1=[12−22]−(−3.6056)[1000]=[4.60562−22]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}

Apply to get: Hu=u−(2vvTvTv)v\boxed{Hu = u - (2\frac{vv^T}{v^T v})v}

  • สำหรับ First Column; uu ให้ใช้เป็น first column of AA แล้ว vv ก็เป็นอันข้างบนที่ได้มา v1v_1
  • Second column; คล้าย ๆ กัน แต่ uu ก็ใช้เป็น second column of AA
  • …

H1A=[−3.60560.2774−3.60560−3.05090.131504.05093.86850−2.05090.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}

Step 2: Annihilate second column of submatrix (เอาแค่ส่วนล่างขวามาแค่นั้น)

Remove first row and column to get:
A2=[−3.05090.13154.05093.8685−2.05090.1315]A_2 = \begin{bmatrix} -3.0509 & 0.1315 \\ 4.0509 & 3.8685 \\ -2.0509 & 0.1315 \end{bmatrix}

Set a=[−3.05094.0509−2.0509]a = \begin{bmatrix} -3.0509 \\ 4.0509 \\ -2.0509 \end{bmatrix} (first column of A2A_2)

Since a1<0a_1 < 0, set α=∥a∥2=5.4702\alpha = \|a\|_2 = 5.4702

v2=a−αe1=[−8.52114.0509−2.0509]v_2 = a - \alpha e_1 = \begin{bmatrix} -8.5211 \\ 4.0509 \\ -2.0509 \end{bmatrix}

Apply H2′H'_2 to A2A_2 to get

H2′A2=[5.47022.742102.627400.7598]H'_2A_2=\begin{bmatrix} 5.4702 & 2.7421 \\ 0 & 2.6274 \\ 0 & 0.7598 \end{bmatrix}

Apply to get:
H2H1A=[−3.60560.2774−3.605605.47022.7421002.6274000.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}

Step 3: Annihilate third column of submatrix

Remove first two rows and columns:
A3=[2.62740.7598]A_3 = \begin{bmatrix} 2.6274 \\ 0.7598 \end{bmatrix}

Since first entry > 0, set α=−∥a∥2=−2.7351\alpha = -\|a\|_2 = -2.7351

v3=[5.36250.7598]v_3 = \begin{bmatrix} 5.3625 \\ 0.7598 \end{bmatrix}

Apply to get:
H3H2H1A=[−3.60560.2774−3.605605.47022.742100−2.7351000]=RH_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

Therefore:
R1=[−3.60560.2774−3.605605.47022.742100−2.7351]R_1 = \begin{bmatrix} -3.6056 & 0.2774 & -3.6056 \\ 0 & 5.4702 & 2.7421 \\ 0 & 0 & -2.7351 \end{bmatrix}(Top square portion of RR)

Step 4: Form c=QTbc = Q^T b

Apply H1H_1 to bb:
H1b=[0−0.86851.8685−0.8685]H_1 b = \begin{bmatrix} 0 \\ -0.8685 \\ 1.8685 \\ -0.8685 \end{bmatrix}

Apply H2H_2 to second through fourth entries:
H2H1b=[02.19370.4128−0.1315]H_2 H_1 b = \begin{bmatrix} 0 \\ 2.1937 \\ 0.4128 \\ -0.1315 \end{bmatrix}

Apply H3H_3 to third and fourth entries:
c=H3H2H1b=[02.1937−0.36−0.2410]c = H_3 H_2 H_1 b = \begin{bmatrix} 0 \\ 2.1937 \\ -0.36 \\ -0.2410 \end{bmatrix}

Step 5: Solve R1x=c1R_1 x = c_1 by back substitution

[−3.60560.2774−3.605605.47022.742100−2.7351]x=[02.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}

Solution:
x=[−0.10580.33510.1316]\boxed{x = \begin{bmatrix} -0.1058 \\ 0.3351 \\ 0.1316 \end{bmatrix}}
📄 CSS322_Doable_L10_Ex4.pdf

Properties of Householder Transformations

Running Time Comparison

For solving least-squares problems:

  • Householder (HH): 2mn2−2n33+O(mn)2mn^2 - \frac{2n^3}{3} + O(mn)
  • Normal equations: mn2+n33+O(mn)mn^2 + \frac{n^3}{3} + O(mn)

Analysis:

  • If m≈nm \approx n: The two are about equal
  • If m≫nm \gg 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

where ∥E∥/∥A∥≈ϵmach\|E\| / \|A\| \approx \epsilon_{mach}

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 AA 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=[cs−sc]G = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}

where c=cos⁡θc = \cos\theta and s=sin⁡θs = \sin\theta, with θ\theta being the angle of rotation.

มันก็คือ Rotation Matrix จากวิชา Mathematica Summary

Orthogonality

Check that GG is orthogonal:
GGT=[cs−sc][c−ssc]=[c2+s200c2+s2]=[1001]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}
This works because c2+s2=cos⁡2θ+sin⁡2θ=1c^2 + s^2 = \cos^2\theta + \sin^2\theta = 1 ✓


Finding the Rotation Parameters

Goal: Given a 2-vector a=[a1a2]a = \begin{bmatrix} a_1 \\ a_2 \end{bmatrix}, choose cc and ss so that:

Ga=[cs−sc][a1a2]=[α0]Ga = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}\begin{bmatrix} a_1 \\ a_2 \end{bmatrix} = \begin{bmatrix} \alpha \\ 0 \end{bmatrix}

Derivation: Rewrite as a linear system:

[a1a2a2−a1][cs]=[α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}

After Gaussian elimination and back-substitution:
[a1a20−a1−a22/a1][cs]=[α−αa2/a1]\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}
s=αa2a12+a22,c=αa1a12+a22s = \frac{\alpha a_2}{a_1^2 + a_2^2}, \quad c = \frac{\alpha a_1}{a_1^2 + a_2^2}

Using c2+s2=1c^2 + s^2 = 1, we get:
α=a12+a22\boxed{\alpha = \sqrt{a_1^2 + a_2^2}}

Therefore:
c=a1a12+a22,s=a2a12+a22\boxed{c = \frac{a_1}{\sqrt{a_1^2 + a_2^2}}, \quad s = \frac{a_2}{\sqrt{a_1^2 + a_2^2}}}

Avoiding Overflow/Underflow

Problem: Computing a12a_1^2 or a22a_2^2 can cause unnecessary overflow or underflow.

ใน ALU มี Component อะไรป้องกันพวกนี้อยู่…

Solution 1: Use Tangent (if ∣a1∣>∣a2∣|a_1| > |a_2|)

Work with t=tan⁡θt = \tan\theta instead:
t=sc=a2a1t = \frac{s}{c} = \frac{a_2}{a_1}

Then:
c=11+t2,s=ct\boxed{c = \frac{1}{\sqrt{1 + t^2}}, \quad s = ct}

Solution 2: Use Cotangent (if ∣a2∣>∣a1∣|a_2| > |a_1|)

Work with τ=cot⁡θ\tau = \cot\theta instead:

τ=cs=a1a2\tau = \frac{c}{s} = \frac{a_1}{a_2}

Then:
s=11+τ2,c=sτ\boxed{s = \frac{1}{\sqrt{1 + \tau^2}}, \quad c = s\tau}
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=[43]a = \begin{bmatrix} 4 \\ 3 \end{bmatrix}

Solution:

Since magnitudes are reasonable, compute directly:

c=a1a12+a22=45=0.8c = \frac{a_1}{\sqrt{a_1^2 + a_2^2}} = \frac{4}{5} = 0.8
s=a2a12+a22=35=0.6s = \frac{a_2}{\sqrt{a_1^2 + a_2^2}} = \frac{3}{5} = 0.6

Alternative using tangent:
t=a2a1=34=0.75t = \frac{a_2}{a_1} = \frac{3}{4} = 0.75
c=11+t2=11+(0.75)2=0.8c = \frac{1}{\sqrt{1 + t^2}} = \frac{1}{\sqrt{1 + (0.75)^2}} = 0.8
s=ct=(0.8)(0.75)=0.6s = ct = (0.8)(0.75) = 0.6
Rotation matrix:

G=[0.80.6−0.60.8]G = \begin{bmatrix} 0.8 & 0.6 \\ -0.6 & 0.8 \end{bmatrix}
Verification:

Ga=[0.80.6−0.60.8][43]=[50]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} ✓

Generalizing to m-Vectors

ชีวิตจริง Matrix ไม่ได้มีแค่ 2 rows เหมือนตัวอย่างข้างบน

  • Goal: To annihilate any desired component of an mm-vector.
  • Method: Apply the same technique by rotating the target component (say jj) with another component (say ii).

Process

  1. Use the two selected components as before to determine the appropriate 2×22 \times 2 rotation matrix
  2. Find the Givens rotation GG such that:
    Ga=[cs−sc][aiaj]=[α0]Ga = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}\begin{bmatrix} a_i \\ a_j \end{bmatrix} = \begin{bmatrix} \alpha \\ 0 \end{bmatrix}
  3. Embed GG as a 2×22 \times 2 submatrix in rows and columns ii and jj of the mm-dimensional identity matrix ImI_m

Example: Embedded Givens Matrix

For the case m=5m = 5, i=2i = 2, j=4j = 4:

[100000c0s0001000−s0c000001][a1a2a3a4a5]=[a1αa30a5]\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}

  • ก็คือ
    • gii=cg_{ii}=c
    • gij=sg_{ij}=s
    • gji=−sg_{ji}=-s
    • gjj=cg_{jj}=c
  • ซึ่งจะทำให้ a4=0a_4=0 ตามใจอยาก

Algorithm: QR via Givens Rotations

Process:

  • Use a sequence of Givens rotations to annihilate all entries of AA below the main diagonal one entry at a time
  • Transform AA to upper triangular form, which is RR
  • 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 aia_i (it will change to nonzero, i.e., to α\alpha)
    • Do NOT use an entry above the main diagonal as aia_i (it will mess up previously finished columns)

✅ Safe strategy: Always rotate the target entry with the diagonal entry

Forming Q

Just like Householder:

  • The product of all Givens rotation matrices used in reverse order is QQ
  • We never need to explicitly compute QQ to solve least-squares problems

Example: If we need three Givens rotations:
G3G2G1A=RG_3 G_2 G_1 A = R

Then:
Q=G1TG2TG3TQ = G_1^T G_2^T G_3^T
To get c=QTbc = Q^T b, simply compute:
c=G3(G2(G1b))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:

Ax=[100010001−110−1010−11]x≅[1237194124177111177475]=bAx = \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

Solution:

  • Observation: AA 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 a51a_{51}

The first nonzero entry to annihilate is a51a_{51}.

Rotate using entries a11a_{11} (diagonal) and a51a_{51}:

  • a1=a11=1a_1 = a_{11} = 1
  • a2=a51=−1a_2 = a_{51} = -1

c=a1a12+a22=112+(−1)2=12≈0.7071c = \frac{a_1}{\sqrt{a_1^2 + a_2^2}} = \frac{1}{\sqrt{1^2 + (-1)^2}} = \frac{1}{\sqrt{2}} \approx 0.7071

s=a2a12+a22=−112+(−1)2=−12≈−0.7071s = \frac{a_2}{\sqrt{a_1^2 + a_2^2}} = \frac{-1}{\sqrt{1^2 + (-1)^2}} = \frac{-1}{\sqrt{2}} \approx -0.7071

First Givens rotation matrix:
G1=[0.7071000−0.707100100000010000001000.70710000.70710000001]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}

Apply to AA and bb:
G1A=[1.41420−0.7071010001−110000.70710−11],G1b=[42194124177111707475]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}

Step 2: Annihilate a41a_{41}

Rotate using entries from first and fourth positions:

  • a1=1.4142a_1 = 1.4142
  • a2=−1a_2 = -1

c=1.4142(1.4142)2+(−1)2=0.8165c = \frac{1.4142}{\sqrt{(1.4142)^2 + (-1)^2}} = 0.8165
s=−1(1.4142)2+(−1)2=−0.5774s = \frac{-1}{\sqrt{(1.4142)^2 + (-1)^2}} = -0.5774

Second Givens rotation matrix:
G2=[0.816500−0.5774000100000010000.5774000.816500000010000001]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}

Apply:
G2G1A=[1.7321−0.5744−0.574401000100.8165−0.4082000.70710−11],G2G1b=[−376194124176051707475]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}

✅ First column complete!

Step 3: Annihilate a62a_{62}

For the second column, annihilate a62a_{62} using diagonal and sixth entries:

  • a1=1a_1 = 1
  • a2=−1a_2 = -1

c=12≈0.7071,s=−12≈−0.7071c = \frac{1}{\sqrt{2}} \approx 0.7071, \quad s = \frac{-1}{\sqrt{2}} \approx -0.7071

Third Givens rotation matrix:
G3=[10000000.7071000−0.707100100000010000001000.70710000.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}

Apply:
G3G2G1A=[1.7321−0.5744−0.574401.4142−0.707100100.8165−0.4082000.7071000.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}

G3G2G1b=[−3761036.6241760517071708.4]G_3 G_2 G_1 b = \begin{bmatrix} -376 \\ 1036.6 \\ 2417 \\ 605 \\ 1707 \\ 1708.4 \end{bmatrix}

Completing the Process

Continue to annihilate:

  • Entry (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: 3mn2−n3+O(mn)3mn^2 - n^3 + O(mn)
    • This is 50% more than Householder for dense matrices
    • BUT if AA 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: 2mn2+O(mn)2mn^2 + O(mn)

  • Worse than Householder
  • Requires more storage than Householder

Stability:

  • Backward stable after some modifications
  • BUT: Columns of computed QQ may not be orthonormal to each other due to cancellations (unlike Householder)

Advantage:

  • Gram-Schmidt produces one additional column of QQ after each iteration (unlike Householder)
  • Useful when you need QQ explicitly during the process

Best use case: Educational purposes or when columns of QQ are needed incrementally

Final Comparison Table

MethodRunning TimeStabilityBest ForKey Feature
Normal Equationsmn2+n33mn^2 + \frac{n^3}{3}Conditionally stableWell-conditioned, denseFastest
Householder2mn2−2n332mn^2 - \frac{2n^3}{3}Backward stableGeneral dense matricesMost reliable
Givens3mn2−n33mn^2 - n^3Backward stableSparse matricesExploits sparsity
Gram-Schmidt2mn22mn^2Conditionally stableNeed QQ columnsIncremental QQ

Practical Recommendations

Choose Normal Equations when:

  • Speed is critical
  • Matrix is well-conditioned (κ(A)\kappa(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 QQ column-by-column
  • Not recommended for production code

Key Takeaways

  1. Least-squares problems arise when we have more equations than unknowns
  2. Two main approaches:
    • Solve normal equations: ATAx=ATbA^T A x = A^T b
    • Use QR factorization: A=QRA = QR, solve R1x=c1R_1 x = c_1
  3. QR is more stable but slower than normal equations
  4. Three methods for QR factorization:
    • Householder (best general-purpose)
    • Givens (best for sparse)
    • Gram-Schmidt (educational)
  5. Backward stability is crucial for numerical reliability
  6. 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.