covmats.CovViaEigenFactorization#
- class covmats.CovViaEigenFactorization(*args, **kwargs)[source]#
Representation of a covariance provided via eigenfactorization
- Parameters:
eigenfactorization (sequence) – A sequence (nominally a tuple) containing the eigenvalue and eigenvector arrays as computed by scipy.sparse.eigsh.
Notes
Let the covariance matrix be \(A\), let \(V\) be matrix of eigenvectors, and let \(W\) 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 \(x\) is performed by computing \(x^T (V W^{-1/2})\), where the inverse square root can be taken element-wise. \(\log\det{A}\) is calculated as \(tr(\log{W})\), where the \(\log\) 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
Aand a data pointx.>>> import numpy as np >>> import covmats >>> 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 eigenfactorization of
Aand create the Covariance object.>>> w, v = np.linalg.eigh(A) >>> cov = covmats.CovViaEigenFactorization((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) True >>> res = cov.log_pdet >>> ref = np.linalg.slogdet(A)[-1] >>> np.allclose(res, ref) True
- __init__(eigenfactorization: Tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]]]) None[source]#
Initialize the instance.
- Parameters:
(eig_vals (Tuple[NDArrayFloat, NDArrayFloat]) –
1D vector of eigen values with size n_pc.
- 2D arrays of eigen vectors (columns) with size (Ns, n_pc). Ns being the
number of elements in the original covariance matrix.
eig_vects) (Tuple[NDArrayFloat, NDArrayFloat]) –
1D vector of eigen values with size n_pc.
- 2D arrays of eigen vectors (columns) with size (Ns, n_pc). Ns being the
number of elements in the original covariance matrix.
Properties
Hermitian adjoint.
Transpose this linear operator.
Explicit dense representation of the covariance matrix.
Return the Eigen values.
Return the Eigen vectors.
Log of the pseudo-determinant of the covariance matrix.
Return the number of eigen vectors/values, i.e. principal components.
Number of points in the domain (n).
Explicit dense representation of the precision matrix with shape (n, n).
Rank of the covariance matrix.
Shape of the covariance matrix (n, n).
Subspace size of the covariance matrix.
Methods
Hermitian adjoint.
Perform a colorizing transformation on data.
Matrix-matrix or matrix-vector multiplication.
Representation of a covariance provided via choleksy factorization.
Representation of a covariance provided via diagonal.
Representation of a covariance provided via eigendecomposition.
Return a representation of a covariance from its precision matrix.
Return the diagonal entries of the matrix (variances).
Return the sparse factor L of the LL^T factorization of the eigen matrix.
Return the trace of the covariance matrix (sum of diagonal elements).
Matrix-matrix multiplication.
Matrix-vector multiplication.
Adjoint matrix-matrix multiplication.
Adjoint matrix-vector multiplication.
Draw samples from the multivariate normal N(0, A).
Return $Q^{-1} b = ZD^{-1}Z^{T}b$.
Explicit dense representation of the covariance matrix with shape (n, n).
Transpose this linear operator.
Perform a whitening transformation on data.