Mahalanobis distance is a measure of separation between vectors that accounts for the scales and relationships of their components. Unlike ordinary Euclidean distance, it measures displacement relative to a specified covariance matrix: differences along highly variable directions contribute less, while differences along tightly concentrated directions contribute more. Introduced by the Indian statistician Prasanta Chandra Mahalanobis in 1936, it is widely used in multivariate statistics, pattern recognition, and anomaly detection.
Definition
Let , and let be a symmetric positive-definite matrix. The Mahalanobis distance between and , relative to , is
where denotes transpose and is the inverse of .
Frequently, the reference point is the mean vector of a probability distribution. The distance of an observation from that mean is then
The quantity
is the squared Mahalanobis distance. Software and statistical literature sometimes report this squared quantity simply as “Mahalanobis distance,” so the convention must be checked when interpreting results.
The matrix is typically a population covariance matrix or an estimate obtained from reference data. Its inverse is often called the precision matrix. The distance is not a property of the two points alone: changing the reference covariance changes their separation.
Scale and correlation
Standardization by variability
For a single variable with mean and standard deviation ,
Thus, one-dimensional Mahalanobis distance is the absolute standardized deviation from the mean.
If the covariance matrix is diagonal,
then
This is Euclidean distance after each coordinate has been divided by its standard deviation. If is the identity matrix, Mahalanobis distance reduces exactly to Euclidean distance.
Adjustment for correlated variables
When variables are correlated, standardizing each coordinate separately is insufficient. Mahalanobis distance also accounts for their correlation.
Consider two variables with unit variances and correlation , where . For a displacement ,
If , then the displacements and have the same Euclidean length, but their squared Mahalanobis distances are
and
The first displacement follows the usual joint variation of positively correlated variables. The second runs against that pattern and is therefore much more unusual relative to the reference covariance.
Geometric interpretation
Ellipsoidal contours
Points at a fixed distance from satisfy
These sets are ellipsoids; in two dimensions they are ellipses.
Writing the spectral decomposition as
the columns of are orthonormal eigenvectors, and
contains positive eigenvalues. If , then
The ellipsoid’s axes follow the eigenvectors, and its semiaxis lengths are . Directions with larger variance permit larger displacements at the same Mahalanobis distance. This decomposition also connects the distance to principal component analysis.
Whitening
Define transformed coordinates by
Then
This transformation, called whitening, turns covariance-adjusted distance into ordinary Euclidean distance. If a random vector has covariance , its whitened version has identity covariance. Whitening removes linear correlations, but it does not generally establish statistical independence; that stronger conclusion holds for jointly normal variables.
Metric properties
For a fixed positive-definite ,
defines an inner product, and Mahalanobis distance is the associated distance. It therefore defines a metric: it is nonnegative, symmetric, zero only for identical points, and satisfies the triangle inequality.
The squared distance is not itself a metric because it generally fails the triangle inequality. Likewise, if a different covariance matrix is chosen separately for each pair of points, the resulting rule need not be a metric.
Invariance under changes of coordinates
Mahalanobis distance is invariant under invertible affine transformations, provided the covariance is transformed consistently.
Let
where is invertible. The transformed covariance is
Then
Consequently, changing measurement units, rotating coordinates, or applying an invertible linear mixing does not change the distance when the covariance is updated accordingly.
This invariance does not generally extend to transformations that discard information, such as projection onto fewer dimensions.
Relationship to probability distributions
Multivariate normal distributions
For a multivariate normal distribution with mean and positive-definite covariance , the probability density function is
Thus, equal-density contours are also equal-Mahalanobis-distance ellipsoids. For a fixed Gaussian distribution, density decreases as distance from its mean increases.
If
with the true parameters used in the distance, then
a chi-squared distribution with degrees of freedom. This gives probability regions of the form
which contain probability .
This chi-squared result requires multivariate normality and known population parameters. It does not automatically hold when the mean and covariance are estimated from the same observations being assessed.
Beyond normality
Mahalanobis distance remains mathematically meaningful for nonnormal distributions with a finite, positive-definite covariance matrix. Its interpretation as a direct measure of rarity is then more limited.
For any random vector with mean and nonsingular covariance ,
regardless of normality. However, the full distribution of depends on the distribution of .
Covariance alone cannot describe multimodality, curved dependence, or all aspects of tail behavior. A point close to the overall mean can therefore lie in a low-density region, especially when the data contain separated clusters.
Estimation from data
For observations , a common estimate uses the sample mean
and sample covariance
The estimated squared distance is
Its statistical behavior depends on whether belongs to the estimation sample or is an independent new observation.
A related statistic is Hotelling’s , used in hypothesis testing about a multivariate population mean. For independent observations from a multivariate normal population,
Under the null hypothesis that the population mean is , and for ,
This concerns the estimated mean, not the distance of an individual observation.
Applications
Anomaly detection. Large distances identify observations far from a reference center relative to its covariance structure. This can reveal unusual combinations of measurements that would not appear extreme when examined separately.
Classification. A common covariance matrix produces a shared geometry for comparing observations with class centers. Under Gaussian class models with equal covariance and equal prior probabilities, choosing the nearest center by Mahalanobis distance is equivalent to choosing the most probable class. With unequal covariances, Gaussian classification also requires covariance-determinant terms and, where applicable, class-prior terms.
Clustering and similarity search. In cluster analysis and pattern matching, covariance-adjusted distances account for elongated groups and correlated features. Cluster-specific covariance matrices describe different local shapes, although they do not necessarily define a single global metric.
Multivariate monitoring. Distances combine several correlated measurements into a single statistic for detecting changes in an industrial or observational process.
Metric learning. In machine learning, a generalized squared distance is often written
where is learned from data rather than necessarily chosen as an inverse covariance. A positive-definite gives a metric; a positive-semidefinite may give only a pseudometric.
Limitations and numerical computation
Singular and poorly estimated covariance
The sample covariance has rank at most , so it is necessarily singular when . Exact linear dependencies among variables can also make it singular.
A generalized inverse can replace the ordinary inverse, but then nonzero displacements in the covariance null space receive zero distance. The result is a pseudometric on the full space, although it can be a genuine metric on the retained subspace.
Near-singularity is also problematic. Small estimated eigenvalues receive large inverse weights, potentially magnifying noise and making distances unstable.
Regularization can stabilize the calculation. One example is
Shrinkage toward a diagonal or otherwise structured covariance matrix is another approach. These modifications change the geometry and generally sacrifice exact affine invariance.
Sensitivity to outliers
Ordinary means and covariance estimates are sensitive to extreme observations. An outlier can shift the estimated center and inflate variance in its own direction, reducing its apparent distance—an effect called masking. Robust location and covariance estimates are used to reduce such effects, although their calibration and performance depend on the estimation method and assumptions.
Stable calculation
Explicitly forming is usually unnecessary. If a Cholesky decomposition gives
solve
and compute
This avoids constructing an inverse and generally improves numerical stability. When the covariance is singular or nearly singular, eigenvalue methods or singular-value decomposition can make the treatment of small-variance directions explicit.