Skip to content


[ ]


class PearsonCorrelation(Metric):
 | def __init__(self) -> None

This Metric calculates the sample Pearson correlation coefficient (r) between two tensors. Each element in the two tensors is assumed to be a different observation of the variable (i.e., the input tensors are implicitly flattened into vectors and the correlation is calculated between the vectors).

This implementation is mostly modeled after the streaming_pearson_correlation function in Tensorflow. See

This metric delegates to the Covariance metric the tracking of three [co]variances:

  • covariance(predictions, labels), i.e. covariance
  • covariance(predictions, predictions), i.e. variance of predictions
  • covariance(labels, labels), i.e. variance of labels

If we have these values, the sample Pearson correlation coefficient is simply:

r = covariance / (sqrt(predictions_variance) * sqrt(labels_variance))

if predictions_variance or labels_variance is 0, r is 0


class PearsonCorrelation(Metric):
 | ...
 | def __call__(
 |     self,
 |     predictions: torch.Tensor,
 |     gold_labels: torch.Tensor,
 |     mask: Optional[torch.BoolTensor] = None
 | )


  • predictions : torch.Tensor
    A tensor of predictions of shape (batch_size, ...).
  • gold_labels : torch.Tensor
    A tensor of the same shape as predictions.
  • mask : torch.BoolTensor, optional (default = None)
    A tensor of the same shape as predictions.


class PearsonCorrelation(Metric):
 | ...
 | def get_metric(self, reset: bool = False)


  • The accumulated sample Pearson correlation.


class PearsonCorrelation(Metric):
 | ...
 | @overrides
 | def reset(self)