Home > Software engineering >  Should one calculate QR decomposition before Least Squares to speed up the process?
Should one calculate QR decomposition before Least Squares to speed up the process?

Time:11-07

I am reading the book "Introduction to linear algebra" by Gilbert Strang. The section is called "Orthonormal Bases and Gram-Schmidt". The author several times emphasised the fact that with orthonormal basis it's very easy and fast to calculate Least Squares solution, since Qᵀ*Q = I, where Q is a design matrix with orthonormal basis. So your equation becomes x̂ = Qᵀb.

And I got the impression that it's a good idea to every time calculate QR decomposition before applying Least Squares. But later I figured out time complexity for QR decomposition and it turned out to be that calculating QR decomposition and after that applying Least Squares is more expensive than regular x̂ = inv(AᵀA)Aᵀb.

  1. Is that right that there is no point in using QR decomposition to speed up Least Squares? Or maybe I got something wrong?

  2. So the only purpose of QR decomposition regarding Least Squares is numerical stability?

CodePudding user response:

There are many ways to do least squares; typically these vary in applicability, accuracy and speed.

Perhaps the Rolls-Royce method is to use SVD. This can be used to solve under-determined (fewer obs than states) and singular systems (where A'*A is not invertible) and is very accurate. It is also the slowest.

QR can only be used to solve non-singular systems (that is we must have A'*A invertible, ie A must be of full rank), and though perhaps not as accurate as SVD is also a good deal faster.

The normal equations ie

compute P = A'*A
solve P*x = A'*b

is the fastest (perhaps by a large margin if P can be computed efficiently, for example if A is sparse) but is also the least accurate. This too can only be used to solve non singular systems.

Inaccuracy should not be taken lightly nor dismissed as some academic fanciness. If you happen to know that the problems ypu will be solving are nicely behaved, then it might well be fine to use an inaccurate method. But otherwise the inaccurate routine might well fail (ie say there is no solution when there is, or worse come up with a totally bogus answer).

I'm a but confused that you seem to be suggesting forming and solving the normal equations after performing the QR decomposition. The usual way to use QR in least squares is, if A is nObs x nStates:

    decompose A as A = Q*(R ) 
                         (0 )
    transform b into b~ = Q'*b
    (here R is upper triangular)
    solve R * x =  b# for x, 
    (here b# is the first nStates entries of b~)
  • Related