📚 The CoCalc Library - books, templates and other resources
License: OTHER
Hypothesis Testing
Credits: Forked from CompStats by Allen Downey. License: Creative Commons Attribution 4.0 International.
Part One
As an example, let's look at differences between groups. The example I use in Think Stats is first babies compared with others. The first
module provides code to read the data into three pandas Dataframes.
The apparent effect we're interested in is the difference in the means. Other examples might include a correlation between variables or a coefficient in a linear regression. The number that quantifies the size of the effect, whatever it is, is the "test statistic".
For the first example, I extract the pregnancy length for first babies and others. The results are pandas Series objects.
The actual difference in the means is 0.078 weeks, which is only 13 hours.
The null hypothesis is that there is no difference between the groups. We can model that by forming a pooled sample that includes first babies and others.
Then we can simulate the null hypothesis by shuffling the pool and dividing it into two groups, using the same sizes as the actual sample.
The result of running the model is two NumPy arrays with the shuffled pregnancy lengths:
Then we compute the same test statistic using the simulated data:
If we run the model 1000 times and compute the test statistic, we can see how much the test statistic varies under the null hypothesis.
Here's the sampling distribution of the test statistic under the null hypothesis, with the actual difference in means indicated by a gray line.
The p-value is the probability that the test statistic under the null hypothesis exceeds the actual value.
In this case the result is about 15%, which means that even if there is no difference between the groups, it is plausible that we could see a sample difference as big as 0.078 weeks.
We conclude that the apparent effect might be due to chance, so we are not confident that it would appear in the general population, or in another sample from the same population.
Part Two
We can take the pieces from the previous section and organize them in a class that represents the structure of a hypothesis test.
HypothesisTest
is an abstract parent class that encodes the template. Child classes fill in the missing methods. For example, here's the test from the previous section.
Now we can run the test by instantiating a DiffMeansPermute object:
And we can plot the sampling distribution of the test statistic under the null hypothesis.
As an exercise, write a class named DiffStdPermute
that extends DiffMeansPermute
and overrides TestStatistic
to compute the difference in standard deviations. Is the difference in standard deviations statistically significant?
Now let's run DiffMeansPermute again to see if there is a difference in birth weight between first babies and others.
In this case, after 1000 attempts, we never see a sample difference as big as the observed difference, so we conclude that the apparent effect is unlikely under the null hypothesis. Under normal circumstances, we can also make the inference that the apparent effect is unlikely to be caused by random sampling.
One final note: in this case I would report that the p-value is less than 1/1000 or 0.001. I would not report that p=0, because the apparent effect is not impossible under the null hypothesis; just unlikely.