.. 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 linear combination of distributions ============================================ .. 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-23 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 26-27 We create the distributions associated to the input random variables .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: Python X1 = ot.Exponential(1.5) X2 = ot.Normal(4.0, 1.0) .. GENERATED FROM PYTHON SOURCE LINES 31-32 We define an offset `a0` .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python a0 = 2.0 .. GENERATED FROM PYTHON SOURCE LINES 35-36 We create the `weights` .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python weight = [5.0, 1.0] .. GENERATED FROM PYTHON SOURCE LINES 39-40 We create the affine combination :math:`Y` : .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python distribution = ot.LinearCombinationDistribution([X1, X2], weight, a0) print(distribution) .. rst-class:: sphx-glr-script-out .. code-block:: none LinearCombinationDistribution(Normal(mu = 6, sigma = 1) + Exponential(lambda = 0.3, gamma = 0)) .. GENERATED FROM PYTHON SOURCE LINES 44-45 We get its mean : .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. 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 49-50 Its variance: .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. 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 54-55 The 90% quantile: .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. 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 59-60 We can get the probability of the :math:`Y` random variable to exceed 10.0 : .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. 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 64-65 We draw its PDF : .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. 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 69-70 We draw its CDF : .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. 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 75-85 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 87-88 We create the distribution associated to the dice roll : .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python X = ot.FiniteDiscreteDistribution([[i] for i in range(1, 7)]) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Let's roll the dice a few times ! .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. 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 96-98 .. code-block:: Python N = 20 .. GENERATED FROM PYTHON SOURCE LINES 99-100 We create a collection of identically distributed :math:`X_i` .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python coll = [X] * N .. GENERATED FROM PYTHON SOURCE LINES 103-104 We create the weights and an affine combination .. GENERATED FROM PYTHON SOURCE LINES 104-107 .. code-block:: Python weight = [1.0] * N distribution = ot.LinearCombinationDistribution(coll, weight) .. GENERATED FROM PYTHON SOURCE LINES 108-109 We compute the probability to exceed a sum of 100 after 20 dice rolls .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. 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 112-113 We draw its PDF .. GENERATED FROM PYTHON SOURCE LINES 113-116 .. 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 117-118 and its CDF .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. 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 122-123 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 123-124 .. 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 `