Random variables

class pyabc.random_variables.Distribution(*args, **kwargs)[source]

Bases: pyabc.parameters.ParameterStructure

Distribution of parameters for a model.

A distribution is a collection of RVs and/or distributions. Essentially something like a dictionary of random variables or distributions. The variables from which the distribution is initialized are independent.

This should be used to define a prior.

copy()pyabc.random_variables.Distribution[source]

Copy the distribution

Returns

copied_distribution – A copy of the distribution.

Return type

Distribution

classmethod from_dictionary_of_dictionaries(dict_of_dicts: dict)pyabc.random_variables.Distribution[source]

Create distribution from dictionary of dictionaries

Parameters

dict_of_dicts (dict) – The keys of the dict indicate the parameters names. The values are itself dictionaries representing scipy.stats distribution. I.e. the have the key “name” and at least one of the keys “args” or “kwargs”.

Returns

distribution – Created distribution.

Return type

Distribution

get_parameter_names() → list[source]

Sorted list of parameter names.

Returns

sorted_names – Sorted list of parameter names.

Return type

list

pdf(x: Union[pyabc.parameters.Parameter, dict])[source]

Get combination of probability density function (for continuous variables) and probability mass function (for discrete variables) at point x

Parameters

x (Union[Parameter, dict]) – Evaluate at the given Parameter x.

rvs()pyabc.parameters.Parameter[source]

Sample from joint distribution

Returns

parameter – A parameter which was sampled.

Return type

Parameter

update_random_variables(**random_variables)[source]

Update random variables within the distribution

Parameters

**random_variables – keywords are the parameters’ names, the values are random variable.

class pyabc.random_variables.LowerBoundDecorator(component: pyabc.random_variables.RV, lower_bound: float)[source]

Bases: pyabc.random_variables.RVDecorator

Impose a strict lower bound on a random variable. Condition RV X to X > lower bound. In particular P(X = lower_bound) = 0.

Note

Sampling is done via rejection. Up to 10000 samples are taken from the decorated RV. The first sample within the permitted range is then taken. Otherwise None is returned.

Parameters
  • component (RV) – The decorated random variable.

  • lower_bound (float) – The lower bound.

__init__(component: pyabc.random_variables.RV, lower_bound: float)[source]

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

cdf(x, *args, **kwargs)[source]

Cumulative distribution function.

Parameters

x (float) – Cumulative distribution function at x.

Returns

density – Cumulative distribution function at x.

Return type

float

copy()[source]

Copy the random variable.

Returns

copied_rv – A copy of the random variable.

Return type

RVBase

decorator_repr()[source]

Represent the decorator itself.

Template method.

The __repr__ method used decorator_repr and the __repr__ of the decorated RV to build a combined representation.

Returns

decorator_repr – A string representing the decorator only.

Return type

str

pdf(x, *args, **kwargs)[source]

Probability density function

Parameters

x (float) – Probability density at x.

Returns

density – Probability density at x.

Return type

float

pmf(x, *args, **kwargs)[source]

Probability mass function

Parameters

x (int) – Probability mass at x.

Returns

mass – The mass at x.

Return type

float

rvs(*args, **kwargs)[source]

Sample from the RV.

Returns

sample – A sample from the random variable.

Return type

float

class pyabc.random_variables.ModelPerturbationKernel(nr_of_models: int, probability_to_stay: Optional[float] = None)[source]

Bases: object

Model perturbation kernel.

Parameters
  • nr_of_models (int) – Number of models

  • probability_to_stay (Union[float, None]) – If None, probability to stay is set to 1/nr_of_models. Otherwise, the supplied value is used.

__init__(nr_of_models: int, probability_to_stay: Optional[float] = None)[source]

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

pmf(n: int, m: int) → float[source]
Parameters
  • n (int) – Model target nr.

  • m (int) – Model source nr.

Returns

probability – Probability with which to jump from m to n.

Return type

float

rvs(m: int) → int[source]

Sample a Kernel jump from model m to another model.

Parameters

m (int) – Model source nr.

Returns

target – Target model nr.

Return type

int

class pyabc.random_variables.RV(name: str, *args, **kwargs)[source]

Bases: pyabc.random_variables.RVBase

Concrete random variable.

Parameters
  • name (str) – Name of the distribution as in scipy.stats

  • args – Arguments as in scipy.stats matching the distribution with name “name”.

  • kwargs – Keyword arguments as in scipy.stats matching the distribution with name “name”.

__init__(name: str, *args, **kwargs)[source]

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

cdf(x, *args, **kwargs)[source]

Cumulative distribution function.

Parameters

x (float) – Cumulative distribution function at x.

Returns

density – Cumulative distribution function at x.

Return type

float

copy()[source]

Copy the random variable.

Returns

copied_rv – A copy of the random variable.

Return type

RVBase

distribution

the scipy.stats. … distribution object

classmethod from_dictionary(dictionary: dict)pyabc.random_variables.RV[source]

Construct random variable from dictionary.

Parameters

dictionary (dict) –

A dictionary with the keys

  • ”name” (mandatory)

  • ”args” (optional)

  • ”kwargs” (optional)

as in scipy.stats.

Note

Either the “args” or the “kwargs” key has to be present.

pdf(x, *args, **kwargs)[source]

Probability density function

Parameters

x (float) – Probability density at x.

Returns

density – Probability density at x.

Return type

float

pmf(x, *args, **kwargs)[source]

Probability mass function

Parameters

x (int) – Probability mass at x.

Returns

mass – The mass at x.

Return type

float

rvs(*args, **kwargs)[source]

Sample from the RV.

Returns

sample – A sample from the random variable.

Return type

float

class pyabc.random_variables.RVBase[source]

Bases: abc.ABC

Random variable abstract base class.

Note

Why introduce another random variable class and not just use the one’s provided in scipy.stats?

This funny construction is done because scipy.stats distributions are not pickleable. This class is really a very thin wrapper around scipy.stats distributions to make them pickleable. It is important to be able to pickle them to execute the ACBSMC algorithm in a distributed cluster environment

abstract cdf(x: float, *args, **kwargs) → float[source]

Cumulative distribution function.

Parameters

x (float) – Cumulative distribution function at x.

Returns

density – Cumulative distribution function at x.

Return type

float

abstract copy()pyabc.random_variables.RVBase[source]

Copy the random variable.

Returns

copied_rv – A copy of the random variable.

Return type

RVBase

abstract pdf(x: float, *args, **kwargs) → float[source]

Probability density function

Parameters

x (float) – Probability density at x.

Returns

density – Probability density at x.

Return type

float

abstract pmf(x, *args, **kwargs) → float[source]

Probability mass function

Parameters

x (int) – Probability mass at x.

Returns

mass – The mass at x.

Return type

float

abstract rvs(*args, **kwargs) → float[source]

Sample from the RV.

Returns

sample – A sample from the random variable.

Return type

float

class pyabc.random_variables.RVDecorator(component: pyabc.random_variables.RVBase)[source]

Bases: pyabc.random_variables.RVBase

Random variable decorater base class.

Implement a decorator pattern.

Further decorators should derive from this class.

It stores the decorated random variable in self.component

Overwrite the method decorator_repr the represent the decorator type. The decorated variable will then be automatically included in the call to __repr__.

Parameters

component (RVBase) – The random variable to be decorated.

__init__(component: pyabc.random_variables.RVBase)[source]

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

cdf(x, *args, **kwargs)[source]

Cumulative distribution function.

Parameters

x (float) – Cumulative distribution function at x.

Returns

density – Cumulative distribution function at x.

Return type

float

component

The decorated random variable

copy()[source]

Copy the random variable.

Returns

copied_rv – A copy of the random variable.

Return type

RVBase

decorator_repr() → str[source]

Represent the decorator itself.

Template method.

The __repr__ method used decorator_repr and the __repr__ of the decorated RV to build a combined representation.

Returns

decorator_repr – A string representing the decorator only.

Return type

str

pdf(x, *args, **kwargs)[source]

Probability density function

Parameters

x (float) – Probability density at x.

Returns

density – Probability density at x.

Return type

float

pmf(x, *args, **kwargs)[source]

Probability mass function

Parameters

x (int) – Probability mass at x.

Returns

mass – The mass at x.

Return type

float

rvs(*args, **kwargs)[source]

Sample from the RV.

Returns

sample – A sample from the random variable.

Return type

float