Quant Memo
Advanced

Mahalanobis Distance and Multivariate Outliers

A way of measuring distance that accounts for correlation, so a data point can be flagged as a genuine outlier even when it looks perfectly ordinary on every variable taken one at a time.

Prerequisites: The Sample Covariance Matrix and Eigenvalue Bias

Picture a scatter plot of a stock's daily return against its sector's daily return. Normally these move together — up days tend to come together, down days tend to come together — so the cloud of points stretches out as a diagonal ellipse rather than a circle. Now consider two unusual days. Day A: the stock is up 3% and the sector is down 3%. Day B: the stock is up 6% and the sector is up 5.8%. Which day is the stranger one? Measured by plain distance from the center of the cloud, Day B looks far more extreme — both numbers are large. But Day B sits right on the diagonal, exactly where the normal relationship predicts a big up day for both should land. Day A, despite having smaller individual numbers, breaks the relationship entirely — the stock and its sector moved in opposite directions, which almost never happens. Day A is the real outlier. Ordinary Euclidean distance from the mean gets this backwards, because it ignores correlation. Mahalanobis distance is built to get it right.

off-diagonal: small Euclidean,
large Mahalanobis large Mahalanobis on-diagonal: large Euclidean, small Mahalanobis
The off-diagonal point sits close to the cloud's center in raw distance but breaks the correlation pattern — high Mahalanobis distance. The on-diagonal point is far from the center in raw distance but consistent with how the variables normally move together — low Mahalanobis distance.

The formula, one symbol at a time

For a point xx (a vector of pp measurements) with population or sample mean μ\mu and covariance matrix Σ\Sigma, the Mahalanobis distance squared is:

D2=(xμ)Σ1(xμ)D^2 = (x - \mu)' \, \Sigma^{-1} \, (x - \mu)

Correlation explorer
X →Y ↑
ρ = 0.80r² = 0.64relationship: strong positive

Drag the correlation slider in the explorer above toward a strong value like 0.8, matching the worked example below — notice how the cloud tightens into a diagonal band; a point that strays off that band, even slightly, is exactly what Mahalanobis distance is built to flag, regardless of how close it sits to the center in plain Euclidean terms.

In words: take the vector of how far xx is from the mean on each variable, and instead of just squaring and summing those raw differences (which is what plain Euclidean distance does), weight them by the inverse of the covariance matrix. The inverse covariance automatically down-weights directions where the variables naturally vary a lot together (so a big joint move there isn't surprising) and up-weights directions that go against the normal correlation pattern (so a small move in an unusual direction counts for a lot). When Σ\Sigma is diagonal (no correlation, unequal variances), this reduces to ordinary scaled Euclidean distance — each variable divided by its own variance. It's the off-diagonal, correlation terms in Σ1\Sigma^{-1} that do the real work of catching relationship-breaking outliers.

Mahalanobis distance isn't just "Euclidean distance but bigger" — it's differently shaped. It shrinks distances along the natural direction of correlation and stretches them across it, so it can rank a numerically small deviation as a severe outlier, and a numerically large deviation as unremarkable.

Worked example 1: computing it explicitly

Two variables, stock return x1x_1 and sector return x2x_2, with mean μ=(0,0)\mu = (0, 0) (both centered), variance of each Var(x1)=Var(x2)=4\text{Var}(x_1) = \text{Var}(x_2) = 4 (in percent-squared units), and covariance Cov(x1,x2)=3.2\text{Cov}(x_1,x_2) = 3.2 (correlation =3.2/4=0.8= 3.2/4 = 0.8, strongly positive). So:

Σ=(43.23.24)\Sigma = \begin{pmatrix} 4 & 3.2 \\ 3.2 & 4 \end{pmatrix}

Determinant: 4×43.2×3.2=1610.24=5.764\times4 - 3.2\times3.2 = 16 - 10.24 = 5.76. Inverse:

Σ1=15.76(43.23.24)(0.6940.5560.5560.694)\Sigma^{-1} = \frac{1}{5.76}\begin{pmatrix} 4 & -3.2 \\ -3.2 & 4 \end{pmatrix} \approx \begin{pmatrix} 0.694 & -0.556 \\ -0.556 & 0.694 \end{pmatrix}

Take point A =(3,3)= (3, -3) (stock up 3, sector down 3 — off the diagonal). Euclidean distance from origin: 32+32=184.24\sqrt{3^2+3^2} = \sqrt{18} \approx 4.24. Mahalanobis: D2=(3,3)Σ1(3,3)D^2 = (3,-3)\,\Sigma^{-1}\,(3,-3)' . First Σ1(3,3)=(0.694×30.556×(3), 0.556×3+0.694×(3))=(2.08+1.67, 1.672.08)=(3.75, 3.75)\Sigma^{-1}(3,-3)' = (0.694\times3 - 0.556\times(-3),\ -0.556\times3+0.694\times(-3)) = (2.08+1.67,\ -1.67-2.08) = (3.75,\ -3.75). Then D2=3×3.75+(3)×(3.75)=11.25+11.25=22.5D^2 = 3\times3.75 + (-3)\times(-3.75) = 11.25+11.25 = 22.5, so D4.74D \approx 4.74 — the Mahalanobis distance is actually larger than the Euclidean one here, because moving off the diagonal is exactly what the correlation structure says shouldn't happen.

Worked example 2: the on-diagonal point

Take point B =(6,5.8)= (6, 5.8) (both up, close to the diagonal). Euclidean distance: 62+5.82=36+33.64=69.648.35\sqrt{6^2+5.8^2} = \sqrt{36+33.64}=\sqrt{69.64}\approx 8.35 — much larger raw distance than point A. Mahalanobis: Σ1(6,5.8)=(0.694×60.556×5.8, 0.556×6+0.694×5.8)=(4.163.22, 3.34+4.02)=(0.94, 0.68)\Sigma^{-1}(6,5.8)' = (0.694\times6 - 0.556\times5.8,\ -0.556\times6+0.694\times5.8) = (4.16-3.22,\ -3.34+4.02) = (0.94,\ 0.68). Then D2=6×0.94+5.8×0.68=5.64+3.94=9.58D^2 = 6\times0.94 + 5.8\times0.68 = 5.64+3.94 = 9.58, D3.10D\approx3.10. Despite having a much larger Euclidean distance (8.35 vs 4.24), point B has a smaller Mahalanobis distance (3.10 vs 4.74) than point A — confirming the earlier intuition precisely: the off-diagonal move is the real outlier, the on-diagonal move is comparatively ordinary, and only Mahalanobis distance gets the ranking right.

What this means in practice

  • Multivariate anomaly detection. Flagging an unusual trading day, an anomalous combination of risk factor exposures, or a data point that breaks a normally reliable correlation (a stock decoupling from its sector, a hedge that stops behaving like a hedge) is exactly a Mahalanobis-distance problem, not a per-variable threshold problem.
  • It underlies robust covariance estimation. High-breakdown estimators like the Minimum Covariance Determinant algorithm iteratively use Mahalanobis distance to identify and downweight the points least consistent with the bulk of the data.
  • It's the building block of Hotelling's T-squared test — that test is precisely a Mahalanobis distance from a hypothesized mean, scaled and referred to an F-distribution.
  • It needs a trustworthy Σ\Sigma. Since D2D^2 depends on Σ1\Sigma^{-1}, and Σ\Sigma itself can be distorted by the very outliers you're trying to detect, naive Mahalanobis distance using the ordinary sample covariance can be self-defeating — which is exactly why outlier-resistant covariance estimators exist as an input to a more reliable Mahalanobis calculation.

The classic confusion: assuming a point that's far from the mean on every individual variable must automatically have a large Mahalanobis distance, or that a point that's close to the mean on every variable must be safe. Neither is guaranteed. Mahalanobis distance depends on the direction of the deviation relative to the correlation structure, not on raw magnitude — a large joint move along the natural "grain" of the correlation can be perfectly unremarkable, while a small move against that grain can be the most anomalous point in the dataset.

Related concepts

Practice in interviews

Further reading

  • Mahalanobis (1936), On the Generalized Distance in Statistics
  • Johnson & Wichern, Applied Multivariate Statistical Analysis, ch. 4
ShareTwitterLinkedIn