{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"condition-numbers","title":"The condition number, and how many digits a problem throws away","url":"https://addaly.com/learn/maths-you-need/condition-numbers","position":72,"of":76,"minutes":9,"answer":"The condition number σ_max/σ_min says how many digits a problem throws away, about log10(κ) of them, and the usual source of a large one is unscaled or correlated features.","keyIdea":"The condition number σ_max/σ_min says how many digits a problem throws away, about log10(κ) of them, and the usual source of a large one is unscaled or correlated features, which is why standardising columns fixes both a numerically meaningless fit and a slow gradient descent at once."},"body":{"format":"markdown","text":"## Some problems amplify error\n\nGive a computation an input that is wrong in its seventh digit. Some computations return an answer wrong in the seventh digit; others return an answer wrong in the first. The second kind are **ill-conditioned**, and the condition number is how much they amplify the error. Nothing about the algorithm or the hardware changes this. It is a property of the problem.\n\nFor a matrix, module 2's singular values give the number directly:\n\n```\nκ = σ_max / σ_min\n```\n\nthe ratio of the largest stretch the matrix applies to the smallest. A matrix that stretches one direction by 1,000 and another by 0.001 has `κ = 10^6`. The identity has `κ = 1`. A singular matrix, which flattens some direction entirely, has `κ = ∞`.\n\n## The rule of thumb\n\nSolving a linear system, fitting a least-squares model, or inverting a matrix with condition number `κ` loses about `log10(κ)` digits of accuracy. Float32 has seven; float64 has sixteen.\n\n```\nκ = 10^2   float32 keeps 5 digits   float64 keeps 14\nκ = 10^6   float32 keeps 1 digit    float64 keeps 10\nκ = 10^8   float32 keeps nothing    float64 keeps 8\nκ = 10^16  float64 keeps nothing\n```\n\nThe Hilbert matrix, whose entries are `1/(i + j − 1)`, is the classic case: the 5 × 5 version has `κ ≈ 5 × 10^5`, the 10 × 10 version `κ ≈ 10^13`. Solving a 10 × 10 Hilbert system in float64 gives three correct digits; in float32, garbage with no warning. Module 2's advice to solve rather than invert stands, but it does not rescue a problem this badly conditioned. Nothing does, except changing the problem.\n\n## Where it comes from in practice: scale\n\nThe commonest ill-conditioned matrix in machine learning is the one you build from unscaled features. Take a regression with one feature in the range 0 to 1 and another in the range 0 to 1,000,000. The matrix `XᵀX` from module 2's normal equations has one direction stretched by about `10^12` relative to the other: `κ ≈ 10^12`. Solving it in float64 leaves four digits. Solving it in float32 leaves none, and the coefficient on the small feature comes out as noise.\n\nStandardise both features to mean 0 and spread 1, and `κ` drops to the ratio set by their correlation, usually under 10. Same data, same model, twelve orders of magnitude of conditioning recovered by subtracting a mean and dividing by a spread. This is the numerical reason behind the feature-scaling advice in `machine-learning-foundations`; the statistical reasons are there, and this is the arithmetic one.\n\nPolynomial features are worse. Fitting `x, x², …, x^10` on `x` between 0 and 1,000 gives columns ranging from `10^3` to `10^30`, a condition number beyond float64's reach, and coefficients that are pure rounding error. Rescale `x` to `[−1, 1]` first, or use an orthogonal polynomial basis, which is the same fix by another name.\n\n## Gradient descent feels it too\n\nModule 3's curvature lesson showed that plain gradient descent zigzags when the loss is much more curved in one direction than another, and that the ratio of curvatures sets the number of steps. That ratio *is* the condition number of the Hessian. A quadratic loss with `κ = 10^4` needs on the order of `10^4` steps of plain gradient descent to converge to a fixed accuracy; with `κ = 10` it needs about ten. Feature scaling, normalisation layers, and adaptive optimisers such as Adam are all, from this angle, ways of reducing the condition number the optimiser sees. The numerical and the optimisation problems are one problem.\n\n## Correlated features\n\nTwo columns that are nearly copies of each other, a price in dollars and the same price in cents, make `XᵀX` nearly singular, because the matrix cannot tell them apart. The condition number rises without limit as the correlation approaches 1, and the fitted coefficients become huge, opposite in sign and meaningless, while the predictions stay fine. The L2 penalty of module 6 adds `λ` to every singular value, which bounds `κ` by `σ_max / λ` and is the numerical reason ridge regression was invented, before anyone described it as a prior.\n\n## Computing it\n\n```python\nimport numpy as np\nX = np.column_stack([np.random.rand(1000), 1e6 * np.random.rand(1000)])\nprint(np.linalg.cond(X))                 # about 1e6 for X, 1e12 for X.T @ X\nXs = (X - X.mean(0)) / X.std(0)\nprint(np.linalg.cond(Xs))                # a few\n```\n\n`np.linalg.cond` computes the singular-value ratio. Print it for any matrix you are about to solve with, and read `log10` of the result as the digits you are about to lose. Above `10^8` in float32 or `10^15` in float64, the answer the solver returns is not an approximation of the truth; it is an artefact of rounding, and the solver will not say so."},"check":{"question":"A least-squares fit with a feature in metres and another in nanometres gives coefficients that change wildly when one row of data is edited, though predictions look fine. What is going on?","options":[{"text":"The two features are nearly collinear, so the model cannot separate them.","correct":false,"tempting":"Collinearity is a different route to ill-conditioning; here the cause is a scale ratio of 10⁹ between columns, which happens even when the features are unrelated."},{"text":"Least squares is unstable and gradient descent should be used instead.","correct":false,"tempting":"Gradient descent sees the same Hessian and the same condition number, and would take on the order of κ steps; rescaling the feature is the fix under either solver."},{"text":"The edited row is an outlier that squared error weights heavily.","correct":false,"tempting":"An outlier moves the fit, but a well-conditioned fit moves it by a bounded amount; wild swings from a single ordinary edit are the mark of a condition number that has consumed every digit."},{"text":"A 10⁹ scale ratio gives κ near 10¹⁸; the coefficients are noise.","correct":true}]},"prev":{"slug":"quantisation-as-rounding","title":"Quantisation is rounding with a scale: int8, int4 and the outlier problem","url":"https://addaly.com/learn/maths-you-need/quantisation-as-rounding"},"next":{"slug":"variance-through-a-layer","title":"How variance travels through a layer, and the initialisation that follows","url":"https://addaly.com/learn/maths-you-need/variance-through-a-layer"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'The condition number, and how many digits a problem throws away', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/condition-numbers (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"The condition number, and how many digits a problem throws away","courseTitle":"The Maths You Actually Need","module":{"index":8,"slug":"numbers-in-a-machine","title":"Numbers inside a machine"},"answer":null,"keyIdea":"The condition number σ_max/σ_min says how many digits a problem throws away, about log10(κ) of them, and the usual source of a large one is unscaled or correlated features, which is why standardising columns fixes both a numerically meaningless fit and a slow gradient descent at once.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"some-problems-amplify-error","runs":[{"text":"Some problems amplify error"}]},{"t":"p","runs":[{"text":"Give a computation an input that is wrong in its seventh digit. Some computations return an answer wrong in the seventh digit; others return an answer wrong in the first. The second kind are "},{"text":"ill-conditioned","b":true},{"text":", and the condition number is how much they amplify the error. Nothing about the algorithm or the hardware changes this. It is a property of the problem."}]},{"t":"p","runs":[{"text":"For a matrix, module 2’s singular values give the number directly:"}]},{"t":"code","lang":"","label":"Text","text":"κ = σ_max / σ_min"},{"t":"p","runs":[{"text":"the ratio of the largest stretch the matrix applies to the smallest. A matrix that stretches one direction by 1,000 and another by 0.001 has "},{"text":"κ = 10^6","code":true},{"text":". The identity has "},{"text":"κ = 1","code":true},{"text":". A singular matrix, which flattens some direction entirely, has "},{"text":"κ = ∞","code":true},{"text":"."}]},{"t":"h2","id":"the-rule-of-thumb","runs":[{"text":"The rule of thumb"}]},{"t":"p","runs":[{"text":"Solving a linear system, fitting a least-squares model, or inverting a matrix with condition number "},{"text":"κ","code":true},{"text":" loses about "},{"text":"log10(κ)","code":true},{"text":" digits of accuracy. Float32 has seven; float64 has sixteen."}]},{"t":"code","lang":"","label":"Text","text":"κ = 10^2   float32 keeps 5 digits   float64 keeps 14\nκ = 10^6   float32 keeps 1 digit    float64 keeps 10\nκ = 10^8   float32 keeps nothing    float64 keeps 8\nκ = 10^16  float64 keeps nothing"},{"t":"p","runs":[{"text":"The Hilbert matrix, whose entries are "},{"text":"1/(i + j − 1)","code":true},{"text":", is the classic case: the 5 × 5 version has "},{"text":"κ ≈ 5 × 10^5","code":true},{"text":", the 10 × 10 version "},{"text":"κ ≈ 10^13","code":true},{"text":". Solving a 10 × 10 Hilbert system in float64 gives three correct digits; in float32, garbage with no warning. Module 2’s advice to solve rather than invert stands, but it does not rescue a problem this badly conditioned. Nothing does, except changing the problem."}]},{"t":"h2","id":"where-it-comes-from-in-practice-scale","runs":[{"text":"Where it comes from in practice: scale"}]},{"t":"p","runs":[{"text":"The commonest ill-conditioned matrix in machine learning is the one you build from unscaled features. Take a regression with one feature in the range 0 to 1 and another in the range 0 to 1,000,000. The matrix "},{"text":"XᵀX","code":true},{"text":" from module 2’s normal equations has one direction stretched by about "},{"text":"10^12","code":true},{"text":" relative to the other: "},{"text":"κ ≈ 10^12","code":true},{"text":". Solving it in float64 leaves four digits. Solving it in float32 leaves none, and the coefficient on the small feature comes out as noise."}]},{"t":"p","runs":[{"text":"Standardise both features to mean 0 and spread 1, and "},{"text":"κ","code":true},{"text":" drops to the ratio set by their correlation, usually under 10. Same data, same model, twelve orders of magnitude of conditioning recovered by subtracting a mean and dividing by a spread. This is the numerical reason behind the feature-scaling advice in "},{"text":"machine-learning-foundations","code":true},{"text":"; the statistical reasons are there, and this is the arithmetic one."}]},{"t":"p","runs":[{"text":"Polynomial features are worse. Fitting "},{"text":"x, x², …, x^10","code":true},{"text":" on "},{"text":"x","code":true},{"text":" between 0 and 1,000 gives columns ranging from "},{"text":"10^3","code":true},{"text":" to "},{"text":"10^30","code":true},{"text":", a condition number beyond float64’s reach, and coefficients that are pure rounding error. Rescale "},{"text":"x","code":true},{"text":" to "},{"text":"[−1, 1]","code":true},{"text":" first, or use an orthogonal polynomial basis, which is the same fix by another name."}]},{"t":"h2","id":"gradient-descent-feels-it-too","runs":[{"text":"Gradient descent feels it too"}]},{"t":"p","runs":[{"text":"Module 3’s curvature lesson showed that plain gradient descent zigzags when the loss is much more curved in one direction than another, and that the ratio of curvatures sets the number of steps. That ratio "},{"text":"is","i":true},{"text":" the condition number of the Hessian. A quadratic loss with "},{"text":"κ = 10^4","code":true},{"text":" needs on the order of "},{"text":"10^4","code":true},{"text":" steps of plain gradient descent to converge to a fixed accuracy; with "},{"text":"κ = 10","code":true},{"text":" it needs about ten. Feature scaling, normalisation layers, and adaptive optimisers such as Adam are all, from this angle, ways of reducing the condition number the optimiser sees. The numerical and the optimisation problems are one problem."}]},{"t":"h2","id":"correlated-features","runs":[{"text":"Correlated features"}]},{"t":"p","runs":[{"text":"Two columns that are nearly copies of each other, a price in dollars and the same price in cents, make "},{"text":"XᵀX","code":true},{"text":" nearly singular, because the matrix cannot tell them apart. The condition number rises without limit as the correlation approaches 1, and the fitted coefficients become huge, opposite in sign and meaningless, while the predictions stay fine. The L2 penalty of module 6 adds "},{"text":"λ","code":true},{"text":" to every singular value, which bounds "},{"text":"κ","code":true},{"text":" by "},{"text":"σ_max / λ","code":true},{"text":" and is the numerical reason ridge regression was invented, before anyone described it as a prior."}]},{"t":"h2","id":"computing-it","runs":[{"text":"Computing it"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\nX = np.column_stack([np.random.rand(1000), 1e6 * np.random.rand(1000)])\nprint(np.linalg.cond(X))                 # about 1e6 for X, 1e12 for X.T @ X\nXs = (X - X.mean(0)) / X.std(0)\nprint(np.linalg.cond(Xs))                # a few"},{"t":"p","runs":[{"text":"np.linalg.cond","code":true},{"text":" computes the singular-value ratio. Print it for any matrix you are about to solve with, and read "},{"text":"log10","code":true},{"text":" of the result as the digits you are about to lose. Above "},{"text":"10^8","code":true},{"text":" in float32 or "},{"text":"10^15","code":true},{"text":" in float64, the answer the solver returns is not an approximation of the truth; it is an artefact of rounding, and the solver will not say so."}]}],"check":{"question":[{"text":"A least-squares fit with a feature in metres and another in nanometres gives coefficients that change wildly when one row of data is edited, though predictions look fine. What is going on?"}],"options":[{"text":[{"text":"The two features are nearly collinear, so the model cannot separate them."}],"correct":false,"tempting":[{"text":"Collinearity is a different route to ill-conditioning; here the cause is a scale ratio of 10⁹ between columns, which happens even when the features are unrelated."}]},{"text":[{"text":"Least squares is unstable and gradient descent should be used instead."}],"correct":false,"tempting":[{"text":"Gradient descent sees the same Hessian and the same condition number, and would take on the order of κ steps; rescaling the feature is the fix under either solver."}]},{"text":[{"text":"The edited row is an outlier that squared error weights heavily."}],"correct":false,"tempting":[{"text":"An outlier moves the fit, but a well-conditioned fit moves it by a bounded amount; wild swings from a single ordinary edit are the mark of a condition number that has consumed every digit."}]},{"text":[{"text":"A 10⁹ scale ratio gives κ near 10¹⁸; the coefficients are noise."}],"correct":true,"tempting":null}],"order":[1,2,0,3],"why":null},"prev":{"slug":"quantisation-as-rounding","title":"Quantisation is rounding with a scale: int8, int4 and the outlier problem","minutes":10},"next":{"slug":"variance-through-a-layer","title":"How variance travels through a layer, and the initialisation that follows","minutes":9}}}