Quant GT
Browse all lessons
Section 17 · Lesson 17.6

Numerical Stability

Conditioning, rank, and why naive code can silently lose precision.

Numerical methods can fail silently when matrices are ill-conditioned. The condition number κ(A)=σ1/σn\kappa(A) = \sigma_1 / \sigma_n (ratio of largest to smallest singular values) measures sensitivity. A high κ\kappa means small changes in input produce huge changes in output.

For linear systems Ax=bAx = b, the relative error in xx can be up to κ(A)\kappa(A) times the relative error in bb. With double precision (16\sim 16 digits), κ=1010\kappa = 10^{10} leaves about 66 trustworthy digits.

Common pitfalls:

Computing XXX^\top X before solving regression squares the condition number. Use QR or SVD directly.Inverting matrices is a code smell. Solve linear systems instead — same answer, more stable.Subtracting nearly-equal numbers loses precision (catastrophic cancellation).

In quant work, near-singular covariance matrices appear all the time (e.g. when assets are highly correlated). Regularization (shrinkage, ridge) and PCA-based dimensionality reduction are the standard defenses.