cv_mahalanobis#

mvpy.math.cv_mahalanobis(x: ndarray | Tensor, y: ndarray | Tensor, Σ: ndarray | Tensor) ndarray | Tensor[source]#

Computes cross-validated mahalanobis distances between x and y. This is sometimes also referred to as the crossnobis distance.

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

Matrix ([samples …] x features)

yUnion[np.ndarray, torch.Tensor]

Matrix ([samples …] x features)

ΣUnion[np.ndarray, torch.Tensor]

Precision matrix (features x features)

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

Vector or matrix of distances. Note that this will eliminate the trial dimension, too, during cross-validation.

Notes

Crossnobis distance is defined as:

\[d(x, y)^2 = (x_i - y_i)^T Σ^{-1} (x_j - y_j)\]

where \(x\) and \(y\) are the matrices to compute the distance between, and \(Σ\) is the covariance matrix. Note that here \(i\) and \(j\) refer to folds of a cross-validation. Note also that, principally, this metric is a squared measure. For more information, see [2].

References

[2]

Diedrichsen, J., Provost, S., & Zareamoghaddam, H. (2016). On the distribution of cross-validated Mahalanobis distances. arXiv. 10.48550/arXiv.1607.01371

Examples

>>> import torch
>>> from mvpy.estimators import Covariance
>>> from mvpy.math import cv_mahalanobis
>>> x, y = torch.normal(0, 1, (100, 50, 60)), torch.normal(0, 1, (100, 50, 60))
>>> Σ = Covariance().fit(torch.cat((x, y), 0).swapaxes(1, 2)).covariance_
>>> Σ = torch.linalg.inv(Σ)
>>> d = cv_mahalanobis(x, y, Σ)
>>> d.shape
torch.Size([50])