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
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.
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.
🐛 Bug
When computing the Pearson correlation using
torchmetrics.PearsonCorrCoef
, it returns1
for some cases wherey_true
is constant. Mathematically, the Pearson correlation should be undefined (or return NaN), since the standard deviation ofy_true
is zero. In contrast,scipy.stats.pearsonr
returns0
in such cases.To Reproduce
Returns:
Expected behavior
For columns where
y_true
is constant (e.g., y_true[:, 2] = [1, 1]), the Pearson correlation should returnnan
.Environment
Others
Might be related to: scikit-learn/scikit-learn#25258?
The text was updated successfully, but these errors were encountered: