Linalg
This sub-repository contains any modular linear algebra primitives that are useful
for cuthbert and not already provided by jax.
In particular we have:
tria, which computes a lower triangular matrix square root of a given positive definite matrixRsuch thatR @ R.T = A @ A.Tfor a given matrixAthat is not necessarily square.collect_nans_chol, which reorders a generalized Cholesky factor to move a specified subset of rows and columns to the start with remaining dimensions moved to the end and parameterized so that they are ignored in a Bayesian update or logpdf calculation.symmetric_inv_sqrt, which computes the inverse square root of a symmetric matrix. It does so exactly in the case that the matrix is positive definite. In the case of zero or negative singular values, it supports approximate inverse square roots in a similar manner to (Moore-Penrose) pseudo-inversion.
cuthbertlib.linalg.tria
Implements triangularization operator a matrix via QR decomposition.
tria(A)
A triangularization operator using QR decomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
The matrix to triangularize. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
A lower triangular matrix \(R\) such that \(R R^\top = A A^\top\). |
Reference
Arasaratnam and Haykin (2008): Square-Root Quadrature Kalman Filtering
Source code in cuthbertlib/linalg/tria.py
cuthbertlib.linalg.collect_nans_chol
Implements collection of NaNs and reordering within a Cholesky factor.
collect_nans_chol(flag, chol, *rest)
Converts chol into an order chol factor with NaNs moved to the bottom right.
Specifically, converts a generalized Cholesky factor of a covariance matrix wit NaNs into an ordered generalized Cholesky factor with NaNs rows and columns moved to the end with diagonal elements set to 1.
Also reorders the rest of the arguments in the same way along the first axis and sets to 0 for dimensions where flag is True.
Example behavior:
flag = jnp.array([False, True, False, True])
new_flag, new_chol, new_mean = collect_nans_chol(flag, chol, mean)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
ArrayLike
|
Array, boolean array indicating which entries are NaN True for NaN entries, False for valid |
required |
chol
|
ArrayLike
|
Array, Cholesky factor of the covariance matrix |
required |
rest
|
Any
|
Any, rest of the arguments to be reordered in the same way along the first axis |
()
|
Returns:
| Type | Description |
|---|---|
Any
|
flag, chol and rest reordered so that valid entries are first and NaNs are last. Diagonal elements of chol are set to 1/√2π so that normalization is correct |
Source code in cuthbertlib/linalg/collect_nans_chol.py
cuthbertlib.linalg.symmetric_inv_sqrt
Implements inverse square root of a symmetric matrix.
symmetric_inv_sqrt(A, rtol=None, ignore_nan_dims=False)
Computes the inverse square root of a symmetric matrix.
I.e., a lower triangular matrix \(L\) such that \(L L^{\top} = A^{-1}\) (for positive definite \(A\)). Note that this is not unique and will generally not match the Cholesky factor of \(A^{-1}\).
For singular matrices, small singular values will be cut off reminiscent of the Moore-Penrose pseudoinverse - https://docs.jax.dev/en/latest/_autosummary/jax.numpy.linalg.pinv.html.
In the case of singular or indefinite \(A\), the output will be an approximation and \(L L^{\top} = A^{-1}\) will not hold in general.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ArrayLike
|
A symmetric matrix. |
required |
rtol
|
float | ArrayLike | None
|
The relative tolerance for the singular values.
Cutoff for small singular values; singular values smaller than
|
None
|
ignore_nan_dims
|
bool
|
Whether to treat dimensions with NaN on the diagonal as missing and ignore all rows and columns associated with them (with result in those dimensions being NaN on the diagonal and zero off-diagonal). |
False
|
Returns:
| Type | Description |
|---|---|
Array
|
A lower triangular matrix \(L\) such that \(L L^{\top} = A^{-1}\) (for valid dimensions). |
Source code in cuthbertlib/linalg/symmetric_inv_sqrt.py
chol_cov_with_nans_to_cov(chol_cov)
Converts a Cholesky factor to a covariance matrix.
NaNs on the diagonal specify dimensions to be ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chol_cov
|
ArrayLike
|
A Cholesky factor of a covariance matrix with NaNs on the diagonal specifying dimensions to be ignored. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
A covariance matrix equivalent to chol_cov @ chol_cov.T in dimensions where the Cholesky factor is valid and for invalid dimensions (ones with NaN on the diagonal in chol_cov) with NaN on the diagonal and zero off-diagonal. |