.. 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 :ref:`Go to the end ` 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 7-13 .. code-block:: Python 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 14-28 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 31-32 We create the distributions associated to the input random variables : .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python X1 = ot.Exponential(1.5) X2 = ot.Normal(4.0, 1.0) .. GENERATED FROM PYTHON SOURCE LINES 36-37 We define an offset `a0` : .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python a0 = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 40-41 We create the `weights` : .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python weight = [5.0, 1.0] .. GENERATED FROM PYTHON SOURCE LINES 44-45 We create the affine combination :math:`Y` : .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python distribution = ot.RandomMixture([X1, X2], weight, a0) print(distribution) .. rst-class:: sphx-glr-script-out .. code-block:: none RandomMixture(Normal(mu = 6, sigma = 1) + Exponential(lambda = 0.3, gamma = 0)) .. GENERATED FROM PYTHON SOURCE LINES 49-50 We get its mean : .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python mean = distribution.getMean()[0] print("Mean : %.3f" % mean) .. rst-class:: sphx-glr-script-out .. code-block:: none Mean : 9.333 .. GENERATED FROM PYTHON SOURCE LINES 54-55 Its variance: .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python variance = distribution.getCovariance()[0, 0] print("Variance : %.3f" % variance) .. rst-class:: sphx-glr-script-out .. code-block:: none Variance : 12.111 .. GENERATED FROM PYTHON SOURCE LINES 59-60 The 90% quantile: .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python quantile = distribution.computeQuantile(0.9)[0] print("0.9-quantile : %.3f" % quantile) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9-quantile : 13.825 .. GENERATED FROM PYTHON SOURCE LINES 64-65 We can get the probability of the :math:`Y` random variable to exceed 10.0 : .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python prb = distribution.computeSurvivalFunction(10.0) print("Probability : %.3f" % prb) .. rst-class:: sphx-glr-script-out .. code-block:: none Probability : 0.315 .. GENERATED FROM PYTHON SOURCE LINES 69-70 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python 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 74-75 We draw its CDF : .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. code-block:: Python 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 80-90 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 92-93 We create the distribution associated to the dice roll : .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python X = ot.UserDefined([[i] for i in range(1, 7)]) .. GENERATED FROM PYTHON SOURCE LINES 96-97 Let's roll the dice a few times ! .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python sample = X.getSample(10) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 ] 0 : [ 6 ] 1 : [ 6 ] 2 : [ 2 ] 3 : [ 4 ] 4 : [ 3 ] 5 : [ 2 ] 6 : [ 3 ] 7 : [ 1 ] 8 : [ 5 ] 9 : [ 5 ] .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: Python N = 20 .. GENERATED FROM PYTHON SOURCE LINES 104-105 We create a collection of identically distributed :math:`X_i` : .. GENERATED FROM PYTHON SOURCE LINES 105-107 .. code-block:: Python coll = [X] * N .. GENERATED FROM PYTHON SOURCE LINES 108-109 We create the weights and an affine combination : .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python weight = [1.0] * N distribution = ot.RandomMixture(coll, weight) .. GENERATED FROM PYTHON SOURCE LINES 113-114 We compute the probability to exceed a sum of 100 after 20 dice rolls : .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python print("Probability : %.3g" % distribution.computeComplementaryCDF(100)) .. rst-class:: sphx-glr-script-out .. code-block:: none Probability : 1.58e-05 .. GENERATED FROM PYTHON SOURCE LINES 117-118 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: Python 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 122-123 and its CDF : .. GENERATED FROM PYTHON SOURCE LINES 123-127 .. code-block:: Python 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 128-129 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 129-130 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_create_random_mixture.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_create_random_mixture.ipynb ` .. 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-zip :download:`Download zipped: plot_create_random_mixture.zip `