Conditioning of Problems
Some problems are sensitive in themselves: a tiny change in the data changes the answer a lot, whatever algorithm you use. The condition number measures this sensitivity. It separates two questions that are easy to confuse: is the problem sensitive (conditioning), and does the algorithm add more error than necessary (Stability of Algorithms)?
The relative condition number
For a problem , perturb the input by a relative amount and look at the relative change in the output. The ratio, in the limit of small perturbations, is the relative condition number:
Since the input is already rounded (relative error about ), even a perfect algorithm should expect a relative output error near . Rule of thumb: you lose about digits.
| Problem | Ill-conditioned when | |
|---|---|---|
| (cancellation) | ||
| never | ||
| never | ||
| large | ||
| near zeros of |
Subtraction is ill-conditioned when the result is small: this is the problem-level view of Catastrophic Cancellation.
Measuring a condition number
Perturb the input by a small relative amount and compare:
import numpy as np
def kappa_estimate(f, x, rel=1e-7):
y = f(x)
dx = rel * x
return abs((f(x + dx) - y) / y) / rel
for name, f, x, exact in [
("exp at 50", np.exp, 50.0, 50.0),
("log at 1.001", np.log, 1.001, 1 / abs(np.log(1.001))),
("cos at 1.5707", np.cos, 1.5707, abs(1.5707 * np.tan(1.5707))),
]:
print(f"{name:14s}: estimated {kappa_estimate(f, x):10.2f}, formula {exact:10.2f}")exp at 50 : estimated 50.00, formula 50.00
log at 1.001 : estimated 1000.50, formula 1000.50
cos at 1.5707 : estimated 16305.95, formula 16305.95
Conditioning of root finding
The roots of a polynomial can be extremely sensitive to its coefficients. Wilkinson's famous example is : changing the coefficient of (which is ) by moves some roots by several units and makes them complex.
import numpy as np
coeffs = np.poly(np.arange(1, 21)) # expand (x-1)...(x-20)
perturbed = coeffs.copy()
perturbed[1] -= 2.0**-23 # x^19 coefficient: -210 -> -210 - 2^-23
roots = np.sort_complex(np.roots(perturbed))
print("largest imaginary part:", round(float(np.max(np.abs(roots.imag))), 3))
print("four of the roots:", np.round(roots[13:17], 3))largest imaginary part: 2.813
four of the roots: [13.992-2.519j 13.992+2.519j 16.731-2.813j 16.731+2.813j]
A simple root of moves by about when the coefficient changes by ; for around 15 that factor is about . The general lesson: never compute eigenvalues by finding roots of the characteristic polynomial.
Conditioning is not about the algorithm
An ill-conditioned problem cannot be rescued by a cleverer method. What you can do is:
- Reformulate to a better-conditioned problem (eigenvalues from the matrix, not from the characteristic polynomial; a polynomial basis of Chebyshev polynomials instead of monomials).
- Get better data or accept fewer digits.
- Regularize: change the problem slightly to make it well conditioned (ridge regression).
For linear systems the condition number of a matrix plays the same role (Conditioning of Linear Systems).
In software
np.linalg.cond(A)returns the matrix condition number; NumPy and SciPy warn about ill-conditioned solves (LinAlgWarning).- Feature scaling in machine learning improves the conditioning of the optimization problem, which is why gradient descent converges faster on standardized features.
Exercises
Exercise 1: Condition of a power
Find for .
Answer
.
Solution
, independent of .
Exercise 2: Digits lost
A problem has and is solved by a stable algorithm in double precision. About how many correct digits can you expect?
Answer
About 7.
Solution
Relative error .
Exercise 3: Condition of near (code)
Compute for at from the formula and check it by perturbing by relatively.
Answer
About both ways.
Solution
import numpy as np
x = 1.57
formula = abs(x * (1 + np.tan(x) ** 2) / np.tan(x))
rel = 1e-9
measured = abs((np.tan(x * (1 + rel)) - np.tan(x)) / np.tan(x)) / rel
print(round(float(formula), 1), round(float(measured), 1))1971.6 1971.6
Test yourself
A problem with condition number solved in double precision can be expected to give about…
- 16 correct digits
- 4 correct digits
- 12 correct digits
- no correct digits
Can a better algorithm fix an ill-conditioned problem?
- Yes, always
- No; only reformulating the problem or better data helps
- Only in double precision
- Only for linear problems
For the problem is ill-conditioned near…
- large