Skip to content

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

x -> log p(d | x), a scalar. Constants may be dropped.

required
fisher callable

(x, v) -> J^T F J v: the Fisher metric F of the noise model (e.g. diag(1 / sigma^2) for Gaussian noise) pulled back to the latent by the Jacobian J of the forward model, applied to a tangent v with the structure of x. Must not include the prior.

required
fisher_sqrt callable

(x, z) -> J^T F^{1/2} z for z with the structure of data_shape, such that fisher_sqrt(x, z) with z ~ N(0, I) has covariance J^T F J.

required
data_shape pytree of jax.ShapeDtypeStruct

Structure, shapes and dtypes of the z taken by fisher_sqrt (usually those of the data).

required
fisher_operator callable

x -> (fmv, fsqrt) with fmv(v) = J^T F J v and fsqrt(z) = J^T F^{1/2} z. If given, it is called once per solve and the returned functions are applied inside the solve, instead of fisher(x, .) and fisher_sqrt(x, .). Useful when linearising the model once is cheaper than re-evaluating it at every application; costs the memory of the stored linearisation. fmv and fsqrt must be pytrees (e.g. jax.tree_util.Partial, or built from the function returned by jax.vjp) so that operators at several points can be built and applied with jax.vmap.

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 x (standard-normal prior) to an array of exactly the data's shape. Must be differentiable and vmappable. Its output is cast to the data's dtype. Compute it in the latent's precision: a float32 forward model with a float64 latent limits the fits to float32 accuracy, which shows up as line-search failures.

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 data.shape. Positive and finite. (NIFTy.re's Gaussian takes the inverse standard deviation instead.)

required
mask array of bool

True where a data point is used, shape data.shape. Masked points get zero weight.

None
linearize_once bool

If True, also provide fisher_operator, so each solve linearises the forward model once instead of at every application of J^T F J.

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 (via jax.linear_transpose), which must equal a,
  • c = -v . H v with H the Hessian of log_likelihood, which for a well-specified model near the data is close to a and 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 a == b. Default 1e-6 in float64, 1e-3 in float32.

None

Returns:

Type Description
dict

"fisher", "fisher_sqrt" and "hessian": the quadratic forms a, b and c per direction; "hessian_ratio": sum(c) / sum(a).

Raises:

Type Description
ValueError

If a function returns the wrong structure or shape, J^T F J is negative along some direction, fisher, fisher_sqrt and fisher_operator (if given) disagree, or the Hessian of the log-likelihood has the opposite sign (e.g. a negative log-likelihood was passed).

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).