Search

Search papers and lessons

Condition Number

Level 3 · 10 min read

The condition number of a matrix is the ratio of its largest to smallest stretch, and it bounds how much a small error in the data can grow in the answer of a linear system.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • ReadReady to readNot yet

TL;DR

The condition number of a matrix measures how much the matrix can magnify errors. For an invertible matrix it is the ratio of its largest stretch to its smallest stretch, and a large value means that a tiny change in the data can cause a large change in the answer of a linear system. It tells you in advance how many digits of an answer you can trust.

Builds on: Matrix Norm, Matrix Inverse, Singular Value Decomposition (SVD) · Leads to: Preconditioning

Where it's used

Engineering simulations. A structural engineer who solves for how a frame bends under load, or a geophysicist who fits a model to measured signals, reduces the problem to a linear system Ax=bA x = b. The matrix comes from measurements and modelling choices that are never exact. Before trusting the answer, the engineer checks the condition number of AA.

Curve fitting. Fitting a polynomial of high degree, as in linear regression, to data over a wide range of inputs leads to a matrix whose columns are nearly the same direction. Its condition number is large, so the fitted coefficients can change a lot when the data changes in the last digit.

Numerical libraries. Standard linear algebra routines often return an estimate of the condition number along with the solution, so the caller can see that the answer may be unreliable.

Optimization. The speed at which gradient descent converges on a quadratic problem depends on the condition number of its Hessian matrix. A large value means slow progress along the flat directions, which is why Preconditioning exists.

How it works

Take one tiny instance. The matrix AA is diagonal, and it acts on a vector (u,v)(u, v) by sending it to (100u,v)(100u, v). It stretches the first coordinate by 100 and leaves the second alone. We solve Ax=bA x = b for b=(100,0)b = (100, 0), then see what a small change in bb does to xx.

  1. Find the stretches. AA stretches horizontal vectors by 100 and vertical vectors by 1. The largest stretch is 100 and the smallest is 1. Check: the ratio of largest to smallest is 100.
  2. Solve for the original answer. The vector (1,0)(1, 0) is mapped to (100,0)(100, 0), so x=(1,0)x = (1, 0). Check: Ax=(100,0)=bA x = (100, 0) = b.
  3. Change the data slightly. Replace bb with b′=(100,1)b' = (100, 1), so the error in the data is (0,1)(0, 1). The relative size of this change is 1/100=11 / 100 = 1 percent.
  4. Solve again. The vector (1,1)(1, 1) is mapped to (100,1)(100, 1), so x′=(1,1)x' = (1, 1). The error in the answer is (0,1)(0, 1), and its relative size is 1/1=1001 / 1 = 100 percent. Check: Ax′=(100,1)=b′A x' = (100, 1) = b'.
  5. Compare. A 1 percent change in the data became a 100 percent change in the answer. The amplification factor is 100, the same as the ratio of largest to smallest stretch. That ratio is the condition number.
flowchart LR
  B[b, exact data] --> X[x = A inverse b]
  B2[b plus small error] --> X2[x plus error in x]
  X --> C[compare]
  X2 --> C

The math

Definition: Condition number

For an invertible matrix AA, the condition number in a chosen norm is

κ(A)=∥A∥ ∥A−1∥\kappa(A) = \|A\| \, \|A^{-1}\|

where ∥A∥\|A\| is the matrix norm (see Matrix Norm), the largest ratio of output length to input length, with lengths measured by a vector norm, and A−1A^{-1} is the Matrix Inverse.

Step 1 in symbols: the stretch ratio

In the 2-norm, ∥A∥\|A\| equals the largest singular value σmax⁡\sigma_{\max}, and ∥A−1∥\|A^{-1}\| equals 1/σmin⁡1/\sigma_{\min}, where σmin⁡\sigma_{\min} is the smallest singular value (see Singular Value Decomposition (SVD)). So

κ(A)=σmax⁡σmin⁡\kappa(A) = \frac{\sigma_{\max}}{\sigma_{\min}}

For a symmetric positive definite matrix the singular values equal the eigenvalues, so κ(A)=λmax⁡/λmin⁡\kappa(A) = \lambda_{\max} / \lambda_{\min}. For A=diag⁡(100,1)A = \operatorname{diag}(100, 1) the singular values are 100 and 1, so κ(A)=100\kappa(A) = 100.

Step 2 in symbols: solving

For the diagonal matrix, A−1=diag⁡(1/100,1)A^{-1} = \operatorname{diag}(1/100, 1). Then x=A−1b=(1/100⋅100, 1⋅0)=(1,0)x = A^{-1} b = (1/100 \cdot 100, \, 1 \cdot 0) = (1, 0), which matches step 2 of the walkthrough.

Steps 3 and 4 in symbols: the error bound

Let xx solve Ax=bA x = b, and let x+δxx + \delta x solve the equation with the perturbed data b+δbb + \delta b. Subtracting the two equations gives A δx=δbA \, \delta x = \delta b, so δx=A−1δb\delta x = A^{-1} \delta b. Taking norms,

∥δx∥≤∥A−1∥ ∥δb∥\|\delta x\| \le \|A^{-1}\| \, \|\delta b\|

Also b=Axb = A x, so ∥b∥≤∥A∥ ∥x∥\|b\| \le \|A\| \, \|x\|, which gives 1/∥x∥≤∥A∥/∥b∥1/\|x\| \le \|A\| / \|b\|. Multiplying the two inequalities,

∥δx∥∥x∥≤κ(A) ∥δb∥∥b∥\frac{\|\delta x\|}{\|x\|} \le \kappa(A) \, \frac{\|\delta b\|}{\|b\|}

The left side is the relative error in the answer and the right side is the condition number times the relative error in the data. In the walkthrough, κ=100\kappa = 100 and the relative error in the data is 0.01, so the bound allows 1. The actual relative error was 1, so the bound is reached in this case.

Step 5 in symbols: reading the bound

The inequality says that the relative error in xx is at most κ(A)\kappa(A) times the relative error in bb. The condition number is a property of AA alone. The vector bb only decides which answer you are looking at.

Worked examples

Example
The identity matrix
  1. Let A=IA = I, the 2 by 2 identity. Its singular values are both 1, so κ(I)=1/1=1\kappa(I) = 1 / 1 = 1.
  2. Solving Ix=bI x = b gives x=bx = b.
  3. Change the data by δb=(0,0.01)\delta b = (0, 0.01). The answer changes by the same amount, δx=(0,0.01)\delta x = (0, 0.01).
  4. The relative change in the data and in the answer are equal, so the amplification is 1.
  5. Interpretation: a matrix with condition number 1 passes data errors through unchanged. Nothing is lost in solving.
Example
A stretch of 2 and 0.5
  1. Let A=diag⁡(2,0.5)A = \operatorname{diag}(2, 0.5). Its condition number is κ=2/0.5=4\kappa = 2 / 0.5 = 4.
  2. Solve Ax=(1,1)A x = (1, 1). Then x=(0.5,2)x = (0.5, 2), since 2⋅0.5=12 \cdot 0.5 = 1 and 0.5⋅2=10.5 \cdot 2 = 1.
  3. Change the data by δb=(0,0.01)\delta b = (0, 0.01). The relative size of the change is 0.01/∥(1,1)∥=0.01/1.414≈0.00710.01 / \|(1, 1)\| = 0.01 / 1.414 \approx 0.0071.
  4. The answer changes by δx=A−1δb=(0,0.02)\delta x = A^{-1} \delta b = (0, 0.02). Its relative size is 0.02/∥(0.5,2)∥=0.02/2.062≈0.00970.02 / \|(0.5, 2)\| = 0.02 / 2.062 \approx 0.0097.
  5. Compare: 0.0097/0.0071≈1.370.0097 / 0.0071 \approx 1.37. The amplification is about 1.37, which is below the bound of 4. The bound is a worst case, and this direction is milder.
  6. Interpretation: the matrix can magnify errors by up to 4 times, and here the amplification was smaller.
Example
A nearly singular 2 by 2 system

This is a realistic case: two measurements that are almost the same, so the matrix is close to singular. Let A=(1111.0001)A = \begin{pmatrix} 1 & 1 \\ 1 & 1.0001 \end{pmatrix}. The matrix is symmetric, so its condition number is the ratio of its eigenvalues.

  1. The trace is 1+1.0001=2.00011 + 1.0001 = 2.0001, and the Determinant is 1⋅1.0001−1⋅1=0.00011 \cdot 1.0001 - 1 \cdot 1 = 0.0001.
  2. The larger eigenvalue is about λmax⁡≈2.00005\lambda_{\max} \approx 2.00005. The product of the two eigenvalues is the determinant, so λmin⁡=0.0001/2.00005≈0.00005\lambda_{\min} = 0.0001 / 2.00005 \approx 0.00005.
  3. The condition number is κ≈2.00005/0.00005≈40,001\kappa \approx 2.00005 / 0.00005 \approx 40{,}001.
  4. Suppose the data bb is accurate to one part in a million, a relative error of 10−610^{-6}. The bound gives a relative error in the answer of up to 40,001×10−6≈0.0440{,}001 \times 10^{-6} \approx 0.04, which is 4 percent.
  5. Interpretation: data accurate to about 6 digits gives an answer with only about 1 or 2 correct digits, because log⁡1040,001≈4.6\log_{10} 40{,}001 \approx 4.6 digits are lost to the matrix.

Try it: a system has condition number 10610^6, and its data is accurate to 8 digits, a relative error of 10−810^{-8}. What is the worst-case relative error in the answer? Answer: 106×10−8=10−210^6 \times 10^{-8} = 10^{-2}, so up to 1 percent.

Common mistakes and tradeoffs

It is tempting to think that a small residual means a correct answer. In fact the residual Ax−bA x - b measures how well the equation is satisfied, and the error in the answer can be up to κ(A)\kappa(A) times the relative residual. For a matrix with a large condition number, a solution can satisfy the equation almost exactly and still be far from the true answer.

It is tempting to think that a condition number of 10 is bad and one of 1,000 is terrible in every case. In fact a condition number of κ\kappa costs about log⁡10κ\log_{10} \kappa digits of accuracy. Whether that matters depends on how many correct digits your data has. Losing one digit out of 15 is harmless for many problems.

It is tempting to think that κ\kappa depends on the right-hand side bb. In fact κ(A)\kappa(A) depends only on the matrix. The bound is a worst case over all bb, and a particular bb may do much better.

Tradeoffs: computing κ\kappa exactly needs the singular values or the inverse, which costs about as much as solving the system by Gaussian elimination. Routines therefore use cheaper estimates. The value also depends on the norm chosen, although different norms give values that differ only by a bounded factor. Use κ\kappa to decide how much to trust an answer, not as the only test. When κ\kappa is large, the usual remedy is to change the problem: rescale the variables, choose a better basis, or use a Preconditioning step that reduces the effective condition number before an iterative solver such as Conjugate Gradient runs.

History

In 1948 Alan Turing, in a paper on rounding errors in matrix computations, used the term "condition number" for a quantity that measures how sensitive the solution of a linear system is to errors. Before that, von Neumann and Goldstine had studied how rounding errors grow when large matrices are inverted, in the late 1940s. The idea caught on as electronic computers made large linear systems routine and it became clear that a computed answer can be wrong in its leading digits even when the arithmetic is careful. Numerical libraries now report condition estimates as a standard safeguard.

Recap

  • The condition number of an invertible matrix is κ(A)=∥A∥ ∥A−1∥\kappa(A) = \|A\| \, \|A^{-1}\|. In the 2-norm this is the ratio of largest to smallest singular value.
  • For Ax=bA x = b, the relative error in xx is at most κ(A)\kappa(A) times the relative error in bb.
  • A large condition number means that many digits of the answer can be lost, and it depends on the matrix, not the data.
  • Read next: Preconditioning for changing a system to reduce its condition number, and Conjugate Gradient for a solver whose speed depends on it.

Check yourself

QuestionWhat is the condition number of the matrix diag(4, 2) in the 2-norm? Give a number.Show answer

The singular values are 4 and 2, so κ=4/2=2\kappa = 4 / 2 = 2.

QuestionA matrix has condition number 1000, and the data has a relative error of 0.001. What is the worst-case relative error in the solution?Show answer

The bound gives κ×0.001=1000×0.001=1\kappa \times 0.001 = 1000 \times 0.001 = 1, so the relative error in the answer can be as large as 100 percent. The answer could have no correct digits in the worst case.

QuestionWhy is a small residual not enough to trust a solution of an ill-conditioned system?Show answer

The residual Ax−bA x - b measures how well the equation holds, not how close xx is to the true answer. The error in xx can be up to κ(A)\kappa(A) times the relative residual, so with a large κ\kappa a solution that satisfies the equation almost exactly can still be far off.

QuestionA solver returns an answer with a relative residual of 10−1210^{-12} for a system with κ=1010\kappa = 10^{10}. Roughly how many correct digits can the answer have?Show answer

The bound gives a relative error of up to 1010×10−12=10−210^{10} \times 10^{-12} = 10^{-2}. So the answer may be right only to about two digits, even though the residual looks tiny.

Sources