.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_create_random_mixture.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_auto_probabilistic_modeling_distributions_plot_create_random_mixture.py: Create a random mixture ======================= .. GENERATED FROM PYTHON SOURCE LINES 6-12 .. code-block:: default from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 13-27 Create a mixture of distributions --------------------------------- We define an affine combination of input random variables. .. math:: Y = 2 + 5 X_1 + X_2 where: - :math:`X_1 \sim \mathcal{E}(\lambda=1.5)` - :math:`X_2 \sim \mathcal{N}(\mu=4, \sigma=1)` This notion is different from the Mixture where the combination is made on the probability density functions and not on the univariate random variable. .. GENERATED FROM PYTHON SOURCE LINES 30-31 We create the distributions associated to the input random variables : .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: default X1 = ot.Exponential(1.5) X2 = ot.Normal(4.0, 1.0) .. GENERATED FROM PYTHON SOURCE LINES 35-36 We define an offset `a0` : .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: default a0 = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 39-40 We create the `weights` : .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: default weight = [5.0, 1.0] .. GENERATED FROM PYTHON SOURCE LINES 43-44 We create the affine combination :math:`Y` : .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: default distribution = ot.RandomMixture([X1, X2], weight, a0) print(distribution) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none RandomMixture(Normal(mu = 6, sigma = 1) + Exponential(lambda = 0.3, gamma = 0)) .. GENERATED FROM PYTHON SOURCE LINES 48-49 We get its mean : .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: default mean = distribution.getMean()[0] print("Mean : %.3f"%mean) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Mean : 9.333 .. GENERATED FROM PYTHON SOURCE LINES 53-54 its variance : .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: default variance = distribution.getCovariance()[0, 0] print("Variance : %.3f"%variance) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Variance : 12.111 .. GENERATED FROM PYTHON SOURCE LINES 58-59 the 90% quantile : .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: default quantile = distribution.computeQuantile(0.9)[0] print("0.9-quantile : %.3f"%quantile) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0.9-quantile : 13.825 .. GENERATED FROM PYTHON SOURCE LINES 63-64 We can get the probability of the :math:`Y` random variable to exceed 10.0 : .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: default prb = distribution.computeSurvivalFunction(10.0) print("Probability : %.3f"%prb) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Probability : 0.315 .. GENERATED FROM PYTHON SOURCE LINES 68-69 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default graph = distribution.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_001.png :alt: plot create random mixture :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-74 We draw its CDF : .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: default graph = distribution.drawCDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_002.png :alt: plot create random mixture :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-89 Create a discrete mixture ------------------------- In this paragraph we build the distribution of the value of the sum of 20 dice rolls. .. math:: Y = \sum_{i=1}^{20} X_i where :math:`X_i \sim U(1,2,3,4,5,6)` .. GENERATED FROM PYTHON SOURCE LINES 91-92 We create the distribution associated to the dice roll : .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: default X = ot.UserDefined([[i] for i in range(1,7)]) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Let's roll the dice a few times ! .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: default sample = X.getSample(10) print(sample) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [ v0 ] 0 : [ 2 ] 1 : [ 4 ] 2 : [ 6 ] 3 : [ 6 ] 4 : [ 2 ] 5 : [ 6 ] 6 : [ 5 ] 7 : [ 2 ] 8 : [ 1 ] 9 : [ 5 ] .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: default N = 20 .. GENERATED FROM PYTHON SOURCE LINES 103-104 We create a collection of identically distributed Xi : .. GENERATED FROM PYTHON SOURCE LINES 104-106 .. code-block:: default coll = [X] * N .. GENERATED FROM PYTHON SOURCE LINES 107-108 We create the weights and an affine combination : .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default weight = [1.0] * N distribution = ot.RandomMixture(coll, weight) .. GENERATED FROM PYTHON SOURCE LINES 112-113 We compute the probability to exceed a sum of 100 after 20 dice rolls : .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: default print("Probability : %.3g"%distribution.computeComplementaryCDF(100) ) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Probability : 1.58e-05 .. GENERATED FROM PYTHON SOURCE LINES 116-117 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: default graph = distribution.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_003.png :alt: X0 PDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-122 and its CDF : .. GENERATED FROM PYTHON SOURCE LINES 122-126 .. code-block:: default graph = distribution.drawCDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_004.png :alt: X0 CDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-128 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 128-129 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.382 seconds) .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_create_random_mixture.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_create_random_mixture.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_create_random_mixture.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_