.. 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_and_draw_scalar_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_and_draw_scalar_distributions.py: Create and draw scalar distributions ==================================== .. GENERATED FROM PYTHON SOURCE LINES 7-11 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 12-20 A continuous distribution ------------------------- We build a Normal distribution with parameters: .. math:: \mu = 2.2, \sigma = 0.6 .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python distribution = ot.Normal(2.2, 0.6) print(distribution) .. rst-class:: sphx-glr-script-out .. code-block:: none Normal(mu = 2.2, sigma = 0.6) .. GENERATED FROM PYTHON SOURCE LINES 24-25 We can draw a sample following this distribution with the `getSample` method : .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python size = 10 sample = distribution.getSample(size) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ X0 ] 0 : [ 2.56492 ] 1 : [ 1.4403 ] 2 : [ 1.93704 ] 3 : [ 2.92329 ] 4 : [ 0.891169 ] 5 : [ 2.41003 ] 6 : [ 1.987 ] 7 : [ 3.06235 ] 8 : [ 2.6864 ] 9 : [ 2.67589 ] .. GENERATED FROM PYTHON SOURCE LINES 30-31 We draw its PDF and CDF : .. GENERATED FROM PYTHON SOURCE LINES 31-37 .. code-block:: Python graphPDF = distribution.drawPDF() graphPDF.setTitle( r"PDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$" ) view = viewer.View(graphPDF) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_001.svg :alt: PDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$ :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-45 .. code-block:: Python graphCDF = distribution.drawCDF() graphCDF.setTitle( r"CDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$" ) view = viewer.View(graphCDF) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_002.svg :alt: CDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$ :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-50 A discrete distribution ----------------------- We define a geometric distribution with parameter :math:`p = 0.7`. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python p = 0.7 distribution = ot.Geometric(p) print(distribution) .. rst-class:: sphx-glr-script-out .. code-block:: none Geometric(p = 0.7) .. GENERATED FROM PYTHON SOURCE LINES 55-56 We draw a sample of it : .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python size = 10 sample = distribution.getSample(size) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ X0 ] 0 : [ 1 ] 1 : [ 1 ] 2 : [ 1 ] 3 : [ 2 ] 4 : [ 3 ] 5 : [ 1 ] 6 : [ 2 ] 7 : [ 1 ] 8 : [ 4 ] 9 : [ 1 ] .. GENERATED FROM PYTHON SOURCE LINES 61-62 We draw its PDF and its CDF : .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: Python graphPDF = distribution.drawPDF() graphPDF.setTitle(r"PDF of a geometric distribution with parameter $p = 0.7$") view = viewer.View(graphPDF) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_003.svg :alt: PDF of a geometric distribution with parameter $p = 0.7$ :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: Python graphCDF = distribution.drawCDF() graphCDF.setTitle(r"CDF of a geometric distribution with parameter $p = 0.7$") view = viewer.View(graphCDF) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_004.svg :alt: CDF of a geometric distribution with parameter $p = 0.7$ :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_create_and_draw_scalar_distributions_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-77 Conclusion ---------- The two previous examples look very similar despite their continuous and discrete nature. In the library there is no distinction between continuous and discrete distributions. .. GENERATED FROM PYTHON SOURCE LINES 79-80 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 80-81 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_create_and_draw_scalar_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_and_draw_scalar_distributions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_create_and_draw_scalar_distributions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_create_and_draw_scalar_distributions.zip `