huggingface / huggingface/evaluate
Mahalanobis distance computes X_minus_mu incorrectly
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 341
- PR merge metrics
- No merged PRs in 30d
Description
It seems that on [Line 91](https://github.com/huggingface/evaluate/blob/af3c30561d840b83e54fc5f7150ea58046d6af69/metrics/mahalanobis/mahalanobis.py#L91) in `mahalanobis.py` the quantity `X_minus_mu` seems to be computed incorrectly.
Based on [Wikipedia](https://en.wikipedia.org/wiki/Mahalanobis_distance), both `x` and `mu` should be vectors:

however using `np.mean(...)` without specifying the axis returns a scalar:
```
import numpy as np
reference_distribution = [[1,2], [3,4]]
print(np.mean(reference_distribution))
> 2.5
```
Instead, if the array is `(N, D)` then we can take an overage over the first dimension with `np.mean(..., axis=0)`:
```
import numpy as np
reference_distribution = [[1,2], [3,4]]
print(np.mean(reference_distribution, axis=0))
> [2. 3.]
```
This means that the same scalar is being subtracted from all components of `X` to create `X_minus_mu`, which isn't what we want, right?
Please correct me if my understanding is wrong. Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.