pyabc.model
Models
A model defines how input parameters relate to output simulated data.
- class pyabc.model.FunctionModel(sample_function: Callable[[Parameter], Any], name: str = None)[source]
Bases:
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 FunctionModel.
- 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.
- sample(pars: 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
- static to_model(maybe_model: Callable | Model) Model [source]
Alternative constructor. Accepts either a Model instance or a function and returns always a Model instance.
- Parameters:
maybe_model – Constructs a FunctionModel instance if a function is passed. If a Model instance is passed, the Model instance itself is returned.
- Returns:
model
- Return type:
A valid model instance
- class pyabc.model.IntegratedModel(name: str = 'Model')[source]
Bases:
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: Parameter, sum_stat_calculator: Callable, distance_calculator: Distance, eps_calculator: Epsilon, 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_stat_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:
- integrated_simulate(pars: Parameter, eps: float) ModelResult [source]
Method which integrates simulation and acceptance/rejection in a single method.
- Parameters:
- 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 eitherModelResult(accepted=True, distance=distance)
orModelResult(accepted=True, distance=distance, sum_stat=sum_stat)
in whichdistance
denotes the achieved distance andsum_stat
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:
- 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
FunctionModel
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.
- accept(t: int, pars: Parameter, sum_stat_calculator: Callable, distance_calculator: Distance, eps_calculator: Epsilon, 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_stat_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:
- distance(t: int, pars: Parameter, sum_stat_calculator: Callable, distance_calculator: Distance, x_0: dict) 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_stat_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:
- sample(pars: 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: Parameter, sum_stat_calculator: Callable) ModelResult [source]
Sample, and then calculate the summary statistics.
Called from within ABCSMC during the initialization process.
- Parameters:
- Returns:
model_result – The result with filled summary statistics.
- Return type: