A random variable is a variable whose possible values are numerical outcomes of a random phenomenon.
A discrete random variable is one which may take on only a countable number of distinct values and thus can be quantified.
For example, you can define a random variable X to be the number which comes up when you roll a fair dice. X can take values : [1,2,3,4,5,6] and therefore is a discrete random variable.
The probability distribution of a discrete random variable is a list of probabilities associated with each of its possible values. It is also sometimes called the probability function or the probability mass function.
Some examples of discrete probability distributions are Bernoulli distribution, Binomial distribution, Poisson distribution.
Let’s now see how to work out these distributions using Python:

Importing required libraries

import scipy.stats
import seaborn as sns


1. Bernoulli Distribution : A Bernoulli distribution has only two possible outcomes, namely 1 (success) and 0 (failure), and a single trial, for example, a coin toss.
The Bernoulli distribution is a special case of the binomial distribution where a single trial is conducted (n=1). The size argument decides the number of times to repeat the trials.

Generating Bernoulli distribution using bernoulli.rvs() method from scipy.stats module and plotting histogram of the distribution using distplot() from seaborn library

bernoulli_dist = scipy.stats.bernoulli.rvs(p=0.8,size=10000)

bernoulli_dist_plot = sns.distplot(bernoulli_dist ,
                  bins=30,
                  kde=False,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
bernoulli_dist_plot.set(xlabel='Bernoulli Distribution', ylabel='Frequency')
## [Text(0, 0.5, 'Frequency'), Text(0.5, 0, 'Bernoulli Distribution')]
bernoulli_dist_plot                  


It is visible from the histogram that there are only two possible outcomes.


2. Binomial Distribution : A distribution where only two outcomes are possible, such as success or failure, win or lose and where the probability of success and failure is same for all the trials is called a Binomial Distribution.
The parameters of a binomial distribution are n and p where n is the total number of trials, and p is the probability of success in each trial. The size argument decides the number of times to repeat the trials.

Generating Binomial distribution using binom.rvs() method from scipy.stats module and plotting histogram of the distribution using distplot() from seaborn library


binomial_dist = scipy.stats.binom.rvs(n=3,p=0.8,size=10000)

binomial_dist_plot = sns.distplot(binomial_dist,
                  bins=30,
                  kde=False,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
binomial_dist_plot.set(xlabel='Binomial Distribution', ylabel='Frequency')
## [Text(0, 0.5, 'Frequency'), Text(0.5, 0, 'Binomial Distribution')]
binomial_dist_plot


Since probability of success was greater than 0.5 the distribution is skewed towards the right side.


3. Poisson Distribution : Poisson random variable is typically used to model the number of times an event happened in a time interval. For example, the number of users visited on a website in an interval can be thought of a Poisson process. Poisson distribution is described in terms of the rate (mu) at which the events happen. The size argument decides the number of random variates in the distribution.

Generating Poission distribution using poisson.rvs() method from scipy.stats module and plotting histogram of the distribution using distplot() from seaborn library. In distplot() function, bins stands for number of bins in histogram, color specifies color of histogram, kde is used for specifying density plot and hist_kws argument is for linewidth option. xlabel and ylabel arguments are to set labels for x and y axis.

poisson_dist = scipy.stats.poisson.rvs(mu=3,size=10000)

poisson_dist_plot = sns.distplot(poisson_dist,
                  bins=30,
                  kde=False,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
poisson_dist_plot.set(xlabel='Poisson Distribution', ylabel='Frequency')
## [Text(0, 0.5, 'Frequency'), Text(0.5, 0, 'Poisson Distribution')]
poisson_dist_plot