ma 580; linear least squares - nc state universitypart va: linear least squares, qr solving linear...

98
Part Va: Linear Least Squares, QR MA 580; Linear Least Squares C. T. Kelley NC State University tim [email protected] Version of September 20, 2016 Time for sections 3.7 and 3.8 and chapters 4 and 5 of Pink book. NCSU, Fall 2016 Part Va: Linear Least Squares, Normal Equations, QR Factorization c C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 1 / 97

Upload: others

Post on 16-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

MA 580; Linear Least Squares

C. T. KelleyNC State University

tim [email protected]

Version of September 20, 2016

Time for sections 3.7 and 3.8 and chapters 4 and 5 of Pink

book.

NCSU, Fall 2016Part Va: Linear Least Squares, Normal Equations, QR

Factorization

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 1 / 97

Page 2: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Data fitting or let’s get rich

I will predict the stock market on the assumption that the DowJones Industrial Average depends on the prior day’s

phase of the moon, p(t),

mean temperature in Central Park, c(t),

solar radiation in Grand Canyon, r(t)

linearly

Dow(t) = x1p(t − 1) + x2c(t − 1) + x3r(t − 1)

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 2 / 97

Page 3: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Easy path to great wealth

All I need to do is figure out what

x = (x1, x2, x3)T

is. Let’s collect historical data for a 300 business days

a1 = (p(t1), p(t2), . . . p(t300))T

a2 = (c(t1), c(t2), . . . c(t300))T

a3 = (r(t1), r(t2), . . . r(t300))T

b = (Dow(t1 + 1),Dow(t2 + 1), . . .Dow(t300 + 1))T .

and tune x to make the model look the best.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 3 / 97

Page 4: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

How might we do that?

A least squares fit would find x by minimizing

‖Ax− b‖2

where A = (a1, a2, a3).We could the use the solution to . . .

use the coefficients to predict the stock market on Monday,

sink life savings into that prediction, and

drive away in Lamborghini on Tuesday.

Isn’t math great?

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 4 / 97

Page 5: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Linear Least Squares

Problem, find x ∈ RN so that

‖Ax− b‖2

is minimized.Here A is M × N.

M ≥ N is overdetermined show them the geometry

M = N is square (it’s an equation)

M < N is underdetermined

We’ll begin with the overdetermined case.In our stock market example M = 300 and N = 3.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 5 / 97

Page 6: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Overdetermined Least Squares Problems

The Range of A, Ran(A) is the span of the columns

Ran(A) = {Ax | x ∈ RN} ⊂ RM .

The solution of the least squares problem is x, where Ax isthe projection of b onto Ran(A).

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 6 / 97

Page 7: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Solution with calculus

Assumption: The columns of A are linearly independent.In this case we say A has full column rank.

This implies (check it) that ATA is spd.

So, our job is to minimize

φ(x) =N∑i=1

N∑j=1

aijxj − bi

2

Calculus says we should look at points where ∇φ = 0, ie

∂φ(x)/∂xk = 0 for all 1 ≤ k ≤ N

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 7 / 97

Page 8: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Gradient of φ

Check this out if it’s not clear . . .

∂φ(x)

∂xk= 2

N∑i=1

N∑j=1

aijxj − bi

aik = 2(ATAx− ATb)k

So ∇φ(x) = 0 if and only if

ATAx = ATb

These are the normal equations

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 8 / 97

Page 9: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Is this a max or a min?

It’s a minimum for many reasons.

φ(x)→∞ as ‖x‖ → ∞ because a has full column rank.

The Hessian matrix

Hij =∂2φ(x)

∂xi∂xj

is 2ATA and is spd (because A has full column rank).

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 9 / 97

Page 10: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

The Normal Equations Method

Solvemin ‖Ax− b‖

by using a Cholesky factorization to solve the normal equations

ATAx = ATb

Cost:

N2M for the matrix product

N3/6 for the Cholesky factorization

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 10 / 97

Page 11: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

A few facts

The normal equations method costs too much.

It squares the condition numberwhich we will define later for M 6= N.

It’s very useful for small-scale hand calculations.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 11 / 97

Page 12: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Examples: I

N = 2, M = 3

A =

1 42 53 6

,b =

579

,ATA =

(14 3232 77

),ATb =

(46

109

)

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 12 / 97

Page 13: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Normal Equations

(14 3232 77

)x =

(46

109

)Solution (How did I get this with no work?)

x =

(11

)

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 13 / 97

Page 14: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

M = 3, N = 1

Find the scalar x that minimizes

‖ax − b‖

Normal equations:

‖a‖22x = aTb and so x = aTb/‖a‖22

which says that xa is the projection of b onto a.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 14 / 97

Page 15: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

QR decomposition: M ≥ N

The QR decomposition of an M × N matrix A is

A = QR

where

Q is M × N with orthonormal columns and

R is N × N upper triangular with positive diagonal.

In matlab

[Q,R]=qr(A,0);

The second argument 0 matters.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 15 / 97

Page 16: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Solving Linear Least Squares Problems with QR

If Q is M × N with orthonormal columns, then

QTQ = I, the N × N identity.

QQT = P, the projection onto the column space of Qwhich is Ran(A) if A = QR is the QR decomposition of A.

Claim: x = R−1QTb is the solution of the problem.In matlab, the backslash does the right thing.

x = A\b;

even if A is not square.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 16 / 97

Page 17: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

QR solves the problem

We will work it out in terms of the normal equations.ATAx = ATb and A = QR imply that

RTQTQRx = RTQTb

Since R is nonsingular and QTQ = I, we get

Rx = QTb, that’s itx = R−1QTb

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 17 / 97

Page 18: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Solving least squares problem with QR

Assumptions: M ≥ N and A has full column rank.

Factor A = QR

c← QTb

Solve Rx = c

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 18 / 97

Page 19: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Some Matlab: What you get from qr

>> A=[1 2; 3 4; 5 6; 7 8]

A =

1 2

3 4

5 6

7 8

>> [q,r]=qr(A,0);

>> q

q =

-1.0911e-01 -8.2952e-01

-3.2733e-01 -4.3916e-01

-5.4554e-01 -4.8795e-02

-7.6376e-01 3.4157e-01

>> r

r =

-9.1652e+00 -1.0911e+01

0 -9.7590e-01

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 19 / 97

Page 20: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

QTQ and QQT

>> q’*q

ans =

1.0000e+00 2.4980e-16

2.4980e-16 1.0000e+00

>> q*q’

ans =

7.0000e-01 4.0000e-01 1.0000e-01 -2.0000e-01

4.0000e-01 3.0000e-01 2.0000e-01 1.0000e-01

1.0000e-01 2.0000e-01 3.0000e-01 4.0000e-01

-2.0000e-01 1.0000e-01 4.0000e-01 7.0000e-01

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 20 / 97

Page 21: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Is QQT an orthogonal projector onto Ran(A)

We need P = QQT = P2 = PT and PA = A.

>> p=q*q’; [norm(p*A-A), norm(p*p - p), norm(p’-p)]

ans =

7.5261e-15 5.0915e-16 0

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 21 / 97

Page 22: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Proof of Projection Properties

We’ll use QTQ = IN×N all over.

P2 = Q(QTQ)QT = QQT = P

PT = (QQT )T = QQT = P

PA = PQR = Q(QTQ)R = QR = A

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 22 / 97

Page 23: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

More on QQT

If x∗ is the solution of the least squares problem, then

Ax∗ = QQTb

because both Ax and QQTb are the nearest points in Ran(A) to b.Algebraic argument:

Ax∗ = QRR−1QTb = QQTb.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 23 / 97

Page 24: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

A QR trick

Type help qr at the matlab prompt. You’ll be glad you did.

We’ll see that Q is tricky to store and can get large.

What would you do if you only had R?R = qr(A,0) (sparse) or R = triu(qr(A,0));

R=R(1:N,:);

Hint:

RTR = RTQTQR = ATA.

So RT is the Cholesky factor for the normal equations.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 24 / 97

Page 25: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Combine QR with Normal equations

Solve ATAx = ATb via

x = R−1R−TATb.

Good news:

No matrix-matrix multiplyVery effective if A is sparse because Q could be dense.

Bad news: Conditioning of ATA did not get any better.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 25 / 97

Page 26: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Experimenting

Lets solve min ‖AX− b‖2 where

A =

1 23 45 67 8

and b =

1111

Analytic solution via normal equations

ATA =

(84 100

100 120

)and ATb =

(1620

)I can eyeball it and get x = (−1, 1)T .

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 26 / 97

Page 27: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Solution with QR

>> b=ones(4,1);

>> x1=A\b

x1 =

-1.0000e+00

1.0000e+00

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 27 / 97

Page 28: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Solution with R alone

>> R=triu(qr(A)); R=R(1:N,:)

R =

-9.1652e+00 -1.0911e+01

0 -9.7590e-01

>> R\(R’\(A’*b))

ans =

-1.0000e+00

1.0000e+00

There’s a bit more to this coming later.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 28 / 97

Page 29: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Cost of solve with QR: leading order

Compute QR factorization (later): N2M − N3/3

Multiply b with QT : O(MN)The prefactor depends on how one stores Q

Solve Rx = QTb: N2/2

One can do the factorization without storing Q as a full matrix.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 29 / 97

Page 30: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

The QR factorization is unique

Lemma: If R is upper triangular with positive diagonal, so is R−1.proof: As you do the backsolve for Ax = b you’ll see that

xi = bi/rii −

N∑j=i+1

rijbj

/rii

So (A−1x)i only depends on bj for j ≥ i , hence the inverse is uppertriangular. The ith diagonal element of R−1 is 1/rii > 0.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 30 / 97

Page 31: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

The QR factorization is unique: II

Suppose A has full column rank and

A = QR = Q′R′

are two QR factorizations of A. Then

(Q′)TQ = R′R−1

So the orthogonal matrix (Q′)TQ (products of orthogonal matricesare orthogonal) is upper triangular with positive diagonal. Thismeans all the eigenvalues of (Q′)TQ are 1 and (Q′)TQ is diagonal.So Q′ = Q and R′ = R.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 31 / 97

Page 32: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

QR by a sequence of orthogonal transformations

Suppose you have a sequence of M ×M orthogonal matrices{Qi}Ki=1 and

QK . . .Q1A = R′

where R′ is M × N upper triangular. Then

the upper N × N block of R′ is R

the left M × N block of

(QK . . .Q1)T is Q.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 32 / 97

Page 33: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Stability

Multiplication by orthogonal matrices is stable. The `2 norm doesnot grow. So

‖R‖2 = ‖R′‖2 = ‖QK . . .Q1A‖2. = ‖QTA‖2 = ‖A‖2.

For non-square M × N matrices

‖A‖ = max‖x‖=1

‖Ax‖.

As we’ll see, you’ll rarely store the Qs as full matrices.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 33 / 97

Page 34: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Householder Reflections

A Householder Reflection is

Pu = I− 2uuT

where u is a unit vector.Reflection means that Px is a reflection of x through thehyperplane

x⊥ = {z | zTu = 0}

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 34 / 97

Page 35: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

u = (−1, 1)T/√

2, x = (1/2, 1)t

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 35 / 97

Page 36: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Using Householder Reflections in QR

Simple idea: Given x ∈ RM find u so that

Px =

∗00...0

If we can do this, we can apply a sequence of Householders totransform A to upper triangular.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 36 / 97

Page 37: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

So what’s u?

What we need is

x− 2u(uTx) = ∓‖x‖2e1

How do I know what the right side has to be?So u is a linear combination of x and e1 and is parallel to

w = x± ‖x‖2e1 which means u =x± ‖x‖2e1‖x± ‖x‖2e1‖

How do I pick the ± sgn? Easy, don’t subtract.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 37 / 97

Page 38: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Formula for u

u =x + sign(x1)‖x‖2e1‖x + sign(x1)‖x‖2e1‖

where

sign(x) =

{1 if x ≥ 0−1 if x < 0

Cost: 3M to leading order.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 38 / 97

Page 39: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Don’t take my word for it.

Let x =(3 0 4

)T. The formula says

w = x + sign(x1)‖x‖2e1 = x + 5e1 =

804

and u = w/‖w‖ = w/

√80.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 39 / 97

Page 40: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Keep cranking . . .

Pu = I− 2uuT =1

10

−6 0 −80 1 0−8 0 6

and you can check that

Pux =

−500

.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 40 / 97

Page 41: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Applying Pu to a vector

DO NOT FORM THE MATRIX FOR Pu!!!Rather: to compute z = Pux

Compute uTx

Then z = x− 2(uTx)u

Total cost: 2M to leading order.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 41 / 97

Page 42: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Zeroing out part of a vector

Suppose you want u so that

(z)i = (Pux)i = 0 for M ≤ i ≤ k + 1

and you don’t care about the upper k entries of z.Then

you only need to compute and store a vector of size M − k + 1

and apply the reflection to the lower M − k + 1 entries of x.

Cost of Pu matrix multiply = 2(M − k) to leading order.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 42 / 97

Page 43: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Example

Objective: zero out the last entry in x =(1 3 4

)T.

Process:

Find a vector z ∈ R2 so that

Pz

(34

)=

(±− 5

0

)Then u =

(0 0 . . . zT

)T(if you need u)

To do a Pu matrix multiply you’ll only need z.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 43 / 97

Page 44: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Example Continued

For this example, let x =(3 4

)T,

z =x + 5e1√

80=

1√80

(84

)and

Pz = I−(−.6 −.8−.8 .6

)

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 44 / 97

Page 45: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Padding with zeros

So

u =

0z1z2

and Pu = I−

0 0 00 −.6 −.80 −.8 .6

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 45 / 97

Page 46: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

QR with Householders: A M × N , full rank

for j = 1 : N doCompute u ∈ RM−j+1 so that column Puaj has zeros in rowsj + 1 . . .N.Apply the Householder to A, keeping in mind that you don’thave to touch rows 1, . . . , j − 1.Store u

end for

At the end, you’ve overwritten the upper triangle of A with R.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 46 / 97

Page 47: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

What about the positive diagonal?

You may not get a positive diagonal at the end, but . . .

That’s does not matter for the solvers.

It’s easy to fix by replacing

QR with (QD)(DR)

where D is N × N diagonal with ±1 on the diagonal.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 47 / 97

Page 48: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Cost for Factorization

For each of the N columns you’ll need to

Compute u ∈ RM−j+1 for 3(M − j + 1) multiplies.

Inner loop:

Apply the Householder to each column of a matrix of size(N − j)(M − j + 1). Cost of 2(M − j + 1) for each of theN − j columns.

To leading order∫ N

12(N − j)(M − j + 1) dj = N2M − N3/3 + lower order

So if N = M, it’s twice an LU.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 48 / 97

Page 49: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Multiplying b by QT

IfQT = P1 . . .PN

where Pi = I− 2uiuTi and ui has i nonzeros. Then

PTb = PN . . .P1b

Cost ∫ N

12(M − j + 1) dj = 2MN − N2.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 49 / 97

Page 50: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Cost of Solve

Multiplication with QT .

Multiply b with the Pu’s in the reverse order

Trigangular solve with R: N2/2

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 50 / 97

Page 51: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

QR as a linear solver. M = N

Cost = 2N3/3

Double GEPP

Not worth the expense.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 51 / 97

Page 52: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Bottom Line on QR with Householders

Going through this is one way to prove QR factorization exists.

Storage: Same as A plus one vector of size N.

Householders process one column at a time.

Cost: MN2 − N3/3 (factorization); 2MN − N2/2 (solve)

Stability: no prolblem

Matlab: It’s in the backslash command.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 52 / 97

Page 53: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Givens Rotations

A 2× 2 Givens rotation is a matrix of the form

G =

(c −ss c

)where c = cos(θ), s = sin(θ) for θ ∈ [−π, π].

G is orthogonal

G rotates the unit vector(c −s

)Twhich makes an angle

−θ through an angle θ so that the y -component is 0

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 53 / 97

Page 54: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Why would we ever want such a thing?

G

(c−s

)=

(10

).

An M ×M Givens rotation replaces row and column i and j in theM ×M identity with the 2× 2 Givens rotation in row i and columnj .

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 54 / 97

Page 55: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

M ×M Givens rotation example

Here j = i + 1

G(i , i + 1, θ) =

1 0 . . . 0

0. . .

. . .. . . c −s

... s c 0...

0 1. . .

. . .. . . 0

0 . . . 0 1

.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 55 / 97

Page 56: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

4× 4 example: i = 2, j = 4

G(2, 4, θ) =

1 0 0 00 c 0 −s0 0 1 00 s 0 c

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 56 / 97

Page 57: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Using Givens in QR

To rotate (x1x2

)into

(√x21 + x22

0

)we will need

c = x1/√

x21 + x22 and s = −x2/√

x21 + x22

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 57 / 97

Page 58: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Never take my word for anything.

Gx =

(cx1 − sx2sx1 + cx2

)=

x21+x22√x21+x22−x1x2+x1x2√x21+x22

=

(√x21 + x22

0

)

as asserted.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 58 / 97

Page 59: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Sparse QR

Suppose A has only a few zeros in column 1, say in row 1 (which isok) and rows 1 < k < l < m.

Apply G(1,m, θ) to zero out am,1.

Apply G(1, l , θ) to zero out al ,1.

Apply G(1, k , θ) to zero out ak,1.

So you have a sparse QR using orthogonal transformations.Note: cost is application of G to every column and the appropriatetwo rows.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 59 / 97

Page 60: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Cost of Givens

Move along the columns in QR

Elimination of a row in column j requires application of onerotation for each non-zero at a cost per column of

2 to compute c and s and populate the rotation (outer loop)+ and * do not balance, but a count of 2 is close enough4 to do the matrix vector product (inner loop)

4(M − j + 1) for each nonzero below row j in column j .

If the A is dense, then the total is, to leading order,∫ N

14(N − j)(M − j + 1) dj = 2(N2M − N3/3)

which is twice Householder.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 60 / 97

Page 61: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

When is Givens happy?

Answer, where there are very few zeros below the diagonal.Simple example:

A =

1 20 31 0

So set c = −s = 1

√2 and then

A1 = GA =

c 0 −s0 1 0s 0 c

A =

2√

20 3

0 −√

2

So we’ve added a zero in the next column. Gotta fix that.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 61 / 97

Page 62: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Killing the last non-zero

So now we will use i = 2 and j = 3, to avoid messing with row 1.

c2 = 3/√

11; s2 =√

2/√

11

so

A2 = G2A1 =

1 0 00 c2 −s20 s2 c2

A =

√2√

20 3.31660 0

and we did our job.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 62 / 97

Page 63: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

A sparse QR code is hard to write

You’ll have to

Keep track of fill-in in the upcoming columns

Reorder rows/columns to minimize that fill-in

Carefully store the c’s, s’s, and the history of the row/columninterchanges

Bottom line: use tools! Do not try this at home.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 63 / 97

Page 64: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

But you could do this at home.

Suppose A is N + 1× N upper Hessenberg. That means

aij = 0 if i > j + 1.

Only the subdiagonal has non-zeros. Like this matrix

A =

1 23 40 5

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 64 / 97

Page 65: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Upper Hessenberg QR with Givens

for j=1:N doApply G (j − 1, j , θ) to zero out aj ,j+1

end for

The application of G (j − 1, j) to A does not affect the previouscolumns because you’ll only be multiplying by zero.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 65 / 97

Page 66: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Cost of Upper Hessenberg QR with Givens

At column j you

Build G for cost of 2 (outer loop)

Apply G to rows j and j + 1 for all columns j . . .N.Cost = 4 per column

To leading order ∫ N

14(N − j) dj = 2N2

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 66 / 97

Page 67: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Example

A =

1 23 40 5

We’ll zero out a1,2 = 3 first.

c1 = 1/√

10; s1 = −3/sqrt10;

A1 = G1A =

c1 −s1 0s1 c1 00 0 1

A =

10 1.4/√

10

0 −√.4

0 5

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 67 / 97

Page 68: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Now it’s time to zero out a2,3 = 5

c2 = −.12549; s2 = −.99209

and we have it

G2A1 =

3.1623 4.42720 5.03980 0

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 68 / 97

Page 69: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Bottom Line on Givens

Designed for sparse problems

2× cost of Householder for dense problems

The method of choice for upper Hessenberg

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 69 / 97

Page 70: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Gram-Schmidt Orthogonalization

We will do a QR factorization by brute force.

for j = 1 : N dow = aj

for i = 1 : j − 1 dorij = qT

i aj

end forfor i = 1 : j − 1 do

w = w − rijqi

end forrjj = ‖w‖2; qj = w/‖w‖2

end for

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 70 / 97

Page 71: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

I know you don’t believe me. Here’s an example.

A =

1 21 31 4

So

q1 =1√3

111

and r11 =√

3

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 71 / 97

Page 72: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

There’s more

So

r12 = aT2 q1 =

9√3

and

w = a2 − r12q1 =

234

−3

33

=

−101

So r22 =

√2 and q2 = w/

√2

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 72 / 97

Page 73: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Bottom Line

Q =

1√3−1/√

21√3

01√3

1/√

2

and R =

(√3 3√

3

0√

2

)

So (check it out) QR = A.GS is the way to go when computing by hand.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 73 / 97

Page 74: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Some Matlab

We just did classical Gram-Schmidt

function [q,r]=gram_easy(a)

% GRAM_EASY compute QR via Gram -Schmidt orthogonalization

% Classical version with loops

%

[m,n]=size(a);

%

% Store q and r. I’m storing r as a full matrix.

%

q=zeros(m,n); r=zeros(n,n);

for j=1:n

w=a(:,j);

for i=1:j-1

r(i,j)=a(:,j)’*q(:,i);

end

for i=1:j-1

w=w-r(i,j)*q(:,i);

end

r(j,j)=norm(w);

q(:,j)=w/norm(w);

end

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 74 / 97

Page 75: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

We can vectorize this to death.

For each j , the scalar products aTj qi can be done in parallel.

So we can replace the first for loop with one line:

r(1:j-1,j)=q(:,1:j-1) ’*a(:,j);

Same story with the second loop

w=a(:,j) - q(:,1:j-1)*r(1:j-1,j);

Let’s think about this.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 75 / 97

Page 76: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Before and after; first loop

Before:

for i=1:j-1

r(i,j)=a(:,j) ’*q(:,i);

end

After:

r(1:j-1,j)=q(:,1:j-1) ’*a(:,j);

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 76 / 97

Page 77: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Before and after; second loop

Before:

w=a(:,j);

for i=1:j-1

w=w-r(i,j)*q(:,i);

end

After:

w = a(:,j) - q(:,1:j-1)*r(1:j-1,j);

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 77 / 97

Page 78: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Vectorized Classical Gram-Schmidt (CGS)

function [q,r]=class_gram(a)

% CLASS_GRAM compute QR via Gram -Schmidt

orthogonalization

% Vectorized classical version

%

%function [q,r]= class_gram(a)

[m,n]=size(a);

% Store q and r. I’m storing r as a full matrix.

q=zeros(m,n); r=zeros(n,n);

for j=1:n

r(1:j-1,j)=q(:,1:j-1) ’*a(:,j);

w = a(:,j) - q(:,1:j-1)*r(1:j-1,j);

r(j,j)=norm(w);

q(:,j)=w/norm(w);

end

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 78 / 97

Page 79: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Advantages of CGS

Easy to vectorize/parallelize

MATLAB automatically uses your multi-core computercorrectly

If the vectors are long and there are a lot of them,you get almost perfect parallelism.

There’s only one tiny problem . . .

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 79 / 97

Page 80: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Stability of CGS

Here’s a matrix

A =

1 1 110−7 10−7 010−7 0 10−7

As Matlab for QR, look at ‖QTQ− I‖ and ‖QQ− A‖ and get

>> [qm,rm]=qr(A);

>> [norm(qm*rm-A) norm(qm’*qm - eye(3))]

ans =

3.8459e-16 4.4409e-16

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 80 / 97

Page 81: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

On the other hand . . .

Let’s give CGS a shot!

>> [qc,rc]=class_gram(A);

>> [norm(qc’*qc-eye(3)) norm(qc*rc-A)]

ans =

7.9928e-04 0

That’s bad. What happened?

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 81 / 97

Page 82: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

What about Q?

>> qc

qc =

1.0000e+00 9.9920e-08 9.9920e-08

1.0000e-07 9.9262e-15 -1.0000e+00

1.0000e-07 -1.0000e+00 -7.9928e-04

This is not an orthogonal matrix.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 82 / 97

Page 83: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

You’re dead, of course

>> b = A*ones(3,1);

>> [rm\(qm’*b) rc\(qc’*b)]

ans =

1.0000e+00 1.0056e+00

1.0000e+00 9.9840e-01

1.0000e+00 9.9600e-01

>> [norm(rm\(qm’*b) - ones(3,1)) norm(rc\(qc’*b) - ones(3,1))]

ans =

9.2222e-16 7.0632e-03

You should expect this, since QT is far from Q−1.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 83 / 97

Page 84: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

What happened?

CGS is not QR via a sequence of orthogonal transformations,

unlike Householder or Givens,

and it’s unstable.

You see the instability by loss of orthogonalityTremble in fear!x = R−1QTb is wrong because QT 6= Q−1.

How can we fix this mess?

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 84 / 97

Page 85: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Gram-Schmidt Orthogonalization Twice

for j = 1 : N dow = ajfor i = 1 : j − 1 do

rij = qTi wend forfor i = 1 : j − 1 do

w = w − rijqiend forfor i = 1 : j − 1 do

r1ij = qTi w

end forfor i = 1 : j − 1 do

w = w − r1ij qiend forfor i = 1 : j − 1 do

rij = rij + r1ijend forrjj = ‖w‖2; qj = w/‖w‖2

end for

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 85 / 97

Page 86: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

CGS2 in Matlab

function [q,r]=cgs2(a)

% CGS2 compute QR via Gram-Schmidt orthogonalization twice

% Vectorized classical version; reorthogonalize!

%

%function [q,r]=cgs2(a)

%

[m,n]=size(a);

%

% Store q and r. I’m storing r as a full matrix.

%

q=zeros(m,n); r=zeros(n,n); r1=zeros(n,n);

for j=1:n

r(1:j-1,j)=q(:,1:j-1)’*a(:,j);

w = a(:,j) - q(:,1:j-1)*r(1:j-1,j);

% one more time

r1(1:j-1,j)=q(:,1:j-1)’*w;

w = w - q(:,1:j-1)*r1(1:j-1,j);

r(1:j-1,:)= r(1:j-1,:) + r1(1:j-1,:);

r(j,j)=norm(w);

q(:,j)=w/norm(w);

end

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 86 / 97

Page 87: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Testing . . .

>> [q2,r2]=cgs2(A);

>> [norm(q2’*q2-eye(3)) norm(q2*r2-A) norm(r2\(q2’*b) - ones(3,1))]

ans =

2.2204e-16 0 1.0175e-15

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 87 / 97

Page 88: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Why I love CGS2

Cures the stability problem.

Twice the floating point work.But that’s not an issue if you have a two-core computer.

Easy to code. Easy to understand.

Great if M and N are large.

But Tim, what if I have a single-core computer?

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 88 / 97

Page 89: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Modified Gram-Schmidt (MGS)

Here’s a somewhat more stable version of GS.

for j = 1 : N dow = aj

for i = 1 : j − 1 dorij = qT

i ww = w − rijqi

end forrjj = ‖w‖2; qj = w/‖w‖2

end for

Can you see the difference? You should verify that MGS isequivalent to CGS in exact arithmetic.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 89 / 97

Page 90: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

More on MGS

Equivalent to CGS in exact arithmetic.

I defy you to vectorize this algorithm.

More stable than CGS, but it does not solve all the problems.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 90 / 97

Page 91: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Matlab for MGS

function [q,r]=mgs(a)

% MGS compute QR via modified Gram -Schmidt

%function [q,r]=mgs(a)

%

[m,n]=size(a);

% Store q and r. I’m storing r as a full matrix.

q=zeros(m,n); r=zeros(n,n);

for j=1:n

w=a(:,j);

for i=1:j-1

r(i,j)=q(:,i) ’*w;

w = w - r(i,j)*q(:,i);

end

r(j,j)=norm(w); q(:,j)=w/norm(w);

end

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 91 / 97

Page 92: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Let’s test with the problematic matrix.

>> [qmg,rmg]=mgs(A);

>> [norm(qmg’*qmg-eye(3)) norm(qmg*rmg-A) norm(rmg\(qmg’*b) - ones(3,1))]

>> ans =

1.1304e-10 0 5.8735e-03

Wow! Q′ ∗Q is much better, but the answer still stinks!

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 92 / 97

Page 93: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

One last trick.

We can test for loss of orthogonality. If, at column j ,

‖aj‖2 + δ‖w‖2 = ‖aj‖2

to working precision, then we reorthogonalize.

We use δ = 10−3 later in the course. Does no good here.

Use δ = 10−9 and reorthogonalize 2 of 3 times. Works.

No guarantees!!

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 93 / 97

Page 94: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Bottom line on Reorthogonalization

Method ‖QTQ − I‖ ‖QR − A‖ ‖R−1QTb − x∗‖Matlab 4.44e-16 3.85e-16 9.22e-16CGS 7.99e-04 0.00e+00 7.06e-03CGS2 2.22e-16 0.00e+00 1.02e-15MGS 1.13e-10 0.00e+00 5.87e-03MGS2 2.22e-16 0.00e+00 1.02e-15

Tim likes CGS2.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 94 / 97

Page 95: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Geometry

If A is M × N, M ≥ N, full column rank.

x∗ = R−1QTb is the unique solution of

min ‖Ax− b‖

It’s the matlab backslash

x = A\b;

QQT is the orthogonal projection onto Ran(A)

Distance of b from Ran(A) is ‖(I−QQT )b‖

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 95 / 97

Page 96: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Bottom line on QR

Don’t write a QR code at home.

Householder for dense, Givens for sparse.

Gram-Schmidt for hand computations.

Also good when you get one column of A at a time.Watch out for loss of orthogonality.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 96 / 97

Page 97: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

What you should remember how to do.

By hand:

Build small examples of Householder and GivensSolve small problemsDo proofs of fewer than ten lines.

On the job

Know how to communicate with the tools.Know what you’re getting at the end.

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 97 / 97

Page 98: MA 580; Linear Least Squares - Nc State UniversityPart Va: Linear Least Squares, QR Solving Linear Least Squares Problems with QR If Q is M N with orthonormal columns, then QTQ = I,

Part Va: Linear Least Squares, QR

Questions?

What is QR for

A =

123

,A = 3,A = −3,A = 1,A = I

c©C. T. Kelley, I. C. F. Ipsen, 2016 Part Va: Linear Least Squares, QR MA 580, Fall 2016 98 / 97