AI Glossary / Mathematics and optimization / Intermediate
Mahalanobis distance
Definition
Mahalanobis distance measures separation between numerical vectors after accounting for a chosen covariance structure. With positive-definite covariance matrix Σ, the distance between x and μ is √[(x−μ)ᵀΣ⁻¹(x−μ)]. It adjusts for both the scale of individual features and their correlations, so it can differ substantially from ordinary Euclidean distance.
What it means
A difference that is common along a highly variable direction can count as less unusual than a smaller difference along a tightly concentrated direction. Correlations also matter: two measurements that usually rise together may make an opposite-direction change especially distinctive.
The reference covariance must be specified. A population covariance, a sample estimate, and a regularized estimate can produce different distances for the same pair of vectors. The choice should reflect the comparison being made, not simply whichever matrix is available.
The distance is a geometric quantity, not automatically a probability or an explanation of why an observation is unusual. Converting it into a tail probability requires additional distributional assumptions and attention to whether the center and covariance are known or estimated.
How it works
Subtract the reference vector to obtain d=x−μ. Solve Σz=d and form dᵀz, then take its square root. This is equivalent to using the inverse covariance in the formula, while a numerical solve can avoid explicitly constructing the inverse.
If Σ is diagonal, the squared distance is the sum of squared coordinate differences divided by their variances. With off-diagonal covariance, the adjustment also rotates the effective geometry. Whitening provides another interpretation: transform coordinates so the chosen covariance becomes identity, then use Euclidean distance.
Positive definiteness gives a genuine distance metric. A singular covariance has no ordinary inverse. Restricting to a supported subspace or using regularization requires an explicit convention; a pseudoinverse on the full space can make distinct vectors have zero separation along ignored directions.
Three practical examples
- A multivariate monitoring exercise can rank observations by covariance-adjusted distance from a reference center.
- A classifier with shared within-class covariance can compare observations with class means using the corresponding geometry.
- A matching or clustering workflow can use a covariance-based metric when feature scale and correlation are relevant to similarity.
Think of it this way
Think of measuring travel across a landscape where some directions are naturally broad and others narrow. Moving along a broad direction counts less than moving across a narrow one. The analogy captures anisotropic scaling, but covariance defines a precise quadratic geometry; it does not represent actual roads or establish that a particular direction is desirable.
Worked example
Consider an original two-feature reference distribution with center μ=(0,0) and covariance matrix Σ having rows (4,1.5) and (1.5,1). Its determinant is 4−2.25=1.75, and both leading principal minors are positive, so it is positive definite. The feature standard deviations are 2 and 1, with correlation 1.5/(2×1)=0.75.
Its inverse has rows (4/7,−6/7) and (−6/7,16/7). Compare points P=(2,1) and Q=(2,−1). Both have Euclidean distance √5 from the center, so an ordinary Euclidean comparison treats them equally.
For P, the squared Mahalanobis distance is (4/7)×2²+2(−6/7)×2×1+(16/7)×1²=8/7. Its distance is √(8/7), approximately 1.069. For Q, the cross term changes sign, giving squared distance 16/7+24/7+16/7=8 and distance √8, approximately 2.828.
The difference reflects positive correlation. P moves both features upward together, following the broad correlated direction. Q moves them in opposite directions, which is less consistent with that covariance pattern.
If we discard the correlation and instead use covariance diag(4,1), both squared distances become 2²/4+1²/1=2. The calculation demonstrates that standardizing each feature separately does not reproduce the full correlation adjustment. It also distinguishes the squared distance from the distance itself.
Illustrative case study
In a fictional manufacturing-analysis project, two sensor readings usually increase together. A monitoring team finds that checking each reading separately misses combinations in which one rises while the other falls. It explores a covariance-adjusted distance as one diagnostic.
Before setting a review threshold, the team examines whether the reference period represents normal operating modes and whether a few unusual records distort the covariance estimate. It also investigates flagged cases using process knowledge. This hypothetical workflow does not claim that a large distance proves a fault or that one threshold fits every machine.
Common misconceptions and mistakes
Do not use the covariance matrix where an implementation expects its inverse. Do not omit the square root and still label the result as the ordinary distance. An inverse or factorization based on a nearly singular covariance can be numerically unstable.
Large distance does not automatically mean an error, and small distance does not prove an observation is safe or typical in every respect. A single covariance ellipse can poorly describe a multimodal population. Also, fitting the reference covariance using evaluation observations can contaminate a comparison intended to assess performance on unseen data.
Business use
Mahalanobis distance can help compare multivariate observations when raw units and correlations would make Euclidean distance misleading. Document the reference population, feature ordering, center, covariance estimation method, and any regularization. Check whether the chosen geometry matches operational similarity.
If the distance supports an alert, evaluate the alerting rule on realistic data and report false alarms and missed events. Changes in operating conditions can alter the appropriate reference distribution. A reusable distance calculation therefore still requires maintenance of the assumptions and data behind its covariance matrix.
People also ask
Is it just Euclidean distance after standardization?
Only in the diagonal-covariance case. The full version also accounts for correlations.
Does it require Gaussian data to calculate?
No. The formula can be used with a suitable positive-definite covariance; Gaussian assumptions matter for some probabilistic interpretations.
What if the covariance is singular?
The ordinary inverse is unavailable. A subspace or regularized approach must be defined, and it changes the interpretation.
Check your understanding
With covariance diag(9,4) and displacement (3,2), what are the squared Mahalanobis distance and distance?
Answer: The squared distance is 9/9+4/4=2; the distance is √2.
Related terms
- Covariance matrix
- Correlation
- Euclidean distance
- Anomaly detection
- Linear discriminant analysis
- Regularization
- whitening
- positive-definite matrix
- inverse covariance
Learn next
Next, compare equal-length displacements along and against a positive correlation direction. Then study covariance estimation and whitening to see how the reference data determine the geometry used in the distance.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Work in finance? Get the free CFO AI Prompt Pack: 7 prompts for finance leaders.
Sources
Primary implementation reference: SciPy, scipy.spatial.distance.mahalanobis, official definition and inverse-covariance argument convention: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.mahalanobis.html . The correlated two-feature example and matrix inverse are original calculations.
Previous: Magnitude of effect Next: Mail survey