Skip to main content

Section D.3 Difference in Standard Deviations

If men are more variable than women, we expect the standard deviation of their heights to be higher. To see if it is, all we have to do is provide a function that takes a sample and computes the difference in standard deviations.
Listing D.3.1. Python Code
def diff_stds(sample):
    """Difference in standard deviation (M minus female)

    sample: DataFrame with `_SEX` and `HTM4` columns

    returns: float
    """
    grouped_heights = sample.groupby("_SEX")["HTM4"]
    return -diff(grouped_heights.std())
Based on a single resampling, it looks like the standard deviation of heights is higher for men.
Listing D.3.2. Python Code
$ diff_stds(sample)
0.7755433526023632
We can use sampling_dist again with a different test statistic.
Listing D.3.3. Python Code
t2 = sampling_dist(brfss, diff_stds)
np.mean(t2)
Here’s the sampling distribution with its mean and 95% CI.
Listing D.3.4. Python Code
plot_sampling_distribution(t2)
plt.title("Difference in standard deviation (cm)", fontsize=14)
The 95% CI does not contain zero, which indicates that the difference is significant at the 5% level. Assuming that the tails of the distribution drop off like the tails of a normal distribution, we can estimate the p-value like this.
Listing D.3.5. Python Code
p_value(t2)
This p-value is just barely big enough to compute, but still so small we can conclude that the apparent difference is very unlikely to be due to chance.
However, comparing standard deviations might not be a sensible way to assess whether men are more variable. Men are taller, on average, so if their heights vary more in absolute terms, that doesn’t necessarily mean that they vary more in relative terms. If we think that variation relative to the mean is more relevant to the variability hypothesis, another test statistic to consider is the coefficient of variation.