Note
Go to the end to download the full example code.
Probability distributions based on OpenTURNS#
In this example, we seek to create a probability distribution based on the OpenTURNS 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
['OTBetaDistribution', 'OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTJointDistribution', 'OTLogNormalDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'OTWeibullDistribution', 'SPBetaDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPJointDistribution', 'SPLogNormalDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution']
and filter the ones based on the OpenTURNS library (their names start with the acronym 'OT'):
ot_distributions = get_available_distributions("OTDistribution")
ot_distributions
['OTBetaDistribution', 'OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTLogNormalDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'OTWeibullDistribution']
Create a distribution#
Then, we can create a probability distribution, e.g. a normal distribution.
Case 1: the OpenTURNS distribution has a GEMSEO class#
For the standard normal distribution (mean = 0 and standard deviation = 1):
distribution_0_1 = create_distribution("OTNormalDistribution")
distribution_0_1
Normal(mu=0.0, sigma=1.0)
For a normal with mean = 1 and standard deviation = 2:
distribution_1_2 = create_distribution("OTNormalDistribution", mu=1.0, sigma=2.0)
distribution_1_2
Normal(mu=1.0, sigma=2.0)
Case 2: the OpenTURNS distribution has no GEMSEO class#
When GEMSEO does not offer a class for the OpenTURNS distribution,
we can use the generic GEMSEO class OTDistribution
to create any OpenTURNS distribution
by setting interfaced_distribution
to its OpenTURNS name
and parameters
as a tuple of OpenTURNS parameter values
(see the documentation of OpenTURNS).
distribution_1_2 = create_distribution(
"OTDistribution", interfaced_distribution="Normal", parameters=(1.0, 2.0)
)
distribution_1_2
Normal(1.0, 2.0)
Plot the distribution#
We can plot both cumulative and probability density functions:
distribution_0_1.plot()
<Figure size 640x320 with 2 Axes>
Get statistics#
Mean#
We can access the mean of the distribution:
distribution_0_1.mean
0.0
Standard deviation#
We can access the standard deviation of the distribution:
distribution_0_1.standard_deviation
1.0
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.65062809, 7.65062809])
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])
Evaluate CDF#
We can evaluate the cumulative density function:
distribution_0_1.compute_cdf(0.5)
0.6914624612740131
Evaluate inverse CDF#
We can evaluate the inverse cumulative density function, here the quantile at 97.5%:
distribution_0_1.compute_inverse_cdf(0.975)
1.9599639845400538
Generate samples#
We can generate 10 samples of the distribution:
distribution_0_1.compute_samples(10)
array([ 0.60820165, -1.2661731 , -0.43826562, 1.2054782 , -2.18138523,
0.35004209, -0.35500705, 1.43724931, 0.81066798, 0.79315601])
Total running time of the script: (0 minutes 0.155 seconds)