.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_multivariate_discrete_urns.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_multivariate_discrete_urns.py: Multivariate discrete distributions =================================== .. GENERATED FROM PYTHON SOURCE LINES 7-23 In this example we present the multivariate discrete distributions of the library as urn models with colored balls: - :class:`~openturns.Multinomial` : draws with replacement, - :class:`~openturns.experimental.MultivariateHypergeometric` : draws without replacement, - :class:`~openturns.experimental.MultivariatePolya` : draws with reinforcement, - :class:`~openturns.FiniteDiscreteDistribution` : user-defined support. All these urn models share the same convention, mimicking :class:`~openturns.Multinomial`: only a subset of the colors has to be tracked. The untracked balls form an implicit rest category, so that in general :math:`X_1 + \dots + X_d \leq N`. The classical definition (:math:`X_1 + \dots + X_d = N`) is recovered when the tracked colors cover the whole urn. In particular the 1D case reduces to :class:`~openturns.Binomial`, :class:`~openturns.Hypergeometric` and to the beta-binomial distribution respectively. .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: Python import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 28-35 Draws with replacement: the multinomial urn -------------------------------------------------- An urn holds balls in two colors with probabilities 0.2 (red) and 0.8 (green). We draw 10 balls with replacement and count how many red and green balls we observe. Both colors are tracked and the probabilities sum to one, so we are in the classical case and red + green = 10. .. GENERATED FROM PYTHON SOURCE LINES 35-44 .. code-block:: Python distribution = ot.Multinomial(10, [0.2, 0.8]) print(distribution) sample = distribution.getSample(5) print(sample) graph = distribution.drawPDF() graph.setTitle("Multinomial urn, draws with replacement") view = otv.View(graph) view.getAxes()[0].set_aspect("equal", adjustable="box") .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_001.svg :alt: Multinomial urn, draws with replacement :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_001.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Multinomial(n = 10, p = [0.2,0.8]) 0 : [ 2 8 ] 1 : [ 4 6 ] 2 : [ 1 9 ] 3 : [ 0 10 ] 4 : [ 1 9 ] .. GENERATED FROM PYTHON SOURCE LINES 45-49 If the urn also holds blue balls with probability 0.5 that we do not track, only the red and green balls are counted: red + green <= 10. The 1D defective case reduces to the binomial distribution: the number of red balls follows Binomial(10, 0.2). .. GENERATED FROM PYTHON SOURCE LINES 49-60 .. code-block:: Python distribution_def = ot.Multinomial(10, [0.2, 0.3]) print(distribution_def) marginal = distribution_def.getMarginal(0) print(marginal) print("PDF at 2 red and 3 green balls:", distribution_def.computePDF([2, 3])) print("PDF at 2 red balls:", marginal.computePDF([2])) graph = distribution_def.drawPDF() graph.setTitle("Defective multinomial urn with untracked blue balls") view = otv.View(graph) view.getAxes()[0].set_aspect("equal", adjustable="box") .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_002.svg :alt: Defective multinomial urn with untracked blue balls :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_002.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Multinomial(n = 10, p = [0.2,0.3]) Binomial(n = 10, p = 0.2) PDF at 2 red and 3 green balls: 0.08505000000000001 PDF at 2 red balls: 0.301989888 .. GENERATED FROM PYTHON SOURCE LINES 61-67 Draws without replacement: the hypergeometric urn -------------------------------------------------- The same urn now holds 10 red and 12 green balls (22 balls in total). We draw 10 balls without replacement and track red and green. Here the tracked colors cover the whole urn, so red + green = 10. .. GENERATED FROM PYTHON SOURCE LINES 67-76 .. code-block:: Python distribution = otexp.MultivariateHypergeometric(10, [10, 12]) print(distribution) sample = distribution.getSample(5) print(sample) graph = distribution.drawPDF() graph.setTitle("Hypergeometric urn, draws without replacement") view = otv.View(graph) view.getAxes()[0].set_aspect("equal", adjustable="box") .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_003.svg :alt: Hypergeometric urn, draws without replacement :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_003.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none MultivariateHypergeometric(n = 10, colors = [10,12], total = 22) [ X0 X1 ] 0 : [ 3 7 ] 1 : [ 5 5 ] 2 : [ 6 4 ] 3 : [ 6 4 ] 4 : [ 7 3 ] .. GENERATED FROM PYTHON SOURCE LINES 77-80 If the urn also holds 13 yellow balls that we do not track, the total population is 35 while the tracked colors sum to 22: red + green <= 10. The 1D defective case reduces to the univariate hypergeometric distribution. .. GENERATED FROM PYTHON SOURCE LINES 80-94 .. code-block:: Python distribution_def = otexp.MultivariateHypergeometric(10, [10, 12], 35) hyper = ot.Hypergeometric(35, 10, 10) print(distribution_def) print("PDF at 3 red and 4 green balls:", distribution_def.computePDF([3, 4])) print( "PDF at 3 red balls:", distribution_def.getMarginal(0).computePDF([3]), hyper.computePDF([3]), ) graph = distribution_def.drawPDF() graph.setTitle("Defective hypergeometric urn with untracked yellow balls") view = otv.View(graph) view.getAxes()[0].set_aspect("equal", adjustable="box") .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_004.svg :alt: Defective hypergeometric urn with untracked yellow balls :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_004.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none MultivariateHypergeometric(n = 10, colors = [10,12], total = 35) PDF at 3 red and 4 green balls: 0.09253979678634561 PDF at 3 red balls: 0.3142182688083362 0.3142182688083362 .. GENERATED FROM PYTHON SOURCE LINES 95-101 Draws with reinforcement: the Polya urn -------------------------------------------------- The urn starts with 3 red and 4 green balls (concentrations). After each draw the ball is replaced together with ``c = 1`` additional ball of the same color. We draw 6 balls and track red and green. .. GENERATED FROM PYTHON SOURCE LINES 101-110 .. code-block:: Python distribution = otexp.MultivariatePolya(6, [3.0, 4.0], 1.0) print(distribution) sample = distribution.getSample(5) print(sample) graph = distribution.drawPDF() graph.setTitle("Polya urn, draws with reinforcement") view = otv.View(graph) view.getAxes()[0].set_aspect("equal", adjustable="box") .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_005.svg :alt: Polya urn, draws with reinforcement :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_005.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none MultivariatePolya(n = 6, alpha = [3,4], c = 1, total = 7) [ X0 X1 ] 0 : [ 2 4 ] 1 : [ 1 5 ] 2 : [ 1 5 ] 3 : [ 4 2 ] 4 : [ 2 4 ] .. GENERATED FROM PYTHON SOURCE LINES 111-113 As for the other urns, untracked colors are allowed: with a total concentration of 12 the 1D defective Polya is a beta-binomial distribution on 0..6. .. GENERATED FROM PYTHON SOURCE LINES 113-122 .. code-block:: Python distribution_def = otexp.MultivariatePolya(6, [2.0, 3.0], 1.0, 12.0) marginal = distribution_def.getMarginal(0) print(marginal) print("support size:", distribution_def.getSupport().getSize()) graph = distribution_def.drawPDF() graph.setTitle("Defective Polya urn with untracked balls") view = otv.View(graph) view.getAxes()[0].set_aspect("equal", adjustable="box") .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_006.svg :alt: Defective Polya urn with untracked balls :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_006.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none MultivariatePolya(n = 6, alpha = [2], c = 1, total = 12) support size: 28 .. GENERATED FROM PYTHON SOURCE LINES 123-131 User-defined support: the FiniteDiscreteDistribution urn --------------------------------------------------------- A single draw from an urn holding 6 red and 4 blue balls is a categorical experiment. Encoding red by 0 and blue by 1, the draw follows a :class:`~openturns.FiniteDiscreteDistribution` with weights given by the ball counts (normalized automatically). It is the single-draw version of the multinomial urn above. .. GENERATED FROM PYTHON SOURCE LINES 131-139 .. code-block:: Python single = ot.FiniteDiscreteDistribution([[0], [1]], [6.0, 4.0]) print(single) print("P(red) =", single.computePDF([0])) print("P(blue) =", single.computePDF([1])) graph = single.drawPDF() graph.setTitle("Single draw from the urn") view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_007.svg :alt: Single draw from the urn :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_multivariate_discrete_urns_007.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none FiniteDiscreteDistribution({x = [0], p = 0.6}, {x = [1], p = 0.4}) P(red) = 0.6 P(blue) = 0.4 .. GENERATED FROM PYTHON SOURCE LINES 140-141 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 141-142 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_multivariate_discrete_urns.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_multivariate_discrete_urns.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_multivariate_discrete_urns.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_multivariate_discrete_urns.zip `