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
distkey with the class name, anargskey with a list of required arguments and optionally akwargskey 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
distkey with the class name, anargskey with a list of required arguments and akwargskey with optional arguments.required_argsandoptional_argsare used to infer theargsandkwargs.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 insimpple.model.Modelto avoid inefficient sampling.- Parameters:
value (float) – Value to which the parameter is fixed.
- 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)
- 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\)
- 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()andprior_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 thedistattribute.- 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)
- 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)