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

Fix pearson aggregation #998

Merged
merged 12 commits into from
Apr 29, 2022
2 changes: 1 addition & 1 deletion tests/regression/test_pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _sk_pearsonr(preds, target):
],
)
class TestPearsonCorrcoef(MetricTester):
atol = 1e-2
atol = 1e-8
Borda marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.parametrize("compute_on_cpu", [True, False])
@pytest.mark.parametrize("ddp", [True, False])
Expand Down
30 changes: 22 additions & 8 deletions torchmetrics/regression/pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,34 @@ def _final_aggregation(
mx1, my1, vx1, vy1, cxy1, n1 = means_x[0], means_y[0], vars_x[0], vars_y[0], corrs_xy[0], nbs[0]
for i in range(1, len(means_x)):
mx2, my2, vx2, vy2, cxy2, n2 = means_x[i], means_y[i], vars_x[i], vars_y[i], corrs_xy[i], nbs[i]

nb = n1 + n2
mean_x = (n1 * mx1 + n2 * mx2) / nb
mean_y = (n1 * my1 + n2 * my2) / nb
var_x = 1 / (n1 + n2 - 1) * ((n1 - 1) * vx1 + (n2 - 1) * vx2 + ((n1 * n2) / (n1 + n2)) * (mx1 - mx2) ** 2)
var_y = 1 / (n1 + n2 - 1) * ((n1 - 1) * vy1 + (n2 - 1) * vy2 + ((n1 * n2) / (n1 + n2)) * (my1 - my2) ** 2)

corr1 = n1 * cxy1 + n1 * (mx1 - mean_x) * (my1 - mean_y)
corr2 = n2 * cxy2 + n2 * (mx2 - mean_x) * (my2 - mean_y)
corr_xy = (corr1 + corr2) / (n1 + n2)
# var_x
magic_element_x1 = (n1 + 1) * mean_x - n1 * mx1
vx1 += (magic_element_x1 - mx1) * (magic_element_x1 - mean_x) - (magic_element_x1 - mean_x) ** 2
magic_element_x2 = (n2 + 1) * mean_x - n2 * mx2
vx2 += (magic_element_x2 - mx2) * (magic_element_x2 - mean_x) - (magic_element_x2 - mean_x) ** 2
var_x = vx1 + vx2

# var_y
magic_element_y1 = (n1 + 1) * mean_y - n1 * my1
vy1 += (magic_element_y1 - my1) * (magic_element_y1 - mean_y) - (magic_element_y1 - mean_y) ** 2
magic_element_y2 = (n2 + 1) * mean_y - n2 * my2
vy2 += (magic_element_y2 - my2) * (magic_element_y2 - mean_y) - (magic_element_y2 - mean_y) ** 2
var_y = vy1 + vy2

# corr
cxy1 += (magic_element_x1 - mx1) * (magic_element_y1 - mean_y) - (magic_element_x1 - mean_x) * (
magic_element_y1 - mean_y
)
cxy2 += (magic_element_x2 - mx2) * (magic_element_y2 - mean_y) - (magic_element_x2 - mean_x) * (
magic_element_y2 - mean_y
)
corr_xy = cxy1 + cxy2
Borda marked this conversation as resolved.
Show resolved Hide resolved

mx1, my1, vx1, vy1, cxy1, n1 = mean_x, mean_y, var_x, var_y, corr_xy, nb

return var_x, var_y, corr_xy, nb


Expand Down Expand Up @@ -123,5 +138,4 @@ def compute(self) -> Tensor:
var_y = self.var_y
corr_xy = self.corr_xy
n_total = self.n_total

return _pearson_corrcoef_compute(var_x, var_y, corr_xy, n_total)