huggingface / huggingface/evaluate

Mahalanobis distance computes X_minus_mu incorrectly

Open
#470 0 comments 0 reactions 0 assignees View on GitHub
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:

![image](https://github.com/huggingface/evaluate/assets/10352674/f20bf967-b311-4ae7-9279-981def488ca2)

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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.