.. 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 Click :ref:`here ` 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-15 .. code-block:: default from __future__ import print_function 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 16-22 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 25-30 We first create the marginals of the distribution : - a Normal distribution ; - a Gumbel distribution. .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: default marginals = [ot.Normal(), ot.Gumbel()] .. GENERATED FROM PYTHON SOURCE LINES 33-34 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 34-41 .. code-block:: default 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:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_001.png :alt: PDF of the first marginal :class: sphx-glr-multi-img * .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_002.png :alt: PDF of the second marginal :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 42-43 The CDF is also available with the `drawCDF` method. .. GENERATED FROM PYTHON SOURCE LINES 45-46 We then have the minimum required to create a bivariate distribution, assuming no dependency structure : .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: default distribution = ot.ComposedDistribution(marginals) .. GENERATED FROM PYTHON SOURCE LINES 49-50 We can draw the PDF (here in dimension 2) : .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: default graph = distribution.drawPDF() view = otv.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_003.png :alt: [X0,X1] iso-PDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-55 We also draw the CDF : .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: default graph = distribution.drawCDF() view = otv.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_004.png :alt: [X0,X1] iso-CDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-61 If a dependance between marginals is needed we have to create the copula specifying the dependency structure, here a :class:`~openturns.NormalCopula` : .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: default R = ot.CorrelationMatrix(2) R[0, 1] = 0.3 copula = ot.NormalCopula(R) print(copula) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none NormalCopula(R = [[ 1 0.3 ] [ 0.3 1 ]]) .. GENERATED FROM PYTHON SOURCE LINES 67-68 We create the bivariate distribution with the desired copula and draw it. .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: default distribution = ot.ComposedDistribution(marginals, copula) graph = distribution.drawPDF() view = otv.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_005.png :alt: [X0,X1] iso-PDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-88 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 88-95 .. code-block:: default dim = 2 distribution = ot.Normal(dim) graph = distribution.drawPDF() graph.setTitle("Bivariate standard unit gaussian PDF") view = otv.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_006.png :alt: Bivariate standard unit gaussian PDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-100 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 100-108 .. code-block:: default 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:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_007.png :alt: Bivariate Student PDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-116 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 118-119 We first generate random points in the square. .. GENERATED FROM PYTHON SOURCE LINES 119-123 .. code-block:: default distUniform2 = ot.ComposedDistribution([ot.Uniform(-1.0, 1.0)]*2) N = 100 sample = distUniform2.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 124-125 We then build the points and weights for the `UserDefined` distribution. .. GENERATED FROM PYTHON SOURCE LINES 125-131 .. code-block:: default 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 132-133 We build the distribution : .. GENERATED FROM PYTHON SOURCE LINES 133-137 .. code-block:: default distribution = ot.UserDefined(points, weights) graph = distribution.drawPDF() graph.setTitle("User defined PDF") .. GENERATED FROM PYTHON SOURCE LINES 138-139 We can draw a sample from this distribution with the `getSample` method : .. GENERATED FROM PYTHON SOURCE LINES 139-144 .. code-block:: default omega = distribution.getSample(100) cloud = ot.Cloud(omega, 'black', 'fdiamond', 'Sample from UserDefined distribution') graph.add(cloud) view = otv.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_draw_multivariate_distributions_008.png :alt: User defined PDF :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 145-146 As expected most values are near the edge of the square where the PDF is the higher. .. GENERATED FROM PYTHON SOURCE LINES 148-149 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 149-150 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.265 seconds) .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_create_draw_multivariate_distributions.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. 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-jupyter :download:`Download Jupyter notebook: plot_create_draw_multivariate_distributions.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_