Models

A model defines how input parameters relate to output simulated data.

class pyabc.model.IntegratedModel(name: str = 'Model')[source]

Bases: pyabc.model.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: pyabc.parameters.Parameter, sum_stats_calculator: Callable, distance_calculator: pyabc.distance.base.Distance, eps_calculator: pyabc.epsilon.base.Epsilon, acceptor: pyabc.acceptor.acceptor.Acceptor, x_0: dict)[source]

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 (Callable) – A function which calculates summary statistics. The user is free to use or ignore this function.

  • distance_calculator (pyabc.Distance) – The distance function. The user is free to use or ignore this function.

  • eps_calculator (pyabc.Epsilon) – The acceptance thresholds.

  • acceptor (pyabc.Acceptor) – The acceptor judging whether to accept, based on distance and epsilon.

  • x_0 (dict) – The observed summary statistics.

Returns

model_result – The result with filled accepted field.

Return type

ModelResult

integrated_simulate(pars: pyabc.parameters.Parameter, eps: float)pyabc.model.ModelResult[source]

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().

Returns

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.

Return type

ModelResult

class pyabc.model.Model(name: str = 'Model')[source]

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 (default = "model")) – A descriptive name of the model. This name can simplify further analysis for the user as it is stored in the database.

__init__(name: str = 'Model')[source]

Initialize self. See help(type(self)) for accurate signature.

accept(t: int, pars: pyabc.parameters.Parameter, sum_stats_calculator: Callable, distance_calculator: pyabc.distance.base.Distance, eps_calculator: pyabc.epsilon.base.Epsilon, acceptor: pyabc.acceptor.acceptor.Acceptor, x_0: dict)[source]

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 (Callable) – A function which calculates summary statistics. The user is free to use or ignore this function.

  • distance_calculator (pyabc.Distance) – The distance function. The user is free to use or ignore this function.

  • eps_calculator (pyabc.Epsilon) – The acceptance thresholds.

  • acceptor (pyabc.Acceptor) – The acceptor judging whether to accept, based on distance and epsilon.

  • x_0 (dict) – The observed summary statistics.

Returns

model_result – The result with filled accepted field.

Return type

ModelResult

distance(t: int, pars: pyabc.parameters.Parameter, sum_stats_calculator: Callable, distance_calculator: pyabc.distance.base.Distance, x_0: dict)pyabc.model.ModelResult[source]

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.

Returns

model_result – The result with filled distance.

Return type

ModelResult

sample(pars: pyabc.parameters.Parameter)[source]

Return a sample from the model evaluated at 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.

Returns

sample – The sampled data.

Return type

any

summary_statistics(t: int, pars: pyabc.parameters.Parameter, sum_stats_calculator: Callable)pyabc.model.ModelResult[source]

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.

Returns

model_result – The result with filled summary statistics.

Return type

ModelResult

class pyabc.model.ModelResult(sum_stats: dict = None, distance: float = None, accepted: bool = None, weight: float = 1.0)[source]

Bases: object

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

__init__(sum_stats: dict = None, distance: float = None, accepted: bool = None, weight: float = 1.0)[source]

Initialize self. See help(type(self)) for accurate signature.

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

Bases: pyabc.model.Model

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.

__init__(sample_function: Callable[[pyabc.parameters.Parameter], Any], name: str = None)[source]

Initialize self. See help(type(self)) for accurate signature.

static assert_model(model_or_function)[source]

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.

Returns

model

Return type

SimpleModel or Model

sample(pars: pyabc.parameters.Parameter)[source]

Return a sample from the model evaluated at 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.

Returns

sample – The sampled data.

Return type

any