pcntoolkit.math_functions.prior#

Attributes#

Classes#

BasePrior

Helper class that provides a standard way to create an ABC using

CenteredRandomPrior

A random effect drawn directly at the group scale.

LinearPrior

Helper class that provides a standard way to create an ABC using

Prior

Helper class that provides a standard way to create an ABC using

RandomPrior

Helper class that provides a standard way to create an ABC using

Functions#

make_prior(→ BasePrior)

Build a prior from keyword arguments.

prior_from_args(→ BasePrior)

Module Contents#

class BasePrior(name: str = 'theta', dims: Tuple[str, ...] | str | None = None, mapping: str = 'identity', mapping_params: tuple = None, **kwargs)#

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

__eq__(other: BasePrior)#
apply_mapping(x: Any) → Any#
compile(model: pymc.Model, X: xarray.DataArray, be: xarray.DataArray, be_maps: dict[str, dict[str, int]], Y: xarray.DataArray) → Any#
classmethod from_dict(dict: BasePrior.from_dict.dict, version: str | None = None) → BasePrior#
abstractmethod set_name(name: str) → None#
to_dict()#
abstractmethod transfer(idata: xarray.DataTree, **kwargs) → BasePrior#
abstractmethod update_data(model, X, be, be_maps, Y)#
property dims#
property has_random_effect: bool#
Abstractmethod:

mapping = 'identity'#
mapping_params = (0, 1)#
name = 'theta'#
sample_dims = ()#
class CenteredRandomPrior(mu: BasePrior | None = None, sigma: BasePrior | None = None, name: str = 'theta', dims: Tuple[str, ...] | str | None = None, mapping: str = 'identity', mapping_params: tuple[float, ...] = None, **kwargs)#

Bases: RandomPrior

A random effect drawn directly at the group scale.

Differs from RandomPrior only in how the offsets are sampled: here they are drawn at the group’s own scale, rather than at unit scale and then multiplied by it. The two describe the same model but sample differently.

transfer(idata: xarray.DataTree, **kwargs) → CenteredRandomPrior#
class LinearPrior(slope: BasePrior | None = None, intercept: BasePrior | None = None, name: str = 'theta', dims: Tuple[str, ...] | str | None = None, mapping: str = 'identity', mapping_params: tuple[float, ...] = None, basis_function: pcntoolkit.math_functions.basis_function.BasisFunction = LinearBasisFunction(), **kwargs)#

Bases: BasePrior

Helper class that provides a standard way to create an ABC using inheritance.

classmethod from_dict(dict: LinearPrior.from_dict.dict, version: str | None = None) → LinearPrior#
set_name(name)#
to_dict()#
transfer(idata: xarray.DataTree, **kwargs) → LinearPrior#
update_data(model: pymc.Model, X: xarray.DataArray, be: xarray.DataArray, be_maps: dict[str, dict[str, int]], Y: xarray.DataArray)#
basis_function#
property dims#
property has_random_effect#
intercept#
sample_dims = ('observations',)#
slope#
class Prior(name: str = 'theta', dims: Tuple[str, ...] | str | None = None, mapping: str = 'identity', mapping_params: tuple[float, ...] = None, dist_name: str = 'Normal', dist_params: Tuple[float | int | list[float | int], ...] = None, **kwargs)#

Bases: BasePrior

Helper class that provides a standard way to create an ABC using inheritance.

classmethod from_dict(dict: Prior.from_dict.dict, version: str | None = None)#
set_name(name: str) → None#
to_dict()#
transfer(idata: xarray.DataTree, **kwargs) → Prior#
update_data(model: pymc.Model, X: xarray.DataArray, be: xarray.DataArray, be_maps: dict[str, dict[str, int]], Y: xarray.DataArray)#
dist_name = 'Normal'#
dist_params = (0, 10.0)#
property has_random_effect#
sample_dims = ()#
class RandomPrior(mu: BasePrior | None = None, sigma: BasePrior | None = None, name: str = 'theta', dims: Tuple[str, ...] | str | None = None, mapping: str = 'identity', mapping_params: tuple[float, ...] = None, **kwargs)#

Bases: BasePrior

Helper class that provides a standard way to create an ABC using inheritance.

classmethod from_dict(dict: RandomPrior.from_dict.dict, version: str | None = None) → RandomPrior#
set_name(name: str)#
to_dict()#
transfer(idata: xarray.DataTree, **kwargs) → RandomPrior#
update_data(model: pymc.Model, X: xarray.DataArray, be: xarray.DataArray, be_maps: dict[str, dict[str, int]], Y: xarray.DataArray)#
property dims#
property has_random_effect#
mu#
offsets#
sample_dims = ('observations',)#
scaled_offsets#
sigma#
sigmas#
make_prior(name: str = 'theta', **kwargs) → BasePrior#

Build a prior from keyword arguments.

Parameters:
  • name (str) – Name of the prior.

  • linear (bool) – Build a LinearPrior (a slope/intercept pair through a basis function).

  • random (bool) – Build a random-effect prior, i.e. a per-batch-effect offset around a shared mean.

  • centered (bool) –

    Only meaningful together with random. Selects how the random effect is parameterised - the two forms describe the same model but give the samplers different geometry:

    • False (default) - non-centered: the offsets are drawn at unit scale and multiplied by the group scale, offset = sigma * ZeroSumNormal(1). This is the standard choice for NUTS, which mixes better when the scale and the offsets are a priori independent.

    • True - centered: the offsets are drawn directly at the group scale, offset ~ ZeroSumNormal(sigma). Prefer this for mode-based approximations (inference_method=”laplace”).

prior_from_args(name: str, args: Dict[str, Any], dims: Tuple[str, ...] | str | None = None) → BasePrior#
DEFAULT_PRIOR_ARGS#
PM_DISTMAP#