.. 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 Click :ref:`here ` 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-35 .. code-block:: default from gemseo.api import configure_logger from gemseo.uncertainty.api import create_distribution from gemseo.uncertainty.api import get_available_distributions from matplotlib import pyplot as plt configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-38 First of all, we can access the names of the available probability distributions from the API: .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: default all_distributions = get_available_distributions() print(all_distributions) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['ComposedDistribution', 'OTComposedDistribution', 'OTDiracDistribution', 'OTDistribution', 'OTExponentialDistribution', 'OTNormalDistribution', 'OTTriangularDistribution', 'OTUniformDistribution', 'SPComposedDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 42-44 and filter the ones based on the SciPy library (their names start with the acronym 'SP'): .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: default sp_distributions = [dist for dist in all_distributions if dist.startswith("SP")] print(sp_distributions) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['SPComposedDistribution', 'SPDistribution', 'SPExponentialDistribution', 'SPNormalDistribution', 'SPTriangularDistribution', 'SPUniformDistribution'] .. GENERATED FROM PYTHON SOURCE LINES 48-54 Create a distribution --------------------- Then, we can create a probability distribution for a two-dimensional random variable whose components are independent and distributed as the standard normal distribution (mean = 0 and standard deviation = 1): .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: default distribution_0_1 = create_distribution("x", "SPNormalDistribution", 2) print(distribution_0_1) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none norm(mu=0.0, sigma=1.0) .. GENERATED FROM PYTHON SOURCE LINES 58-60 or create another distribution with mean = 1 and standard deviation = 2 for the marginal distributions:: .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: default distribution_1_2 = create_distribution( "x", "SPNormalDistribution", 2, mu=1.0, sigma=2.0 ) print(distribution_1_2) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none norm(mu=1.0, sigma=2.0) .. GENERATED FROM PYTHON SOURCE LINES 66-69 We could also use the generic :class:`.SPDistribution` which allows access to all the SciPy distributions but this requires to know the signature of the methods of this library: .. GENERATED FROM PYTHON SOURCE LINES 69-78 .. code-block:: default distribution_1_2 = create_distribution( "x", "SPDistribution", 2, interfaced_distribution="norm", parameters={"loc": 1.0, "scale": 2.0}, ) print(distribution_1_2) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none norm(loc=1.0, scale=2.0) .. GENERATED FROM PYTHON SOURCE LINES 79-83 Plot the distribution --------------------- We can plot both cumulative and probability density functions for the first marginal: .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: default distribution_0_1.plot(show=False) # Workaround for HTML rendering, instead of ``show=True`` plt.show() .. image-sg:: /examples/uncertainty/distributions/images/sphx_glr_plot_sp_distribution_001.png :alt: Probability distribution of x(0), PDF, Cumulative density function :srcset: /examples/uncertainty/distributions/images/sphx_glr_plot_sp_distribution_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-94 .. note:: We can provide a marginal index as first argument of the :meth:`.Distribution.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 96-99 Get mean -------- We can access the mean of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: default print(distribution_0_1.mean) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0. 0.] .. GENERATED FROM PYTHON SOURCE LINES 102-105 Get standard deviation ---------------------- We can access the standard deviation of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 105-107 .. code-block:: default print(distribution_0_1.standard_deviation) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [1. 1.] .. GENERATED FROM PYTHON SOURCE LINES 108-112 Get numerical range ------------------- We can access the range, ie. the difference between the numerical minimum and maximum, of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: default print(distribution_0_1.range) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [array([-7.03448383, 7.03448691]), array([-7.03448383, 7.03448691])] .. GENERATED FROM PYTHON SOURCE LINES 115-119 Get mathematical support ------------------------ We can access the range, ie. the difference between the minimum and maximum, of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 119-121 .. code-block:: default print(distribution_0_1.support) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [array([-inf, inf]), array([-inf, inf])] .. GENERATED FROM PYTHON SOURCE LINES 122-125 Generate samples ---------------- We can generate 10 samples of the distribution: .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: default print(distribution_0_1.compute_samples(10)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[-0.44712856 -1.37311732] [ 1.2245077 0.31515939] [ 0.40349164 0.84616065] [ 0.59357852 -0.85951594] [-1.09491185 0.35054598] [ 0.16938243 -1.31228341] [ 0.74055645 -0.03869551] [-0.9537006 -1.61577235] [-0.26621851 1.12141771] [ 0.03261455 0.40890054]] .. GENERATED FROM PYTHON SOURCE LINES 128-133 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 133-135 .. code-block:: default print(distribution_0_1.compute_cdf([0.0, 1.0])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0.5 0.84134475] .. GENERATED FROM PYTHON SOURCE LINES 136-142 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 142-143 .. code-block:: default print(distribution_0_1.compute_inverse_cdf([0.5, 0.975])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [0. 1.95996398] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.197 seconds) .. _sphx_glr_download_examples_uncertainty_distributions_plot_sp_distribution.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sp_distribution.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sp_distribution.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_