Particles and Populations

A particle contains the sampled parameters and simulated data. A population gathers all particles collected in one SMC iteration.

class pyabc.population.Particle(m: int, parameter: pyabc.parameters.Parameter, weight: float, accepted_sum_stats: List[dict], accepted_distances: List[float], rejected_sum_stats: List[dict] = None, rejected_distances: List[float] = None, accepted: bool = True)[source]

Bases: object

An (accepted or rejected) particle, containing the information that will also be stored in the database. Stores all summary statistics that were generated during the creation of this particle, and a flag indicating whether this particle was accepted or not.

Parameters
  • m (int) – The model index.

  • parameter (Parameter) – The model specific parameter.

  • weight (float, 0 <= weight <= 1) – The weight of the particle.

  • accepted_sum_stats (List[dict]) – List of accepted summary statistics. This list is usually of length 1. This list is longer only if more than one sample is taken for a particle. This list has length 0 if the particle is rejected.

  • accepted_distances (List[float]) – A particle can contain more than one sample. If so, the distances of the individual samples are stored in this list. In the most common case of a single sample, this list has length 1.

  • rejected_sum_stats (List[dict]) – List of rejected summary statistics.

  • rejected_distances (List[float]) – List of rejected distances.

  • accepted (bool) – True if particle was accepted, False if not.

Note

There are two different ways of weighting particles: First, the weights can be calculated as emerges from the importance sampling. Second, the weights of particles belonging to one model can be summed to, after normalization, find model probabilities. Then, the weights of all particles belonging to one model can be summed to one. Weighting is transferred to the second way in _normalize_weights() in order to also have access to model probabilities. This mode is also stored in the database. If one needs access to the first weighting scheme later on again, one has to perform backwards transformation, multiplying the weights with the model probabilities.

__init__(m: int, parameter: pyabc.parameters.Parameter, weight: float, accepted_sum_stats: List[dict], accepted_distances: List[float], rejected_sum_stats: List[dict] = None, rejected_distances: List[float] = None, accepted: bool = True)[source]

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

class pyabc.population.Population(particles: List[pyabc.population.Particle])[source]

Bases: object

A population contains a list of particles and offers standardized access to them. Upon initialization, the particle weights are normalized and model probabilities computed as described in _normalize_weights.

__init__(particles: List[pyabc.population.Particle])[source]

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

get_accepted_sum_stats() → List[dict][source]

Return a list of all accepted summary statistics.

get_for_keys(keys)[source]

Get dataframe of population values. Possible entries of keys: weight, distance, sum_stat, parameter.

Returns

  • Dictionary where the keys are associated to same-ordered lists

  • of the corresponding values.

get_list() → List[pyabc.population.Particle][source]
Returns

Return type

A copy of the underlying particle list.

get_model_probabilities() → dict[source]

Get probabilities of the individual models.

Returns

model_probabilities – The model probabilities.

Return type

List

get_weighted_distances() → pandas.core.frame.DataFrame[source]

Create DataFrame of (distance, weight)’s. The particle weights are multiplied by the model probabilities. If one simulation per particle was performed, the weights thus sum to 1. If more than one simulation per particle was performed, this does not have to be the case, and post-normalizing may be necessary.

Returns

weighted_distances – A pd.DataFrame containing in column ‘distance’ the distances and in column ‘w’ the scaled weights.

Return type

pd.DataFrame:

get_weighted_sum_stats() → tuple[source]

Get weights and summary statistics.

Returns

weights, sum_stats

Return type

2-Tuple of lists

to_dict() → dict[source]

Create a dictionary representation, creating a list of particles for each model.

Returns

store – A dictionary with the models as keys and a list of particles for each model as values.

Return type

dict

update_distances(distance_to_ground_truth: Callable[[dict], float])[source]

Update the distances of all summary statistics of all particles according to the passed distance function (which is typically different from the distance function with which the original distances were computed).

Parameters

distance_to_ground_truth – Distance function to the observed summary statistics.