Skip to content

matrices⚓︎

Covariance matrix tools.

CovMat ⚓︎

Covariance matrix class.

Main tasks:

  • Unify the covariance representations: full, diagonal, reduced-rank sqrt.
  • Streamline init. and printing.
  • Convenience transformations with caching/memoization. This (hiding it internally) would be particularly useful if the covariance matrix changes with time (but repeat).

Left ⚓︎

Left sqrt.

L such that $$ C = L L^T .$$

Note that L is typically rectangular, but not triangular, and that its width is somewhere betwen the rank and M.

M ⚓︎

ndims

Right ⚓︎

Right sqrt. Ref CovMat.Left.

V ⚓︎

Eigenvectors, output corresponding to ews.

ews ⚓︎

Eigenvalues. Only outputs the positive values (i.e. len(ews)==rk).

full ⚓︎

Full covariance matrix

kind ⚓︎

Form in which matrix was specified.

rk ⚓︎

Rank, i.e. the number of positive eigenvalues.

trunc ⚓︎

Truncation threshold.

__init__(data, kind='full_or_diag', trunc=1.0) ⚓︎

Construct object.

The covariance (say P) can be input (specified in the following ways):

kind    | data
--------|-------------
'full'  | full M-by-M array (P)
'diag'  | diagonal of P (assumed diagonal)
'E'     | ensemble (N-by-M) with sample cov P
'A'     | as 'E', but pre-centred by mean(E,axis=0)
'Right' | any R such that P = R.T@R (e.g. weighted form of 'A')
'Left'  | any L such that P = L@L.T

diag() ⚓︎

Diagonal of covariance matrix

has_done_EVD() ⚓︎

Whether or not eigenvalue decomposition has been done for matrix.

pinv() ⚓︎

Pseudo-inverse. Uses trunc-level.

sym_sqrt() ⚓︎

S such that C = S@S (and i.e. S is square). Uses trunc-level.

sym_sqrt_inv() ⚓︎

S such that C^{-1} = S@S (and i.e. S is square). Uses trunc-level.

transform_by(fun) ⚓︎

Generalize scalar functions to covariance matrices (via Taylor expansion).

lazy_property ⚓︎

Lazy evaluation of property.

Should represent non-mutable data, as it replaces itself.

From https://stackoverflow.com/q/3012421

basis_beginning_with_ones(ndim) ⚓︎

Basis whose first vector is ones(ndim).

chol_reduce(Right) ⚓︎

Return rnk-by-ndim R such that R.T@R - R.T@R ≈ 0.

Examples:

>>> from dapper.stats import mean0
>>> X = mean0(np.random.randn(20, 5), axis=1)
>>> C = X.T @ X
>>> # sla.cholesky(C) throws error
>>> R = chol_reduce(X)
>>> R.shape[1] == 5
True

funm_psd(a, fun, check_finite=False) ⚓︎

Matrix function evaluation for pos-sem-def mat.

Adapted from sla.funm doc.

Examples:

>>> def sqrtm_psd(A):
...     return funm_psd(A, sqrt)

genOG(M) ⚓︎

Generate random orthonormal matrix.

genOG_1(N, opts=()) ⚓︎

Random orthonormal mean-preserving matrix.

Source: ienks code of Sakov/Bocquet.

genOG_modified(M, opts=(0, 1.0)) ⚓︎

Do genOG with modifications.

Caution: although 'degree' ∈ (0,1) for all versions, they're not supposed going to be strictly equivalent.

Testing: scripts/sqrt_rotations.py

randcorr(M) ⚓︎

(Makeshift) random corr mat.

randcov(M) ⚓︎

(Makeshift) random cov mat.