TExcelTAD
Home › Numerical Methods › Stability of Algorithms

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 y~\tilde{y} be the computed value of y=f(x)y = f(x).

  • The forward error is ∣y~−y∣\lvert\tilde{y} - y\rvert (or relative): how wrong is the answer?
  • The backward error is the smallest ∣Δx∣\lvert\Delta x\rvert such that y~=f(x+Δx)\tilde{y} = f(x + \Delta x) 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 uu. The two notions are linked by the condition number:

relative forward error≲κf(x)×relative backward error.\text{relative forward error} \lesssim \kappa_f(x) \times \text{relative backward error}.

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 In=∫01xnex−1 dxI_n = \int_0^1 x^ne^{x-1}\,dx satisfy I0=1−e−1I_0 = 1 - e^{-1} and, integrating by parts, In=1−nIn−1I_n = 1 - nI_{n-1}. All InI_n lie between 00 and 1n+1\frac{1}{n+1}. Running the recurrence forward multiplies the initial rounding error by n!n!:

python · try it yourself
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: In−1=1−InnI_{n-1} = \frac{1 - I_n}{n} divides the error by nn at each step. Even a crude starting guess I40≈0I_{40} \approx 0 gives excellent values lower down:

python · try it yourself
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 I20I_{20}) 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 f(x)=1−cos⁡xx2f(x) = \frac{1 - \cos x}{x^2} is well conditioned near 00 (f→12f \to \frac{1}{2}), but the formula as written cancels catastrophically. The equivalent 12(sin⁡(x/2)x/2)2\frac{1}{2}\left(\frac{\sin(x/2)}{x/2}\right)^2 is stable.

python · try it yourself
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 κ\kappa is small, the algorithm is unstable: look for cancellation, error-amplifying recurrences, or unnecessary intermediate quantities (like forming ATAA^TA).
  • If κ\kappa is large, even a stable algorithm gives few digits: reformulate the problem.
  • Test with backward error: for Ax=bA\mathbf{x} = \mathbf{b}, the relative residual ∥b−Ax~∥∥A∥ ∥x~∥\frac{\lVert\mathbf{b} - A\tilde{\mathbf{x}}\rVert}{\lVert A\rVert\,\lVert\tilde{\mathbf{x}}\rVert} near uu 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 In=1−nIn−1I_n = 1 - nI_{n-1}, an error ϵ\epsilon in I0I_0 becomes what error in InI_n?

Answer

(−1)nn! ϵ(-1)^n n!\,\epsilon.

Solution

The error ene_n satisfies en=−nen−1e_n = -ne_{n-1}, so en=(−1)nn! e0e_n = (-1)^nn!\,e_0. With e0≈10−17e_0 \approx 10^{-17} and 20!≈2.4×101820! \approx 2.4 \times 10^{18}, the error at n=20n = 20 is in the tens, as the run above shows (−30-30 instead of 0.0460.046).

Exercise 2: Backward error of addition

Show that floating-point addition is backward stable.

Answer

fl⁡(a+b)=a(1+δ)+b(1+δ)\operatorname{fl}(a + b) = a(1 + \delta) + b(1 + \delta) with ∣δ∣≤u\lvert\delta\rvert \le u.

Solution

The computed sum is the exact sum of the slightly perturbed inputs a(1+δ)a(1 + \delta) and b(1+δ)b(1 + \delta), each with relative perturbation at most uu.

Exercise 3: A stable variance (code)

Write Welford's one-pass variance (Mk=Mk−1+xk−Mk−1kM_k = M_{k-1} + \frac{x_k - M_{k-1}}{k}, Sk=Sk−1+(xk−Mk−1)(xk−Mk)S_k = S_{k-1} + (x_k - M_{k-1})(x_k - M_k), variance Sn/nS_n/n) and compare it with the one-pass formula x2‾−xˉ2\overline{x^2} - \bar{x}^2 on data with mean 10910^9.

Answer

Welford agrees with np.var; the naive formula is badly wrong.

Solution
python · try it yourself
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…

  1. it never makes rounding errors
  2. its result is the exact answer for a slightly perturbed input
  3. it runs backward
  4. its forward error is always below uu

Forward error is roughly bounded by…

  1. backward error divided by κ\kappa
  2. κ\kappa times the backward error
  3. uu
  4. the residual

The forward recurrence In=1−nIn−1I_n = 1 - nI_{n-1} is unstable because…

  1. the integrals are ill-conditioned
  2. it multiplies errors by nn at each step
  3. of overflow
  4. it uses subtraction only once