The Mahalanobis distance is a very specialized measure of distance, used to compare a point and an -dimensional probability distribution .

There are two ways to think about this comparison: either as a direct comparison between the point and the distribution, or as a comparison between the point and the centroid of the distribution.

Point and centroid: Interpreted as the number of standard deviations away is from the centroid of .

Point and distribution: Provides a measure of how well the point “fits into” the distribution, taking into account its shape, position, and orientation in -dimensional space.

If is the positive semi-definite covariance matrix of the distribution and is its mean vector, then the Mahalanobis distance is given as

Given a known value (or reasonable estimate) of and , the Mahalanobis distance can be used for nearest-neighbor search by letting , where and are a query vector and a neighbor candidate.

This can be useful when the features are correlated, as the Mahalanobis distance takes correlation into account. In particular, it can be useful for anomaly detection, since an anomaly by definition fails to conform to expected correlations between features.