Linear algebra
Mahalanobis distance
linalg.mahalanobisAlso called mahalanobis, mahalanobis distance, mahal
Distance from x to a distribution with mean μ and covariance Σ, in units that account for the covariance.
Notation
the point
x ∈ ℝd
mean vector
μ ∈ ℝd
covariance matrix, symmetric positive definite
Σ ∈ ℝd×d, symmetric positive definite
dimension
Size, at least 1
Result: the distance
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ 2.2360679775
Given
Result
≈ 0.816496580928
Given
Result
≈ 1.41421356237
Given
Result
Common mistakes
Uses Σ where Σ−1 belongs
The quadratic form multiplies by the covariance instead of its inverse. SciPy's mahalanobis takes the inverse covariance as its third argument.
Fix: Solve Σv = (x − μ) and use √((x − μ)Tv), or pass the inverse to scipy.spatial.distance.mahalanobis.
Returns the squared distance
The final square root is missing.
Fix: Take the square root of the quadratic form.
Source
Bishop 2006
Bishop, Christopher M. Pattern Recognition and Machine Learning. Springer, 2006.