Skip to content

Commit

Permalink
Improve calculations in Pearson (#1729)
Browse files Browse the repository at this point in the history
* fixing
* changelog
* fix condition in multi case
  • Loading branch information
SkafteNicki authored Apr 26, 2023
1 parent c3628b0 commit 2c25f5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed FID matrix square root calculation from `scipy` to `torch` ([#1708](https://github.com/Lightning-AI/torchmetrics/pull/1708))


- Changed calculation in `PearsonCorrCoeff` to be more robust in certain cases ([#1729](https://github.com/Lightning-AI/torchmetrics/pull/1729))

### Deprecated

- Deprecated domain metrics import from package root (
Expand Down
20 changes: 16 additions & 4 deletions src/torchmetrics/functional/regression/pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ def _pearson_corrcoef_update(
# Data checking
_check_same_shape(preds, target)
_check_data_shape_to_num_outputs(preds, target, num_outputs)
cond = n_prior.mean() > 0

n_obs = preds.shape[0]
mx_new = (n_prior * mean_x + preds.mean(0) * n_obs) / (n_prior + n_obs)
my_new = (n_prior * mean_y + target.mean(0) * n_obs) / (n_prior + n_obs)
if cond:
mx_new = (n_prior * mean_x + preds.sum(0)) / (n_prior + n_obs)
my_new = (n_prior * mean_y + target.sum(0)) / (n_prior + n_obs)
else:
mx_new = preds.mean(0)
my_new = target.mean(0)

n_prior += n_obs
var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)
var_y += ((target - my_new) * (target - mean_y)).sum(0)

if cond:
var_x += ((preds - mx_new) * (preds - mean_x)).sum(0)
var_y += ((target - my_new) * (target - mean_y)).sum(0)

else:
var_x += preds.var(0) * (n_obs - 1)
var_y += target.var(0) * (n_obs - 1)
corr_xy += ((preds - mx_new) * (target - mean_y)).sum(0)
mean_x = mx_new
mean_y = my_new
Expand Down

0 comments on commit 2c25f5d

Please sign in to comment.