Use per-dimension mean (axis=0) in mahalanobis metric - #786
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
The Mahalanobis distance centers X on the reference distribution's mean vector (per-column centroid), but `np.mean(reference_distribution)` with no axis collapses the 2D array to a single scalar grand mean, broadcast across all features. `np.cov(reference_distribution.T)` already treats each column as a variable, confirming the per-column intent. As a result a point sitting on the distribution's own centroid reported a large nonzero distance. Use `np.mean(reference_distribution, axis=0)` and add a doctest showing a centroid point has distance 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Mahalanobis distance centers
Xon the mean vector (per-column centroid) of the reference distribution, but_computeusesnp.mean(reference_distribution)with no axis, which collapses the 2-D reference array to a single scalar grand mean and broadcasts it across every feature:The very next line,
np.cov(reference_distribution.T), treats each column as a variable — confirming the per-dimension intent. As a result the metric is wrong for any distribution whose feature means differ.Reproduction
Fix
Tests
Added a doctest showing a point on the distribution's centroid has distance 0. The existing doctest (
reference_distribution=[[0, 1], [1, 0]]) is symmetric — its column means equal the grand mean — so it yieldsarray([0.5])under both the old and new code and remains unchanged.