.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/distributions/discrete_1d/plot_distribution_discrete_1D.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_distributions_discrete_1d_plot_distribution_discrete_1D.py: Distribution Discrete 1D example ================================= This examples shows the use of the univariate discrete distributions class. In particular: .. GENERATED FROM PYTHON SOURCE LINES 9-12 - How to define one of the univariate discrete distributions supported by UQpy - How to extract the moments of the distribution - How to draw random samples from the distribution .. GENERATED FROM PYTHON SOURCE LINES 17-18 Import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: default import matplotlib.pyplot as plt from UQpy.distributions.collection.Binomial import Binomial .. GENERATED FROM PYTHON SOURCE LINES 26-33 Example of a 1D discrete distribution -------------------------------------- Define a univariate binomial distribution. By using the :code:`__bases__` attribute we can verify that the Binomial distribution extends the :class:`DistributionDiscrete1D` baseclass, while in order to define the :class:`Binomial` distribution, two parameters are required, namely, :code:`n` and :code:`p`. .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: default print(Binomial.__bases__) dist = Binomial(n=5, p=0.4) .. rst-class:: sphx-glr-script-out .. code-block:: none (,) .. GENERATED FROM PYTHON SOURCE LINES 42-46 Print the mean, standard deviation, skewness, and kurtosis of the distribution. ------------------------------------------------------------------------------- Using the moments method existing in all univariate distributions, the user can retrieve the available moments. The order in which the moments are extracted can be seen in the moments_list variable. .. GENERATED FROM PYTHON SOURCE LINES 49-56 .. code-block:: default moments_list = ['mean', 'variance', 'skewness', 'kurtosis'] m = dist.moments() print('Moments with inherited parameters:') for i, moment in enumerate(moments_list): print(moment + ' = {0:.2f}'.format(m[i])) .. rst-class:: sphx-glr-script-out .. code-block:: none Moments with inherited parameters: mean = 2.00 variance = 1.20 skewness = 0.18 kurtosis = -0.37 .. GENERATED FROM PYTHON SOURCE LINES 57-64 Generate 5000 random samples from the binomial distribution. ------------------------------------------------------------- The number of samples is provided as `nsamples` (default :math:`1`). The user can fix the seed of the pseudo random generator via input random_state. Important: the output of rvs is a `(nsamples, 1)` ndarray. .. GENERATED FROM PYTHON SOURCE LINES 67-81 .. code-block:: default y1 = dist.rvs(nsamples=5000) print('Shape of output provided by rvs is (nsamples, dimension), i.e. here:') print(y1.shape) plt.hist(y1[:, 0], bins=50) plt.xlabel('x') plt.ylabel('count') plt.show() .. image-sg:: /auto_examples/distributions/discrete_1d/images/sphx_glr_plot_distribution_discrete_1D_001.png :alt: plot distribution discrete 1D :srcset: /auto_examples/distributions/discrete_1d/images/sphx_glr_plot_distribution_discrete_1D_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of output provided by rvs is (nsamples, dimension), i.e. here: (5000, 1) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.064 seconds) .. _sphx_glr_download_auto_examples_distributions_discrete_1d_plot_distribution_discrete_1D.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/distributions/discrete_1d/plot_distribution_discrete_1D.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_distribution_discrete_1D.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_distribution_discrete_1D.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_