Probability distributions based on SciPy

In this example, we seek to create a probability distribution based on the SciPy library.

from __future__ import annotations

from gemseo import configure_logger
from gemseo.uncertainty import create_distribution
from gemseo.uncertainty import get_available_distributions

configure_logger()
<RootLogger root (INFO)>

First of all, we can access the names of the available probability distributions from the API:

all_distributions = get_available_distributions()
all_distributions
['OTComposedDistribution', 'OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'OTWeibullDistribution', 'SPComposedDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution']

and filter the ones based on the SciPy library (their names start with the acronym ‘SP’):

sp_distributions = get_available_distributions("SPDistribution")
sp_distributions
['SPDistribution', 'SPExponentialDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution']

Create a distribution

Then, we can create a probability distribution for a two-dimensional random variable with independent components that follow a normal distribution.

Case 1: the SciPy distribution has a GEMSEO class

For the standard normal distribution (mean = 0 and standard deviation = 1):

distribution_0_1 = create_distribution("x", "SPNormalDistribution", 2)
distribution_0_1
norm[2](mu=0.0, sigma=1.0)

For a normal with mean = 1 and standard deviation = 2:

distribution_1_2 = create_distribution(
    "x", "SPNormalDistribution", 2, mu=1.0, sigma=2.0
)
distribution_1_2
norm[2](mu=1.0, sigma=2.0)

Case 2: the SciPy distribution has no GEMSEO class

When GEMSEO does not offer a class for the SciPy distribution, we can use the generic GEMSEO class SPDistribution to create any SciPy distribution by setting interfaced_distribution to its SciPy name and parameters as a dictionary of SciPy parameter names and values (see the documentation of SciPy).

distribution_1_2 = create_distribution(
    "x",
    "SPDistribution",
    2,
    interfaced_distribution="norm",
    parameters={"loc": 1.0, "scale": 2.0},
)
distribution_1_2
norm[2](loc=1.0, scale=2.0)

Plot the distribution

We can plot both cumulative and probability density functions for the first marginal:

distribution_0_1.plot()
Probability distribution of x[0]
<Figure size 640x320 with 2 Axes>

Note

We can provide a marginal index as first argument of the Distribution.plot() method but in the current version of GEMSEO, all components have the same distributions and so the plot will be the same.

Get statistics

Mean

We can access the mean of the distribution:

distribution_0_1.mean
array([0., 0.])

Standard deviation

We can access the standard deviation of the distribution:

distribution_0_1.standard_deviation
array([1., 1.])

Numerical range

We can access the range, i.e. the difference between the numerical minimum and maximum, of the distribution:

distribution_0_1.range
[array([-7.03448383,  7.03448691]), array([-7.03448383,  7.03448691])]

Mathematical support

We can access the range, i.e. the difference between the minimum and maximum, of the distribution:

distribution_0_1.support
[array([-inf,  inf]), array([-inf,  inf])]

Compute CDF

We can compute the cumulative density function component per component (here the probability that the first component is lower than 0. and that the second one is lower than 1.):

distribution_0_1.compute_cdf([0.0, 1.0])
array([0.5       , 0.84134475])

Compute inverse CDF

We can compute the inverse cumulative density function component per component (here the quantile at 50% for the first component and the quantile at 97.5% for the second one):

distribution_0_1.compute_inverse_cdf([0.5, 0.975])
array([0.        , 1.95996398])

Generate samples

We can generate 10 samples of the distribution:

distribution_0_1.compute_samples(10)
array([[-0.168939  ,  0.08942066],
       [-0.57059308, -0.47415389],
       [-0.25021353,  0.9821562 ],
       [ 1.06133089, -0.01806411],
       [ 0.28957147,  1.31410183],
       [-0.91075412,  1.62403116],
       [ 0.11543377,  0.15815581],
       [ 0.29673275, -0.64925908],
       [ 1.94038565, -1.33828019],
       [-0.29433651,  0.02264067]])

Total running time of the script: (0 minutes 0.194 seconds)

Gallery generated by Sphinx-Gallery