.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/uncertainty/distributions/plot_sp_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_sp_distribution.py: Probability distributions based on SciPy ======================================== In this example, we seek to create a probability distribution based on the SciPy library. .. GENERATED FROM PYTHON SOURCE LINES 28-40 .. code-block:: Python from __future__ import annotations from gemseo.uncertainty import create_distribution from gemseo.uncertainty import get_available_distributions from gemseo.uncertainty.distributions.scipy.distribution_settings import ( SPDistribution_Settings, ) from gemseo.uncertainty.distributions.scipy.normal_settings import ( SPNormalDistribution_Settings, ) .. GENERATED FROM PYTHON SOURCE LINES 41-43 First of all, we can access the names of the available probability distributions from the API: .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. 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 47-49 and filter the ones based on the SciPy library (their names start with the acronym 'SP'): .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python sp_distributions = get_available_distributions("SPDistribution") sp_distributions .. rst-class:: sphx-glr-script-out .. code-block:: none ['SPBetaDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPLogNormalDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 53-61 Create a distribution --------------------- Then, we can create a probability distribution, e.g. a normal distribution. Case 1: the SciPy distribution has a GEMSEO class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For the standard normal distribution (mean = 0 and standard deviation = 1): .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python distribution_0_1 = create_distribution("SPNormalDistribution") distribution_0_1 .. rst-class:: sphx-glr-script-out .. code-block:: none norm(mu=0.0, sigma=1.0) .. GENERATED FROM PYTHON SOURCE LINES 65-66 For a normal with mean = 1 and standard deviation = 2: .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python distribution_1_2 = create_distribution("SPNormalDistribution", mu=1.0, sigma=2.0) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none norm(mu=1.0, sigma=2.0) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Same from settings defined as a Pydantic model: .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: Python distribution_1_2 = create_distribution( "SPNormalDistribution", settings=SPNormalDistribution_Settings(mu=1.0, sigma=2.0) ) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none norm(mu=1.0, sigma=2.0) .. GENERATED FROM PYTHON SOURCE LINES 77-86 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 :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 `__). .. GENERATED FROM PYTHON SOURCE LINES 86-93 .. code-block:: Python distribution_1_2 = create_distribution( "SPDistribution", interfaced_distribution="norm", parameters={"loc": 1.0, "scale": 2.0}, ) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none norm(loc=1.0, scale=2.0) .. GENERATED FROM PYTHON SOURCE LINES 94-95 Same from settings defined as a Pydantic model: .. GENERATED FROM PYTHON SOURCE LINES 95-103 .. code-block:: Python distribution_1_2 = create_distribution( "SPDistribution", settings=SPDistribution_Settings( interfaced_distribution="norm", parameters={"loc": 1.0, "scale": 2.0} ), ) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none norm(loc=1.0, scale=2.0) .. GENERATED FROM PYTHON SOURCE LINES 104-107 Plot the distribution --------------------- We can plot both cumulative and probability density functions: .. GENERATED FROM PYTHON SOURCE LINES 107-109 .. code-block:: Python distribution_0_1.plot() .. image-sg:: /examples/uncertainty/distributions/images/sphx_glr_plot_sp_distribution_001.png :alt: norm(mu=0.0, sigma=1.0) :srcset: /examples/uncertainty/distributions/images/sphx_glr_plot_sp_distribution_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 110-115 Get statistics -------------- Mean ~~~~ We can access the mean of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python distribution_0_1.mean .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.0) .. GENERATED FROM PYTHON SOURCE LINES 118-121 Standard deviation ~~~~~~~~~~~~~~~~~~ We can access the standard deviation of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: Python distribution_0_1.standard_deviation .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(1.0) .. GENERATED FROM PYTHON SOURCE LINES 124-129 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 129-131 .. code-block:: Python distribution_0_1.range .. rst-class:: sphx-glr-script-out .. code-block:: none array([-7.03448383, 7.03448691]) .. GENERATED FROM PYTHON SOURCE LINES 132-137 Mathematical support ~~~~~~~~~~~~~~~~~~~~ We can access the range, i.e. the difference between the minimum and maximum, of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python distribution_0_1.support .. rst-class:: sphx-glr-script-out .. code-block:: none array([-inf, inf]) .. GENERATED FROM PYTHON SOURCE LINES 140-143 Evaluate CDF ------------ We can evaluate the cumulative density function: .. GENERATED FROM PYTHON SOURCE LINES 143-145 .. code-block:: Python distribution_0_1.compute_cdf(0.5) .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.6914624612740131) .. GENERATED FROM PYTHON SOURCE LINES 146-150 Evaluate inverse CDF -------------------- We can evaluate the inverse cumulative density function, here the quantile at 97.5%: .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: Python distribution_0_1.compute_inverse_cdf(0.975) .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(1.959963984540054) .. GENERATED FROM PYTHON SOURCE LINES 153-156 Generate samples ---------------- We can generate 10 samples of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 156-157 .. code-block:: Python distribution_0_1.compute_samples(10) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 0.74094511, 0.0820873 , -0.23021294, -0.07620066, -1.82483552, -0.88207143, -0.50546445, -0.25714944, 1.78600828, -3.28449264]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.070 seconds) .. _sphx_glr_download_examples_uncertainty_distributions_plot_sp_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_sp_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sp_distribution.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sp_distribution.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_