TExcelTAD
Home › Numerical Methods › Conditioning of Problems

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 y=f(x)y = f(x), perturb the input by a relative amount Δxx\frac{\Delta x}{x} and look at the relative change in the output. The ratio, in the limit of small perturbations, is the relative condition number:

κf(x)=lim⁡Δx→0∣Δy/y∣∣Δx/x∣=∣x f′(x)f(x)∣.\kappa_f(x) = \lim_{\Delta x \to 0}\frac{\lvert\Delta y/y\rvert}{\lvert\Delta x/x\rvert} = \left\lvert\frac{x\,f'(x)}{f(x)}\right\rvert.

Since the input is already rounded (relative error about u=1.1×10−16u = 1.1 \times 10^{-16}), even a perfect algorithm should expect a relative output error near κf(x) u\kappa_f(x)\,u. Rule of thumb: you lose about log⁡10κ\log_{10}\kappa digits.

Problem κf(x)\kappa_f(x) Ill-conditioned when
f(x)=x+cf(x) = x + c ∣xx+c∣\left\lvert\frac{x}{x + c}\right\rvert x≈−cx \approx -c (cancellation)
f(x)=cxf(x) = cx 11 never
f(x)=xf(x) = \sqrt{x} 12\frac{1}{2} never
f(x)=exf(x) = e^x ∣x∣\lvert x\rvert ∣x∣\lvert x\rvert large
f(x)=ln⁡xf(x) = \ln x 1∣ln⁡x∣\frac{1}{\lvert\ln x\rvert} x≈1x \approx 1
f(x)=cos⁡xf(x) = \cos x ∣xtan⁡x∣\lvert x\tan x\rvert near zeros of cos⁡x\cos x

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:

python · try it yourself
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 p(x)=(x−1)(x−2)⋯(x−20)p(x) = (x - 1)(x - 2)\cdots(x - 20): changing the coefficient of x19x^{19} (which is −210-210) by 2−23≈10−72^{-23} \approx 10^{-7} moves some roots by several units and makes them complex.

python · try it yourself
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 rr of pp moves by about δ r19∣p′(r)∣\frac{\delta\,r^{19}}{\lvert p'(r)\rvert} when the x19x^{19} coefficient changes by δ\delta; for rr around 15 that factor is about 101010^{10}. 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 κf(x)\kappa_f(x) for f(x)=xnf(x) = x^n.

Answer

κ=∣n∣\kappa = \lvert n\rvert.

Solution

∣x⋅nxn−1xn∣=∣n∣\left\lvert\frac{x \cdot nx^{n-1}}{x^n}\right\rvert = \lvert n\rvert, independent of xx.

Exercise 2: Digits lost

A problem has κ=109\kappa = 10^{9} and is solved by a stable algorithm in double precision. About how many correct digits can you expect?

Answer

About 7.

Solution

Relative error ≈κu≈109⋅1.1×10−16≈10−7\approx \kappa u \approx 10^9 \cdot 1.1 \times 10^{-16} \approx 10^{-7}.

Exercise 3: Condition of tan⁡\tan near π/2\pi/2 (code)

Compute κ\kappa for f(x)=tan⁡xf(x) = \tan x at x=1.57x = 1.57 from the formula ∣x(1+tan⁡2x)tan⁡x∣\left\lvert\frac{x(1 + \tan^2x)}{\tan x}\right\rvert and check it by perturbing xx by 10−910^{-9} relatively.

Answer

About 1.97×1031.97 \times 10^{3} both ways.

Solution
python · try it yourself
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 101210^{12} solved in double precision can be expected to give about…

  1. 16 correct digits
  2. 4 correct digits
  3. 12 correct digits
  4. no correct digits

Can a better algorithm fix an ill-conditioned problem?

  1. Yes, always
  2. No; only reformulating the problem or better data helps
  3. Only in double precision
  4. Only for linear problems

For f(x)=ln⁡xf(x) = \ln x the problem is ill-conditioned near…

  1. x=0x = 0
  2. x=1x = 1
  3. x=ex = e
  4. large xx