Skip to main content

Section 3.2 Summarizing a PMF

In Chapter 1 we computed the mean of a sample by adding up the elements and dividing by the number of elements. Here’s a simple example.
Listing 3.2.1. Python Code
$ seq = [1, 2, 2, 3, 5]

n = len(seq)
mean = np.sum(seq) / n
mean
np.float64(2.6)
Now suppose we compute the PMF of the values in the sequence.
Listing 3.2.2. Python Code
pmf = Pmf.from_seq(seq)
Given the Pmf, we can still compute the mean, but the process is different β€” we have to multiply the probabilities and quantities and add up the products.
Listing 3.2.3. Python Code
$ mean = np.sum(pmf.ps * pmf.qs)
mean
np.float64(2.6)
Notice that we don’t have to divide by n, because we already did that when we normalized the Pmf. Pmf objects have a mean method that does the same thing.
Listing 3.2.4. Python Code
$ pmf.mean()
np.float64(2.6)
Given a Pmf, we can compute the variance by computing the deviation of each quantity from the mean.
Listing 3.2.5. Python Code
deviations = pmf.qs - mean
Then we multiply the squared deviations by the probabilities and add up the products.
Listing 3.2.6. Python Code
$ var = np.sum(pmf.ps * deviations**2)
var
np.float64(1.84)
The var method does the same thing.
Listing 3.2.7. Python Code
$ pmf.var()
np.float64(1.84)
From the variance, we can compute the standard deviation in the usual way.
Listing 3.2.8. Python Code
$ np.sqrt(var)
np.float64(1.3564659966250536)
Or the std method does the same thing.
Listing 3.2.9. Python Code
$ pmf.std()
np.float64(1.3564659966250536)
Pmf also provides a mode method that finds the value with the highest probability.
Listing 3.2.10. Python Code
$ pmf.mode()
np.int64(2)
We’ll see more methods as we go along, but that’s enough to get started.