July 2, 2017
1 Effect Size
Examples and exercises for a tutorial on statistical inference.
Copyright 2016 Allen Downey
License: Creative Commons Attribution 4.0 International
Main question: how can we express the difference between groups
import numpy
import scipy.stats
# seed the random number generator so we all get the same results
numpy.random.seed(17)
%matplotlib inline
1
In [12]: mu1, sig1 = 178, 7.7
male_height = scipy.stats.norm(mu1, sig1)
male_height
The following function evaluates the normal (Gaussian) probability density function (PDF)
within 4 standard deviations of the mean. It takes an rv object and returns a pair of NumPy
arrays.
xs, ys = eval_pdf(female_height)
pyplot.plot(xs, ys, label='female', linewidth=4, color=COLOR3)
pyplot.xlabel('height (cm) of the popu')
None
2
Lets assume for now that those are the true distributions for the population.
Ill use rvs to generate random samples from the population distributions. Note that these are
totally random, totally representative samples, with no measurement error!
In [25]: male_sample = male_height.rvs(1000) # random sample of the popu
In [8]: female_sample = female_height.rvs(1000)
Both samples are NumPy arrays. Now we can compute sample statistics like the mean and
standard deviation.
In [9]: mean1, std1 = male_sample.mean(), male_sample.std()
mean1, std1
Out[9]: (178.16511665818112, 7.8419961712899502)
The sample mean is close to the population mean, but not exact, as expected.
In [10]: mean2, std2 = female_sample.mean(), female_sample.std()
mean2, std2
Out[10]: (163.48610226651135, 7.382384919896662)
And the results are similar for the female sample.
Now, there are many ways to describe the magnitude of the difference between these distribu-
tions. An obvious one is the difference in the means:
In [11]: difference_in_means = male_sample.mean() - female_sample.mean()
difference_in_means # in cm
Out[11]: 14.679014391669767
On average, men are 1415 centimeters taller. For some applications, that would be a good
way to describe the difference, but there are a few problems:
Without knowing more about the distributions (like the standard deviations) its hard to
interpret whether a difference like 15 cm is a lot or not.
The magnitude of the difference depends on the units of measure, making it hard to compare
across different studies.
There are a number of ways to quantify the difference between distributions. A simple option
is to express the difference as a percentage of the mean.
Exercise 1: what is the relative difference in means, expressed as a percentage?
a percentage makes it easier to compare between different studies, say, compare to beattles.
In [14]: # Solution goes here
difference_in_means / male_sample.mean()
Out[14]: 0.082389946286916566
In [15]: difference_in_means / female_sample.mean()
Out[15]: 0.089787536605040408
STOP HERE: Well regroup and discuss before you move on.
3
1.2 Part Two
An alternative way to express the difference between distributions is to see how much they over-
lap. To define overlap, we choose a threshold between the two means. The simple threshold is the
midpoint between the means:
Out[16]: 170.82560946234622
A better, but slightly more complicated threshold is the place where the PDFs cross.
Out[17]: 170.6040359174722
In this example, theres not much difference between the two thresholds.
Now we can count how many men are below the threshold:
Out[18]: 164
Out[20]: 174
The overlap is the total area under the curves that ends up on the wrong side of the thresh-
old.
Out[21]: 0.33799999999999997
Or in more practical terms, you might report the fraction of people who would be misclassified
if you tried to use height to guess sex:
Out[22]: 0.16899999999999998
4
Another way to quantify the difference between distributions is whats called probability of
superiority, which is a problematic term, but in this context its the probability that a randomly-
chosen man is taller than a randomly-chosen woman.
Exercise 2: Suppose I choose a man and a woman at random. What is the probability that the
man is taller?
In [37]: x = range(20)
y = range(20,40)
zip(x,y)
"""or"""
prob_sup/len(male_sample)
Out[45]: 0.92000000000000004
Overlap (or misclassification rate) and probability of superiority have two good properties:
As probabilities, they dont depend on units of measure, so they are comparable between
studies.
They are expressed in operational terms, so a reader has a sense of what practical effect the
difference makes.
1.2.1 Cohens d
There is one other common way to express the difference between distributions. Cohens d is the
difference in means, standardized by dividing by the standard deviation. Heres a function that
computes it:
5
In [46]: def CohenEffectSize(group1, group2):
"""Compute Cohen's d.
returns: float
"""
diff = group1.mean() - group2.mean()
Computing the denominator is a little complicated; in fact, people have proposed several ways
to do it. This implementation uses the pooled standard deviation, which is a weighted average
of the standard deviations of the two groups.
And heres the result for the difference in height between men and women.
Out[47]: 1.9698908086614964
Most people dont have a good sense of how big d = 1.9 is, so lets make a visualization to get
calibrated.
Heres a function that encapsulates the code we already saw for computing overlap and prob-
ability of superiority.
6
Heres the function that takes Cohens d, plots normal distributions with the given effect size,
and prints their overlap and superiority.
xs, ys = eval_pdf(treatment)
pyplot.fill_between(xs, ys, label='treatment', color=COLOR2, alpha=0.7
o, s = overlap_superiority(control, treatment)
print('overlap', o)
print('superiority', s)
In [50]: plot_pdfs(2)
overlap 0.311
superiority 0.93
7
And an interactive widget you can use to visualize what different values of d mean:
overlap 0.041
superiority 1.0
Because mean and standard deviation have the same units, their ratio is dimensionless, so
we can compare d across different studies.
In fields that commonly use d, people are calibrated to know what values should be consid-
ered big, surprising, or important.
Given d (and the assumption that the distributions are normal), you can compute overlap,
superiority, and related statistics.
In summary, the best way to report effect size often depends on the audience and your goals.
There is often a tradeoff between summary statistics that have good technical properties and statis-
tics that are meaningful to a general audience.
8
1.2.2 To summarize
We need to create percentange to compare the result between different groups. * Method 1: over-
lap compute a threshold using both means, calculate the misclassification rate. * Method 2:
random test prob of A > B * Method 3: compare difference of means using pooled variance. (Co-
hens d)
In [ ]: