Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Pearson Correlation Output for Constant y_true in torchmetrics #2920

Open
FrancescaDr opened this issue Jan 29, 2025 · 2 comments
Open
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.6.x

Comments

@FrancescaDr
Copy link

FrancescaDr commented Jan 29, 2025

🐛 Bug

When computing the Pearson correlation using torchmetrics.PearsonCorrCoef, it returns 1 for some cases where y_true is constant. Mathematically, the Pearson correlation should be undefined (or return NaN), since the standard deviation of y_true is zero. In contrast, scipy.stats.pearsonr returns0 in such cases.

To Reproduce

import torchmetrics
import torch
from scipy import stats

y_pred = torch.tensor([[-0.1816,  0.6568,  0.9788, -0.1425], [-0.4111,  0.3940,  1.4834,  0.1322]])
y_true = torch.tensor([[4.0268,  5.9401,  1.0000,  1.0000], [6.4956,  5.6684,  1.0000,  1.0000]])

pearson_corr = torchmetrics.PearsonCorrCoef(num_outputs=4)
print('Torchmetrics: ', pearson_corr(y_pred, y_true))
print('Scipy: ', stats.pearsonr(y_pred, y_true))

Returns:

Torchmetrics:  tensor([-1.0000,  1.0000,  1.0000,     nan])
Scipy:  PearsonRResult(statistic=array([-1.,  1.,  0.,  0.], dtype=float32), pvalue=array([1., 1., 1., 1.], dtype=float32))

Expected behavior

For columns where y_true is constant (e.g., y_true[:, 2] = [1, 1]), the Pearson correlation should return nan.

Environment

  • TorchMetrics version (if build from source, add commit SHA): 1.6.1
  • Python: 3.11.0
  • Torch: 2.5.1

Others

Might be related to: scikit-learn/scikit-learn#25258?

@FrancescaDr FrancescaDr added bug / fix Something isn't working help wanted Extra attention is needed labels Jan 29, 2025
Copy link

Hi! thanks for your contribution!, great first issue!

@Vidit-Ostwal
Copy link

Vidit-Ostwal commented Feb 2, 2025

This is some unexpected behaviour, I went through the code

In the particular case of yours, this particular condition will hit and this warning should be raised

    bound = math.sqrt(torch.finfo(var_x.dtype).eps)
    if (var_x < bound).any() or (var_y < bound).any():
        rank_zero_warn(
            "The variance of predis can cause instability in Pearictions or target is close to zero. Thson correlation"
            "coefficient, leading to wrong results. Consider re-scaling the input if possible or computing using a"
            f"larger dtype (currently using {var_x.dtype}).",
            UserWarning,
        )

I tried with a different y_true and y_pred tensor with same y_pred bring constant, and the result was expected, nan was in the output.
I think this is unique to your particular case of input tensor.

@Borda Borda added the v1.6.x label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed v1.6.x
Projects
None yet
Development

No branches or pull requests

3 participants