Likelihood¶
mgsl.Likelihood
¶
A likelihood on a latent with a standard-normal prior.
The latent x can be any pytree of real floating-point arrays. The prior on x is
always N(0, I): a model with another prior is written in standardised (whitened)
coordinates. For example, a parameter z ~ N(m, L L^T) becomes z = m + L x, and a
positive parameter with a log-normal prior becomes exp(mu + s x); the forward model
then starts from x.
The object is a pytree: arrays it holds (for example data captured by a
jax.tree_util.Partial) are passed to compiled code at run time, and everything else
(functions, shapes) is static. Functions that close over arrays directly get those arrays
compiled in as constants instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_likelihood
|
callable
|
|
required |
fisher
|
callable
|
|
required |
fisher_sqrt
|
callable
|
|
required |
data_shape
|
pytree of jax.ShapeDtypeStruct
|
Structure, shapes and dtypes of the |
required |
fisher_operator
|
callable
|
|
None
|
Notes
Functions (including bound methods) are compared by identity when JAX decides whether to
reuse compiled code, so a Likelihood rebuilt after its data changed is recompiled. Data
held inside a jax.tree_util.Partial are passed at run time and never recompile; data an
unchanged function object reads from mutable state are not seen by already compiled code.
mgsl.gaussian(forward, data, *, noise_std, mask=None, linearize_once=False)
¶
Gaussian likelihood d = forward(x) + n, n ~ N(0, diag(noise_std^2)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forward
|
callable
|
The forward model, latent |
required |
data
|
array
|
The data. Masked entries may hold any value, including NaN. |
required |
noise_std
|
float or array
|
Noise standard deviation: a scalar or an array of exactly |
required |
mask
|
array of bool
|
|
None
|
linearize_once
|
bool
|
If True, also provide |
False
|
Returns:
| Type | Description |
|---|---|
Likelihood
|
With the data and weights as run-time arrays. |
mgsl.check_likelihood(lik, x, key, n_dirs=8, rtol=None)
¶
Check that a likelihood's functions are consistent with each other at x.
Along n_dirs random directions v it compares
a = v . fisher(x, v),b = |fisher_sqrt(x, .)^T v|^2(viajax.linear_transpose), which must equala,c = -v . H vwithHthe Hessian oflog_likelihood, which for a well-specified model near the data is close toaand always has the same sign.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lik
|
Likelihood
|
|
required |
x
|
pytree
|
A latent value. Run it at a few points, e.g. zeros (the prior mean) and a fit's centre: the Hessian comparison is most informative near the posterior. |
required |
key
|
Array
|
Random key for the directions. |
required |
n_dirs
|
int
|
Number of random directions. |
8
|
rtol
|
float
|
Tolerance for |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a function returns the wrong structure or shape, |
Warns:
| Type | Description |
|---|---|
UserWarning
|
If the Hessian-to-Fisher ratio is outside [2/3, 3/2], which suggests a missing weight or factor (or a point far from the data). |