Skip to content

Mahalanobis distance

Mahalanobis distance scales a vector by its covariance. A checked two-feature example connects correlation, whitening, squared distance and fitted-reference caveats.

Mahalanobis distance measures separation relative to a reference covariance matrix: it scales deviations by their variability and accounts for correlations between them. A quant can use it to screen a vector of market features for unusual combinations. Two moves can each look ordinary on their own yet be unusual together if they break the usual relationship between the features.

For a vector x\mathbf{x}, reference centre μ\boldsymbol\mu and positive definite covariance matrix Σ\Sigma, define

D(x;μ,Σ)=(x−μ)⊤Σ−1(x−μ).D(\mathbf{x};\boldsymbol\mu,\Sigma) =\sqrt{(\mathbf{x}-\boldsymbol\mu)^\top\Sigma^{-1}(\mathbf{x}-\boldsymbol\mu)}.
The expression inside the square root is the squared Mahalanobis distance, D2D^2. Distance and squared distance rank observations identically for a fixed reference, but their numerical values and thresholds differ. For pairwise distance, replace x−μ\mathbf{x}-\boldsymbol\mu by x−y\mathbf{x}-\mathbf{y} and use one fixed covariance matrix. SciPy’s definition uses the square root.

The direction of the move matters

Consider a fictional daily market-feature monitor. Both features have mean zero, measured in their declared feature units, and the reference covariance is supplied as
Σ=(41.21.21).\Sigma=\begin{pmatrix}4&1.2\\1.2&1\end{pmatrix}.
Feature 1 has standard deviation 2 units, feature 2 has standard deviation 1 unit, and their correlation is 1.2/(2×1)=0.61.2/(2\times1)=0.6. Take observation xA=(2,1)⊤\mathbf{x}_A=(2,1)^\top. Each coordinate is one marginal standard deviation above its mean. The inverse is
Σ−1=12.56(1−1.2−1.24),\Sigma^{-1}=\frac{1}{2.56} \begin{pmatrix}1&-1.2\\-1.2&4\end{pmatrix},

so

DA2=22−2(1.2)(2)(1)+4(12)2.56=3.22.56=1.25,DA=1.25≈1.118.D_A^2=\frac{2^2-2(1.2)(2)(1)+4(1^2)}{2.56} =\frac{3.2}{2.56}=1.25, \qquad D_A=\sqrt{1.25}\approx1.118.

Now change only the second coordinate: xB=(2,−1)⊤\mathbf{x}_B=(2,-1)^\top. Its ordinary Euclidean distance from zero is still 5\sqrt{5}, and the sum of squared marginal standardised deviations is still 12+(−1)2=21^2+(-1)^2=2. But

DB2=4+4.8+42.56=5,DB=5≈2.236.D_B^2=\frac{4+4.8+4}{2.56}=5, \qquad D_B=\sqrt{5}\approx2.236.

Positive co-movement is part of the reference. A has both features above their means; B moves against that relationship. B therefore has twice A’s Mahalanobis distance, even though their separate marginal magnitudes match. This is a covariance comparison, not a statement that B is a bad trade or a faulty record.

Original fictional two-feature geometry. With covariance rows (4, 1.2) and (1.2, 1), A=(2,1) has squared distance 1.25 and B=(2,-1) has squared distance 5. Whitening maps A to (1,0.5) and B to (1,-2); squared-distance contours 1 and 4 become circles.
Original fictional two-feature geometry. With covariance rows (4, 1.2) and (1.2, 1), A=(2,1) has squared distance 1.25 and B=(2,-1) has squared distance 5. Whitening maps A to (1,0.5) and B to (1,-2); squared-distance contours 1 and 4 become circles.
Original fictional scenario. Equal Mahalanobis distance traces ellipses in the original coordinates and circles after whitening. Both panels use equal horizontal and vertical scales within the panel; the panels have different axis ranges.

Whitening makes the geometry ordinary

If Σ=LL⊤\Sigma=LL^\top is a Cholesky decomposition, transform the centred vector to z=L−1(x−μ)\mathbf{z}=L^{-1}(\mathbf{x}-\boldsymbol\mu). Then

D2=z⊤z=∥z∥2.D^2=\mathbf{z}^\top\mathbf{z}=\lVert\mathbf{z}\rVert^2.
This is whitening: the transformed reference covariance is the identity. Different whitening transformations may rotate the coordinates, while preserving their length. Standardising each feature separately gives unit marginal variances but leaves correlations in place. Kessy, Lewin and Strimmer explain this distinction and the rotational freedom.

Here,

L=(200.60.8),z1=x12,z2=x2−0.3x10.8.L=\begin{pmatrix}2&0\\0.6&0.8\end{pmatrix}, \qquad z_1=\frac{x_1}{2}, \qquad z_2=\frac{x_2-0.3x_1}{0.8}.

A becomes (1,0.5)(1,0.5), giving 12+0.52=1.251^2+0.5^2=1.25. B becomes (1,−2)(1,-2), giving 12+(−2)2=51^2+(-2)^2=5. The second whitened coordinate measures the deviation remaining after removing the linear co-movement with feature 1.

The resulting distance is dimensionless. Changing a feature from percentage points to basis points leaves the distance unchanged if its centre and covariance are transformed consistently. Rescaling the observations while retaining the old covariance changes the calculation.

Geometry does not supply a probability by itself

The distance requires a centre and covariance, not a normal distribution. A probabilistic interpretation needs additional assumptions.

If X\mathbf{X} has a pp-dimensional multivariate normal distribution and μ,Σ\boldsymbol\mu,\Sigma are its true population parameters, then
D2(X;μ,Σ)∼χp2.D^2(\mathbf{X};\boldsymbol\mu,\Sigma)\sim\chi_p^2.
Whitening gives pp independent standard normal coordinates, whose squared sum has the chi-squared distribution. The result applies to the squared distance. Penn State’s multivariate-normal notes, §4.3 develop the corresponding ellipses.
Replacing the population parameters with an estimated mean and covariance does not preserve that exact law. A new observation scored against a fitted training sample and an observation used to fit that sample are different statistical situations. For example, with invertible unbiased sample covariance SS from nn observations in pp dimensions, the in-sample squared distances satisfy
∑i=1n(xi−xˉ)⊤S−1(xi−xˉ)=(n−1)p.\sum_{i=1}^n(\mathbf{x}_i-\bar{\mathbf{x}})^\top S^{-1}(\mathbf{x}_i-\bar{\mathbf{x}})=(n-1)p.
This follows by writing the sum as the trace of S−1(n−1)SS^{-1}(n-1)S. The distances share their fitted reference and obey this constraint; they are not independent draws from the population chi-square law. Testing a population mean instead involves the sample-size scaling and calibration of the Hotelling T-squared test.

The covariance estimate is part of the model

A useful monitoring score needs a reference population, observation horizon and information cutoff. For a daily monitor, fit the reference using data available before the scored day. Pooling distinct regimes can make a distance small simply because the pooled covariance is broad.

The matrix also needs care:

  • Singularity: a centred sample covariance has rank at most n−1n-1. If the number of features p≥np\ge n, it cannot be inverted; duplicate or exactly dependent features can cause singularity even with more observations. A pseudoinverse ignores null-space directions, so it does not restore a full-space distance. A deviation outside the support of a truly singular population is not made ordinary by that operation.
  • Near singularity: small eigenvalues receive large inverse weights, amplifying errors in estimated low-variance directions. Dimension reduction or a declared regularised estimator, such as a Ledoit–Wolf covariance estimator, changes the reference geometry. The scikit-learn covariance guide discusses unstable inversion and shrinkage.
  • Contamination: unusual observations can pull the estimated centre and inflate covariance, reducing their own apparent distances. Robust location and covariance estimates can help expose this masking, as the scikit-learn robust-distance example illustrates. Robust estimation still needs a defined target population; genuine crisis observations may belong in a risk analysis.
Compute the quadratic form through a linear solve or Cholesky factorisation rather than explicitly forming an inverse merely to score a vector. Report the estimator and whether scores are in sample or out of sample. A large distance is an outlier diagnostic relative to that reference, with no automatic trading signal or universal false-alarm rate.
For the underlying linear algebra, Positive Definite Matrices develops quadratic forms, invertibility and Cholesky factorisation; the full lesson requires membership, with a public preview. Expectation, Variance and Covariance provides the marginal-scale and correlation foundations; its full lesson is available with a free signed-in account.

Sources

Last updated
  • multivariate statistics
  • distance
  • covariance
  • whitening
  • outlier detection
  • statistical diagnostics
← Back to the glossaryBack to top ↑