What is Mahalanobis distance?

Mahalanobis distance measures how far a point is from a distribution while accounting for the scale and correlation of that distribution's own dimensions, unlike Euclidean distance which treats every dimension equally.
Created: Updated: 3 min read

Mahalanobis distance measures how far a point is from a distribution while accounting for how that distribution is spread out and correlated across dimensions, which sets it apart from Euclidean distance and the other distance functions covered elsewhere in this glossary that treat every dimension as equally scaled and independent of the others.

What problem does Mahalanobis distance actually solve?

Euclidean distance treats every dimension of a vector identically, adding up squared differences without asking whether one dimension naturally varies far more than another or whether two dimensions tend to move together. Real data often doesn’t behave this way: one dimension might naturally range across thousands of units while another stays within a narrow band of single digits, and Euclidean distance would let that first dimension dominate the result purely because of its scale, not because it’s actually more meaningful. Mahalanobis distance corrects for this by using the statistical spread and correlation of the underlying data itself, captured in what’s called a covariance matrix, to reweight each dimension’s contribution so that scale differences and correlations between dimensions no longer distort the result.

How does accounting for correlation change what counts as “close”?

Beyond just rescaling individual dimensions, Mahalanobis distance also accounts for the case where two dimensions tend to move together, such that a point deviating from that pattern is treated as unusual even if its raw Euclidean distance looks small. Picture two correlated dimensions where high values in one dimension typically accompany high values in the other: a point that has a high value in the first dimension but a low value in the second breaks that expected pattern, and Mahalanobis distance flags it as further away than a simple dimension-by-dimension comparison would suggest, precisely because it violates the correlation the rest of the data follows. This is why Mahalanobis distance is often described as measuring distance in a way that’s shaped by the data’s own statistical structure, rather than by the raw geometry of the space the vectors happen to sit in.

Why isn’t Mahalanobis distance more commonly used inside HNSW itself?

Computing Mahalanobis distance requires knowing that covariance matrix in advance and performing extra matrix operations for every single comparison, both of which add meaningful computational cost compared to the simpler distance functions covered elsewhere in this glossary, such as Euclidean distance or cosine similarity, which HNSW is typically built and tuned around. Because HNSW‘s performance depends heavily on how cheap each individual distance calculation is, covered in this site’s page on how distance calculations affect index performance, most practical vector search systems either normalize or transform their data ahead of time so a cheaper distance function becomes appropriate, rather than paying the ongoing cost of Mahalanobis distance at search time. Mahalanobis distance still sees use in statistics and anomaly detection, where its sensitivity to a dataset’s actual shape is exactly the point, but it’s a less common fit for the high-throughput approximate search HNSW is designed for.

Mahalanobis distance illustrates a broader point that applies across this glossary’s distance functions: the right choice depends on whether a dataset’s dimensions are independent and similarly scaled, or whether they carry structure worth accounting for directly. From here, the page on metric space covers the more general mathematical properties a distance function needs to have, and the page on choosing the right distance function works through this same trade-off in practical terms.