Mahalanobis Distance Calculator
Measure how far a point sits from a multivariate dataset, adjusted for the spread and correlation of its features. Get the Mahalanobis distance, the squared distance, a chi-square outlier verdict with p-value, and a Euclidean comparison — with every matrix shown. No signup, nothing uploaded.
How it works
The Mahalanobis distance, introduced by P. C. Mahalanobis in 1936, measures the distance from a point x to the centre μ of a distribution, weighted by the inverse covariance matrix so that spread and correlation are taken into account. Unlike Euclidean distance it is scale-invariant per feature and aware of how features move together, which is why it is the standard multivariate outlier and anomaly metric.
D²(x) = (x − μ)ᵀ Σ⁻¹ (x − μ), D = √(D²)
The calculator works in five steps:
- Mean vector. Average each feature column across the
nobservations to getμ. - Covariance matrix. Build
Σ_jk = (1/D) Σ (x_ij − μ_j)(x_ik − μ_k), using divisorD = n − 1for the sample estimator (matchesnumpy.cov) orD = nfor the population estimator (matches scikit-learnEmpiricalCovariance). - Invert the covariance. Compute
Σ⁻¹by Gauss–Jordan elimination with partial pivoting. To stay accurate when features have very different scales, the tool factors Σ into standard deviations and a correlation matrix, inverts the well-conditioned correlation matrix, then scales back. If the determinant is effectively zero — collinear features, a constant feature, or n ≤ p — the matrix is singular and the tool stops with an explanation. - Quadratic form. With
d = x − μ, the squared distance isD² = dᵀ Σ⁻¹ dandD = √(D²). In point-to-point mode the same Σ⁻¹ is applied tod = a − b. - Outlier test. Under multivariate normality D² follows a chi-square distribution with p degrees of freedom (NIST e-Handbook), so the tool reports the critical value
χ²(p, 1 − α)and the p-valueP(χ²_p > D²), computed from the regularized incomplete gamma function, and flags the point when D² exceeds the critical value.
As a credibility check the calculator computes D² a second way — by solving the linear system Σ y = d and taking dᵀ y, the route SciPy prefers over forming the inverse explicitly — and confirms the two agree to floating-point precision. All arithmetic is double-precision and rounded only for display.
Worked examples
Frequently asked questions
Sources & references
- Mahalanobis, P. C. (1936) — On the generalised distance in statistics (the original definition)
- scikit-learn — EmpiricalCovariance.mahalanobis (covariance convention and reference implementation)
- NIST/SEMATECH e-Handbook of Statistical Methods — multivariate outlier detection and the χ² distance threshold
The formulas on this page were last cross-checked against these sources on 2026-07-16. Mahalanobis distance is a stable mathematical definition, so this tool needs no rate or schedule updates — only the worked examples are periodically re-reconciled against NumPy and SciPy.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.