
Introduction
Understanding standard deviation is a given for statisticians and data scientists, as the metric provides insight into the reliability and variability of the data being analyzed. As we’ve come to see, Python’s statistics module facilitates various statistical operations, including the calculation of standard deviation. The function pstdev() is specifically designed to compute the population standard deviation, which is the standard deviation of a complete set of data, as opposed to a sample’s.
Let’s have a look at the statistics.pstdev() function and how to use it effectively.
Using the statistics.pstdev() Function
As you have no doubt come to expect, we must first import the statistics module before using the pstdev() function:
import statistics
The pstdev() function has a simple syntax:
statistics.pstdev(data, mu=None)
The parameters are:
- data: This parameter accepts a sequence of numerical data (like a list or tuple)
- mu: This is an optional parameter representing the mean of the data; will be calcuated if not provided
Let’s take a look at a practical example.
The pstdev() function works with a variety of data structures, including lists and tuples. In the below example, the list data contains numerical values that can be used to calculate the population standard deviation. Using the pstdev() function to calculate the population standard deviation:
import statistics
# Population data
data = [10, 12, 23, 23, 16, 23, 21, 16]
# Calculate population standard deviation
population_std_dev = statistics.pstdev(data)
print("Population Standard Deviation:", population_std_dev)
# Output: # Output: 4.898979485566356
To explicitly include the population mean, you can pass the mu paramter to the function.
import statistics # Population mean mean = 18 # Population data data = [10, 12, 23, 23, 16, 23, 21, 16] # Calculate population standard deviation with provided mean population_std_dev_with_mu = statistics.pstdev(data, mu=mean) print(population_std_dev_with_mu) # Output: 4.898979485566356
Allowing for the user to supply the mean is useful if one wants to test theoretical mean values rather than the mean available with the current population, and could be useful for hypothesis testing. In extremely large datasets, it could also reduce computation time, though in the moern era in this is far less of a concern than it once was.
Calling pstdev() on an empty sequence raises a StatisticsError. Here is an example:
empty_data = []
try:
statistics.pstdev(empty_data)
except statistics.StatisticsError as e:
print("Error:", e)
Similarly, passing non-numeric types (like strings or None), Python will raise a TypeError.
What’s the key differnce between pstdev() and stdev(), you might ask? When computing the mean of a sample, the denominator is n-1, while when computing the mean of an entire population, the denominator is simply n. That’s it!
Wrapping Up
In this article, we discussed the statistics.pstdev() function, poutlined its uses, and showed how to use it. Check out the official Python documentaiton for more information.
