InferenceProcess#

class dynode.infer.inference.InferenceProcess(*, numpyro_model: Callable[[SimulationConfig, Tuple[Array, ...] | Array | None], Solution], inference_prngkey: Array = Array([0, 8675314], dtype=uint32))#

Bases: BaseModel

An Inference process for fitting a CompartmentalModel to data.

Meant to be an Abstract class for specific inference methods.

__init__(**data: Any) None#

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Methods

get_samples([group_by_chain, ...])

Get the posterior samples from the inference process.

infer(**kwargs)

Fit the numpyro_model to data using the inference process.

model_post_init(context, /)

This function is meant to behave like a BaseModel method to initialise private attributes.

to_arviz()

Return the results of a fit as an arviz InferenceData object.

Attributes

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

numpyro_model

inference_prngkey

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

numpyro_model: Callable[[SimulationConfig, Tuple[Array, ...] | Array | None], Solution]#
inference_prngkey: Array#
_inference_complete: bool = ModelPrivateAttr(default=False)#
_inferer: MCMC | SVI | None = ModelPrivateAttr()#
_inference_state: HMCState | SVIRunResult | None = ModelPrivateAttr()#
_inferer_kwargs: dict | None = ModelPrivateAttr(default=PydanticUndefined, default_factory=<function InferenceProcess.<lambda>>)#
infer(**kwargs) MCMC | SVI#

Fit the numpyro_model to data using the inference process.

Additional keyword arguments are passed to the numpyro_model.

Returns#

MCMC | SVI

The MCMC or SVI object used for inference.

get_samples(group_by_chain=False, exclude_deterministic=True) dict[str, Array]#

Get the posterior samples from the inference process.

Parameters#

group_by_chainbool

whether or not to group posterior samples by chain or not. Adds a leading dimension to return dict’s values if True. Does nothing if the inference_method does not support chains such as in SVI.

exclude_deterministicbool

whether or not to exclude parameters generated from numpyro.deterministic as keys in the returned dictionary, by default True.

Returns#

dict[str, Array] A dictionary of posterior samples, where keys are parameter sites and values are the corresponding samples, possibly arranged by chain/sample in the case of MCMC.

to_arviz() InferenceData#

Return the results of a fit as an arviz InferenceData object.

Returns#

arviz.InferenceData

arviz InferenceData object containing both priors and posterior_predictive.

Raises#

AssertionError

if fitting has not yet been run via infer()

_abc_impl = <_abc._abc_data object>#
model_post_init(context: Any, /) None#

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Args:

self: The BaseModel instance. context: The context.