pyabc.inference_util
Inference utilities
This module contains utility functions for the inference process.
- class pyabc.inference_util.AnalysisVars(model_prior: RV, parameter_priors: List[Distribution], model_perturbation_kernel: ModelPerturbationKernel, transitions: List[Transition], models: List[Model], summary_statistics: Callable, x_0: dict, distance_function: Distance, eps: Epsilon, acceptor: Acceptor, min_acceptance_rate: float, min_eps: float, stop_if_single_model_alive: bool, max_t: int, max_total_nr_simulations: int, prev_total_nr_simulations: int, max_walltime: timedelta, init_walltime: datetime, min_eps_diff: float, prev_eps: float)[source]
Bases:
object
Contract object class for passing analysis variables.
Used e.g. to create new sampling tasks or check early stopping.
- __init__(model_prior: RV, parameter_priors: List[Distribution], model_perturbation_kernel: ModelPerturbationKernel, transitions: List[Transition], models: List[Model], summary_statistics: Callable, x_0: dict, distance_function: Distance, eps: Epsilon, acceptor: Acceptor, min_acceptance_rate: float, min_eps: float, stop_if_single_model_alive: bool, max_t: int, max_total_nr_simulations: int, prev_total_nr_simulations: int, max_walltime: timedelta, init_walltime: datetime, min_eps_diff: float, prev_eps: float)[source]
- pyabc.inference_util.create_analysis_id()[source]
Create a universally unique id for a given analysis. Used by the inference routine to uniquely associated results with analyses.
- pyabc.inference_util.create_prior_pdf(model_prior: RV, parameter_priors: List[Distribution]) Callable [source]
Create a function that calculates a sample’s prior density.
- Parameters:
model_prior (The model prior.)
parameter_priors (The parameter priors, one for each model.)
- Returns:
prior_pdf
- Return type:
The prior density function.
- pyabc.inference_util.create_simulate_from_prior_function(model_prior: RV, parameter_priors: List[Distribution], models: List[Model], summary_statistics: Callable) Callable [source]
Create a function that simulates from the prior.
Similar to _create_simulate_function, apart here we sample from the prior and accept all.
- Parameters:
model_prior (The model prior.)
parameter_priors (The parameter priors.)
models (List of all models.)
summary_statistics (Computes summary statistics from model output.)
- Returns:
A function that returns a sampled particle.
- Return type:
simulate_one
- pyabc.inference_util.create_simulate_function(t: int, model_probabilities: DataFrame, model_perturbation_kernel: ModelPerturbationKernel, transitions: List[Transition], model_prior: RV, parameter_priors: List[Distribution], models: List[Model], summary_statistics: Callable, x_0: dict, distance_function: Distance, eps: Epsilon, acceptor: Acceptor, evaluate: bool = True, proposal_id: int = 0) Callable [source]
Create a simulation function which performs the sampling of parameters, simulation of data and acceptance checking, and which is then passed to the sampler.
- Parameters:
t (The time index to simulate for.)
model_probabilities (The last generation's model probabilities.)
model_perturbation_kernel (The model perturbation kernel.)
transitions (The parameter transition kernels.)
model_prior (The model prior.)
parameter_priors (The parameter priors.)
models (List of all models.)
summary_statistics – Function to compute summary statistics from model output.
x_0 (The observed summary statistics.)
distance_function (The distance function.)
eps (The epsilon threshold.)
acceptor (The acceptor.)
evaluate – Whether to actually evaluate the sample. Should be True except for certain preliminary settings.
proposal_id – Identifier for the proposal distribution.
- Returns:
simulate_one (callable) – Function that samples parameters, simulates data, and checks acceptance.
.. note:: – For some of the samplers, the sampling function needs to be serialized in order to be transported to where the sampling happens. Therefore, the returned function should be light, and in particular not contain references to the ABCSMC class.
- pyabc.inference_util.create_transition_pdf(transitions: List[Transition], model_probabilities: DataFrame, model_perturbation_kernel: ModelPerturbationKernel) Callable [source]
Create the transition probability density function for time t.
- Parameters:
transitions (The list of parameter transition functions.)
model_probabilities (The last generation's model probabilities.)
model_perturbation_kernel (The kernel perturbing the models.)
- Returns:
transition_pdf
- Return type:
The transition density function.
- pyabc.inference_util.create_weight_function(prior_pdf: Callable, transition_pdf: Callable) Callable [source]
Create a function that calculates a sample’s importance weight. The weight is the prior divided by the transition density and the acceptance step weight.
- Parameters:
prior_pdf (The prior density.)
transition_pdf (The transition density.)
- Returns:
weight_function
- Return type:
The importance sample weight function.
- pyabc.inference_util.eps_from_hist(history: History, t: int = None) float [source]
Read epsilon value for time t from history. Defaults to latest.
- pyabc.inference_util.evaluate_preliminary_particle(particle: Particle, t, ana_vars: AnalysisVars) Particle [source]
Evaluate a preliminary particle. I.e. compute distance and check acceptance.
- Returns:
evaluated_particle
- Return type:
The evaluated particle
- pyabc.inference_util.evaluate_proposal(m_ss: int, theta_ss: Parameter, t: int, models: List[Model], summary_statistics: Callable, distance_function: Distance, eps: Epsilon, acceptor: Acceptor, x_0: dict, weight_function: Callable, proposal_id: int) Particle [source]
Evaluate a proposed parameter.
- Parameters:
m_ss (The proposed (model, parameter) sample.)
theta_ss (The proposed (model, parameter) sample.)
t (The current time.)
models (List of all models.)
summary_statistics – Function to compute summary statistics from model output.
distance_function (The distance function.)
eps (The epsilon threshold.)
acceptor (The acceptor.)
x_0 (The observed summary statistics.)
weight_function (Function by which to reweight the sample.)
proposal_id (Id of the transition kernel.)
- Returns:
particle (A particle containing all information.)
Data for the given parameters theta_ss are simulated, summary statistics
computed and evaluated.
- pyabc.inference_util.generate_valid_proposal(t: int, m: ndarray, p: ndarray, model_prior: RV, parameter_priors: List[Distribution], model_perturbation_kernel: ModelPerturbationKernel, transitions: List[Transition])[source]
Sample a parameter for a model.
- Parameters:
t (Population index to generate for.)
m (Indices of alive models.)
p (Probabilities of alive models.)
model_prior (The model prior.)
parameter_priors (The parameter priors.)
model_perturbation_kernel (The model perturbation kernel.)
transitions (The transitions, one per model.)
- Returns:
(m_ss, theta_ss)
- Return type:
Model, parameter.
- pyabc.inference_util.only_simulate_data_for_proposal(m_ss: int, theta_ss: Parameter, t: int, models: List[Model], summary_statistics: Callable, weight_function: Callable, proposal_id: int) Particle [source]
Simulate data for parameters.
Similar to evaluate_proposal, however here for the passed parameters only data are simulated, but no distances calculated or acceptance checked. That needs to be done post-hoc then, not checked here.
- pyabc.inference_util.termination_criteria_fulfilled(current_eps: float, min_eps: float, prev_eps: float, min_eps_diff: float, stop_if_single_model_alive: bool, nr_of_models_alive: int, acceptance_rate: float, min_acceptance_rate: float, total_nr_simulations: int, max_total_nr_simulations: int, walltime: timedelta, max_walltime: timedelta, t: int, max_t: int) bool [source]
Check termination criteria.
- Parameters:
current_eps (The last generation's epsilon value.)
min_eps (The minimum allowed epsilon value.)
stop_if_single_model_alive (Whether to stop with a single model left.)
nr_of_models_alive (The number of models alive in the last generation.)
acceptance_rate (The last generation's acceptance rate.)
min_acceptance_rate (The minimum acceptance rate.)
total_nr_simulations (The total number of simulations so far.)
max_total_nr_simulations (Bound on the total number of simulations.)
walltime (Walltime passed since start of the analysis.)
max_walltime (Maximum allowed walltime.)
t (The last generation's time index.)
max_t (The maximum allowed time index.)
- Return type:
True if any criterion is met, otherwise False.