Skip to content
induwara.lk
Premium
induwara.lkAI · Machine learning

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.

By Induwara AshinsanaUpdated Jul 16, 2026
Mahalanobis distance calculator
Mode

Distance of one point x from the dataset's centre μ, with the χ² outlier test.

Rows are observations, columns are features. Separate values with commas, spaces, or tabs. Needs at least 2 rows, and more rows than features (n > p).

Same number of values as the dataset has columns.

Covariance estimator
Significance level α
Examples

Outlier at α = 0.05

D² = 6.2500 exceeds the χ²2 critical value 5.9915 at 95% confidence. p-value = 0.0439 — the chance a genuine sample from this distribution sits at least this far from the centre.

Mahalanobis D
2.5000
2-D · 5 obs
Squared (D²)
6.2500
dᵀ Σ⁻¹ d
Euclidean
3.6056
Uncorrected ‖d‖₂
p-value
0.0439
vs α = 0.05
Decimals

Cross-check. The explicit-inverse quadratic form dᵀ Σ⁻¹ d gives D² = 6.2500; the independent route — solving Σ y = d and taking dᵀ y, the way SciPy computes it — gives 6.2500. They reconcile, as they must.

Mean, covariance and its inverse

Mean vector μ (2 features)

μ0.00000.0000

Difference d = x − μ (2 features)

d3.00002.0000

Covariance Σ (2 features)

f14.00000.0000
f20.00001.0000

Inverse covariance Σ⁻¹ (2 features)

f10.25000.0000
f20.00001.0000

Method: D² = (x − μ)ᵀ Σ⁻¹ (x − μ); μ and Σ from the dataset (n − 1 divisor); outlier test D² ~ χ² with p degrees of freedom — Mahalanobis (1936), scikit-learn, NIST e-Handbook. Nothing leaves this page.

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:

  1. Mean vector. Average each feature column across the n observations to get μ.
  2. Covariance matrix. Build Σ_jk = (1/D) Σ (x_ij − μ_j)(x_ik − μ_k), using divisor D = n − 1 for the sample estimator (matches numpy.cov) or D = n for the population estimator (matches scikit-learn EmpiricalCovariance).
  3. 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.
  4. Quadratic form. With d = x − μ, the squared distance is D² = dᵀ Σ⁻¹ d and D = √(D²). In point-to-point mode the same Σ⁻¹ is applied to d = a − b.
  5. 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-value P(χ²_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

Point-to-distribution outlier test (2 features), sample covariance

  1. Dataset (X, Y): (−2,−1) (−2,1) (0,0) (2,−1) (2,1), so μ = (0, 0)
  2. Sample covariance Σ = [[4, 0], [0, 1]] — X spreads 4× wider, no correlation
  3. Inverse Σ⁻¹ = [[0.25, 0], [0, 1]]; query x = (3, 2), so d = (3, 2)
  4. D² = 0.25·3² + 1·2² = 2.25 + 4 = 6.25, D = √6.25 = 2.5
  5. χ²(2, 0.95) = 5.991; 6.25 > 5.991 → outlier, p-value = e^(−3.125) = 0.0439
  6. Euclidean for contrast = √(9 + 4) = 3.606 — no verdict, over-weights X

Point-to-point under the shared covariance

  1. Points a = (2, 1), b = (−2, −1); difference d = (4, 2)
  2. Same Σ⁻¹ = [[0.25, 0], [0, 1]] from the dataset above
  3. D² = 0.25·4² + 1·2² = 4 + 4 = 8, D = √8 = 2.828
  4. Euclidean = √(16 + 4) = √20 = 4.472
  5. Mahalanobis is smaller — the gap lies along the wide X axis, down-weighted

One dimension — the z-score identity (edge case)

  1. Dataset [2, 4, 4, 4, 5, 5, 7, 9], single feature, mean μ = 5
  2. Sum of squared deviations = 9 + 3 + 0 + 4 + 16 = 32
  3. Population σ² = 32 / 8 = 4, so σ = 2 (population estimator)
  4. Query x = 9: D = |9 − 5| / 2 = 2.0 — exactly the absolute z-score
  5. Sample estimator σ² = 32 / 7 gives σ = 2.138 and D = 1.871

Frequently asked questions

Sources & references

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

Rate this tool
Be the first to rate

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.