How to do it...

The pmf of a binomial outcome is obtained in Python using a set of functions in the binom module within scipy.stats.

The steps to plot the probability mass function of a discrete random variable are as follows:

  1. Import the relevant packages:
from scipy.stats import binom
import matplotlib.pyplot as plt
import numpy as np
  1. Specify the total number of experiments and the probability of an event occurring:
n=5
p=0.4
  1. Extract the probability of an event occurring in x occasions out of the n experiments.

Define x. In our case, x can take values from 0 to the maximum (which is n):

x=np.arange(0,(n+1))
print(x)
[0 1 2 3 4 5]

Pass x through the pmf function. Note that the pmf function calculates the f(x) value as discussed in the previous section:

binom.pmf(x, n, p)
array([ 0.07776, 0.2592 , 0.3456 , 0.2304 , 0.0768 , 0.01024])

The probability of each value of x happening for the given n and p is given in the preceding output.

Now the output is obtained, let's do a few cross-checks to see if the output obtained is as per our expectations.

  1. The sum of the probabilities of all the values of x combined should be equal to 1:
np.sum(binom.pmf(x, n, p))
0.99999999999999978
  1. Check the probability of x=1 using the formula listed previously:

The output is equal to the output obtained when passing x through the pmf function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset