mahalanobis#
- mvpy.math.mahalanobis(x: ndarray | Tensor, y: ndarray | Tensor, Σ: ndarray | Tensor, *args: Any) ndarray | Tensor[source]#
Computes mahalanobis distance between x and y using inverse covariance matrix Σ.
- 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.
Notes
Mahalanobis distance is defined as:
\[d(x, y) = \sqrt{(x - y)^T Σ^{-1} (x - y)}\]where \(x\) and \(y\) are the matrices to compute the distance between, and \(Σ\) is the covariance matrix.
Examples
>>> import torch >>> from mvpy.math import mahalanobis >>> from mvpy.estimators import Covariance >>> x, y = torch.normal(0, 1, (10, 50)), torch.normal(0, 1, (10, 50)) >>> Σ = Covariance().fit(torch.cat((x, y), 0)).covariance_ >>> Σ = torch.linalg.inv(Σ) >>> d = mahalanobis(x, y, Σ) >>> d.shape torch.Size([10])