Model#

class simpple.model.ForwardModel(parameters, log_likelihood=None, forward=None)[source]#

A model whose likelihood calls a forward model as the mean.

Note

Subclasses should call super().__init__() to ensure that the fixed_p and vary_p attributes are properly set. See Writing Model Classes.

Parameters:
  • parameters (dict) – dictionary of parameters mapping parameter names to a prior (simpple.Distribution object).

  • log_likelihood (Callable | None) – log-likelihood function that accepts a dictionary of parameters. This function should call forward.

  • forward (Callable | None) – Forward model function that accepts a dictionary of parameters as first argument.

forward(parameters, *args, **kwargs)[source]#

Evaluate the forward model

Internally, this wraps the forward function that was given at initialization and passes all arguments to it.

Parameters:

parameters (dict | ArrayLike) – Dictionary or array of parameters. If an array is used, the order must be the same as Model.keys(), without the fixed parameters. Dictionaries can optionally include fixed parameter.s

Returns:

The forward model evalulated at parameters.

Return type:

np.ndarray

get_posterior_pred(chains, n_samples, *args, **kwargs)[source]#

Get posterior predictive samples

Parameters:
  • chains (dict | ArrayLike) – Posterior chains organized as a dictionary or an array. If an array, should have shape (ndim, nsamples) where nsamples is the total number of samples in the chain and unrelated to the n_samples argument below.

  • n_samples (int) – Number of samples to pick from the chains.

Returns:

Forward model realizations corresponding to posterior samples

Return type:

np.ndarray

get_prior_pred(n_samples, *args, **kwargs)[source]#

Get prior predictive samples

Parameters:

n_samples (int) – Number of samples to generate

Returns:

Forward model realizations corresponding to prior samples

Return type:

ndarray

class simpple.model.Model(parameters, log_likelihood=None)[source]#

Simpple model

Note

Subclasses should call super().__init__() to ensure that the fixed_p and vary_p attributes are properly set. See Writing Model Classes.

Parameters:
  • parameters (dict[str, Distribution]) – dictionary of parameters mapping parameter names to a prior (simpple.Distribution object).

  • log_likelihood (Callable | None) – log-likelihood function that accepts a dictionary of parameters.

classmethod from_yaml(path, *args, **kwargs)[source]#

Create a model from a YAML file

The YAML file should have:

  • A class field mapping to the name of the Model class

  • A parameters field mapping parameter names to YAML distribution specs. See also simpple.distributions.Distribution.from_yaml_dict().

  • An args field listing the required arguments for initialization. Required only for model subclasses that accept arguments.

  • A kwargs field listing the keyword arguments for initialization. Accepted only for model subclasses that accept keyword arguments. log_likelihood and forward are treated as special keyword arguments that refer to functions and we try to resolve them with simpple.load.resolve().

Note: All extra arguments (args) and keyword arguments (kwargs) are passed to this function are passed to the model initialization, and they override their YAML counterpart.

See also: Writing Models to and from YAML Files.

Parameters:

path (Path | str) – Path of the YAML file

Returns:

Model object

Return type:

Model

get_prior_samples(n_samples, fmt='dict', seed=None, fixed=False)[source]#

Generate prior samples for all parameters

Parameters:
  • n_samples (int) – Number of samples

  • fmt (str) – Format of the samples (dict or array)

  • fixed (bool) – Whether fixed parameters should be included. Defaults to False.

  • seed (int | Generator | ndarray[int] | None)

Returns:

Dictionary of prior samples

Return type:

dict | ndarray

keys(fixed=False)[source]#

Get the ordered list of parameter names

Parameters:

fixed (bool) – Whether fixed parameters should be included. Defaults to False.

Returns:

List of parameter names

Return type:

list[str]

log_likelihood(parameters, *args, **kwargs)[source]#

Calculate the log-likelihood of the model

Internally, this wraps the log_likelihood function that was given at initialization and passes all arguments to it.

Parameters:

parameters (dict | ArrayLike) – Dictionary or array of parameters. If an array is used, the order must be the same as Model.keys(), without the fixed parameters. Dictionaries can optionally include fixed parameter.s

Returns:

The log-likelihood value at parameters.

Return type:

float

log_prior(parameters)[source]#

Log of the prior probability for the model

Parameters:

parameters (dict | ArrayLike) – Dictionary or array of parameters. If an array is used, the order must be the same as Model.keys(), without the fixed parameters. Fixed parameters are always ignored even in dictionaries.

Returns:

Log-prior probability

Return type:

float

log_prob(parameters, *args, **kwargs)[source]#

Log posterior probability for the model

All extra arguments are passed to the self.log_likelihood().

Parameters:

parameters (dict | ArrayLike) – Dictionary or array of parameters. If an array is used, the order must be the same as Model.keys(), without the fixed parameters. Dictionaries can optionally include fixed parameter.s

Returns:

Log posterior probability

Return type:

float

nautilus_prior()[source]#

Builds and return a nautilus.Prior for the model.

Fixed parameters are not included.

Returns:

Nautilus Prior object.

Return type:

Prior

property ndim#

Number of dimensions (variable parameters) in the model

property optional_args: list[str]#

List of optional (keyword) arguments at initialization, generated with simpple.utils.find_args()

prior_transform(u, fixed=False)[source]#

Prior transform of the model

Takes samples from a uniform distribution between 0 and 1 for all parameters and returns samples transformed according to the prior.

Parameters:
  • u (ArrayLike | dict) – Samples from the uniform distribution. Can be a dict or an array ordered as Model.keys().

  • fixed (bool) – Whether fixed parameters should be included. Defaults to False.

Returns:

Prior samples, as a dict or an array depending on the input type.

Return type:

np.ndarray | dict

property required_args: list[str]#

List of required arguments at initialization, generated with simpple.utils.find_args()