.. 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 6-13 .. code-block:: Python 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 14-22 A continuous distribution ------------------------- We build a normal distribution with parameters : .. math:: \mu = 2.2, \sigma = 0.6 .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. 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 26-27 We can draw a sample following this distribution with the `getSample` method : .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python size = 10 sample = distribution.getSample(size) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ X0 ] 0 : [ 2.90698 ] 1 : [ 2.4694 ] 2 : [ 2.37417 ] 3 : [ 2.69831 ] 4 : [ 2.28606 ] 5 : [ 2.08412 ] 6 : [ 2.87742 ] 7 : [ 1.80004 ] 8 : [ 1.67943 ] 9 : [ 2.99115 ] .. GENERATED FROM PYTHON SOURCE LINES 32-33 We draw its PDF and CDF : .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. 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.png :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.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-47 .. 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.png :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.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 48-52 A discrete distribution ----------------------- We define a geometric distribution with parameter :math:`p = 0.7`. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. 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 57-58 We draw a sample of it : .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python size = 10 sample = distribution.getSample(size) print(sample) .. rst-class:: sphx-glr-script-out .. code-block:: none [ X0 ] 0 : [ 3 ] 1 : [ 1 ] 2 : [ 1 ] 3 : [ 2 ] 4 : [ 1 ] 5 : [ 2 ] 6 : [ 1 ] 7 : [ 1 ] 8 : [ 1 ] 9 : [ 2 ] .. GENERATED FROM PYTHON SOURCE LINES 63-64 We draw its PDF and its CDF : .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. 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.png :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.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. 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.png :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.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 74-79 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 81-82 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 82-83 .. 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 `