Note
Go to the end to download the full example code or to run this example in your browser via Binder
Distribution Discrete 1D example
This examples shows the use of the univariate discrete distributions class. In particular:
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
Import the necessary modules.
import matplotlib.pyplot as plt
from UQpy.distributions.collection.Binomial import Binomial
Example of a 1D discrete distribution
Define a univariate binomial distribution.
By using the __bases__ attribute we can verify that the Binomial distribution extends the
DistributionDiscrete1D baseclass, while in order to define the Binomial distribution, two
parameters are required, namely, n and p.
(<class 'UQpy.distributions.baseclass.DistributionDiscrete1D.DistributionDiscrete1D'>,)
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.
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]))
Moments with inherited parameters:
mean = 2.00
variance = 1.20
skewness = 0.18
kurtosis = -0.37
Generate 5000 random samples from the binomial distribution.
The number of samples is provided as nsamples (default \(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.

Shape of output provided by rvs is (nsamples, dimension), i.e. here:
(5000, 1)
Total running time of the script: ( 0 minutes 0.066 seconds)