.. 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-11 .. code-block:: Python import openturns as ot import openturns.viewer as otv .. 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:: Python 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:: Python a0 = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 38-39 We create the `weights` .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python 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:: 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 47-48 We get its mean : .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. 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 52-53 Its variance: .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. 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 57-58 The 90% quantile: .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. 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 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:: 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 67-68 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: Python graph = distribution.drawPDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_001.svg :alt: plot create random mixture :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_001.svg :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:: Python graph = distribution.drawCDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_002.svg :alt: plot create random mixture :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_002.svg :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:: Python 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:: Python sample = X.getSample(10) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 ] 0 : [ 1 ] 1 : [ 3 ] 2 : [ 4 ] 3 : [ 4 ] 4 : [ 5 ] 5 : [ 3 ] 6 : [ 2 ] 7 : [ 3 ] 8 : [ 4 ] 9 : [ 5 ] .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: Python N = 20 .. GENERATED FROM PYTHON SOURCE LINES 102-103 We create a collection of identically distributed :math:`X_i` .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: Python 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:: Python 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:: 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 115-116 We draw its PDF .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: Python graph = distribution.drawPDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_003.svg :alt: X0 PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 120-121 and its CDF .. GENERATED FROM PYTHON SOURCE LINES 121-124 .. code-block:: Python graph = distribution.drawCDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_004.svg :alt: X0 CDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_random_mixture_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 125-126 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 126-127 .. code-block:: Python otv.View.ShowAll() .. _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 `