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:
- Import the relevant packages:
from scipy.stats import binom
import matplotlib.pyplot as plt
import numpy as np
- Specify the total number of experiments and the probability of an event occurring:
n=5
p=0.4
- 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.
- 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
- 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.