pearsonr#

mvpy.math.pearsonr(x: ndarray | Tensor, y: ndarray | Tensor, *args: Any) ndarray | Tensor[source]#

Computes pearson correlations between x and y. Note that correlations are always computed over the final dimension.

Parameters:
xUnion[np.ndarray, torch.Tensor]

Matrix ([samples …] x features)

yUnion[np.ndarray, torch.Tensor]

Matrix ([samples …] x features)

Returns:
Union[np.ndarray, torch.Tensor]

Vector or matrix of pearson correlations

Notes

Pearson correlations are defined as:

\[r = \frac{\sum{(x_i - \bar{x})(y_i - \bar{y})}}{\sqrt{\sum{(x_i - \bar{x})^2} \sum{(y_i - \bar{y})^2}}}\]

where \(x_i\) and \(y_i\) are the \(i\)-th elements of \(x\) and \(y\), respectively.

Examples

>>> import torch
>>> from mvpy.math import pearsonr
>>> x = torch.tensor([1, 2, 3])
>>> y = torch.tensor([4, 5, 6])
>>> pearsonr(x, y)
tensor(1., dtype=torch.float64)