# Models¶

Models for ABCSMC.

class pyabc.model.IntegratedModel(name: str = 'model')

A model class which integrates simulation, distance calculation and rejection/acceptance.

This can bring performance improvements if the user can calculate the distance function on the fly during model simulation and interrupt the simulation if the current acceptance threshold cannot be satisfied anymore.

Subclass this model and implement integrated_simulate to define your own integrated model..

accept(t: int, pars, sum_stats_calculator, distance_calculator: pyabc.distance_functions.DistanceFunction, eps_calculator: pyabc.epsilon.Epsilon, acceptor: pyabc.acceptor.Acceptor, x_0: dict)

Sample, calculate summary statistics, calculate distance, and then accept or not accept a parameter.

Called from within ABCSMC in each iteration to evaluate a parameter.

Parameters: t (int) – Current time point. pars (Parameter) – The model parameters. sum_stats_calculator – A function which calculates summary statistics. The user is free to use or ignore this function. distance_calculator (pyabc.DistanceFunction) – The distance function. The user is free to use or ignore this function. eps_calculator (pyabc.Epsilon) – The acceptance thresholds. acceptor (Acceptor) – The acceptor judging whether to accept, based on distance and epsilon. x_0 (dict) – The observed summary statistics. model_result – The result with filled accepted field. ModelResult
integrated_simulate(pars, eps: float) → pyabc.model.ModelResult

Method which integrates simulation and acceptance/rejection in a single method.

Parameters: pars (Parameter) – Parameters at which to evaluate the model eps (float) – Current acceptance threshold. If required, it is effortlessly possible to instead use the entire epsilon_calculator object passed to accept(). model_result – In case the parameter evaluation is rejected, this method should simply return ModelResult(accepted=False). If the parameter was accepted, this method should return either ModelResult(accepted=True, distance=distance) or ModelResult(accepted=True, distance=distance, sum_stats=sum_stats) in which distance denotes the achieved distance and sum_stats the summary statistics (e.g. simulated data) of the run. Note that providing the summary statistics is optional. If they are provided, then they are also logged in the database. ModelResult
class pyabc.model.Model(name: str = 'model')

Bases: object

General model. This is the most flexible model class, but also the most complicated one to use. This is an abstract class and not functional on its own. Derive concrete subclasses for actual usage.

The individual steps

• sample
• summary_statistics
• distance
• accept

can be overwritten.

To use this class, at least the sample method has to be overriden.

Note

Most likely you do not want to use this class directly, but the SimpleModel instead, or even just pass a plain function as model.

Parameters: name (str, optional) – A descriptive name of the model. This name can simplify further analysis for the user as it is stored in the database.
accept(t, pars, sum_stats_calculator, distance_calculator: pyabc.distance_functions.DistanceFunction, eps_calculator: pyabc.epsilon.Epsilon, acceptor: pyabc.acceptor.Acceptor, x_0)

Sample, calculate summary statistics, calculate distance, and then accept or not accept a parameter.

Called from within ABCSMC in each iteration to evaluate a parameter.

Parameters: t (int) – Current time point. pars (Parameter) – The model parameters. sum_stats_calculator – A function which calculates summary statistics. The user is free to use or ignore this function. distance_calculator (pyabc.DistanceFunction) – The distance function. The user is free to use or ignore this function. eps_calculator (pyabc.Epsilon) – The acceptance thresholds. acceptor (Acceptor) – The acceptor judging whether to accept, based on distance and epsilon. x_0 (dict) – The observed summary statistics. model_result – The result with filled accepted field. ModelResult
distance(t, pars, sum_stats_calculator, distance_calculator: pyabc.distance_functions.DistanceFunction, x_0) → pyabc.model.ModelResult

Sample, calculate summary statistics, and then calculate the distance.

Not required in the current implementation.

Parameters: t (int) – Current time point. pars (Parameter) – Model parameters. sum_stats_calculator (Callable) – A function which calculates summary statistics, as passed to pyabc.smc.ABCSMC. The user is free to use or ignore this function. distance_calculator (Callable) – A function which calculates the distance, as passed to pyabc.smc.ABCSMC. The user is free to use or ignore this function. x_0 (dict) – Observed summary statistics. model_result – The result with filled distance. ModelResult
sample(pars)

Return a sample from the model evaluated ar parameters pars. This can be raw data, or already summarized statistics thereof.

This method has to be implemented by any subclass.

Parameters: pars (Parameter) – Dictionary of parameters. sample – The sampled data. any
summary_statistics(t, pars, sum_stats_calculator) → pyabc.model.ModelResult

Sample, and then calculate the summary statistics.

Called from within ABCSMC during the initialization process.

Parameters: t (int) – Current time point. pars (Parameter) – Model parameters. sum_stats_calculator (Callable) – A function which calculates summary statistics, as passed to pyabc.smc.ABCSMC. The user is free to use or ignore this function. model_result – The result with filled summary statistics. ModelResult
class pyabc.model.ModelResult(sum_stats=None, distance=None, accepted=None)

Bases: object

Result of a model evaluation. Allows to flexibly return summary statistics, distances and accepted/rejected.

class pyabc.model.SimpleModel(sample_function: Callable[pyabc.parameters.Parameter, Any], name=None)

A model which is initialized with a function which generates the samples. For most cases this class will be adequate. Note that you can also pass a plain function to the ABCSMC class, which then gets automatically converted to a SimpleModel.

Parameters: sample_function (Callable[[Parameter], Any]) – Returns the sample to be passed to the summary statistics method. This function as a single argument which is a Parameter. name (str. optional) – The name of the model. If not provided, the names if inferred from the function name of sample_function.
static assert_model(model_or_function)

Alternative constructor. Accepts either a Model instance or a function and returns always a Model instance.

Parameters: model_or_function (Model, function) – Constructs a SimpleModel instance if a function is passed. If a Model instance is passed, the Model instance itself is returned. model SimpleModel or Model
sample(pars)

Return a sample from the model evaluated ar parameters pars. This can be raw data, or already summarized statistics thereof.

This method has to be implemented by any subclass.

Parameters: pars (Parameter) – Dictionary of parameters. sample – The sampled data. any