Visualizing the probability mass function

pmf gives us an output of the mass (probability) associated with each likely output. In the example that we looked at earlier, we have the probability associated with each value of x (although the probabilities vary depending on the value of x).

In this section, we will look at plotting the probability mass function for each possible value of x:

  1. Import the relevant packages for plotting:
import matplotlib.pyplot as plt
  1. Initialize the plots:
fig, ax = plt.subplots(1, 1)
  1. Plot the values of x along with the probability mass function associated with each value of x:
ax.plot(x, binom.pmf(x, n, p), 'bo', ms=8, label='binom pmf')
[<matplotlib.lines.Line2D at 0x681f474518>]
  1. Show the plot:
plt.show()

The preceding code results in the following output:

So far, we have looked at a random variable that has a binomial output. The code snippets in the following steps helps us to calculate the pmf of a variable that has a multinomial output:

  1. Import the relevant packages:
from scipy.stats import multinomial
  1. Initialize the values of N and the probability associated with each outcome:
rv = multinomial(8, [0.3, 0.2, 0.5])

Note that, in the preceding code, N=8 and the probabilities associated with each outcome are [0.3, 0.2, 0.5].

  1. Calculate the probability mass function for the three outcomes to appear using a given set of values:
rv.pmf([1, 3, 4])
0.042000000000000072

Note that the values [1, 3 ,4] represent the number of times the first outcome is expected to occur (once), the number of times the second outcome is expected to occur (thrice), and the number of times the third outcome is expected to occur (four times).

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

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