# Random Variables¶

class pyabc.random_variables.Distribution(*args, **kwargs)

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

Copy the distribution

Returns: copied_distribution – A copy of the distribution. Distribution
classmethod from_dictionary_of_dictionaries(dict_of_dicts: dict) → pyabc.random_variables.Distribution

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”. distribution – Created distribution. Distribution
get_parameter_names() → list

Sorted list of parameter names.

Returns: sorted_names – Sorted list of parameter names. list
pdf(x: Union[pyabc.parameters.Parameter, dict])

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

Sample from joint distribution

Returns: parameter – A parameter which was sampled. Parameter
update_random_variables(**random_variables)

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)

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.
cdf(x)

Cumulative distribution function.

Parameters: x (float) – Cumulative distribution function at x. density – Cumulative distribution function at x. float
copy()

Copy the random variable.

Returns: copied_rv – A copy of the random variable. RVBase
decorator_repr()

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. str
pdf(x)

Probability density function

Parameters: x (float) – Probability density at x. density – Probability density at x. float
pmf(x)

Probability mass function

Parameters: x (int) – Probability mass at x. mass – The mass at x. float
rvs()

Sample from the RV.

Returns: sample – A sample from the random variable. float
class pyabc.random_variables.ModelPerturbationKernel(nr_of_models: int, probability_to_stay: Union[float, NoneType] = None)

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.
pmf(n: int, m: int) → float
Parameters: n (int) – Model target nr. m (int) – Model source nr. probability – Probability with which to jump from m to n. float
rvs(m: int) → int

Sample a Kernel jump from model m to another model.

Parameters: m (int) – Model source nr. target – Target model nr. int
class pyabc.random_variables.RV(name: str, *args, **kwargs)

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”.
cdf(x, *args, **kwargs)

Cumulative distribution function.

Parameters: x (float) – Cumulative distribution function at x. density – Cumulative distribution function at x. float
copy()

Copy the random variable.

Returns: copied_rv – A copy of the random variable. RVBase
distribution = None

the scipy.stats. … distribution object

classmethod from_dictionary(dictionary: dict) → pyabc.random_variables.RV

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)

Probability density function

Parameters: x (float) – Probability density at x. density – Probability density at x. float
pmf(x, *args, **kwargs)

Probability mass function

Parameters: x (int) – Probability mass at x. mass – The mass at x. float
rvs(*args, **kwargs)

Sample from the RV.

Returns: sample – A sample from the random variable. float
class pyabc.random_variables.RVBase

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

cdf(x: float, *args, **kwargs) → float

Cumulative distribution function.

Parameters: x (float) – Cumulative distribution function at x. density – Cumulative distribution function at x. float
copy() → pyabc.random_variables.RVBase

Copy the random variable.

Returns: copied_rv – A copy of the random variable. RVBase
pdf(x: float, *args, **kwargs) → float

Probability density function

Parameters: x (float) – Probability density at x. density – Probability density at x. float
pmf(x, *args, **kwargs) → float

Probability mass function

Parameters: x (int) – Probability mass at x. mass – The mass at x. float
rvs(*args, **kwargs) → float

Sample from the RV.

Returns: sample – A sample from the random variable. float
class pyabc.random_variables.RVDecorator(component: 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.
cdf(x, *args, **kwargs)

Cumulative distribution function.

Parameters: x (float) – Cumulative distribution function at x. density – Cumulative distribution function at x. float
component = None

The decorated random variable

copy()

Copy the random variable.

Returns: copied_rv – A copy of the random variable. RVBase
decorator_repr() → str

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. str
pdf(x, *args, **kwargs)

Probability density function

Parameters: x (float) – Probability density at x. density – Probability density at x. float
pmf(x, *args, **kwargs)

Probability mass function

Parameters: x (int) – Probability mass at x. mass – The mass at x. float
rvs(*args, **kwargs)

Sample from the RV.

Returns: sample – A sample from the random variable. float