fips.Covariance#
- class fips.Covariance(data, space, name=None)[source]#
Bases:
OperatorA covariance: a symmetric operator from a
Spaceto itself.The matrix is kept in its cheapest form, as an
Operator’s is: a diagonal covariance is its variances, a Kronecker one its factors, a block diagonal one each block in its own form. On top of an operator’s methods it gives thevariancesand, for a positive-definite covariance,solve(),logdet(),quadratic_form()andsqrt(), each done the way its form makes cheap.- Parameters:
data (LinearOperator | sp.spmatrix | sp.sparray | npt.ArrayLike) – The matrix, square, rows and columns in the order of space. An array or a sparse matrix is copied, and the copy is read-only, as for an
Operator.space (
Space) – The labels of both axes.name (
str|None) – What the covariance is (“prior_cov”, “obs_cov”, …).
- Raises:
TypeError – If data is a pandas object or an
Operator(its labels would be ignored: usecov.align(space, space), orcov.datafor the matrix), or space is not a Space.ValueError – If the matrix does not have one row and one column per label, it stores a NaN, or a dense or sparse matrix is not symmetric: its largest
|A - A^T|is more than 1e-10 of its largest entry (the message names the two entries and their labels). A diagonal or Kronecker form is symmetric by construction, and a block diagonal one is checked block by block. Also if the matrix is not positive semidefinite: an eigenvalue is below zero by more than roundoff,n * epstimes the largest (the message names the smallest).
Notes
The positive semidefinite check is the root
sample()draws with, built here and kept: the Cholesky factor of a dense matrix (n^3 / 3operations, andn x nmore memory), of each group of correlated rows of a sparse one, of each block of a block diagonal one and of each factor of a Kronecker one, or the square roots of a diagonal. A singular matrix, which has no Cholesky factor, is eigendecomposed instead (several times the cost), with one INFO line. A covariance fips builds from a correlation model or computes (a sum, a reordering, a posterior) is positive semidefinite by construction and is not checked.See also
Covariance.diagonalA covariance from its variances.
Covariance.block_diagIndependent covariances side by side.
fips.ExponentialA correlation model, which
with_stdmakes a covariance.Operator.from_frameA covariance from a labeled DataFrame.
Examples
>>> import numpy as np >>> import pandas as pd >>> from fips import Covariance, Space, Vector >>> space = Space({"flux": pd.Index(["a", "b"], name="cell")}) >>> S = Covariance(np.array([[2.0, 1.0], [1.0, 2.0]]), space) >>> S.variances.values array([2., 2.]) >>> S.solve(Vector([3.0, 3.0], space)).values array([1., 1.])
Attributes
Methods
|
Return the covariance of aggregates, \(W S W^T\). |
|
Put independent covariances side by side: each on its own blocks, zero between. |
|
Build a diagonal covariance from its variances. |
|
Return |
Return \(x^T S^{-1} x\), the squared Mahalanobis length of x. |
|
|
Return one draw from \(N(0, S)\), a vector on the covariance's space. |
|
Return \(S^{-1} b\), for a positive-definite covariance \(S\). |
|
Return a square root \(L\), with \(L L^T = S\). |
Inherited
From Operator: align, cols, data, dense, from_blocks, from_frame, name, round_labels, rows, shape, sparse, T, to_frame