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 A and a data point x.

>>> 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 A and 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

H

Hermitian adjoint.

T

Transpose this linear operator.

covariance

Explicit dense representation of the covariance matrix.

eig_vals

Return the Eigen values.

eig_vects

Return the Eigen vectors.

log_pdet

Log of the pseudo-determinant of the covariance matrix.

n_pc

Return the number of eigen vectors/values, i.e. principal components.

n_pts

Number of points in the domain (n).

ndim

precision

Explicit dense representation of the precision matrix with shape (n, n).

rank

Rank of the covariance matrix.

shape

Shape of the covariance matrix (n, n).

subspace_size

Subspace size of the covariance matrix.

Methods

adjoint

Hermitian adjoint.

colorize

Perform a colorizing transformation on data.

dot

Matrix-matrix or matrix-vector multiplication.

from_cholesky

Representation of a covariance provided via choleksy factorization.

from_diagonal

Representation of a covariance provided via diagonal.

from_eigendecomposition

Representation of a covariance provided via eigendecomposition.

from_precision

Return a representation of a covariance from its precision matrix.

get_diagonal

Return the diagonal entries of the matrix (variances).

get_sparse_LLT_factor

Return the sparse factor L of the LL^T factorization of the eigen matrix.

get_trace

Return the trace of the covariance matrix (sum of diagonal elements).

matmat

Matrix-matrix multiplication.

matvec

Matrix-vector multiplication.

rmatmat

Adjoint matrix-matrix multiplication.

rmatvec

Adjoint matrix-vector multiplication.

sample_mvnormal

Draw samples from the multivariate normal N(0, A).

solve

Return $Q^{-1} b = ZD^{-1}Z^{T}b$.

todense

Explicit dense representation of the covariance matrix with shape (n, n).

transpose

Transpose this linear operator.

whiten

Perform a whitening transformation on data.