Stability of Algorithms
A well-conditioned problem can still be solved badly: an algorithm may introduce more error than the data's rounding warrants. An algorithm is stable when its errors are no worse than the problem's conditioning makes unavoidable. This lesson defines backward stability and shows unstable algorithms next to stable ones for the same problem.
Forward and backward error
Let be the computed value of .
- The forward error is (or relative): how wrong is the answer?
- The backward error is the smallest such that exactly: for which nearby input is our answer exactly right?
An algorithm is backward stable if its backward error is always a modest multiple of . The two notions are linked by the condition number:
So a backward stable algorithm gives answers as accurate as the conditioning allows, and that is all one can ask. The basic floating-point operations, Householder QR and Gaussian elimination with partial pivoting (in practice) are backward stable.
Example: an unstable recurrence
The integrals satisfy and, integrating by parts, . All lie between and . Running the recurrence forward multiplies the initial rounding error by :
import numpy as np
from scipy.integrate import quad
I = 1 - np.exp(-1)
for n in range(1, 21):
I = 1 - n * I
if n in (5, 10, 15, 18, 20):
exact = quad(lambda x: x**n * np.exp(x - 1), 0, 1)[0]
print(f"n = {n:2d}: recurrence {I: .6e}, exact {exact:.6e}")n = 5: recurrence 1.455329e-01, exact 1.455329e-01
n = 10: recurrence 8.387707e-02, exact 8.387707e-02
n = 15: recurrence 5.903379e-02, exact 5.901754e-02
n = 18: recurrence -2.945367e-02, exact 5.011985e-02
n = 20: recurrence -3.019239e+01, exact 4.554488e-02
Run backward instead: divides the error by at each step. Even a crude starting guess gives excellent values lower down:
import numpy as np
from scipy.integrate import quad
I = 0.0 # rough guess for I_40
values = {}
for n in range(40, 0, -1):
I = (1 - I) / n # this is I_{n-1}
values[n - 1] = I
for n in (20, 10, 0):
exact = quad(lambda x: x**n * np.exp(x - 1), 0, 1)[0]
print(f"n = {n:2d}: backward {values[n]:.15f}, exact {exact:.15f}")n = 20: backward 0.045544884075818, exact 0.045544884075818
n = 10: backward 0.083877070103394, exact 0.083877070103394
n = 0: backward 0.632120558828558, exact 0.632120558828558
The problem (computing ) is perfectly well conditioned. The forward recurrence is an unstable algorithm for it; the backward recurrence is stable.
Example: stable and unstable formulas for the same thing
The function is well conditioned near (), but the formula as written cancels catastrophically. The equivalent is stable.
import numpy as np
for x in (1e-3, 1e-6, 1e-8):
unstable = (1 - np.cos(x)) / x**2
stable = 0.5 * (np.sin(x / 2) / (x / 2)) ** 2
print(f"x = {x:.0e}: unstable {unstable:.12f}, stable {stable:.12f}")x = 1e-03: unstable 0.499999958326, stable 0.499999958333
x = 1e-06: unstable 0.500044450291, stable 0.500000000000
x = 1e-08: unstable 0.000000000000, stable 0.500000000000
How to tell which is which
- If the answer is bad and is small, the algorithm is unstable: look for cancellation, error-amplifying recurrences, or unnecessary intermediate quantities (like forming ).
- If is large, even a stable algorithm gives few digits: reformulate the problem.
- Test with backward error: for , the relative residual near means the solver did its job.
In software
- LAPACK's routines are designed to be backward stable; error bounds in its documentation are stated as backward errors times condition numbers.
- Recurrences are everywhere: Bessel functions, orthogonal polynomials, IIR filters, financial compounding. Libraries like SciPy choose the stable direction (e.g. Miller's backward recurrence for Bessel functions).
Exercises
Exercise 1: Error growth
In the forward recurrence , an error in becomes what error in ?
Answer
.
Solution
The error satisfies , so . With and , the error at is in the tens, as the run above shows ( instead of ).
Exercise 2: Backward error of addition
Show that floating-point addition is backward stable.
Answer
with .
Solution
The computed sum is the exact sum of the slightly perturbed inputs and , each with relative perturbation at most .
Exercise 3: A stable variance (code)
Write Welford's one-pass variance (, , variance ) and compare it with the one-pass formula on data with mean .
Answer
Welford agrees with np.var; the naive formula is badly wrong.
Solution
import numpy as np
rng = np.random.default_rng(11)
x = 1e9 + rng.normal(0, 2, 5000)
M, S = 0.0, 0.0
for k, xk in enumerate(x, start=1):
M_old = M
M = M + (xk - M) / k
S = S + (xk - M_old) * (xk - M)
print(f"Welford {S / len(x):.6f}, np.var {np.var(x):.6f}, naive {np.mean(x * x) - np.mean(x) ** 2:.6f}")Welford 3.995055, np.var 3.995055, naive 0.000000
Test yourself
An algorithm is backward stable if…
- it never makes rounding errors
- its result is the exact answer for a slightly perturbed input
- it runs backward
- its forward error is always below
Forward error is roughly bounded by…
- backward error divided by
- times the backward error
- the residual
The forward recurrence is unstable because…
- the integrals are ill-conditioned
- it multiplies errors by at each step
- of overflow
- it uses subtraction only once