.. 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_draw_multivariate_distributions.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_draw_multivariate_distributions.py: Create and draw multivariate distributions ========================================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 In this example we create and draw multidimensional distributions. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python import openturns as ot import openturns.viewer as otv from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 13-35 Create a multivariate model with a :class:`~openturns.JointDistribution` ------------------------------------------------------------------------ In this paragraph we use :class:`~openturns.JointDistribution` class to build a multivariate distribution of dimension :math:`\inputDim`, from: - :math:`\inputDim` scalar distributions whose cumulative distribution functions are denoted by :math:`(F_1, \dots, F_\inputDim)`, called the *instrumental marginals*, - and a core :math:`K` which is a multivariate distribution of dimension :math:`\inputDim` whose range is included in :math:`[0,1]^\inputDim`. First case: the core is a copula ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In this case, we use a core which is a copula. Thus, the instrumental marginals are the marginals of the multivariate distribution. We first create the marginals of the distribution: - a :class:`~openturns.Normal` distribution; - a :class:`~openturns.Gumbel` distribution. We use a :class:`~openturns.ClaytonCopula` as dependence structure. .. GENERATED FROM PYTHON SOURCE LINES 35-40 .. code-block:: Python marginals = [ot.Normal(), ot.Gumbel()] theta = 2.0 cop = ot.ClaytonCopula(theta) distribution = ot.JointDistribution(marginals, cop) .. GENERATED FROM PYTHON SOURCE LINES 41-45 We can check here that the instrumental marginals really are the marginal distributions. In the following graphs, we draw the instrumental marginals and the real marginals, obtained with the method :meth:`~openturns.Distribution.getMarginal`. First, we draw the probability density functions of each component. .. GENERATED FROM PYTHON SOURCE LINES 45-57 .. code-block:: Python graph_PDF_0 = marginals[0].drawPDF() graph_PDF_0.add(distribution.getMarginal(0).drawPDF()) graph_PDF_0.setLegends(["instrumental marg", "marg"]) graph_PDF_0.setTitle("First component") view = otv.View(graph_PDF_0) graph_PDF_1 = marginals[1].drawPDF() graph_PDF_1.add(distribution.getMarginal(1).drawPDF()) graph_PDF_1.setLegends(["instrumental marg", "marg"]) graph_PDF_1.setTitle("Second component") view = otv.View(graph_PDF_1) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_001.svg :alt: First component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_001.svg :class: sphx-glr-multi-img * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_002.svg :alt: Second component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_002.svg :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 58-59 Then, we draw the cumulative distribution functions. .. GENERATED FROM PYTHON SOURCE LINES 59-71 .. code-block:: Python graph_CDF_0 = marginals[0].drawCDF() graph_CDF_0.add(distribution.getMarginal(0).drawCDF()) graph_CDF_0.setLegends(["instrumental marg", "marg"]) graph_CDF_0.setTitle("First component") view = otv.View(graph_CDF_0) graph_CDF_1 = marginals[1].drawCDF() graph_CDF_1.add(distribution.getMarginal(1).drawCDF()) graph_CDF_1.setLegends(["instrumental marg", "marg"]) graph_CDF_1.setTitle("Second component") view = otv.View(graph_CDF_1) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_003.svg :alt: First component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_003.svg :class: sphx-glr-multi-img * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_004.svg :alt: Second component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_004.svg :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 72-74 At last, we check that the copula of the multivariate distribution is the specified core which was a copula. .. GENERATED FROM PYTHON SOURCE LINES 74-87 .. code-block:: Python cop_dist = distribution.getCopula() graph_cop = cop_dist.drawPDF() # Get the Contour Drawable's actual implementation from the Graph # produced by drawPDF in order to access all its methods contour_cop = cop.drawPDF().getDrawable(1).getImplementation() contour_cop.setLineStyle("dashed") # Remove the colorbar contour_cop.setColorBarPosition("") graph_cop.add(contour_cop) # Add the contour without a colorbargraph_cop.add(cop.drawPDF()) graph_cop.setTitle("Distribution copula and core") view = otv.View(graph_cop) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_005.svg :alt: Distribution copula and core :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-89 We can draw the PDF of the bivariate distribution: .. GENERATED FROM PYTHON SOURCE LINES 89-92 .. code-block:: Python graph = distribution.drawPDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_006.svg :alt: X0 iso-PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-94 We also draw the CDF : .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: Python graph = distribution.drawCDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_007.svg :alt: X0 iso-CDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_007.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 99-110 Second case: the core is not a copula ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In this case, we use a core which is not a copula. Thus, the instrumental marginals are not the marginals of the multivariate distribution. We first create the instrumental marginals of the distribution: - a :class:`~openturns.Normal` distribution; - a :class:`~openturns.Gumbel` distribution. We use a :class:`~openturns.Dirichlet` as the core. .. GENERATED FROM PYTHON SOURCE LINES 110-114 .. code-block:: Python inst_marginals = [ot.Normal(), ot.Gumbel()] core_dir = ot.Dirichlet([2.0, 1.5, 2.5]) distribution = ot.JointDistribution(inst_marginals, core_dir) .. GENERATED FROM PYTHON SOURCE LINES 115-119 We can check here that the instrumental marginals are not the marginal distributions. In the following graphs, we draw the instrumental marginals and the real marginals, obtained with the method :meth:`~openturns.Distribution.getMarginal`. First, we draw the probability density functions of each component. .. GENERATED FROM PYTHON SOURCE LINES 119-131 .. code-block:: Python graph_PDF_0 = inst_marginals[0].drawPDF() graph_PDF_0.add(distribution.getMarginal(0).drawPDF()) graph_PDF_0.setLegends(["instrumental marg", "marg"]) graph_PDF_0.setTitle("First component") view = otv.View(graph_PDF_0) graph_PDF_1 = inst_marginals[1].drawPDF() graph_PDF_1.add(distribution.getMarginal(1).drawPDF()) graph_PDF_1.setLegends(["instrumental marg", "marg"]) graph_PDF_1.setTitle("Second component") view = otv.View(graph_PDF_1) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_008.svg :alt: First component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_008.svg :class: sphx-glr-multi-img * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_009.svg :alt: Second component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_009.svg :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 132-133 Then, we draw the cumulative distribution functions. .. GENERATED FROM PYTHON SOURCE LINES 133-145 .. code-block:: Python graph_CDF_0 = inst_marginals[0].drawCDF() graph_CDF_0.add(distribution.getMarginal(0).drawCDF()) graph_CDF_0.setLegends(["instrumental marg", "marg"]) graph_CDF_0.setTitle("First component") view = otv.View(graph_CDF_0) graph_CDF_1 = inst_marginals[1].drawCDF() graph_CDF_1.add(distribution.getMarginal(1).drawCDF()) graph_CDF_1.setLegends(["instrumental marg", "marg"]) graph_CDF_1.setTitle("Second component") view = otv.View(graph_CDF_1) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_010.svg :alt: First component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_010.svg :class: sphx-glr-multi-img * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_011.svg :alt: Second component :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_011.svg :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 146-147 At last, we check that the copula of the multivariate distribution is not the specified core. .. GENERATED FROM PYTHON SOURCE LINES 147-161 .. code-block:: Python cop_dist = distribution.getCopula() graph_cop = cop_dist.drawPDF() cop_dist_draw = graph_cop.getDrawable(1) levels = cop_dist_draw.getLevels() graph_core = core_dir.drawPDF() core_draw = graph_core.getDrawable(0).getImplementation() core_draw.setColorBarPosition("") core_draw.setLevels(levels) core_draw.setLineStyle("dashed") graph_cop.add(core_draw) graph_cop.setTitle("Distribution copula and core") view = otv.View(graph_cop) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_012.svg :alt: Distribution copula and core :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_012.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 162-163 We can draw the PDF of the bivariate distribution. .. GENERATED FROM PYTHON SOURCE LINES 163-166 .. code-block:: Python graph = distribution.drawPDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_013.svg :alt: X0 iso-PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_013.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 167-168 We also draw the CDF. .. GENERATED FROM PYTHON SOURCE LINES 168-171 .. code-block:: Python graph = distribution.drawCDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_014.svg :alt: X0 iso-CDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_014.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 172-186 Use some native multivariate models ----------------------------------- Some models in the library are natively multivariate. We present examples of three of them: - the :class:`~openturns.Normal` distribution, - the :class:`~openturns.Student` distribution, - the :class:`~openturns.UserDefined` distribution. The Normal distribution ^^^^^^^^^^^^^^^^^^^^^^^ The :class:`~openturns.Normal` distribution is natively multivariate. Here we define a bivariate standard unit Normal distribution and display its PDF. .. GENERATED FROM PYTHON SOURCE LINES 186-193 .. code-block:: Python dim = 2 distribution = ot.Normal(dim) graph = distribution.drawPDF() graph.setTitle("Bivariate standard unit Normal PDF") view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_015.svg :alt: Bivariate standard unit Normal PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_015.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 194-198 The Student distribution ^^^^^^^^^^^^^^^^^^^^^^^^ The :class:`~openturns.Student` distribution is natively multivariate. Here we define a Student distribution in dimension 2 and display its PDF : .. GENERATED FROM PYTHON SOURCE LINES 198-206 .. code-block:: Python dim = 2 R = ot.CorrelationMatrix(dim) R[1, 0] = -0.2 distribution = ot.Student(4, [0.0, 1.0], [1.0, 1.0], R) graph = distribution.drawPDF() graph.setTitle("Bivariate Student PDF") view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_016.svg :alt: Bivariate Student PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_016.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 207-214 The UserDefined distribution ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ We can also define our own distribution with the :class:`~openturns.UserDefined` distribution. For instance consider the square :math:`[-1,1] \times [-1, 1]` with some random points uniformly drawn. For each point the weight chosen is the square of the distance to the origin. The :class:`~openturns.UserDefined` class normalizes the weights. .. GENERATED FROM PYTHON SOURCE LINES 216-217 We first generate random points in the square. .. GENERATED FROM PYTHON SOURCE LINES 217-221 .. code-block:: Python distUniform2 = ot.JointDistribution([ot.Uniform(-1.0, 1.0)] * 2) N = 100 sample = distUniform2.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 222-223 We then build the points and weights for the `UserDefined` distribution. .. GENERATED FROM PYTHON SOURCE LINES 223-229 .. code-block:: Python points = [] weights = [] for i in range(N): points.append(sample[i, :]) weights.append((sample[i, 0] ** 2 + sample[i, 1] ** 2) ** 2) .. GENERATED FROM PYTHON SOURCE LINES 230-231 We build the distribution : .. GENERATED FROM PYTHON SOURCE LINES 231-235 .. code-block:: Python distribution = ot.UserDefined(points, weights) graph = distribution.drawPDF() graph.setTitle("User defined PDF") .. GENERATED FROM PYTHON SOURCE LINES 236-237 We can generate and display a sample from this distribution. .. GENERATED FROM PYTHON SOURCE LINES 237-242 .. code-block:: Python omega = distribution.getSample(100) cloud = ot.Cloud(omega, "black", "fdiamond", "Sample from UserDefined distribution") graph.add(cloud) view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_017.svg :alt: User defined PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_017.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 243-244 As expected most values are near the edge of the square where the PDF is the higher. .. GENERATED FROM PYTHON SOURCE LINES 246-247 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 247-248 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_create_draw_multivariate_distributions.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_draw_multivariate_distributions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_create_draw_multivariate_distributions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_create_draw_multivariate_distributions.zip `