- Introduced by
- Mahalanobis, P. C.
- Year
- 1936
- Type
- Distance (covariance-adjusted, correlation-aware)
- Family
- Distance
What does it do?
Mahalanobis distance is a statistical distance that measures how far a vector x lies from a reference point μ, with the differences scaled by the covariance matrix Σ of the variables.
Unlike Euclidean distance, it accounts for correlations between variables and for differences in their spread. The measure is attributed to P. C. Mahalanobis.
The intuition behind the method
Euclidean distance treats every variable as independent and equally spread. When variables are correlated, a point can be an unusual combination of values while still being close in raw Euclidean terms. Mahalanobis distance divides out the covariance structure, so a deviation along a direction in which the data normally vary a lot counts for less than the same raw deviation along a direction where the data vary little. The result is a distance expressed in units of the data's own spread and correlation rather than in the original measurement units.
History
The measure is attributed to P. C. Mahalanobis. In applied multivariate statistics it is used for screening multivariate outliers and, as an extreme-value score, for identifying unusual multivariate observations.
When to use it
Use Mahalanobis distance when each observation is a vector of numerical variables and you want the distance from an observation to a centre (for example a sample mean) to reflect correlations and different spreads among the variables. Typical cases are screening for multivariate outliers and checking how far a new record lies from a reference group. Its usual probabilistic reading, in which the squared distance is compared with a chi-square distribution, assumes approximately multivariate normal data. Do not use it for categorical variables, and do not use it when there are too few observations relative to the number of variables for Σ to be estimated reliably. It is also not a ranking method: it does not weight criteria or order alternatives, so a decision problem that requires ranking needs a separate MCDM method.
Limitations and considerations
- The distance depends entirely on Σ. If Σ is estimated from a sample, errors in that estimate carry directly into every distance computed from it.
- The formula measures distance from a single point to a reference centre; it has no weighting step and does not by itself combine or rank alternatives.
- Because the mean and covariance are sensitive to extreme observations, a reference sample that contains outliers can distort the distances used to detect them.
- The chi-square interpretation and the Gaussian density link assume approximately multivariate normal data, and the distance is sensitive to failures of normality, which can distort the distances and their interpretation.
- Under some conditions Mahalanobis distance can mask a real outlier (a false negative) or swamp a normal case (a false positive). It is therefore not a fully reliable indicator of multivariate outliers and should be used with caution.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- The inverse covariance term Σ^(-1) in the formula down-weights deviations along directions where the variables are strongly correlated or highly variable, so the distance reflects the joint structure of the data rather than raw differences.
- The distance is invariant to nonsingular linear transformations of the variables, provided the mean and covariance are transformed consistently, so separate unit conversion of each variable is not needed for this purpose. The distance still depends on the estimated covariance matrix.
- When Σ is the identity matrix, the formula reduces to Euclidean distance, as the worked example shows (the distance between [3, 5] and [5, 3] is the square root of 8).
Applications
- Multivariate outlier screening, by flagging observations whose distance from a reference mean is large relative to the covariance of the variables. For screening, the squared distance is compared with a chi-square distribution with degrees of freedom equal to the number of variables, and a very conservative probability level such as p < .001 is considered appropriate. This is a screening rule rather than a formal test, and the chi-square reference is approximate when μ and Σ are estimated from the data.
- Checking whether a new observation resembles a reference group when its variables are correlated.
- Assigning an observation to the nearest of several groups based on distance to each group mean. This use assumes that the groups have similar covariance structures, and it is a simple rule rather than a full classification method, which is usually preferred for formal assignment.
How it works
How it works
Take the difference between the observation x and the reference point μ, then form the quadratic term with the inverse covariance matrix Σ^(-1) in the middle. Take the square root of that quadratic form to obtain d. The formula requires Σ to be invertible. In practice μ is usually the sample mean of a reference sample and Σ is its sample covariance matrix; the formula itself does not specify this estimation. The estimate needs enough observations relative to the number of variables for Σ to be stable, and any estimation error carries into every distance computed from it. When a case is screened for outlier status, its mean and covariance are often estimated from the remaining cases, so that the case does not influence its own reference.
Formula
d_{Mah}(\mathbf{x},\boldsymbol{\mu}) = \sqrt{(\mathbf{x}-\boldsymbol{\mu})^\top \Sigma^{-1}(\mathbf{x}-\boldsymbol{\mu})}Common pitfalls
- Σ must be invertible. If there are not enough observations relative to the number of variables, or if some variables are exact linear combinations of others, the estimated covariance matrix is singular and the formula cannot be evaluated.
- When variables are highly correlated, Σ can be nearly singular. Its inverse then becomes numerically unstable, and small changes in the data can produce large changes in the distances.
- Applying the formula to a single raw vector without a reference mean and covariance does not give a meaningful distance; the result depends on which centre and which covariance estimate are used.
Frequently asked questions
Is Mahalanobis distance the same as Euclidean distance?
It reduces to Euclidean distance when the covariance matrix Σ is the identity matrix, as in the worked example. In general, the inverse covariance term changes the weight given to each direction, so the two distances differ whenever the variables are correlated or have different spreads.
Do I need to normalise the variables before computing it?
Not for scale in the usual sense: the covariance term accounts for the spread of each variable, and the distance is invariant to nonsingular linear transformations of the variables when the mean and covariance are transformed consistently. The formula still requires Σ to be estimable and invertible.
Can Mahalanobis distance be used to rank alternatives in a decision matrix?
The formula in this entry measures distance between two vectors and does not include criteria weights or an ordering step. Using it to rank alternatives would require an additional method that defines how the distances are combined into a ranking.
Sources
Mahalanobis, P. C. (1936). Mahalanobis Distance. Proceedings of the National Institute of Sciences of India
Open publication reference2015_Book_DataMining, p. 243
Using Multivariate Statistics (6th Edition), p. 108
Using Multivariate Statistics (5th Edition), p. ?