Written and maintained by CASRAI Editorial Board
Last updated
What is Mahalanobis distance?
Mahalanobis distance is a measure of how far a point lies from the centre of a distribution, scaled by the spread and the correlations in that distribution. Where Euclidean distance treats every direction alike, Mahalanobis distance counts a step along a high-variance direction as smaller than the same step along a low-variance direction, and it accounts for variables that move together. It was introduced by P. C. Mahalanobis in 1936. Because the result is unitless, it is a multivariate generalisation of the absolute z-score: how many standard deviations a point is from the mean, in several variables at once.
Mahalanobis distance formula
For a point x, a mean vector μ and a covariance matrix Σ, the squared distance is:
D² = (x − μ)′ Σ−1 (x − μ), and the distance is D = √D².
- x: the observation, a vector with one entry per variable.
- μ: the mean vector (in practice, the sample means of each variable).
- Σ: the covariance matrix of the variables (in practice, the sample covariance matrix, with variances on the diagonal and covariances off it).
- Σ−1: its matrix inverse, which is what down-weights high-variance and correlated directions.
- ′: transpose. (x − μ)′ is the same vector written as a row.
If Σ is the identity matrix (unit variances, no correlation), Σ−1 drops out and D equals the ordinary Euclidean distance.
Worked example with the arithmetic shown
Take five observations on two variables, X and Y: (2, 3), (4, 5), (6, 4), (8, 8) and (5, 10).
Step 1, means. Mean of X = (2+4+6+8+5)/5 = 5. Mean of Y = (3+5+4+8+10)/5 = 6.
Step 2, deviations from the means. X: −3, −1, 1, 3, 0. Y: −3, −1, −2, 2, 4.
Step 3, sample covariance matrix (divide by n − 1 = 4).
- Var(X) = (9+1+1+9+0)/4 = 20/4 = 5
- Var(Y) = (9+1+4+4+16)/4 = 34/4 = 8.5
- Cov(X,Y) = (9+1−2+6+0)/4 = 14/4 = 3.5
So S = [[5, 3.5], [3.5, 8.5]].
Step 4, inverse. det(S) = 5 × 8.5 − 3.5 × 3.5 = 42.5 − 12.25 = 30.25. For a 2×2 matrix [[a, b], [b, d]] the inverse is (1/det) × [[d, −b], [−b, a]], so S−1 = (1/30.25) × [[8.5, −3.5], [−3.5, 5]] = [[0.2810, −0.1157], [−0.1157, 0.1653]].
Step 5, squared distance for the first observation (2, 3). The deviation vector is d = (−3, −3). Then d′S−1d = (1/30.25) × [8.5(−3)2 + 2(−3.5)(−3)(−3) + 5(−3)2] = (76.5 − 63 + 45)/30.25 = 58.5/30.25 = 1.934, so D = 1.391.
Step 6, the fifth observation (5, 10). d = (0, 4), so D² = (5 × 16)/30.25 = 80/30.25 = 2.645, so D = 1.626.
All five observations:
| Observation | Deviation d | D² | D |
|---|---|---|---|
| (2, 3) | (−3, −3) | 1.934 | 1.391 |
| (4, 5) | (−1, −1) | 0.215 | 0.464 |
| (6, 4) | (1, −2) | 1.405 | 1.185 |
| (8, 8) | (3, 2) | 1.802 | 1.342 |
| (5, 10) | (0, 4) | 2.645 | 1.626 |
Notice the contrast with Euclidean distance. From the centre (5, 6), observation (2, 3) is 4.24 units away and (5, 10) is 4.00 units away, so Euclidean distance ranks (2, 3) as the more extreme. Mahalanobis distance reverses that: (2, 3) lies along the direction in which X and Y rise together, where variation is normal, while (5, 10) breaks the pattern, so it is the more unusual point. (These values match a numerical check with NumPy and SciPy.)
Interpreting D² and the chi-square cutoff
If the data are multivariate normal, D² follows a chi-squared distribution whose degrees of freedom equal the number of variables. That gives a principled cutoff: compare each D² with a chi-square critical value at df = number of variables. For df = 2 (as in the example), the critical value is 5.99 at the 0.95 quantile, 9.21 at the 0.99 quantile and 13.82 at the 0.999 quantile. You can look up other values with a critical value guide or a chi-square table; the chi-square test entry explains the distribution family.
| Result | How to read it |
|---|---|
| D² near 0 | The case sits close to the centre of the data cloud. |
| D² below the chosen chi-square critical value (df = number of variables) | The case is not flagged as a multivariate outlier at that threshold. |
| D² above the critical value | The case is flagged as unusual relative to the other cases; inspect it before deciding anything. |
In the worked example the largest D² is 2.645, below even the 0.95 cutoff of 5.99, so no case is flagged. Five cases is far too few for real screening; the example only shows the arithmetic.
Mahalanobis distance for multivariate outliers
The most common use is screening for multivariate outliers: cases that look ordinary on each variable alone but are unusual in combination. A flagged case is a prompt to check for data-entry errors and to understand the case, not an instruction to delete it. For univariate screening, see how to find an outlier in statistics and the trade-offs in winsorizing versus trimming. Mahalanobis distance is also related to the leverage statistic in regression, and SPSS can save it as a screening variable, as shown in the MANOVA in SPSS guide. Mahalanobis distance is also used for matching in observational studies, covered in propensity score matching in Stata.
Mahalanobis vs Euclidean distance
Euclidean distance is Mahalanobis distance with an identity covariance matrix. It changes when you change units (centimetres versus metres) and ignores correlation. Mahalanobis distance is unitless and scale-invariant and uses the correlations in the data, which is why it ranked the two points in the example differently.
Assumptions and common mistakes
- Normality for the chi-square cutoff. The chi-square reference holds for data from a multivariate normal distribution. The distance itself can be computed for any data, but the cutoff is then only a rough guide.
- More cases than variables. The covariance matrix must be invertible. MathWorks’
mahaldocumentation states that the reference sample must have more rows than columns. - Outliers distort the mean and covariance. The sample mean and covariance matrix are sensitive to outliers, so robust estimators such as the Minimum Covariance Determinant are often used instead.
- Squared versus unsquared. Software differs, as the next table shows. Comparing an unsquared distance to a chi-square critical value (or the reverse) is the commonest error.
- Passing the covariance where the inverse is expected. SciPy asks for the inverse.
| Software | Function | Covariance argument | Returns |
|---|---|---|---|
| SciPy | scipy.spatial.distance.mahalanobis(u, v, VI) |
VI, the inverse covariance matrix | The distance (not squared) |
| R | stats::mahalanobis(x, center, cov, inverted = FALSE) |
cov (set inverted = TRUE if it already holds the inverse) |
Squared distance for each row |
| MATLAB | mahal(Y, X) |
Estimated from the reference sample X | Squared distance for each row of Y |
How to report it (APA example)
Report the method, the threshold, the degrees of freedom and what you did with flagged cases. For example:
Multivariate outliers were screened using Mahalanobis distance. Cases with D² exceeding the chi-square critical value at p < .001 (df = 3, critical value = 16.27) were flagged; two of 214 cases met this criterion and were examined before analysis.
Replace the numbers with your own: df is your number of variables and the critical value comes from the chi-square distribution at your chosen alpha. Italicise p and the statistical symbols as your style guide requires.
How to calculate Mahalanobis distance in Excel, R or Python
Python (SciPy and NumPy)
import numpy as np
from scipy.spatial.distance import mahalanobis
from scipy.stats import chi2
X = np.array([[2,3],[4,5],[6,4],[8,8],[5,10]], float)
mu = X.mean(axis=0)
VI = np.linalg.inv(np.cov(X, rowvar=False)) # inverse covariance
d = [mahalanobis(row, mu, VI) for row in X] # unsquared
d2 = np.square(d) # squared
cutoff = chi2.ppf(0.999, df=X.shape[1]) # 13.82 for df = 2
R
X <- cbind(x = c(2,4,6,8,5), y = c(3,5,4,8,10))
d2 <- mahalanobis(X, colMeans(X), cov(X)) # already squared
cutoff <- qchisq(0.999, df = ncol(X)) # 13.82
d2 > cutoff
Excel
With the data in A2:B6, compute the means with AVERAGE, build the 2×2 covariance matrix in D2:E3 with COVARIANCE.S, and put each row’s deviation in columns F:G (value minus mean). Then, for row 2, enter this as an array formula (Ctrl+Shift+Enter in older Excel):
=MMULT(MMULT(F2:G2, MINVERSE($D$2:$E$3)), TRANSPOSE(F2:G2))
This returns D² (squared). Take the square root for D, and get the cutoff with =CHISQ.INV.RT(0.001, 2).
Frequently asked questions
Is Mahalanobis distance squared or not?
It depends on the software. SciPy returns the unsquared distance; R’s mahalanobis and MATLAB’s mahal return the squared distance. The chi-square cutoff applies to the squared value.
Can Mahalanobis distance be negative?
No. D² is a quadratic form with a positive-definite inverse covariance matrix, so it is zero at the mean and positive elsewhere.
What degrees of freedom do I use for the cutoff?
The number of variables used to compute the distance.
How is Mahalanobis distance different from Euclidean distance?
Euclidean distance ignores variances and correlations; Mahalanobis distance scales by the covariance matrix, and the two coincide when that matrix is the identity.
Sources
Links checked 2026-10-29.
- SciPy documentation, scipy.spatial.distance.mahalanobis.
- R Core documentation, stats::mahalanobis.
- MathWorks documentation, mahal.
- Wikipedia, Mahalanobis distance, for the chi-squared property, the robust-estimator note and the history; a secondary reference.








