.. 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-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 ['OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTJointDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'OTWeibullDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPJointDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 44-46 and filter the ones based on the SciPy library (their names start with the acronym 'SP'): .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python sp_distributions = get_available_distributions("SPDistribution") sp_distributions .. rst-class:: sphx-glr-script-out .. code-block:: none ['SPDistribution', 'SPExponentialDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution', 'SPWeibullDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 50-59 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): .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python distribution_0_1 = create_distribution("x", "SPNormalDistribution", 2) distribution_0_1 .. rst-class:: sphx-glr-script-out .. code-block:: none norm[2](mu=0.0, sigma=1.0) .. GENERATED FROM PYTHON SOURCE LINES 63-64 For a normal with mean = 1 and standard deviation = 2: .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: Python distribution_1_2 = create_distribution( "x", "SPNormalDistribution", 2, mu=1.0, sigma=2.0 ) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none norm[2](mu=1.0, sigma=2.0) .. GENERATED FROM PYTHON SOURCE LINES 70-79 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 79-88 .. code-block:: Python distribution_1_2 = create_distribution( "x", "SPDistribution", 2, interfaced_distribution="norm", parameters={"loc": 1.0, "scale": 2.0}, ) distribution_1_2 .. rst-class:: sphx-glr-script-out .. code-block:: none norm[2](loc=1.0, scale=2.0) .. GENERATED FROM PYTHON SOURCE LINES 89-93 Plot the distribution --------------------- We can plot both cumulative and probability density functions for the first marginal: .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python distribution_0_1.plot() .. image-sg:: /examples/uncertainty/distributions/images/sphx_glr_plot_sp_distribution_001.png :alt: Probability distribution of x[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 96-102 .. note:: We can provide a marginal index as first argument of the :meth:`.BaseDistribution.plot` method but in the current version of |g|, all components have the same distributions and so the plot will be the same. .. GENERATED FROM PYTHON SOURCE LINES 104-109 Get statistics -------------- Mean ~~~~ We can access the mean of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. code-block:: Python distribution_0_1.mean .. rst-class:: sphx-glr-script-out .. code-block:: none array([0., 0.]) .. GENERATED FROM PYTHON SOURCE LINES 112-115 Standard deviation ~~~~~~~~~~~~~~~~~~ We can access the standard deviation of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python distribution_0_1.standard_deviation .. rst-class:: sphx-glr-script-out .. code-block:: none array([1., 1.]) .. GENERATED FROM PYTHON SOURCE LINES 118-123 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 123-125 .. code-block:: Python distribution_0_1.range .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-7.03448383, 7.03448691]), array([-7.03448383, 7.03448691])] .. GENERATED FROM PYTHON SOURCE LINES 126-131 Mathematical support ~~~~~~~~~~~~~~~~~~~~ We can access the range, i.e. the difference between the minimum and maximum, of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: Python distribution_0_1.support .. rst-class:: sphx-glr-script-out .. code-block:: none [array([-inf, inf]), array([-inf, inf])] .. GENERATED FROM PYTHON SOURCE LINES 134-139 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.): .. GENERATED FROM PYTHON SOURCE LINES 139-141 .. code-block:: Python distribution_0_1.compute_cdf([0.0, 1.0]) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.5 , 0.84134475]) .. GENERATED FROM PYTHON SOURCE LINES 142-148 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): .. GENERATED FROM PYTHON SOURCE LINES 148-150 .. code-block:: Python distribution_0_1.compute_inverse_cdf([0.5, 0.975]) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0. , 1.95996398]) .. GENERATED FROM PYTHON SOURCE LINES 151-154 Generate samples ---------------- We can generate 10 samples of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 154-155 .. code-block:: Python distribution_0_1.compute_samples(10) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[-1.01967745, -0.63583219], [ 0.97730345, -0.00911666], [-0.09546088, 0.40049171], [-0.26242995, -0.01162742], [-0.26414642, 1.55155751], [-0.17575777, -0.91735817], [ 0.12648392, 0.17941366], [ 0.34737665, 0.10629626], [ 0.78929172, 0.11003329], [ 0.40434153, 0.68647646]]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.174 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_