Distributions#

class simpple.distributions.Distribution[source]#

Abstract base class for distributions.

Defines the interface distributions must implement.

All distributions should have a __repr__() showing initialization parameters, as well as log_prob(), prior_transform() and sample() methods.

classmethod from_yaml_dict(yaml_dict)[source]#

Create a distribution from a YAML dictionary

The dictionary should have a dist key with the class name, an args key with a list of required arguments and optionally a kwargs key with optional arguments.

See also: Writing Models to and from YAML Files.

Parameters:

yaml_dict (dict) – YAML dictionary typically directly loaded from a file

Returns:

A distribution of the type specified by the “dist” key in the dictionary

abstractmethod log_prob(x)[source]#

Log probability density of the distribution

This should be implemented by subclasses.

Parameters:

x (float | ArrayLike)

Return type:

float | np.ndarray

property optional_args: list[str]#

List of optional (keyword) arguments at initialization, generated with simpple.utils.find_args()

abstractmethod prior_transform(u)[source]#

Prior transform of the distribution

This should be implemented by subclasses. The prior transform should take values from the uniform unit interval and project them to the prior space. This is usually the inverse CDF, or percent-point function, of the distribution.

Parameters:

u (float | ArrayLike)

Return type:

float | np.ndarray

property required_args: list[str]#

List of required arguments at initialization, generated with simpple.utils.find_args()

sample(size=None, seed=None)[source]#

Draw samples from the distribution

Generates random uniform samples and calls the prior transform.

Parameters:
  • size (int | None) – Shape of the samples

  • seed (int | ndarray[int] | None) – Random seed to use

Returns:

Random samples with shape size

Return type:

ndarray

to_yaml_dict()[source]#

Generate YAML dict

The dictionary will have a dist key with the class name, an args key with a list of required arguments and a kwargs key with optional arguments.

required_args and optional_args are used to infer the args and kwargs.

See also: Writing Models to and from YAML Files.

Returns:

YAML dictionary describing the distribution.

Return type:

dict

class simpple.distributions.Fixed(value)[source]#

Fixed distribution

This is essentially a delta function centered on value. Is handled as a special case in simpple.model.Model to avoid inefficient sampling.

Parameters:

value (float) – Value to which the parameter is fixed.

log_prob(x)[source]#

Log probability of a fixed variable.

Returns np.inf at value and -np.inf elsewhere.

Parameters:

x (float | ArrayLike) – Value(s) at which to evaluate the log probability.

Returns:

Log probability value(s)

Return type:

float | np.narray

prior_transform(u)[source]#

Prior transform (inverse CDF) of the fixed distribution

Parameters:

u (float | ArrayLike) – Uniform samples between 0 and 1

Returns:

Transformed samples, all at value.

Return type:

float | np.ndarray

class simpple.distributions.LogUniform(low, high)[source]#

Log-uniform distribution

\[\begin{split}p(x) = \begin{cases} \frac{1}{x \ln(b/a)} & \text{if } a \leq x < b \\ 0 & \text{otherwise} \end{cases}\end{split}\]
Parameters:
  • low (float) – Lower bound (b)

  • high (float) – Upper bound (a)

log_prob(x)[source]#

Log-probability for the log-uniform distribution

Parameters:

x (float | ArrayLike) – Value(s) at which to evaluate the log probability.

Returns:

Log probability value(s)

Return type:

np.ndarray

prior_transform(u)[source]#

Prior transform (inverse CDF) of the log-uniform distribution.

Parameters:

u (float | ArrayLike) – Uniform samples between 0 and 1

Returns:

Transformed samples on a logarithmic scale between self.low and self.high

Return type:

np.ndarray

class simpple.distributions.Normal(mu, sigma)[source]#

Normal distribution

\[p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left( -\frac{(x - \mu)^2}{2\sigma^2} \right)\]
Parameters:
  • mu (float) – Mean \(\mu\)

  • sigma (float) – Standard deviation \(\sigma\)

log_prob(x, *args, **kwargs)[source]#

Log probability density function of the normal distribution

Parameters:

x (float | ArrayLike) – Value(s) at which to evaluate the log probability.

Returns:

Log probability value(s)

Return type:

np.ndarray

prior_transform(u)[source]#

Prior transform (inverse CDF) of the normal distribution.

Parameters:

u (float | ArrayLike) – Uniform samples between 0 and 1

Returns:

Transformed samples centered around \(\mu\) with standard deviation \(\simga\).

Return type:

np.ndarray

class simpple.distributions.ScipyDistribution(dist, *args, **kwargs)[source]#

Distribution based on a scipy random variable.

Note

While this class can be convenient, custom log_prob() and prior_transform() functions written with Numpy are usually faster.

Parameters:

dist (rv_continuous | Callable) – Scipy random variable (RV), either already instantiated or not. If the RV is not instantiated, it will be during init and all args and kwargs are passed to it.

property dist#

Underlying scipy distribution

log_prob(x, *args, **kwargs)[source]#

Log probability density function.

Calls the scipy distribution’s logpdf().

Parameters:

x (float | ArrayLike) – Value(s) at which to evaluate the log probability.

Returns:

Log probability value(s)

Return type:

np.ndarray

prior_transform(u, **kwargs)[source]#

Prior transform (inverse CDF) for nested sampling

Calls the scipy distributiion’s ppf() (percent point function).

Parameters:

u (float | ArrayLike) – Uniform samples between 0 and 1

Returns:

Transformed samples

Return type:

float | np.ndarray

sample(size=None, seed=None, **kwargs)[source]#

Draw samples from the distribution

Calls the scipy distribution’s rvs() method.

Parameters:
  • size (int | None) – Shape of the samples

  • seed (int | Generator | ndarray[int] | None) – Random seed to use (int or numpy generator)

Returns:

Random samples with shape size

Return type:

ndarray

to_yaml_dict()[source]#

Generate YAML dict

Serves the same purpose as Distribution.to_yaml_dict(), but the arguments and keyword arguments are inferred from the dist attribute.

Returns:

YAML dictionary describing the distribution

Return type:

dict

class simpple.distributions.TruncatedNormal(mu, sigma, low=None, high=None)[source]#
\[\begin{split}f(x; \mu, \sigma, a, b) = \begin{cases} \dfrac{1}{\sigma} \dfrac{\phi\left(\frac{x - \mu}{\sigma}\right)} {\Phi\left(\frac{b - \mu}{\sigma}\right) - \Phi\left(\frac{a - \mu}{\sigma}\right)} & \text{if } a \leq x \leq b \\ 0 & \text{otherwise} \end{cases}\end{split}\]

where \(\phi\) is the standard normal distribution and \(\Phi\) the standard normal CDF.

See Truncated normal distribution on Wikipedia.

Parameters:
  • mu (float)

  • sigma (float)

  • low (float | None)

  • high (float | None)

log_prob(x, *args, **kwargs)[source]#

Log probability density function.

Parameters:

x (float | ArrayLike) – Value(s) at which to evaluate the log probability.

Returns:

Log probability value(s)

Return type:

np.ndarray

prior_transform(u)[source]#

Prior transform (inverse CDF) of the truncated distribution.

Parameters:

u (float | ArrayLike) – Uniform samples between 0 and 1

Returns:

Transformed samples between self.low and self.high following a normal distribution

Return type:

float | np.ndarray

class simpple.distributions.Uniform(low, high)[source]#

Uniform distribution

\[\begin{split}p(x) = \begin{cases} \frac{1}{b - a} & \text{if } a \leq x < b \\ 0 & \text{otherwise} \end{cases}\end{split}\]
Parameters:
  • low (float) – Lower bound (b)

  • high (float) – Upper bound (a)

log_prob(x)[source]#

Log-probability for the uniform distribution

Parameters:

x (float | ArrayLike) – Value(s) at which to evaluate the log probability.

Returns:

Log probability value(s)

Return type:

float | np.ndarray

prior_transform(u)[source]#

Prior transform (inverse CDF) of the uniform distribution.

Parameters:

u (float | ArrayLike) – Uniform samples between 0 and 1

Returns:

Transformed samples between self.low and self.high

Return type:

float | np.ndarray