from_diagonal
- static Covariance.from_diagonal(diagonal)
Return a representation of a covariance matrix from its diagonal.
- Parameters:
- diagonal : array_like
The diagonal elements of a diagonal matrix.
- Returns:
- Covariance
Return a representation of a covariance matrix from its diagonal.
The diagonal elements of a diagonal matrix.
A Covariance object representing diagonal.
Let the diagonal elements of a diagonal covariance matrix be stored in the vector .
When all elements of 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 elements of 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 elements of as zeros.
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 = np.diag(rng.random(n))
>>> x = rng.random(size=n)
Extract the diagonal from A and create the Covariance object.
>>> d = np.diag(A)
>>> cov = stats.Covariance.from_diagonal(d)
Compare the functionality of the Covariance object against a
reference implementations.
>>> res = cov.whiten(x)
>>> ref = np.diag(d**-0.5) @ x
>>> np.allclose(res, ref)
True
>>> res =