Least squares, and the road not to take

Refining the solution alone refines nothing

QR's least-squares error at a tangent of one grows as the square of the condition number, against the exact solution of the stored data, and the prediction was that refinement in working precision would not help while a residual computed in twice the precision would take the term down by four decades. The first half holds. The second fails completely: refining the solution with an exactly computed residual leaves the error at 1.8 times ten to the minus four at a condition of ten to the seven, unchanged in every digit shown, and the slope stays at 1.95. The same exact residual used to refine the residual and the solution together, through the augmented system and the same QR, takes it to ten to the minus fourteen in one step and to the exact solution in two.

Worth reading first: The projection and the right angle · Orthogonal is a number · Buying the accuracy back.

Where QR pays the square measured Householder QR’s least-squares solution against the exact solution of the stored data, which fraction-free elimination over the integers can compute, and found that QR’s error grows as the square of the condition number whenever the data lie at an angle to the column space and the solution is ordinary in size: a slope of 1.98 at a tangent of one. Wedin’s perturbation theory says that term belongs to the problem and not the method, κ2tan⁡θ\kappa^2 \tan \theta times the roundoff, and the measurement confirmed that no algorithm working in binary64 on the stored data escapes it.

It also found that the residual, unlike the solution, is computed with an error of slope one, uκu\kappa, at every angle: QR returns an accurate residual and an inaccurate solution from the same factors. The projection and the right angle had set the two quantities side by side as the foot of a perpendicular and the perpendicular itself, and the measurement says the factorisation knows the second far better than the first.

That essay ended on the obvious repair. For a square linear system, iterative refinement, computing the residual b−Axb - Ax and solving for a correction with the factorisation already in hand, reduces the error to its forward limit, and with the residual computed in higher precision it reduces it further. The prediction with a sign was in two parts: that one or two steps of refinement in working precision would not lower QR’s error at κ=107\kappa = 10^7 by more than a factor of three, because the error is in the stored data and not in the factorisation; and that with the residual computed in twice the working precision the κ2\kappa^2 term would fall by a factor of at least 10410^4, since then the stored data would be refined against rather than absorbed.

The first part holds. The second fails as completely as a prediction can, and then holds with room to spare for a different refinement, which is the more useful finding.

Four refinements of one answer

The problems are the earlier essay’s: 24 by 6, singular values spread geometrically down to 1/κ1/\kappa, the data at a tangent tan⁡θ\tan \theta to the column space, and the solution random so that the fit lies along the top singular vectors. The reference is the exact solution of the stored doubles. Every refinement starts from QR’s answer and reuses the same Householder factors.

Two refinements are tried, each in two precisions. The first refines the solution alone: compute s=b−Axs = b - Ax, solve the least-squares problem min⁡δ∥s−Aδ∥\min_\delta \|s - A\delta\| with the factors, and add δ\delta to xx. That is what iterative refinement for a square system does, applied to a rectangular one. The second is Björck’s refinement of the augmented system,

(IAAT0)(rx)=(b0),\begin{pmatrix} I & A \\ A^{\mathsf{T}} & 0 \end{pmatrix} \begin{pmatrix} r \\ x \end{pmatrix} = \begin{pmatrix} b \\ 0 \end{pmatrix},

which is the least-squares problem written as a square system in the residual and the solution together. Its residuals are $f = b

  • r - Ax$ and g=−ATrg = -A^{\mathsf{T}} r, and the same QR solves for corrections to both. In each case the residuals are computed either in binary64 or exactly, from the doubles’ exact values in integer arithmetic, and rounded once; anything of at least twice the working precision gives the same result on problems this size.
The relative error of a least-squares solution against the exact solution of the stored data, after each step of four refinements of QR's answer, at a condition number of ten to the seventh and a tangent of one, median of five drawssolution alone, residual in binary64: 1.8e-4, 1.8e-4, 1.8e-4, 1.8e-4; solution alone, residual exact: 1.8e-4, 1.8e-4, 1.8e-4, 1.8e-4; residual and solution, binary64: 1.8e-4, 1.7e-4, 1.7e-4, 1.0e-4; residual and solution, exact: 1.8e-4, 1.7e-14, zero, zero after none to three steps.012310⁻¹⁷10⁻¹⁴10⁻¹¹10⁻⁸10⁻⁵10⁻²steps of refinementrelative error in the solutionsolution alone, residual in binary64solution alone, residual exactresidual and solution, binary64residual and solution, exactκ = 10⁷ · tan θ = 1 · 24 × 6 · floored at 10⁻¹⁷one route of four moves
Fig. 1 The relative error of the solution after each step of four refinements of QR’s answer, against the exact solution of the stored data, at a tangent of one, median of five draws: the solution alone with the residual in binary64 (grey) and exact (red), and the residual and solution together in binary64 (amber) and exact (blue). The dial sets the condition number.

At κ=107\kappa = 10^7, QR’s error is 1.8⋅10−41.8 \cdot 10^{-4}. Refining the solution alone with the residual in binary64 leaves it at 1.8⋅10−41.8 \cdot 10^{-4} after one, two and three steps. Refining the solution alone with the residual computed exactly leaves it at 1.8⋅10−41.8 \cdot 10^{-4} after one, two and three steps. Refining both together in binary64 moves it to 1.7⋅10−41.7 \cdot 10^{-4}, 1.7⋅10−41.7 \cdot 10^{-4} and 1.0⋅10−41.0 \cdot 10^{-4}. Refining both together with the residuals computed exactly takes it to 1.7⋅10−141.7 \cdot 10^{-14} after one step and to zero after two: the refined solution is the exact solution of the stored data, rounded to the nearest double in every component.

The dial shows the same shape at every condition number. At 10310^3 and 10510^5 three of the routes sit on QR’s error and the fourth reaches zero in one step.

The prediction’s first half

The first part of the prediction was that refinement in working precision changes nothing of consequence, and on both refinements it holds within its factor of three. The solution refined alone in binary64 does not move at all. The augmented refinement in binary64 wanders: up to 1.4⋅10−61.4 \cdot 10^{-6} from 5.3⋅10−75.3 \cdot 10^{-7} at κ=106\kappa = 10^6 after three steps, down to 1.0⋅10−41.0 \cdot 10^{-4} at 10710^7, never far from where it started. That is the expected behaviour of fixed-precision refinement: it can improve a solver whose factorisation was unstable, and Householder QR is not unstable, so there is nothing for it to improve. The error is the stored problem’s sensitivity times the roundoff in forming the residual, and computing that residual in the same precision gives it back.

The reason given for the prediction was that “the error is in the problem’s stored data and not in the factorisation”. That is the right diagnosis. What it implies about the second half is where the reasoning slipped.

An exact residual, and the same error

The relative error of a least-squares solution after one step of each refinement, against the condition number, at a tangent of one, median of five draws, with QR's own errorQR alone: 4.8e-16 at 10, 2.5e-14 at 100, 1.5e-12 at 1000, 1.3e-10 at 10000, 1.3e-8 at 100000, 5.3e-7 at 1000000, 1.8e-4 at 10000000. After one step: solution alone, residual in binary64 4.4e-16, 2.2e-14, 1.5e-12, 1.3e-10, 1.3e-8, 5.3e-7, 1.8e-4; solution alone, residual exact 3.5e-16, 2.2e-14, 1.5e-12, 1.3e-10, 1.3e-8, 5.3e-7, 1.8e-4; residual and solution, binary64 3.3e-16, 1.6e-14, 1.5e-12, 1.1e-10, 1.3e-8, 7.4e-7, 1.7e-4; residual and solution, exact zero, zero, zero, zero, zero, 1.4e-17, 1.7e-14.10¹10²10³10⁴10⁵10⁶10⁷10⁻¹⁷10⁻¹⁴10⁻¹¹10⁻⁸10⁻⁵10⁻²condition numberrelative error after one stepsolution alone, residual in binary64solution alone, residual exactresidual and solution, binary64residual and solution, exactQR, before any steptan θ = 1 · 24 × 6 · floored at 10⁻¹⁷three lines on the square
Fig. 2 The relative error after one step of each refinement against the condition number, at a tangent of one, median of five draws, with QR’s error before any step (grey line).

Against the condition number the picture is stark. Three of the four routes lie on QR’s own line after a step, from 4.8⋅10−164.8 \cdot 10^{-16} at κ=10\kappa = 10 to 1.8⋅10−41.8 \cdot 10^{-4} at 10710^7, and the exact residual on the solution alone is one of the three. It does not lower the error at any condition number by any factor worth recording. The fourth route, both refined with exact residuals, is at zero or within a few units of the roundoff at every condition number.

The failure has a clean explanation, and it is one that Golub and Wilkinson gave in 1966. The exact residual of the current solution is s=b−Ax=r⋆+A(x⋆−x)s = b - Ax = r^{\star} + A(x^{\star} - x), where x⋆x^{\star} and r⋆r^{\star} are the true solution and residual. The correction step solves a least-squares problem with ss as its data. Its exact answer would be x⋆−xx^{\star} - x, because r⋆r^{\star} is orthogonal to the column space and contributes nothing. But the correction is computed by the same QR in binary64, and a least-squares solve in binary64 of data with a large component r⋆r^{\star} orthogonal to the column space has an error of order uκ2∥r⋆∥u \kappa^2 \|r^{\star}\| — which is the error the original solve made, and for the same reason. The exact residual hands the correction step a problem with exactly the same angle to the column space as the original one. Computing it exactly has removed the rounding in ss, and the rounding in ss was never what put the square into the answer.

On a square system the true residual is zero, so the correction’s data are all in the range, and an exact residual is exactly what is needed. For a least-squares problem the true residual is the largest part of ss, and refining the solution alone keeps feeding it back through the solver.

The angle decides when the solution alone is enough

That explanation predicts when refining the solution alone does work, and the measurement agrees.

The relative error of a least-squares solution at a condition number of ten to the seventh after one step of each refinement, at four angles between the data and the column space, median of five drawsa zero residual: QR 6.3e-11, solution alone, residual in binary64 7.4e-11, solution alone, residual exact zero, residual and solution, binary64 7.4e-11, residual and solution, exact zero; tan θ = 10⁻³: QR 1.8e-7, solution alone, residual in binary64 1.8e-7, solution alone, residual exact 1.8e-7, residual and solution, binary64 8.0e-8, residual and solution, exact 2.4e-17; tan θ = 1: QR 1.8e-4, solution alone, residual in binary64 1.8e-4, solution alone, residual exact 1.8e-4, residual and solution, binary64 1.7e-4, residual and solution, exact 1.7e-14; tan θ = 10³: QR 1.8e-1, solution alone, residual in binary64 1.8e-1, solution alone, residual exact 1.8e-1, residual and solution, binary64 2.3e-1, residual and solution, exact 1.7e-11.10⁻¹⁶10⁻¹²10⁻⁸10⁻⁴1a zero residualQR alone6e-11solution alone, residual in binary647e-11solution alone, residual exactzeroresidual and solution, binary647e-11residual and solution, exactzerotan θ = 10⁻³QR alone2e-7solution alone, residual in binary642e-7solution alone, residual exact2e-7residual and solution, binary648e-8residual and solution, exact2e-17tan θ = 1QR alone2e-4solution alone, residual in binary642e-4solution alone, residual exact2e-4residual and solution, binary642e-4residual and solution, exact2e-14tan θ = 10³QR alone2e-1solution alone, residual in binary642e-1solution alone, residual exact2e-1residual and solution, binary642e-1residual and solution, exact2e-11relative error after one stepκ = 10⁷ · floored at 10⁻¹⁷the angle decides the alone route
Fig. 3 The relative error at a condition number of ten million after one step of each refinement, at four angles between the data and the column space, with QR’s error, median of five draws.

With a zero residual, a consistent system, the exact residual on the solution alone takes QR’s 6.3⋅10−116.3 \cdot 10^{-11} to zero in one step, exactly as for a square system, because there is no r⋆r^{\star} to feed back. At a tangent of 10−310^{-3} it does nothing: QR’s 1.8⋅10−71.8 \cdot 10^{-7} stays at 1.8⋅10−71.8 \cdot 10^{-7}. At a tangent of 10310^3 the error is 0.180.18, the solution has almost no correct digit, and refining it alone leaves it at 0.180.18. Over every condition number and angle measured, wherever κtan⁡θ\kappa \tan\theta is a hundred or more, so that the square’s term dominates the error, one step of the solution alone is within half again of QR’s own error. Where κtan⁡θ\kappa \tan \theta is small, the error is QR’s ordinary uκu\kappa term and the exact residual removes it, as it does at κ=103\kappa = 10^3 and tan⁡θ=10−3\tan \theta = 10^{-3}, where the error falls from 1.1⋅10−141.1 \cdot 10^{-14} to 1.4⋅10−151.4 \cdot 10^{-15}.

The augmented refinement with exact residuals takes every one of the four angles to under 10−1010^{-10} in one step, including the tangent of 10310^3, where it goes from 0.180.18 to 1.7⋅10−111.7 \cdot 10^{-11}, and to the exact solution in two.

Why refining the residual works

The augmented matrix is a saddle-point matrix: an identity block, a zero block, and the constraint ATr=0A^{\mathsf{T}} r = 0 coupling them. The zero that is not a missing entry met the same shape as the system a constrained minimisation produces, where the zero block is a theorem and not sparsity, and here the theorem is the right angle: the residual must be orthogonal to the columns. Writing least squares this way makes the orthogonality an equation the refinement can correct, rather than a property the solver is trusted to deliver, and that is the whole of the difference between the two refinements.

The augmented system’s correction step has right-hand sides f=b−r−Axf = b - r - Ax and g=−ATrg = -A^{\mathsf{T}} r. The first is small when the current rr and xx fit the data, and the second is small when the current rr is orthogonal to the columns. Neither contains the large true residual itself: rr is carried as an unknown, so its bulk is subtracted out before the correction is solved, and what is passed to the factors is only the part of each equation the current iterate gets wrong. The square in QR’s error came from solving data with a large component outside the column space. The augmented correction never asks the factors to solve such data, because the large component has been moved to the left-hand side as a variable.

That is also why it needs the extra precision. In binary64, ff is the difference of bb, rr and AxAx, quantities of order one whose difference is of order uκ2u\kappa^2, and the subtraction loses that difference to rounding. Computed exactly, ff and gg are exactly what the current iterate gets wrong, and the factors, solving a problem with no large residual, solve it to their ordinary accuracy. One step multiplies the error by about uκu\kappa, which is 10−910^{-9} at κ=107\kappa = 10^7; the second step finishes it.

The fitted slope of the solution's error against the condition number, from a hundred to ten to the seventh at a tangent of one, before refinement and after one and three steps of three refinementssolution alone, residual in binary64, before: 1.94; solution alone, residual in binary64, after 1: 1.95; solution alone, residual in binary64, after 3: 1.95; solution alone, residual exact, before: 1.94; solution alone, residual exact, after 1: 1.95; solution alone, residual exact, after 3: 1.95; residual and solution, binary64, before: 1.94; residual and solution, binary64, after 1: 1.98; residual and solution, binary64, after 3: 1.97. The fourth route has no slope: its error is zero after two steps at every condition number.00.511.522.5solution alone, residual in binary64, before1.94solution alone, residual in binary64, after 11.95solution alone, residual in binary64, after 31.95solution alone, residual exact, before1.94solution alone, residual exact, after 11.95solution alone, residual exact, after 31.95residual and solution, binary64, before1.94residual and solution, binary64, after 11.98residual and solution, binary64, after 31.97slope of the error against κ, both on logarithmic axestwo is the square of the condition numberno step takes it off
Fig. 4 The fitted slope of the solution’s error against the condition number, from a hundred to ten million at a tangent of one, before refinement and after one and three steps, for the three refinements that leave an error to fit.

The slopes say the same thing in one number each. Before refinement it is 1.94. After one step and after three, the solution alone gives 1.95 with either residual, and the augmented refinement in binary64 gives 1.94 to 1.98. No step of any of those three moves the square. The fourth has no slope to fit: its error is zero after two steps at every condition number.

A large solution, and a step that does not hurt

The earlier essay’s second kind of problem has the fit drawn at random in the range, so that the solution is about as long as the condition number and QR’s error has slope one, because the square’s term is small relative to so large a solution. There is no square to remove, and the question is only whether any refinement makes things worse.

The same four refinements on a problem whose fit is random in the range, so that the solution is about as long as the condition number: the relative error after one step at three condition numbers, tangent of oneκ = 1000: QR 3.0e-15, solution alone, residual in binary64 1.4e-14, solution alone, residual exact 1.1e-14, residual and solution, binary64 1.5e-14, residual and solution, exact zero; κ = 100000: QR 1.8e-12, solution alone, residual in binary64 1.6e-12, solution alone, residual exact 1.5e-12, residual and solution, binary64 1.4e-12, residual and solution, exact zero; κ = 10000000: QR 1.4e-10, solution alone, residual in binary64 1.9e-10, solution alone, residual exact 2.0e-10, residual and solution, binary64 1.2e-10, residual and solution, exact zero.10⁻¹⁶10⁻¹⁴10⁻¹²10⁻¹⁰κ = 10³, a large solutionQR alone3e-15solution alone, residual in binary641e-14solution alone, residual exact1e-14residual and solution, binary642e-14residual and solution, exactzeroκ = 10⁵, a large solutionQR alone2e-12solution alone, residual in binary642e-12solution alone, residual exact1e-12residual and solution, binary641e-12residual and solution, exactzeroκ = 10⁷, a large solutionQR alone1e-10solution alone, residual in binary642e-10solution alone, residual exact2e-10residual and solution, binary641e-10residual and solution, exactzerorelative error after one steptan θ = 1 · floored at 10⁻¹⁷no square to remove, still one exact step
Fig. 5 The relative error after one step of each refinement on a problem whose solution is about as long as the condition number, at three condition numbers and a tangent of one, with QR’s error.

Two of them do, slightly. At κ=103\kappa = 10^3 QR’s error is 3.0⋅10−153.0 \cdot 10^{-15} and the solution refined alone gives 1.4⋅10−141.4 \cdot 10^{-14} or 1.1⋅10−141.1 \cdot 10^{-14}, by either residual, and the augmented refinement in binary64 1.5⋅10−141.5 \cdot 10^{-14}; at 10710^7, QR’s 1.4⋅10−101.4 \cdot 10^{-10} becomes 1.91.9 to 2.0⋅10−102.0 \cdot 10^{-10} refined alone. These are factors of a few, the cost of adding a correction computed with the error the original solve had. The augmented refinement with exact residuals is zero after one step at all three condition numbers. It does not care which kind of problem it is given, because it solves the stored problem rather than one of its perturbation terms.

What this changes about mixed precision

Buying the accuracy back and the part of a solver that may be rounded describe iterative refinement for square systems, where a factorisation in low precision and a residual in high precision buy back the digits the factorisation lost. The same strategy for least squares is used in practice, and these measurements say exactly how it must be written. Refining the solution alone, with any precision of residual, cannot remove the κ2tan⁡θ\kappa^2\tan\theta term, and on a consistent or nearly consistent problem it is not needed for that purpose anyway. Refining through the augmented system with the residuals in extended precision removes it entirely, in one step on these problems.

The same measurements also say something about trust. A small residual is not a small error for square systems, and for least squares the distinction is sharper still: QR’s residual here is accurate to uκu\kappa while its solution carries uκ2tan⁡θu\kappa^2\tan\theta, so a code that checks its answer by recomputing the residual sees a good residual and learns nothing about the solution. The residual the solution cannot hold found a related gap between a fitted residual and the solution that produced it. The augmented refinement’s first correction is the one quantity in this computation that is computed against the right reference, and it is the natural place to read the solution’s accuracy from.

The cost is small: two residual computations of mnmn operations each, in extended precision, and a correction solved with factors already in hand. The augmented system’s square matrix is never formed, since the QR of AA solves it, and the road that squares the problem is not taken at any point. What was paid for in the earlier essay’s account, that “no algorithm working on the stored data escapes the square”, remains true of every single solve in binary64. Two solves and two exact residuals escape it.

Where these numbers come from

Every problem is 24 by 6, which is small enough for the exact reference and large enough for the slopes to be clean. Five draws at each setting give medians, and every claim above holds on the medians rather than draw by draw. The exact residuals are exact, which is more than a code would use; twice the working precision rounds each component to 10−3210^{-32} relative, far below anything measured here, but a code using a double-double residual rather than an exact one could meet cancellations this setup does not contain. Björck’s refinement is implemented with the full orthogonal factor formed explicitly, which a code would not do; the reflectors give the same products. And the larger system the earlier essay asked about, 200 by 50 at the same condition and angle, was not measured, because the exact reference grows as the cube of the column count in integer digits.

What is settled is the shape. In working precision no refinement moves QR’s square. With exact residuals the solution alone still does not, wherever the residual is large; the residual and solution together do, at once.

Still open: how much precision the residual needs, and a refinement that stops itself

The residual’s precision, as a number. The augmented step multiplies the error by about uκu\kappa when its residuals are exact. The prediction with a sign is that with the residuals computed in a precision of pp bits instead, rounded to that precision after each operation, one step reaches max⁡(uκ,2−pκ2tan⁡θ)\max(u\kappa, 2^{-p}\kappa^2\tan\theta) to within a factor of ten, so that 80-bit extended precision suffices at κ=107\kappa = 10^7 and a tangent of one, and binary128 suffices at every setting here, while a double-double residual matches the exact one on every problem measured.

A refinement that knows when to stop. The second augmented step took every problem here to its exact solution. The prediction is that the norm of the first correction, ∥δx∥/∥x∥\|\delta x\|/\|x\|, is within a factor of three of QR’s true error on every draw, so that the first step’s correction measures the error it removes, and a code can report the original solution’s accuracy for the price of the refinement, at every condition number and angle here.

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 truthIterative refinementLeast squaresQR factorisationResidualSaddle-point systems