.. 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-11 .. code-block:: default 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 12-26 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 29-30 We create the distributions associated to the input random variables : .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: default X1 = ot.Exponential(1.5) X2 = ot.Normal(4.0, 1.0) .. GENERATED FROM PYTHON SOURCE LINES 34-35 We define an offset `a0` : .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: default a0 = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 38-39 We create the `weights` : .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: default weight = [5.0, 1.0] .. GENERATED FROM PYTHON SOURCE LINES 42-43 We create the affine combination :math:`Y` : .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. 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 47-48 We get its mean : .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. 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 52-53 its variance : .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. 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 57-58 the 90% quantile : .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. 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 62-63 We can get the probability of the :math:`Y` random variable to exceed 10.0 : .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. 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 67-68 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: default graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_001.png :alt: plot create random mixture :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-73 We draw its CDF : .. GENERATED FROM PYTHON SOURCE LINES 73-77 .. code-block:: default graph = distribution.drawCDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_002.png :alt: plot create random mixture :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-88 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 90-91 We create the distribution associated to the dice roll : .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: default X = ot.UserDefined([[i] for i in range(1, 7)]) .. GENERATED FROM PYTHON SOURCE LINES 94-95 Let's roll the dice a few times ! .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. 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 99-101 .. code-block:: default N = 20 .. GENERATED FROM PYTHON SOURCE LINES 102-103 We create a collection of identically distributed Xi : .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: default coll = [X] * N .. GENERATED FROM PYTHON SOURCE LINES 106-107 We create the weights and an affine combination : .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: default weight = [1.0] * N distribution = ot.RandomMixture(coll, weight) .. GENERATED FROM PYTHON SOURCE LINES 111-112 We compute the probability to exceed a sum of 100 after 20 dice rolls : .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. 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 115-116 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: default graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_003.png :alt: X0 PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 120-121 and its CDF : .. GENERATED FROM PYTHON SOURCE LINES 121-125 .. code-block:: default graph = distribution.drawCDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_004.png :alt: X0 CDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-127 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 127-128 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.286 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 `_