nifty8.re.likelihood module#

class Likelihood(*, domain=<class 'nifty8.re.model.NoValue'>, init=<class 'nifty8.re.model.NoValue'>, lsm_tangents_shape=None)[source]#

Bases: LazyModel

Storage class for keeping track of the energy, the associated left-square-root of the metric and the metric.

Properties#

energycallable

Function evaluating the negative log-likelihood.

transformationcallable

Function evaluating the geometric transformation of the likelihood.

normalized_residualcallable

Function evaluating the data residual normalized by the standard deviation of the likelihood.

left_sqrt_metriccallable

Function applying the left-square-root of the metric.

metriccallable

Function applying the metric.

domaintree-like structure of ShapeWithDtype

Structure of the input/parameter space.

lsm_tangents_shapetree-like structure of ShapeWithDtype

Structure of the data space.

__call__(primals, **primals_kw)[source]#

Convenience method to access the energy method of this instance.

__init__(*, domain=<class 'nifty8.re.model.NoValue'>, init=<class 'nifty8.re.model.NoValue'>, lsm_tangents_shape=None)[source]#
amend(f: ~typing.Callable, /, *, domain=<class 'nifty8.re.model.NoValue'>, likelihood_argnames=None)[source]#

Amend a forward model to the likelihood.

energy(primals, **primals_kw)[source]#

Applies the energy to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

energy – Energy at the position primals.

Return type:

float

freeze(*, primals, point_estimates)[source]#

Returns a new likelihood with partially inserted primals and the remaining unfrozen/liquid primals.

left_sqrt_metric(primals, tangents, **primals_kw)[source]#

Applies the left-square-root (LSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of shape lsm_tangents_shape.

  • **primals_kw (Any) – Additional arguments passed on to the LSM.

Returns:

metric_sample – Tree-like structure of the same type as primals to which the left-square-root of the metric has been applied to.

Return type:

tree-like structure

property left_sqrt_metric_tangents_shape#

Retrieves the shape of the tangent domain of the left-square-root of the metric.

property lsm_tangents_shape#

Alias for left_sqrt_metric_tangents_shape.

metric(primals, tangents, **primals_kw)[source]#

Applies the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric.

  • **primals_kw (Any) – Additional arguments passed on to the metric.

Returns:

naturally_curved – Tree-like structure of the same type as primals to which the metric has been applied to.

Return type:

tree-like structure

normalized_residual(primals, **primals_kw)[source]#

Applies the normalized_residual to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

normalized_residual – Structure of the same type as lsm_tangents_shape for which the normalized_residual is computed.

Return type:

tree-like structure

right_sqrt_metric(primals, tangents, **primals_kw)[source]#

Applies the right-square-root (RSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of the same shape as primals.

  • **primals_kw (Any) – Additional arguments passed on to the RSM.

Returns:

metric_sample – Tree-like structure of the same type as left_sqrt_metric_tangents_shape.

Return type:

tree-like structure

property right_sqrt_metric_tangents_shape#

Retrieves the shape of the tangent domain of the right-square-root of the metric, a.k.a. the domain.

property rsm_tangents_shape#

Alias for right_sqrt_metric_tangents_shape.

transformation(primals, **primals_kw)[source]#

Applies the coordinate transformation that maps into a coordinate system in which the metric of the likelihood is the Euclidean metric.

Parameters:
  • primals (tree-like structure) – Position at which to transform.

  • **primals_kw (Any) – Additional arguments passed on to the transformation.

Returns:

transformed_sample – Structure of the same type as lsm_tangents_shape to which the geometric transformation has been applied to.

Return type:

tree-like structure

class LikelihoodPartial(likelihood, /, *, primals, point_estimates)[source]#

Bases: Likelihood

Likelihood with partially inserted primals.

__init__(likelihood, /, *, primals, point_estimates)[source]#
property energy#

Applies the energy to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

energy – Energy at the position primals.

Return type:

float

property left_sqrt_metric#

Applies the left-square-root (LSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of shape lsm_tangents_shape.

  • **primals_kw (Any) – Additional arguments passed on to the LSM.

Returns:

metric_sample – Tree-like structure of the same type as primals to which the left-square-root of the metric has been applied to.

Return type:

tree-like structure

likelihood: Likelihood#
property metric#

Applies the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric.

  • **primals_kw (Any) – Additional arguments passed on to the metric.

Returns:

naturally_curved – Tree-like structure of the same type as primals to which the metric has been applied to.

Return type:

tree-like structure

property normalized_residual#

Applies the normalized_residual to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

normalized_residual – Structure of the same type as lsm_tangents_shape for which the normalized_residual is computed.

Return type:

tree-like structure

primals_frozen: Any#
property right_sqrt_metric#

Applies the right-square-root (RSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of the same shape as primals.

  • **primals_kw (Any) – Additional arguments passed on to the RSM.

Returns:

metric_sample – Tree-like structure of the same type as left_sqrt_metric_tangents_shape.

Return type:

tree-like structure

splitx(primals)[source]#

Split the primals into liquid and frozen.

Parameters:

primals (tree-like structure) – Position at which to evaluate the energy.

Returns:

  • primals_liqud (tree-like structure) – Liquid/dynamic part of the position at which to evaluate the energy.

  • primals_frozen (tree-like structure) – Frozen/static part of the position at which to evaluate the energy.

property transformation#

Applies the coordinate transformation that maps into a coordinate system in which the metric of the likelihood is the Euclidean metric.

Parameters:
  • primals (tree-like structure) – Position at which to transform.

  • **primals_kw (Any) – Additional arguments passed on to the transformation.

Returns:

transformed_sample – Structure of the same type as lsm_tangents_shape to which the geometric transformation has been applied to.

Return type:

tree-like structure

class LikelihoodSum(left, right, /, domain=<class 'nifty8.re.model.NoValue'>, init=<class 'nifty8.re.model.NoValue'>, _left_key='lh_left', _right_key='lh_right')[source]#

Bases: Likelihood

__init__(left, right, /, domain=<class 'nifty8.re.model.NoValue'>, init=<class 'nifty8.re.model.NoValue'>, _left_key='lh_left', _right_key='lh_right')[source]#
energy(primals, **kwargs)[source]#

Applies the energy to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

energy – Energy at the position primals.

Return type:

float

left_likelihood: Likelihood#
left_sqrt_metric(primals, tangents, **kwargs)[source]#

Applies the left-square-root (LSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of shape lsm_tangents_shape.

  • **primals_kw (Any) – Additional arguments passed on to the LSM.

Returns:

metric_sample – Tree-like structure of the same type as primals to which the left-square-root of the metric has been applied to.

Return type:

tree-like structure

metric(primals, tangents, **kwargs)[source]#

Applies the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric.

  • **primals_kw (Any) – Additional arguments passed on to the metric.

Returns:

naturally_curved – Tree-like structure of the same type as primals to which the metric has been applied to.

Return type:

tree-like structure

normalized_residual(primals, **kwargs)[source]#

Applies the normalized_residual to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

normalized_residual – Structure of the same type as lsm_tangents_shape for which the normalized_residual is computed.

Return type:

tree-like structure

right_likelihood: Likelihood#
right_sqrt_metric(primals, tangents, **kwargs)[source]#

Applies the right-square-root (RSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of the same shape as primals.

  • **primals_kw (Any) – Additional arguments passed on to the RSM.

Returns:

metric_sample – Tree-like structure of the same type as left_sqrt_metric_tangents_shape.

Return type:

tree-like structure

transformation(primals, **kwargs)[source]#

Applies the coordinate transformation that maps into a coordinate system in which the metric of the likelihood is the Euclidean metric.

Parameters:
  • primals (tree-like structure) – Position at which to transform.

  • **primals_kw (Any) – Additional arguments passed on to the transformation.

Returns:

transformed_sample – Structure of the same type as lsm_tangents_shape to which the geometric transformation has been applied to.

Return type:

tree-like structure

class LikelihoodWithModel(likelihood: nifty8.re.likelihood.Likelihood, f: Callable, /, *, domain=<class 'nifty8.re.model.NoValue'>, init=<class 'nifty8.re.model.NoValue'>, likelihood_argnames=None)[source]#

Bases: Likelihood

__init__(likelihood: ~nifty8.re.likelihood.Likelihood, f: ~typing.Callable, /, *, domain=<class 'nifty8.re.model.NoValue'>, init=<class 'nifty8.re.model.NoValue'>, likelihood_argnames=None)[source]#

Amend the function f to the right of the likelihood.

Parameters:
  • f (Callable) – Function which to amend to the likelihood.

  • likelihood_argnames (tuple or None) – Keys of the keyword arguments of the joined likelihood which to pass to the original likelihood. Passing None indicates the intent to absorb everything not explicitly absorbed by the other call.

Returns:

lh

Return type:

Likelihood

amend(f: ~typing.Callable, *, domain=<class 'nifty8.re.model.NoValue'>, left_argnames=None, likelihood_argnames=None)[source]#

Amend a forward model to the likelihood.

energy(primals, **kwargs)[source]#

Applies the energy to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

energy – Energy at the position primals.

Return type:

float

forward: Callable#
left_sqrt_metric(primals, tangents, **kwargs)[source]#

Applies the left-square-root (LSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of shape lsm_tangents_shape.

  • **primals_kw (Any) – Additional arguments passed on to the LSM.

Returns:

metric_sample – Tree-like structure of the same type as primals to which the left-square-root of the metric has been applied to.

Return type:

tree-like structure

likelihood: Likelihood#
likelihood_argnames: tuple = ()#
metric(primals, tangents, **kwargs)[source]#

Applies the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric.

  • **primals_kw (Any) – Additional arguments passed on to the metric.

Returns:

naturally_curved – Tree-like structure of the same type as primals to which the metric has been applied to.

Return type:

tree-like structure

normalized_residual(primals, **kwargs)[source]#

Applies the normalized_residual to primals.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the energy.

  • **primals_kw (Any) – Additional arguments passed on to the energy.

Returns:

normalized_residual – Structure of the same type as lsm_tangents_shape for which the normalized_residual is computed.

Return type:

tree-like structure

right_sqrt_metric(primals, tangents, **kwargs)[source]#

Applies the right-square-root (RSM) of the metric at primals to tangents.

Parameters:
  • primals (tree-like structure) – Position at which to evaluate the metric.

  • tangents (tree-like structure) – Instance to which to apply the metric. Must be of the same shape as primals.

  • **primals_kw (Any) – Additional arguments passed on to the RSM.

Returns:

metric_sample – Tree-like structure of the same type as left_sqrt_metric_tangents_shape.

Return type:

tree-like structure

transformation(primals, **kwargs)[source]#

Applies the coordinate transformation that maps into a coordinate system in which the metric of the likelihood is the Euclidean metric.

Parameters:
  • primals (tree-like structure) – Position at which to transform.

  • **primals_kw (Any) – Additional arguments passed on to the transformation.

Returns:

transformed_sample – Structure of the same type as lsm_tangents_shape to which the geometric transformation has been applied to.

Return type:

tree-like structure

partial_insert_and_remove(call, insert_axes, flat_fill, *, remove_axes=(), unflatten=None)[source]#

Return a call in which flat_fill is inserted into arguments of call at inset_axes and subsequently removed from its output at remove_axes.

This function is best understood by example:

def _identity(x):
    return x

# _identity takes exactly one argument, thus `insert_axes` and `flat_fill`
# are length one tuples
_id_part = jpartial(
    _identity,
    insert_axes=(jft.Vector({
        "a": (True, False),
        "b": False
    }), ),
    flat_fill=(("THIS IS input['a'][0]", ), )
)
out = _id_part(("THIS IS input['a'][1]", "THIS IS input['b']"))
assert out == jft.Vector(
    {
        "a": ("THIS IS input['a'][0]", "THIS IS input['a'][1]"),
        "b": "THIS IS input['b']"
    }
)