Least squares, and the road not to take

Where QR pays the square

The road through the normal equations squares the condition number, and the road through QR keeps every step orthogonal, so the condition number is untouched. That is true of the triangular system QR ends in, and it is not a statement about the answer. Measured against the exact solution of the stored data, QR's error in a least-squares solution goes as the square of the condition number too, once the data lie well away from the column space: 2·10⁻⁴ at κ = 10⁷ and a tangent of one, five orders above the condition number's own reading. The residual does not pay it. And the normal equations' famous disadvantage is seven decades on a consistent system, one decade at a tangent of one, and nothing at all at a thousand.

Worth reading first: The projection and the right angle · The road that squares the problem · Orthogonal is a number.

The projection and the right angle described two roads from the one condition that defines a least-squares solution, ATr=0A^{\mathsf T}r = 0. Expanded, it is the normal equations, ATAx=ATbA^{\mathsf T}Ax = A^{\mathsf T}b, and “forming ATAA^{\mathsf T}A is the single worst thing that can be done to this problem”, since a condition number of a million becomes 101210^{12} and half of the digits have gone before the solve begins. The other road factors A=QRA = QR and solves Rx=QTbRx = Q^{\mathsf T}b, a triangular system with κ(R)=κ(A)\kappa(R) = \kappa(A). “The condition number is untouched, because every step was orthogonal and orthogonal steps have κ=1\kappa = 1.”

That is a correct statement about RR. The reading it invites is a statement about the answer, that the error in the solution from QR is a modest multiple of the unit roundoff times κ\kappa. Wedin’s perturbation theory has said otherwise since 1973, and the textbooks say so in a line: a least-squares solution’s sensitivity to the matrix is κ+κ2tan⁡θ\kappa + \kappa^2\tan\theta, where θ\theta is the angle between bb and the column space, whichever algorithm is used. The measurement here is about how that looks in practice and what it does to the comparison of the two roads, for which the earlier essay gave one verdict without looking at the angle.

A turned column moves the foot

The picture for why this can be true needs only one column.

A vector, its projection onto a line through the origin, and the projection after the line is turned by a small angle: the foot moves by the turn times the length of the vectorWith the residual long, the foot of the perpendicular moves along the line by about the angle of the turn times the length of the residual. With the vector on the line, a small turn moves the foot only to second order.bfootfoot after the turnthe columnturnedthe residual is the levera turn moves the foot by the residual's length
Fig. 1 A vector, its projection onto a line through the origin, and the projection after the line is turned by a small angle. The foot moves by about the turn times the length of the perpendicular.

The least-squares solution is where the perpendicular from bb meets the column space. Turn the column by a small angle δ\delta, which is what a perturbation of AA does. If bb was lying on the column, the foot barely moves. If bb stands well off it, the perpendicular has length ∥r∥\|r\|, and turning the line moves the foot by about δ∥r∥\delta\|r\|. So the residual is a lever, and its length multiplies whatever turns the column.

With several columns, one of the directions in the column space is thin, of size σmin⁡\sigma_{\min}. A perturbation of size u∥A∥u\|A\| can turn that direction by as much as uκu\kappa, since a small perturbation of a thin direction is a large change of angle. And the foot’s movement along that direction, which has length δ∥r∥\delta\|r\|, has to be expressed in xx by dividing again by σmin⁡\sigma_{\min}. That gives the error in xx a size of uκ⋅∥r∥/σmin⁡u\kappa \cdot \|r\|/\sigma_{\min}, and that is uκ2∥r∥/∥A∥u\kappa^2\|r\|/\|A\|. Neither factor of κ\kappa arises from the arithmetic of any particular route. They are both in the problem. QR is not exempt, because nothing about orthogonal steps prevents a backward error from turning a thin direction, since the backward error is small only relative to the whole matrix.

Scoring against the stored data

To measure this properly, the reference has to be right, and that takes more care here than it usually does. A designed problem b=Ax+rb = Ax + r has a known xx only until AA has been rounded into its stored form. Past that point the exact solution of the stored problem differs from the designed xx by precisely the term under test: κ2tan⁡θ\kappa^2\tan\theta times the rounding in AA. A designed solution as reference would show a square for QR on every problem, for a reason that has nothing to do with QR.

So the reference here is the exact solution of the doubles that were stored. Every double is an integer over a power of two, so the normal equations of those integers can be solved by fraction-free elimination in big integers, with no rounding anywhere, and the result converted to a double at the end. It is the first time here that a route’s error can be read with the problem’s own rounding removed from the reckoning, and the method is one the exact field developed for other reasons.

The problems are of order 24 by 6, with singular values geometric from one to 1/κ1/\kappa, singular vectors random, and κ\kappa from 10 to 10710^7. Five draws are taken at each setting and the median is reported. Five angles are used: a consistent system, and tan⁡θ\tan\theta of 10−610^{-6}, 10−310^{-3}, 1 and 1,000. The routes are Householder QR ending in a triangular solve, and the normal equations solved by Cholesky.

The relative error of a least-squares solution from QR and from the normal equations against the condition number, at a tangent of 1 between the data and the column space, measured against the exact solution of the stored dataAt a tangent of 1, QR: 6e-16, 1e-14, 2e-12, 1e-10, 1e-8, 5e-7, 2e-4. Normal equations: 3e-15, 1e-13, 2e-11, 2e-9, 9e-8, 1e-5, 2e-3, at condition numbers from ten to ten million. Fitted slope for QR: 1.98.10¹10²10³10⁴10⁵10⁶10⁷10⁻¹⁷10⁻¹⁵10⁻¹³10⁻¹¹10⁻⁹10⁻⁷10⁻⁵10⁻³10⁻¹condition numberrelative error of the solutionnormal equationsQRu·κtan θ = 1 · 24 × 6 · five drawsagainst exact arithmetic, on the stored data
Fig. 2 The relative error of a least-squares solution from QR and from the normal equations against the condition number, measured against the exact solution of the stored data, with the unit roundoff times the condition number. The dial sets the tangent of the angle.

At a tangent of one, QR’s error runs from 6⋅10−166 \cdot 10^{-16} at κ=10\kappa = 10 through 2⋅10−122 \cdot 10^{-12} at 10310^3 and 10−810^{-8} at 10510^5 to 2⋅10−42 \cdot 10^{-4} at 10710^7. The fitted slope is 1.98. Those are the digits of a method that squares the condition number, and the dotted line that says what “the condition number is untouched” would predict is five decades below at the top. Turn the dial to a consistent system and it comes down to that line, with a slope of 0.97 and an error of 2⋅10−102 \cdot 10^{-10} at κ=107\kappa = 10^7. QR is exactly what the earlier essay said on that setting.

Then the intermediate tangent shows the two terms handing over, and the formula says where. At a tangent of 10−310^{-3} the two terms in κ+κ2tan⁡θ\kappa + \kappa^2\tan\theta are equal at κ=1/tan⁡θ=103\kappa = 1/\tan\theta = 10^3. The measurement agrees: QR’s error is 3⋅10−153 \cdot 10^{-15} at 10210^2 and 10−1310^{-13} at 10410^4, still near the first term, then 5⋅10−105 \cdot 10^{-10} at 10610^6 and 2⋅10−72 \cdot 10^{-7} at 10710^7, on the way up the second. The overall fitted slope across this tangent is 1.6, which is a straight line drawn through a corner.

What the normal equations cost, now

The road through ATAA^{\mathsf T}A has its error at slope two whatever the angle: 3⋅10−33 \cdot 10^{-3} at κ=107\kappa = 10^7 on a consistent system, 2⋅10−32 \cdot 10^{-3} at a tangent of one, and 2⋅10−12 \cdot 10^{-1} at a thousand. So it changes very little as the angle changes, while QR’s rises to meet it.

How many times QR's error the normal equations' error is, against the tangent of the angle between the data and the column space, at three condition numbersat 1000: 9.5e+2, 4.9e+3, 1.5e+1, 1.0e+0; at 100000: 4.6e+5, 1.3e+4, 6.9e+0, 1.2e+0; at 10000000: 2.5e+6, 4.0e+3, 1.1e+1, 8.7e-1 at tangents 0.000001, 0.001, 1, 1000.10⁻⁶10⁻⁵10⁻⁴10⁻³10⁻²10⁻¹110¹10²10³10⁻¹110¹10²10³10⁴10⁵10⁶10⁷10⁸tan θnormal equations' error over QR'sκ = 10³κ = 10⁵κ = 10⁷solution random · five drawsthe road only squares what is not squared
Fig. 3 How many times QR’s error the normal equations’ error is, against the tangent of the angle, at three condition numbers. At a tangent of a thousand the routes are equal.

The disadvantage is a function of the angle, and the range is enormous. At κ=107\kappa = 10^7 the normal equations’ error is 1.5⋅1071.5 \cdot 10^7 times QR’s on a consistent system, 2.5⋅1062.5 \cdot 10^6 at a tangent of 10−610^{-6}, 4,000 at 10−310^{-3}, eleven at one, and 0.87 at a thousand. At the last of those the normal equations are, by a small margin that is within the scatter, the more accurate road. The road that squares the problem measured its factor at a consistent system and in the setting of the seminormal equations, where this gap is widest. In the last column of the figure the phrase “the single worst thing” has no content, because the road being compared with it has its own square to pay.

That matters in actual practice, because a tangent of order one is very common. A fit of data with noise in it has a residual as large as the noise, and if a model explains half the variance in the data the tangent is one. For that data, and for a matrix whose condition number is a thousand, the normal equations cost a factor of about fifteen, rather than the million that the rule of thumb suggests. Fifteen is still worth avoiding, and what it would take to do so costs little. But the advice’s justification is a different thing from what is usually said, and a person who has profiled a solver and needs a tenfold speed-up should know that this is a factor of ten in error on their problem and not of a million.

A code can read the gap in advance

The figure’s shape has a formula, and the formula uses only two numbers a code already holds after any solve. The normal equations’ error goes as κ2(1+tan⁡θ)\kappa^2(1 + \tan\theta): the square of the condition number from forming the matrix, and the same square on the right-hand side’s large part. QR’s goes as κ+κ2tan⁡θ\kappa + \kappa^2\tan\theta. Their ratio is κ(1+tan⁡θ)/(1+κtan⁡θ)\kappa(1 + \tan\theta)/(1 + \kappa\tan\theta), and that is the factor the choice of road is worth.

Over the twelve settings measured, from a gap of 950 to millions and down to about one, that ratio predicts the measured factor to within about a factor of twenty, mostly nearer. It is a statement about orders of magnitude, in a measurement of five draws per setting, and that is what is wanted from it. The formula has three regimes that a reader can keep in mind. Below a tangent of 1/κ1/\kappa the gap is κ\kappa itself, the rule of thumb in its full strength, a million at κ=106\kappa = 10^6. Between 1/κ1/\kappa and one the gap is 1/tan⁡θ1/\tan\theta, so it depends on the angle and not the matrix: a tangent of 10−310^{-3} gives about a thousand at any condition number that is well above that. And past one the gap is one.

The middle regime is the unfamiliar one. A code with a residual of about a thousandth of its data is at a gap of about a thousand, whether κ\kappa is 10410^4 or 101010^{10}, and the size of the condition number has dropped out of the comparison altogether. The tangent is cheap to read: ∥r∥/∥Ax∥\|r\|/\|Ax\| is available from the solution in hand. An estimate of κ\kappa is available from RR for a few more products, as the condition estimators show. So a code that has solved once can report what the other road would have cost it, or whether it can move to the cheaper one, from numbers it already has.

The residual never pays

At a tangent of one, QR's relative error in the solution and in the residual against the condition number, with the unit roundoff times the condition number and its squareThe solution: 6e-16, 1e-14, 2e-12, 1e-10, 1e-8, 5e-7, 2e-4. The residual: 3e-16, 6e-16, 4e-15, 4e-14, 6e-13, 3e-12, 7e-11. Fitted slopes 1.98 and 1.00.10¹10²10³10⁴10⁵10⁶10⁷10⁻¹⁷10⁻¹⁵10⁻¹³10⁻¹¹10⁻⁹10⁻⁷10⁻⁵10⁻³10⁻¹condition numberrelative errorthe solutionthe residualu·κ²u·κtan θ = 1 · solution randomthe residual never pays the square
Fig. 4 At a tangent of one, QR’s relative error in the solution and in the residual against the condition number, with the unit roundoff times the condition number and its square.

The residual is another matter entirely. Wedin’s bound on it has no square: κ\kappa alone, because the residual is a projection, and a turn of the column by δ\delta moves the projected vector by about δ\delta times its size whatever the thin direction does. In the measurement, at a tangent of one, the residual’s relative error runs from 3⋅10−163 \cdot 10^{-16} to 7⋅10−117 \cdot 10^{-11}, with a fitted slope of 1.00, and it lies below the line for uκu\kappa. At κ=107\kappa = 10^7 the solution’s error is more than a million times the residual’s.

So the same computed problem has its residual right to ten digits and its solution right to four. That goes against an instinct formed by the linear-system case, in which a small residual gives no assurance about the solution in the other direction. Here both are computed by a stable method and each is as good as its problem allows, and the two problems have different condition numbers. A code printing a residual for a poorly conditioned fit with a large residual is printing the good number. And the worst residual belongs to the route is about the separate case in which a route loses the residual’s accuracy for reasons of its own.

And the square needs a small solution

Everything above uses a problem with a random solution. A collection this size has already shown how much that choice can matter: the test that made the answer small found that building bb from a random xx picks out the smallest answer a problem can have. The same thing happens here. A second family of problems uses the same matrices and angles, but with the fit drawn at random in the column space rather than the solution drawn at random.

At a tangent of one, QR's relative error in the solution against the condition number, for a random solution and for a fit drawn at random in the column spaceA random solution: 6e-16, 1e-14, 2e-12, 1e-10, 1e-8, 5e-7, 2e-4. A random fit: 4e-16, 1e-15, 3e-15, 1e-13, 2e-12, 1e-11, 1e-10.10¹10²10³10⁴10⁵10⁶10⁷10⁻¹⁷10⁻¹⁵10⁻¹³10⁻¹¹10⁻⁹10⁻⁷10⁻⁵10⁻³condition numberrelative error of the solutionthe solution randomthe fit randomtan θ = 1 · both by QRa long solution absorbs one κ
Fig. 5 At a tangent of one, QR’s relative error in the solution against the condition number, for a random solution and for a fit drawn at random in the column space.

With a random fit, the thin directions get about the same share of the fit as any other, so the solution has a component of size about κ\kappa along the thinnest. A solution that long absorbs one factor of κ\kappa in its own relative error. QR’s error at κ=107\kappa = 10^7 and a tangent of one falls from 2⋅10−42 \cdot 10^{-4} to 10−1010^{-10}, with a slope of 1.05. At a tangent of a thousand the slope is still 1.05.

The fitted slope of QR's error in the solution against the condition number at five angles, for two kinds of problemthe solution random: 0.97, 0.97, 1.60, 1.98, 1.96; the fit random, so the solution is large: 0.87, 0.87, 0.88, 1.05, 1.05 at tangents 0, 0.000001, 0.001, 1, 1000.00.511.52the solution randomtan θ = 00.97tan θ = 10⁻⁶0.97tan θ = 10⁻³1.60tan θ = 11.98tan θ = 10³1.96the fit random, so the solution is largetan θ = 00.87tan θ = 10⁻⁶0.87tan θ = 10⁻³0.88tan θ = 11.05tan θ = 10³1.05slope of the error against κ, 10² to 10⁷one is κ · two is κ²the square needs a small solution
Fig. 6 The fitted slope of QR’s error in the solution against the condition number at five angles, for two kinds of problem.

The same observation was made about right-hand sides in the road that squares the problem, in the setting of a seminormal route. What is added here is that it controls whether QR itself pays. The condition number with its correct factor reads κ+κ2tan⁡θ/η\kappa + \kappa^2\tan\theta/\eta, where η\eta is ∥A∥∥x∥/∥Ax∥\|A\|\|x\|/\|Ax\|, which runs from one for a solution on the thick directions to κ\kappa for one on the thin. The square is real only for a solution that does not lean on the thin directions. A solution that does has already been declared large, and the relative error is measured against that large size. It is not that QR has become better there; the absolute error is the same, and the denominator has grown.

This leaves a question for anyone holding a real fit. A polynomial basis with a smooth target gives a solution with moderate coefficients, which lies at the small-xx end and pays the full square, and so does anything in which the true parameters are of a sensible size. The valley with no bottom is about a fit of that kind, and its coefficient digits are what this essay’s error is measuring, while a basis built from the points removes the κ\kappa and with it this whole effect.

What the two roads now are

The rule that survives is narrower than the one stated at the top. Prefer QR over the normal equations when the residual is small, because there it is the difference between κ\kappa and κ2\kappa^2. When the residual is comparable to the data the gap has mostly closed, and it should be decided on other grounds: the cost of QR is twice that of forming ATAA^{\mathsf T}A on a tall matrix; the normal equations cannot handle κ\kappa above 1/u1/\sqrt{u} at all, where they fail with a Cholesky breakdown rather than an error, and that was not reached here. Those are good reasons and they remain.

The phrase “orthogonal steps have κ=1\kappa = 1” needs the same narrowing. Orthogonal steps do not amplify what they are given. So the backward error of a QR solve is small, a few units of roundoff in AA and bb, and that is what QR’s stability actually means. It does not mean that an answer from a small backward error is accurate. Whose fault it is is a question to ask of every route, and here its answer is plain. The digits lost at κ=107\kappa = 10^7 and a tangent of one are not lost by QR. They are the problem’s, and QR declines to lose any more of them.

Where these numbers come from

Twenty-four by six is small and five draws are few, and the small size is what makes exact arithmetic affordable. The singular values are geometric and the vectors random, so the matrix has no structure that a structured problem would. The constant in front of the square, about 1/50 of uκ2u\kappa^2 at a tangent of one, depends on the random vectors’ alignment and on the order. The slope is the measured thing here. No route with iterative refinement or with a corrected seminormal step has been measured here, and refinement is known to change the second term. And the condition numbers stop at 10710^7, below the point at which the normal equations fail outright, so the comparison is not of the full range of either route.

Still open: the refinement that cannot fix it, and a larger system with the same angle

Refinement in the same precision. One step of refinement using the residual reduces a linear solve’s error to its forward limit. The prediction with a sign is that on these problems, with a tangent of one and a random solution, one or two steps of fixed-precision refinement do not lower QR’s error at κ=107\kappa = 10^7 by more than a factor of three, because the error is in the problem’s stored data and not in the factorisation, whereas with the residual computed in double the working precision the term falls by a factor of at least 10410^4, since then the stored data are being refined against rather than absorbed.

A larger problem. The prediction is that from 24 by 6 to 200 by 50, at the same κ\kappa and tangent, QR’s error rises by no more than a factor of ten, and that the slope stays above 1.9, so that the square is not a small-matrix effect. If the constant grows like mn\sqrt{mn}, which the backward error of Householder QR allows, it will show up there and not in anything measured so far.

What links here

Computed from the collection, not written here: the essays that point at this one.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A flat tag is an object no other essay names yet.

Condition numberExact ground truthLeast squaresNormal equationsOrthogonal projectionQR factorisationResidual