You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this package the SqMahalanobis distance is implemented and documented as $(x - y)' * Q * (x - y)$. However, the documentation refers to $Q$ as the covariance matrix. Unless I'm mistaken, this is incorrect. In this formulation $Q$ is referred to as either the information matrix, precision matrix, or concentration matrix, which is equivalent to the inverse of the covariance matrix. The traditional square Mahalanobis equation reads $(x - y)' * Q^{-1} * (x - y)$.
I can verify this against the implementation in the Distributions package or a hand-coded version.
using Distributions, Distances
q =rand(3,3)
Q = q'q
x =rand(3)
y =rand(3)
d0 = (x-y)'*inv(Q)*(x-y) # hand-coded
d1 =sqmahal(MvNormal(x, Q), y) # Distributions.jl
d2 =sqmahalanobis(x, y, Q)
d3 =sqmahalanobis(x, y, inv(Q))
d0 ≈ d2 # false
d0 ≈ d1 ≈ d3 # true
Am I misunderstanding the intended use or documentation of this function?
Reacted by Cad Hoyt, Andrew Gough, Stephen Zhang, Tobias and Davide Crucitti
Thank you @btmit I was running some benchmarks on a library I maintain which uses Distances.jl and I was noticing some errors when using Mahalanobis.
I trusted the docstrings when I first wrote those functions without properly testing the results.
I guess this means some of the results I recently published are incorrect; luckily those should not be too important either way.
Activity
dkarrasch commentedon Jun 10, 2024
Judging by, e.g., https://en.wikipedia.org/wiki/Mahalanobis_distance, that seems to be correct. Could you perhaps submit a PR which clarifies the terminology?
davide-grheco commentedon Sep 23, 2026
Thank you @btmit I was running some benchmarks on a library I maintain which uses Distances.jl and I was noticing some errors when using Mahalanobis.
I trusted the docstrings when I first wrote those functions without properly testing the results.
I guess this means some of the results I recently published are incorrect; luckily those should not be too important either way.
nalimilan commentedon Sep 23, 2026
@davide-grheco Would you make a pull request to fix the docs since you seem to master the issue?