.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/uncertainty/distributions/plot_ot_distribution.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_uncertainty_distributions_plot_ot_distribution.py: Probability distributions based on OpenTURNS ============================================ In this example, we seek to create a probability distribution based on the OpenTURNS library. .. GENERATED FROM PYTHON SOURCE LINES 28-37 .. code-block:: Python from __future__ import annotations from gemseo import configure_logger from gemseo.uncertainty import create_distribution from gemseo.uncertainty import get_available_distributions configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-40 First of all, we can access the names of the available probability distributions from the API: .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python all_distributions = get_available_distributions() all_distributions .. rst-class:: sphx-glr-script-out .. code-block:: none ['OTBetaDistribution', 'OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTJointDistribution', 'OTLogNormalDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'OTWeibullDistribution', 'SPBetaDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPJointDistribution', 'SPLogNormalDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 44-46 and filter the ones based on the OpenTURNS library (their names start with the acronym 'OT'): .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python ot_distributions = get_available_distributions("OTDistribution") ot_distributions .. rst-class:: sphx-glr-script-out .. code-block:: none ['OTBetaDistribution', 'OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTLogNormalDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'OTWeibullDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 50-58 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): .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python distribution_0_1 = create_distribution("OTNormalDistribution") distribution_0_1 .. rst-class:: sphx-glr-script-out .. code-block:: none Normal(mu=0.0, sigma=1.0) .. GENERATED FROM PYTHON SOURCE LINES 62-63 For a normal with mean = 1 and standard deviation = 2: .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python distribution_1_2 = create_distribution("OTNormalDistribution", mu=1.0, sigma=2.0) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none Normal(mu=1.0, sigma=2.0) .. GENERATED FROM PYTHON SOURCE LINES 67-77 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 :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 `__). .. GENERATED FROM PYTHON SOURCE LINES 77-82 .. code-block:: Python distribution_1_2 = create_distribution( "OTDistribution", interfaced_distribution="Normal", parameters=(1.0, 2.0) ) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none Normal(1.0, 2.0) .. GENERATED FROM PYTHON SOURCE LINES 83-86 Plot the distribution --------------------- We can plot both cumulative and probability density functions: .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: Python distribution_0_1.plot() .. image-sg:: /examples/uncertainty/distributions/images/sphx_glr_plot_ot_distribution_001.png :alt: Normal(mu=0.0, sigma=1.0) :srcset: /examples/uncertainty/distributions/images/sphx_glr_plot_ot_distribution_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 89-94 Get statistics -------------- Mean ~~~~ We can access the mean of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: Python distribution_0_1.mean .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 97-100 Standard deviation ~~~~~~~~~~~~~~~~~~ We can access the standard deviation of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python distribution_0_1.standard_deviation .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 103-108 Numerical range ~~~~~~~~~~~~~~~ We can access the range, i.e. the difference between the numerical minimum and maximum, of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 108-110 .. code-block:: Python distribution_0_1.range .. rst-class:: sphx-glr-script-out .. code-block:: none array([-7.65062809, 7.65062809]) .. GENERATED FROM PYTHON SOURCE LINES 111-116 Mathematical support ~~~~~~~~~~~~~~~~~~~~ We can access the range, i.e. the difference between the minimum and maximum, of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python distribution_0_1.support .. rst-class:: sphx-glr-script-out .. code-block:: none array([-inf, inf]) .. GENERATED FROM PYTHON SOURCE LINES 119-122 Evaluate CDF ------------ We can evaluate the cumulative density function: .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: Python distribution_0_1.compute_cdf(0.5) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.6914624612740131 .. GENERATED FROM PYTHON SOURCE LINES 125-129 Evaluate inverse CDF -------------------- We can evaluate the inverse cumulative density function, here the quantile at 97.5%: .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. code-block:: Python distribution_0_1.compute_inverse_cdf(0.975) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.9599639845400538 .. GENERATED FROM PYTHON SOURCE LINES 132-135 Generate samples ---------------- We can generate 10 samples of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 135-136 .. code-block:: Python distribution_0_1.compute_samples(10) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 0.60820165, -1.2661731 , -0.43826562, 1.2054782 , -2.18138523, 0.35004209, -0.35500705, 1.43724931, 0.81066798, 0.79315601]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.155 seconds) .. _sphx_glr_download_examples_uncertainty_distributions_plot_ot_distribution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_ot_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_ot_distribution.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_ot_distribution.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_