.. 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 6-7 In this example we create and draw multidimensional distributions. .. GENERATED FROM PYTHON SOURCE LINES 7-14 .. code-block:: Python import openturns as ot import openturns.viewer as otv from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 15-21 Create a multivariate model with `ComposedDistribution` ------------------------------------------------------- In this paragraph we use :math:`~openturns.ComposedDistribution` class to build multidimensional distribution described by its marginal distributions and optionally its dependence structure (a particular copula). .. GENERATED FROM PYTHON SOURCE LINES 24-29 We first create the marginals of the distribution : - a Normal distribution ; - a Gumbel distribution. .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python marginals = [ot.Normal(), ot.Gumbel()] .. GENERATED FROM PYTHON SOURCE LINES 32-33 We draw their PDF. We recall that the `drawPDF` command just generates the graph data. It is the viewer module that enables the actual display. .. GENERATED FROM PYTHON SOURCE LINES 33-40 .. code-block:: Python graphNormalPDF = marginals[0].drawPDF() graphNormalPDF.setTitle("PDF of the first marginal") graphGumbelPDF = marginals[1].drawPDF() graphGumbelPDF.setTitle("PDF of the second marginal") view = otv.View(graphNormalPDF) view = otv.View(graphGumbelPDF) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_001.png :alt: PDF of the first marginal :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_002.png :alt: PDF of the second marginal :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 41-42 The CDF is also available with the `drawCDF` method. .. GENERATED FROM PYTHON SOURCE LINES 44-45 We then have the minimum required to create a bivariate distribution, assuming no dependency structure : .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python distribution = ot.ComposedDistribution(marginals) .. GENERATED FROM PYTHON SOURCE LINES 48-49 We can draw the PDF (here in dimension 2) : .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python graph = distribution.drawPDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_003.png :alt: [X0,X1] iso-PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-54 We also draw the CDF : .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. code-block:: Python graph = distribution.drawCDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_004.png :alt: [X0,X1] iso-CDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-60 If a dependence between marginals is needed we have to create the copula specifying the dependency structure, here a :class:`~openturns.NormalCopula` : .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: Python R = ot.CorrelationMatrix(2) R[0, 1] = 0.3 copula = ot.NormalCopula(R) print(copula) .. rst-class:: sphx-glr-script-out .. code-block:: none NormalCopula(R = [[ 1 0.3 ] [ 0.3 1 ]]) .. GENERATED FROM PYTHON SOURCE LINES 66-67 We create the bivariate distribution with the desired copula and draw it. .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python distribution = ot.ComposedDistribution(marginals, copula) graph = distribution.drawPDF() view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_005.png :alt: [X0,X1] iso-PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-87 Multivariate models ------------------- Some models in the library are natively multivariate. We present examples of three of them : - the Normal distribution ; - the Student distribution ; - the UserDefined distribution. The Normal distribution ^^^^^^^^^^^^^^^^^^^^^^^ The :class:`~openturns.Normal` distribution is natively multivariate. Here we define a bivariate standard unit gaussian distribution and display its PDF. .. GENERATED FROM PYTHON SOURCE LINES 87-94 .. code-block:: Python dim = 2 distribution = ot.Normal(dim) graph = distribution.drawPDF() graph.setTitle("Bivariate standard unit gaussian PDF") view = otv.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_006.png :alt: Bivariate standard unit gaussian PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 95-99 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 99-107 .. 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_007.png :alt: Bivariate Student PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 108-115 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 117-118 We first generate random points in the square. .. GENERATED FROM PYTHON SOURCE LINES 118-122 .. code-block:: Python distUniform2 = ot.ComposedDistribution([ot.Uniform(-1.0, 1.0)] * 2) N = 100 sample = distUniform2.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 123-124 We then build the points and weights for the `UserDefined` distribution. .. GENERATED FROM PYTHON SOURCE LINES 124-130 .. 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 131-132 We build the distribution : .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. code-block:: Python distribution = ot.UserDefined(points, weights) graph = distribution.drawPDF() graph.setTitle("User defined PDF") .. GENERATED FROM PYTHON SOURCE LINES 137-138 We can draw a sample from this distribution with the `getSample` method : .. GENERATED FROM PYTHON SOURCE LINES 138-143 .. 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_008.png :alt: User defined PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 144-145 As expected most values are near the edge of the square where the PDF is the higher. .. GENERATED FROM PYTHON SOURCE LINES 147-148 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 148-149 .. 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 `