.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_quick_start_guide_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_quick_start_guide_distributions.py: Quick start guide to distributions ================================== .. GENERATED FROM PYTHON SOURCE LINES 6-17 Abstract --------- In this example, we present classes for univariate and multivariate distributions. We demonstrate the probabilistic programming capabilities of the library. For univariate distributions, we show how to compute the probability density, the cumulated probability density and the quantiles. We also show how to create graphics. The :class:`~openturns.ComposedDistribution` class, which creates a distribution based on its marginals and its copula, is presented. We show how to truncate any distribution with the :class:`~openturns.TruncatedDistribution` class. .. GENERATED FROM PYTHON SOURCE LINES 19-32 Univariate distribution ----------------------- The library is a probabilistic programming library: it is possible to create a random variable and perform operations on this variable *without* generating a sample. In the OpenTURNS platform, several *univariate distributions* are implemented. The most commonly used are: - :class:`~openturns.Uniform`, - :class:`~openturns.Normal`, - :class:`~openturns.Beta`, - :class:`~openturns.LogNormal`, - :class:`~openturns.Exponential`, - :class:`~openturns.Weibull`. .. GENERATED FROM PYTHON SOURCE LINES 34-41 .. code-block:: Python import openturns.viewer as otv import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 42-46 The uniform distribution ------------------------ Let us create a uniform random variable :math:`\mathcal{U}(2,5)`. .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python uniform = ot.Uniform(2, 5) .. GENERATED FROM PYTHON SOURCE LINES 51-52 The :meth:`~openturns.Distribution.drawPDF` method plots the probability density function. .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python graph = uniform.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_001.png :alt: plot quick start guide distributions :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-59 The :meth:`~openturns.Distribution.computePDF` method computes the probability distribution at a specific point. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python uniform.computePDF(3.5) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.3333333333333333 .. GENERATED FROM PYTHON SOURCE LINES 64-65 The :meth:`~openturns.Distribution.drawCDF` method plots the cumulated distribution function. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python graph = uniform.drawCDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_002.png :alt: plot quick start guide distributions :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-72 The :meth:`~openturns.Distribution.computeCDF` method computes the value of the cumulated distribution function a given point. .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python uniform.computeCDF(3.5) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5 .. GENERATED FROM PYTHON SOURCE LINES 77-78 The :meth:`~openturns.Distribution.getSample` method generates a sample. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python sample = uniform.getSample(10) sample .. raw:: html
X0
04.468085
14.220982
24.276883
34.91059
44.557379
54.453005
63.422504
72.536824
84.410839
92.9383


.. GENERATED FROM PYTHON SOURCE LINES 84-85 The most common way to "see" a sample is to plot the empirical histogram. .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: Python sample = uniform.getSample(1000) graph = ot.HistogramFactory().build(sample).drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_003.png :alt: X0 PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-94 Multivariate distributions with or without independent copula ------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 96-100 We can create multivariate distributions by two different methods: - we can also create a multivariate distribution by combining a list of univariate marginal distribution and a copula, - some distributions are defined as multivariate distributions: :class:`~openturns.Normal`, :class:`~openturns.Dirichlet`, :class:`~openturns.Student`. .. GENERATED FROM PYTHON SOURCE LINES 102-103 Define a multivariate Normal distribution in dimension 4 .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: Python distribution = ot.Normal(4) distribution .. raw:: html
Normal


.. GENERATED FROM PYTHON SOURCE LINES 109-112 Since the method based on a marginal and a copula is more flexible, we illustrate below this principle. In the following script, we define a bivariate distribution made of two univariate distributions (Gaussian and uniform) and an independent copula. The second input argument of the :class:`~openturns.ComposedDistribution` class is optional: if it is not specified, the copula is independent by default. .. GENERATED FROM PYTHON SOURCE LINES 114-119 .. code-block:: Python normal = ot.Normal() uniform = ot.Uniform() distribution = ot.ComposedDistribution([normal, uniform]) distribution .. raw:: html
ComposedDistribution
Index Variable Distribution
0 X0 Normal(mu = 0, sigma = 1)
1 X1 Uniform(a = -1, b = 1)


.. GENERATED FROM PYTHON SOURCE LINES 120-121 We can also use the :class:`~openturns.IndependentCopula` class. .. GENERATED FROM PYTHON SOURCE LINES 123-129 .. code-block:: Python normal = ot.Normal() uniform = ot.Uniform() copula = ot.IndependentCopula(2) distribution = ot.ComposedDistribution([normal, uniform], copula) distribution .. raw:: html
ComposedDistribution
Index Variable Distribution
0 X0 Normal(mu = 0, sigma = 1)
1 X1 Uniform(a = -1, b = 1)


.. GENERATED FROM PYTHON SOURCE LINES 130-131 We see that this produces the same result: in the end of this section, we will change the copula and see what happens. .. GENERATED FROM PYTHON SOURCE LINES 133-134 The :meth:`~openturns.Distribution.getSample` method produces a sample from this distribution. .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: Python distribution.getSample(10) .. raw:: html
X0X1
00.42377520.03731124
10.1448278-0.9660635
21.273868-0.5232126
31.3841880.9786282
40.53662510.720757
51.2830380.8316031
60.6371329-0.8106468
7-0.1327306-0.6646291
8-1.113346-0.1037191
9-0.1808409-0.7662679


.. GENERATED FROM PYTHON SOURCE LINES 139-140 In order to visualize a bivariate sample, we can use the :class:`~openturns.Cloud` class. .. GENERATED FROM PYTHON SOURCE LINES 142-149 .. code-block:: Python sample = distribution.getSample(1000) showAxes = True graph = ot.Graph("X0~N, X1~U", "X0", "X1", showAxes) cloud = ot.Cloud(sample, "blue", "fsquare", "") # Create the cloud graph.add(cloud) # Then, add it to the graph view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_004.png :alt: X0~N, X1~U :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 150-151 We see that the marginals are Gaussian and uniform and that the copula is independent. .. GENERATED FROM PYTHON SOURCE LINES 153-155 Define a plot a copula ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 157-158 The :class:`~openturns.NormalCopula` class allows one to create a Gaussian copula. Such a copula is defined by its correlation matrix. .. GENERATED FROM PYTHON SOURCE LINES 160-165 .. code-block:: Python R = ot.CorrelationMatrix(2) R[0, 1] = 0.6 copula = ot.NormalCopula(R) copula .. raw:: html
NormalCopula


.. GENERATED FROM PYTHON SOURCE LINES 166-167 We can draw the contours of a copula with the :meth:`~openturns.NormalCopula.drawPDF` method. .. GENERATED FROM PYTHON SOURCE LINES 169-172 .. code-block:: Python graph = copula.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_005.png :alt: [X0,X1] iso-PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 173-175 Multivariate distribution with arbitrary copula ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 177-179 Now that we know that we can define a copula, we create a bivariate distribution with normal and uniform marginals and an arbitrary copula. We select the Ali-Mikhail-Haq copula as an example of a non trivial dependence. .. GENERATED FROM PYTHON SOURCE LINES 181-188 .. code-block:: Python normal = ot.Normal() uniform = ot.Uniform() theta = 0.9 copula = ot.AliMikhailHaqCopula(theta) distribution = ot.ComposedDistribution([normal, uniform], copula) distribution .. raw:: html
ComposedDistribution
Index Variable Distribution
0 X0 Normal(mu = 0, sigma = 1)
1 X1 Uniform(a = -1, b = 1)


.. GENERATED FROM PYTHON SOURCE LINES 189-196 .. code-block:: Python sample = distribution.getSample(1000) showAxes = True graph = ot.Graph("X0~N, X1~U, Ali-Mikhail-Haq copula", "X0", "X1", showAxes) cloud = ot.Cloud(sample, "blue", "fsquare", "") # Create the cloud graph.add(cloud) # Then, add it to the graph view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_006.png :alt: X0~N, X1~U, Ali-Mikhail-Haq copula :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 197-198 We see that the sample is quite different from the previous sample with independent copula. .. GENERATED FROM PYTHON SOURCE LINES 200-202 Draw several distributions in the same plot ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 204-205 It is sometimes convenient to create a plot presenting the PDF and CDF on the same graphics. This is possible thanks to Matplotlib. .. GENERATED FROM PYTHON SOURCE LINES 207-214 .. code-block:: Python beta = ot.Beta(5, 7, 9, 10) pdfbeta = beta.drawPDF() cdfbeta = beta.drawCDF() exponential = ot.Exponential(3) pdfexp = exponential.drawPDF() cdfexp = exponential.drawCDF() .. GENERATED FROM PYTHON SOURCE LINES 217-224 .. code-block:: Python grid = ot.GridLayout(2, 2) grid.setGraph(0, 0, pdfbeta) grid.setGraph(0, 1, cdfbeta) grid.setGraph(1, 0, pdfexp) grid.setGraph(1, 1, cdfexp) view = otv.View(grid) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_007.png :alt: plot quick start guide distributions :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_quick_start_guide_distributions_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 225-244 Truncate a distribution ----------------------- Any distribution can be truncated with the :class:`~openturns.TruncatedDistribution` class. Let :math:`f_X` (resp. :math:`F_X`) the PDF (resp. the CDF) of the real random variable :math:`X`. Let :math:`a` and :math:`b` two reals with :math:`a` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_quick_start_guide_distributions.py `