covmats.CovViaDiagonal#

class covmats.CovViaDiagonal(*args, **kwargs)[source]#

Representation of a covariance matrix via its diagonal.

Variables:

diagonal (NDArrayFloat) – The diagonal elements of a diagonal matrix.

Notes

Let the diagonal elements of a diagonal covariance matrix \(D\) be stored in the vector \(d\).

When all elements of \(d\) are strictly positive, whitening of a data point \(x\) is performed by computing \(x \cdot d^{-1/2}\), where the inverse square root can be taken element-wise. \(\log\det{D}\) is calculated as \(-2 \sum(\log{d})\), where the \(\log\) operation is performed element-wise.

Notes

No null nor negative elements are allowed, otherwise the matrix would be singular or indefinite and consequently non invertible.

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 = 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 = covmats.CovViaDiagonal(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 = cov.log_pdet
>>> ref = np.linalg.slogdet(A)[-1]
>>> np.allclose(res, ref)
True
__init__(diagonal: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]) None[source]#

Initialize the instance.

Parameters:

diagonal (ArrayLike) – The diagonal elements of a diagonal matrix.

Properties

D

1D vector representing the diagonal elements of the covariance matrix.

H

Hermitian adjoint.

T

Transpose this linear operator.

covariance

Explicit dense representation of the covariance matrix.

log_pdet

Log of the pseudo-determinant of the covariance matrix.

n_pts

Number of points in the domain (n).

ndim

precision

Explicit dense representation of the precision matrix.

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_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

Solve Ax = b, with A, the current covariance matrix instance.

todense

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

transpose

Transpose this linear operator.

whiten

Perform a whitening transformation on data.