Linearize
This sub-repository provides functions for linearizing conditional distributions with automatic differentiation into a linear-Gaussian form. That is, form an approximate Gaussian defined by the tuple \((H, d, L)\) such that
Additionally, some linearization techniques may apply to an unconditional potential \(G(x)\) and return a tuple \((m, L)\) such that
The former approach requires a conditional distribution that is differentiable with respect to \(x\) and \(y\). The latter approach only requires differentiability with respect to \(x\) and therefore works with e.g. discrete or non-ordinal \(y\).
Linearization techniques
linearize_log_density: Linearize a conditional log density around given points.linearize_moments: Linearize conditional mean and Cholesky covariance functions around a given point.linearize_taylor: Linearize a log potential function around a given point using Taylor expansion.
Linearization with sigma points can also be found in the [quadrature] sub-repository.
Example usage
Specifically for linearize_log_density, the usage is as follows:
from linearize import linearize_log_density
def log_density(x, y):
... # some conditional log density function log p(y|x) that returns a scalar
x, y = ... # some input points
mat, shift, chol_cov = linearize_log_density(log_density, x, y)
Note that when log_density is exactly linear Gaussian, then the output from
linearize_log_density is exact for all points x and y. For non-linear and/or
non-Gaussian log_density, the output is an approximation that will truncate any
singular values of the precision matrix (negative Hessian of log_density).
cuthbertlib.linearize.log_density
Implements linearization of conditional log densities.
linearize_log_density(log_density, x, y, has_aux=False, rtol=None, ignore_nan_dims=False)
Linearizes a conditional log density around given points.
The linearization is exact in the case of a linear-Gaussian log_density, i.e., it returns
\((H, d, L)\) if log_density is of the form
The Cholesky factor of the covariance is calculated using the negative Hessian
of log_density with respect to y as the precision matrix.
symmetric_inv_sqrt is used to calculate the inverse square root by
ignoring any singular values that are sufficiently close to zero
(this is a projection in the case the Hessian is not positive definite).
Alternatively, the Cholesky factor can be provided directly
in linearize_log_density_given_chol_cov.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_density
|
LogConditionalDensity | LogConditionalDensityAux
|
A conditional log density of y given x. Returns a scalar. |
required |
x
|
ArrayLike
|
The input points. |
required |
y
|
ArrayLike
|
The output points. |
required |
has_aux
|
bool
|
Whether |
False
|
rtol
|
float | None
|
The relative tolerance for the singular values of the precision matrix
when passed to |
None
|
ignore_nan_dims
|
bool
|
Whether to treat dimensions with NaN on the diagonal of the precision matrix as missing and ignore all rows and columns associated with them. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Array, Array, Array] | tuple[Array, Array, Array, ArrayTree]
|
Linearized matrix, shift, and Cholesky factor of the covariance matrix.
The auxiliary value is also returned if |
Source code in cuthbertlib/linearize/log_density.py
linearize_log_density_given_chol_cov(log_density, x, y, chol_cov, has_aux=False, ignore_nan_dims=False)
Linearizes a conditional log density around given points.
The linearization is exact in the case of a linear-Gaussian log_density, i.e., it returns
\((H, d)\) if log_density is of the form
where \(L\) is the argument chol_cov.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_density
|
LogConditionalDensity | LogConditionalDensityAux
|
A conditional log density of y given x. Returns a scalar. |
required |
x
|
ArrayLike
|
The input points. |
required |
y
|
ArrayLike
|
The output points. |
required |
chol_cov
|
ArrayLike
|
The Cholesky factor of the covariance matrix of the Gaussian. |
required |
has_aux
|
bool
|
Whether |
False
|
ignore_nan_dims
|
bool
|
Whether to ignore dimensions with NaN on the diagonal of the precision matrix or in y. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Array, Array] | tuple[Array, Array, ArrayTree]
|
Linearized matrix and shift. The auxiliary value is also returned if |
Source code in cuthbertlib/linearize/log_density.py
cuthbertlib.linearize.moments
Implements moment-based linearization.
MeanAndCholCovFunc = Callable[[ArrayLike], tuple[Array, Array]]
module-attribute
MeanAndCholCovFuncAux = Callable[[ArrayLike], tuple[Array, Array, ArrayTree]]
module-attribute
linearize_moments(mean_and_chol_cov_function, x, has_aux=False)
Linearizes conditional mean and chol_cov functions into a linear-Gaussian form.
Takes a function mean_and_chol_cov_function(x) that returns the
conditional mean and Cholesky factor of the covariance matrix of the distribution
\(p(y \mid x)\) for a given input x.
Returns \((H, d, L)\) defining a linear-Gaussian approximation to the conditional distribution \(p(y \mid x) \approx N(y \mid H x + d, L L^\top)\).
mean_and_chol_cov_function has the following signature with has_aux = False:
has_aux = True:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean_and_chol_cov_function
|
MeanAndCholCovFunc | MeanAndCholCovFuncAux
|
A callable that returns the conditional mean and Cholesky factor of the covariance matrix of the distribution for a given input. |
required |
x
|
ArrayLike
|
The point to linearize around. |
required |
has_aux
|
bool
|
Whether |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Array, Array, Array] | tuple[Array, Array, Array, ArrayTree]
|
Linearized matrix, shift, and Cholesky factor of the covariance matrix.
The auxiliary value is also returned if |
References
Source code in cuthbertlib/linearize/moments.py
cuthbertlib.linearize.taylor
Implements Taylor-like linearization.
linearize_taylor(log_potential, x, has_aux=False, rtol=None, ignore_nan_dims=False)
Linearizes a log potential function around a given point using Taylor expansion.
Unlike the other linearization methods, this applies to a potential function with no required notion of observation \(y\) or conditional dependence.
Instead we have the linearization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_potential
|
Callable[[ArrayLike], Array] | Callable[[ArrayLike], tuple[Array, ArrayTree]]
|
A callable that returns a non-negative scalar. Does not need to be a normalized probability density in its input. |
required |
x
|
ArrayLike
|
The point to linearize around. |
required |
has_aux
|
bool
|
Whether |
False
|
rtol
|
float | None
|
The relative tolerance for the singular values of the precision matrix
when passed to |
None
|
ignore_nan_dims
|
bool
|
Whether to treat dimensions with NaN on the diagonal of the precision matrix as missing and ignore all rows and columns associated with them. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Array, Array] | tuple[Array, Array, ArrayTree]
|
Linearized mean and Cholesky factor of the covariance matrix.
The auxiliary value is also returned if |