Models

Models for ABCSMC

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) – A descriptive name of the model. This name can simplify further analysis for the user as it is stored in the database.
accept(pars, sum_stats_calculator, distance_calculator, eps) → pyabc.model.ModelResult

Accept or not accept a parameter.

Parameters:
  • pars (Model parameters) –
  • sum_stats_calculator (A function which calculates summary statistics.) – The user is free to use or ignore this function.
  • distance_calculator (A function which calculates the distance.) – The user is free to use or ignore this function.
  • eps (float) – Acceptance threshold
Returns:

model_result – Result filled with the accepted field.

Return type:

ModelResult

distance(pars, sum_stats_calculator, distance_calculator) → pyabc.model.ModelResult

Calculate the distance

Parameters:
  • pars (Model parameters) –
  • sum_stats_calculator (A function which calculates summary statistics.) – The user is free to use or ignore this function.
  • distance_calculator (A function which calculates the distance.) – The user is free to use or ignore this function.
Returns:

model_result – The result filled with the distance

Return type:

ModelResult

sample(pars)

Return a sample from the model evaluated ar parameters pars.

This method has to be implemented by any subclass.

Parameters:pars (dictionary of parameters) –
Returns:sample – The sampled data.
Return type:any
summary_statistics(pars, sum_stats_calculator) → pyabc.model.ModelResult

Calculate the summary statistics.

Parameters:
  • pars (Model parameters) –
  • sum_stats_calculator (A function which calculates summary statistics) – the user is free to use or ignore this function
Returns:

model_result – The result filled with summary statistics

Return type:

ModelResult

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

Bases: pyabc.model.Model

A model which is initialized with a function which generates the samples. For most cases this class is to be used. Note that you can also pass a plain function to the ABCSMC class. This function get 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()

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)

Return a sample from the model evaluated ar parameters pars.

This method has to be implemented by any subclass.

Parameters:pars (dictionary of parameters) –
Returns:sample – The sampled data.
Return type:any
class pyabc.model.ModelResult(sum_stats=None, distance=None, accepted=None)

Bases: object

Result of a model evaluation. Allows to flexibly return everything from summary_statistics to accepted/rejected.

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

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(pars, sum_stats_calculator, distance_calculator, eps)

Accept or not accept a parameter.

Parameters:
  • pars (Model parameters) –
  • sum_stats_calculator (A function which calculates summary statistics.) – The user is free to use or ignore this function.
  • distance_calculator (A function which calculates the distance.) – The user is free to use or ignore this function.
  • eps (float) – Acceptance threshold
Returns:

model_result – Result filled with the accepted field.

Return type:

ModelResult

integrated_simulate(pars, eps: float) → pyabc.model.ModelResult

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

Parameters:
  • pars (dict) – Parameters at which to evaluate the model
  • eps (float) – Current acceptance threshold
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

summary_statistics(pars, sum_stats_calculator)

Calculate the summary statistics.

Parameters:
  • pars (Model parameters) –
  • sum_stats_calculator (A function which calculates summary statistics) – the user is free to use or ignore this function
Returns:

model_result – The result filled with summary statistics

Return type:

ModelResult