spd_learn.functional.cov_to_corr#

spd_learn.functional.cov_to_corr(S: Tensor, eps: float = 1e-06) → Tensor[source]#

Convert an SPD covariance matrix to a full-rank correlation matrix.

Converts an SPD covariance matrix \(S \in \mathcal{S}_{++}^n\) to a full-rank correlation matrix \(C \in \mathcal{C}_{++}^n\) with unit diagonal:

\[d = \text{clamp}(\text{diag}(S), \min=\epsilon)^{-1/2}\]
\[C = \text{diag}(d) \, S \, \text{diag}(d)\]

with the diagonal explicitly set to 1.

Parameters:
  • S (torch.Tensor) – SPD covariance matrices of shape (…, n, n).

  • eps (float, default=1e-6) – Small positive constant for numerical stability of division.

Returns:

Full-rank correlation matrix with unit diagonal, shape (…, n, n).

Return type:

torch.Tensor

Examples

>>> import torch
>>> from spd_learn.functional import cov_to_corr
>>> S = torch.tensor([[4.0, 1.0], [1.0, 9.0]])
>>> cov_to_corr(S)
tensor([[1.0000, 0.1667],
        [0.1667, 1.0000]])