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.

print(Binomial.__bases__)
dist = Binomial(n=5, p=0.4)
(<class 'UQpy.distributions.baseclass.DistributionDiscrete1D.DistributionDiscrete1D'>,)

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.

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()
plot distribution discrete 1D
Shape of output provided by rvs is (nsamples, dimension), i.e. here:
(5000, 1)

Total running time of the script: ( 0 minutes 0.064 seconds)

Gallery generated by Sphinx-Gallery