Skip to main content

Section D.5 Difference in Robust CV

As an alternative to the coefficient of variation, we can use a more robust measure of variability. One option is the ratio of the median absolute deviation (MAD) to the median. Hereโ€™s a function that computes it.
Listing D.5.1. Python Code
def robust_cv(series):
    """Median absolute deviation.

    series: Series of numbers

    returns: float
    """
    m = series.median()
    deviations = series - m
    return deviations.abs().median() / m
The following function computes the difference in robust CVs. The first line jitters the dataโ€”otherwise we would get the same results from every resampling.
Listing D.5.2. Python Code
def diff_robust_cv(sample):
    """Difference in robust CV (M minus female)

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

    returns: float
    """
    sample["HTM4"] += np.random.normal(0, 1, size=len(sample))
    grouped_heights = sample.groupby("_SEX")["HTM4"]
    rcvs = grouped_heights.apply(robust_cv)
    return -diff(rcvs)
The robust CV of the sample is negative, which suggests that womenโ€™s heights are more variable.
Listing D.5.3. Python Code
diff_robust_cv(sample)
Hereโ€™s the sampling distribution.
Listing D.5.4. Python Code
t4 = sampling_dist(brfss, diff_robust_cv)
np.mean(t4)
Listing D.5.5. Python Code
plot_sampling_distribution(t4, format_str="0.4f")
plt.title("Difference in robust CV", fontsize=14)
And the p-value.
Listing D.5.6. Python Code
p_value(t4)
According to the robust CV, women are more variable, but again the difference is so small it has no practical consequences.