pyabc.random_variables

Random variables

Random variables are used to define the model prior, proposal distribution, and the model.

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

Bases: DistributionBase, ParameterStructure

Distribution of parameters for a model assuming independence.

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

copy() 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) 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]

Get a sorted list of parameter names.

Returns:

sorted_names – Sorted list of parameter names.

Return type:

list

pdf(x: Parameter | dict)[source]

Get probability density at point x (product of marginals).

Combination of probability density functions (for continuous variables) and probability mass function (for discrete variables).

Parameters:

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

rvs(*args, **kwargs) 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.DistributionBase[source]

Bases: ABC

Distribution of parameters for a model, abstract base class.

A distribution is a collection of RVs and/or distributions.

This should be used to define a prior.

abstract pdf(x: Parameter | dict)[source]

Get probability density at point x.

Parameters:

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

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

Sample from joint distribution.

Returns:

parameter – A parameter which was sampled.

Return type:

Parameter

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

Bases: 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: RV, lower_bound: float)[source]
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.RV(name: str, *args, **kwargs)[source]

Bases: 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]
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) 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

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 ABCSMC 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() 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: RVBase)[source]

Bases: RVBase

Random variable decorator 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 to 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: RVBase)[source]
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