Mahalanobis Distance Calculator

Multivariate statistics

Mahalanobis distance1.288
Mahalanobis distance squared1.660
Chi-square p-value (df 2)0.4360
z-scores1.00 and 1.25
How this was calculated
  1. Standardise: zx = (70.0 - 60.0) / 10.0 = 1.00 and zy = (65.0 - 50.0) / 12.0 = 1.25.
  2. Mahalanobis distance squared with correlation 0.60: (1.00^2 - 2 x 0.60 x 1.00 x 1.25 + 1.25^2) / (1 - 0.60^2) = 1.660.
  3. Mahalanobis distance: sqrt(1.660) = 1.288.
  4. Against chi-square with 2 degrees of freedom, D squared 1.660 has tail probability 0.4360.

Euclidean distance treats every direction alike, but real data clouds are elliptical: variables have different spreads and they correlate. The Mahalanobis distance measures remoteness in the cloud's own units - standard deviations along the cloud's axes - so a point far out along the correlation direction and a point the same Euclidean distance against it stop pretending to be equally unusual. It is the standard multivariate outlier score for a reason.

Enter a point, a bivariate centre, the two standard deviations and the correlation. The calculator returns the Mahalanobis distance and its square, the chi-square (2 df) p-value that turns distance into a rarity statement, and the two z-scores for contrast.

Worked examples

Point (70, 65), D 1.288

(70 · 65 · 60 · 50 · 10 · 12 · 0.6)

Mahalanobis distance1.288
Mahalanobis distance squared1.660
Chi-square p-value (df 2)0.4360
z-scores1.00 and 1.25

Point at the centre, D 0.000

(60 · 50 · 60 · 50 · 10 · 12 · 0.6)

Mahalanobis distance0.000
Mahalanobis distance squared0.000
Chi-square p-value (df 2)1.0000
z-scores0.00 and 0.00

Point (80, 38), D 2.610

(80 · 38 · 60 · 50 · 10 · 12 · 0.3)

Mahalanobis distance2.610
Mahalanobis distance squared6.813
Chi-square p-value (df 2)0.0332
z-scores2.00 and -1.00

Distance in the cloud's own units

Standardise first: zx = (x - mux)/sx and zy = (y - muy)/sy. Then correct for correlation: D squared = (zx squared - 2 r zx zy + zy squared)/(1 - r squared). The denominator is the geometry: as r approaches 1 the cloud collapses toward a line, distances perpendicular to it explode, and the formula says so.

The first example: the point (70, 65) against a centre (60, 50) with spreads 10 and 12 and correlation 0.6 standardises to z-scores 1.00 and 1.25, and D squared = (1.00 - 1.50 + 1.5625)/0.64 = 1.660, so D = 1.288. Against chi-square with 2 df the p-value is 0.4360: over two fifths of this cloud sits at least this far out. The z-scores alone would have made the point look more unusual than it is - the correlation tilts the cloud toward it.

That correction is the entire point of the statistic: remoteness is judged against the cloud's shape, not against the axes.

  • D squared = (zx^2 - 2 r zx zy + zy^2) / (1 - r^2)
  • D squared follows chi-square with p degrees of freedom
  • p-value (2 df) = exp(-D squared / 2)
  • Correlation stretches the ruler along the cloud

Worked examples: three points

Example one, above: D squared 1.660, D 1.288, p 0.4360 - an ordinary point of the cloud, despite sitting a full standard deviation or more out on both axes.

Example two: the point sits exactly at the centre (60, 50). Both z-scores are 0.00, D squared is 0.000, D is 0.000 and the p-value is 1.0000. The trivial case, and a necessary control: any implementation that returns anything else for the centroid has a bug.

Example three: the point (80, 38) against the same centre and spreads, with correlation 0.3. The z-scores are 2.00 and -1.00 - the point runs against the (positive) correlation - and D squared = (4 + 1.2 + 1)/0.91 = 6.813, D = 2.610, p = 0.0332. This one is an outlier at the 5 % line: unusual in a way neither z-score alone announces.

The chi-square connection

If the cloud is multivariate normal, D squared is exactly chi-square distributed with degrees of freedom equal to the dimension - 2 here. That turns a distance into a probability: the p-value printed is the share of the cloud lying at least this far out, the rigorous version of 'how many sigmas is that?' for correlated data. For 2 df the tail has the closed form exp(-D squared/2): exp(-6.813/2) = 0.0332 in the third example.

Thresholds follow: D squared of 5.99 and 9.21 mark the 5 % and 1 % contours for 2 df. The third example's 6.813 clears the first and not the second - an outlier worth a look, not yet an alarm. Quality-control charts for bivariate processes plot exactly these ellipses.

The normality assumption is load-bearing. Heavy-tailed clouds produce large Mahalanobis distances more often than chi-square admits, so in robust practice the centre and covariance are estimated robustly (MCD and relatives) before distances are taken - the formula on this page is the same, the inputs are hardened.

Rule of thumb (2 df): D squared above 5.99 is the outer 5 % of a normal cloud; above 9.21, the outer 1 %. The third example's 6.813 sits between them.

Estimating the centre and spread

In practice the centre, spreads and correlation come from the sample itself, which introduces a subtle circularity: an outlier inflates the spreads and drags the centre, shrinking its own distance - masking. Leave-one-out distances or robust estimates break the circle; for screening a handful of points, recomputing without the suspect is the poor man's version and usually suffices.

Sample size disciplines the estimates: a covariance from 15 points is a sketch, from 150 a portrait. The distance formula does not know the difference, so the analyst must - p-values from small samples are optimistic.

Singularity is the hard limit: a correlation of exactly plus or minus 1 makes the denominator zero because the cloud has collapsed to a line and 'distance from the line' is a different question. Near-singular cases (r of 0.99) return enormous distances for points a hair off the line - arithmetically true, statistically fragile.

Where Mahalanobis distance works

Outlier screening is the headline use - in analytical chemistry, process control and data cleaning, where the first example's p = 0.4360 verdict (ordinary) versus the third's 0.0332 (investigate) is the daily decision. It also drives Hotelling's T squared, the multivariate control chart, of which D squared is the single-observation core.

Classification uses it as the Gaussian discriminant: assign a point to the class centre with the smallest Mahalanobis distance and you have linear or quadratic discriminant analysis, depending on whether classes share a covariance. The distance is doing probabilistic work - it is the log-likelihood in disguise.

Anomaly detection systems industrialise the same idea at higher dimensions, with the inverse covariance estimated from nominal data. The bivariate case on this page is where the intuition is built: correlation-aware, scale-free, probability-attached.

Mahalanobis versus z-scores, variable by variable

Screening each variable separately with z-scores misses exactly what this distance catches. In the first example the z-scores 1.00 and 1.25 overstate the point's distance, because the correlation of 0.6 tilts the cloud toward the point's direction; the joint distance of 1.288 is the corrected judgement. In the third example the reverse happens: z-scores of 2.00 and -1.00 look like one marginal flag, while the joint D of 2.610 with p 0.0332 identifies a genuinely unusual combination.

With p variables the univariate approach also multiplies false alarms - p independent 5 % screens false-flag far more than 5 % of good points - while the chi-square calibration of D squared holds the joint rate exactly. That calibration is the professional reason the statistic displaced ad-hoc screening.

The centre case is the calibration's anchor: z-scores of 0.00 and 0.00, distance 0.000, p 1.0000. Every screening rule should be able to say 'nothing here' this cleanly.

Worked check: reading the contours

Plot the first example's cloud: centre (60, 50), spreads 10 and 12, tilted by the correlation of 0.6 so the ellipse leans up-right. The 95 % contour is the ellipse D squared = 5.99; the point (70, 65) at D squared 1.660 sits well inside it, consistent with its p of 0.4360. The third example's point at 6.813 sits just outside the same contour for its own cloud - the picture and the p-value of 0.0332 agree, as they must.

Sketching the ellipse before computing is the practitioner's sanity check: if the number contradicts the picture, one of them is wrong, and it is cheaper to find out on paper than in a report.

The centre example is the contour picture's origin: every ellipse is centred there, the distance is 0.000, and the p-value of 1.0000 says the whole cloud lies at least that far out - trivially, since 'that far' is nowhere.

Computing it well

Numerically, the distance is a quadratic form with the inverse covariance; for two variables the closed form on this page is exact and stable except near a correlation of plus or minus 1, where the honest response is to question the data (a near-perfect correlation usually means a duplicated or derived variable) rather than the arithmetic.

Scaling deserves a sentence: Mahalanobis distance is invariant to any invertible linear rescaling of the variables - change units, rotate axes, the distances stand. That invariance is why it, and not Euclidean distance on standardised variables, is the canonical answer when correlation is present.

For samples, replace the population quantities with estimates and mind the degrees of freedom: with n not much larger than the dimension, the inverse covariance is noisy and distances run optimistic. The bivariate case is forgiving; the habit of checking n against the dimension is what carries to higher-dimensional work.

One more use deserves mention: Mahalanobis distance is the quiet engine inside much of multivariate quality control and chemometrics, where spectra and process fingerprints live in correlated spaces by nature. The bivariate arithmetic on this page - standardise, correct for correlation, compare against chi-square - is the same machinery at industrial scale, and learning it on two variables, with numbers like 1.660 and 6.813 that can be checked by hand, is how the intuition for the large case is built.

FAQs

What is the Mahalanobis distance of (70, 65) from (60, 50) with r 0.6?

1.288.

What is its squared distance?

1.660, with tail probability 0.4360.

What does a point at the means give?

0.000 - no distance at all.

What does the point (80, 38) with r 0.3 give?

2.610, tail 0.0332.

Why not just use z-scores?

Because r 0.6 tilts the cloud: the first point's z-scores 1.00 and 1.25 overstate its distance.

Which tail does D squared use?

Chi-square with 2 degrees of freedom: exp(-6.813/2) = 0.0332 in the third example.

Sources

Related calculators

Manhattan Distance Calculator
Manhattan (taxicab) distance between two points in 2D or 3D — the grid-route length — with the Euclidean distance alongside.
Hamming Distance Calculator
Hamming distance between two whole numbers: the bit positions that differ, shown as aligned binary strings with the XOR value and the set-bit counts of both numbers.
Covariance Calculator
Sample and population covariance for up to eight paired observations, with the correlation coefficient alongside — the measure of whether two series move together.