This is a pre-release version (latest).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy latest / scipy / stats / _covariance / Covariance / from_eigendecomposition

staticmethod

scipy.stats._covariance:Covariance.from_eigendecomposition

source: build-install/usr/lib/python3.14/site-packages/scipy/stats/_covariance.py:269

Signature

staticmethoddef from_eigendecomposition(eigendecomposition)

Summary

Representation of a covariance provided via eigendecomposition.

Parameters

eigendecomposition : sequence

A sequence (nominally a tuple) containing the eigenvalue and eigenvector arrays as computed by scipy.linalg.eigh or numpy.linalg.eigh.

Returns

: Covariance

A Covariance object representing the covariance matrix corresponding to eigendecomposition.

Notes

Let the covariance matrix be , let be matrix of eigenvectors, and let be the diagonal matrix of eigenvalues such that V W V^T = A.

When all of the eigenvalues are strictly positive, whitening of a data point is performed by computing , where the inverse square root can be taken element-wise. is calculated as , where the operation is performed element-wise.

This Covariance class supports singular covariance matrices. When computing _log_pdet, non-positive eigenvalues are ignored. Whitening is not well defined when the point to be whitened does not lie in the span of the columns of the covariance matrix. The convention taken here is to treat the inverse square root of non-positive eigenvalues as zeros.

Examples

Prepare a symmetric positive definite covariance matrix ``A`` and a data point ``x``.
import numpy as np
from scipy import stats
rng = np.random.default_rng()
n = 5
A = rng.random(size=(n, n))
A = A @ A.T  # make the covariance symmetric positive definite
x = rng.random(size=n)
✓
Perform the eigendecomposition of ``A`` and create the `Covariance` object.
w, v = np.linalg.eigh(A)
cov = stats.Covariance.from_eigendecomposition((w, v))
✓
Compare the functionality of the `Covariance` object against reference implementations.
res = cov.whiten(x)
ref = x @ (v @ np.diag(w**-0.5))
np.allclose(res, ref)
res = cov.log_pdet
ref = np.linalg.slogdet(A)[-1]
np.allclose(res, ref)
✓
True

Aliases

  • scipy.stats.Covariance.from_eigendecomposition