Hypothesis testing, beyond the dumb exercises

August 07, 2020

Statistics is hard and generally counter-intuitive; these notes regard one of the most interesting tools it can provide, hypothesis testing.

Hypothesis testing

Whenever a researcher attempts a certain claim based on data, some statistics is involved. Hypothesis testing is a theoretical framework for dealing with claims and assign them a certain reliability. Meaning that we not only want to know if data supports a claim but also with which degree of probability. Hypothesis testing is used for answering questions like: is this new medicine really effective curing a certain disease?, is the new high-speed mass transport reducing commuting time?

In this context the claim is called the hypothesis and it is paired (for convenience) with its counterpart, the null hypothesis which simply negates it.

We are playing with the following (boring) toy example:

In the USA, elementary students are found with an average QI of 100. The principal of the Springfield elementary school, Skinner, makes 30 students of his undertake a test and he finds out they have an average QI of 112.5 (with a standard deviation of 15).

Immediately, he calls the superintendent Chalmers to request more funds for his gifted pupils. But is his claim statistically correct?

Z-test

Z-test is one of the hypothesis testing available tests and it is applicable when:

  • Samples are IID (Independently, identically distributed);
  • The number of samples is large (greater than 30) or the standard deviation is known.

In our example, Skinner collected samples with average which we consider IID. Since , we do not care if is the true standard deviation or a sample one (for we should apply the T-test if is a sample standard deviation).

The Central Limit Theorem (CLT) states that:

For large enough, the random variable is distributed like a Gaussian

Here, represents the mean of the distribution generating the samples. Being the normal distribution symmetric, it naturally follows:

which is more straightforward for our purposes (because we are ultimately interested in the random variable ).

Note that the CLT works no matter what distribution generated the samples; the sample average will be always distributed as a Gaussian.

In our example, Skinner is trying to prove that the mean for QI in Springfield school is not the same as the one for the rest of young kids in the USA. His hypothesis is thus:

  • H1:

while, Chalmers (and the hypothesis testing framework) are evaluating the opposite hypothesis:

  • H0:

In fact, this framework accepts the initial hypothesis by rejecting the null hypothesis (a version of Reductio ad absurdum).

Namely, it is now Chalmers’ job to prove Skinner wrong (and negate further funding). Chalmers picks a certain level of confidence, and claims that he will discard H0 only if it has a probability below .

From the CLT equation we obtain that:

\begin{equation} \mu \sim N(\bar{X},\frac{\sigma^2}{n}) \label{eq:mudist} \end{equation} \begin{equation} Z = \frac{\mu-\bar{X}}{\sigma/\sqrt{n}} \sim N(0, 1) \label{eq:zdist} \end{equation}

The first equation gives the probability distribution function for , based on the observed sample data. The latter is the Z that gives the name to the test and represents the standardization of wrt the normal distribution with mean 0 and variance 1.

The following analysis and test is independent either we work on the or the random variable. I think the former is more intuitive and the latter a residual of pre-computer era where people used to search in precomputed tables the values needed instead of interacting with a computer. I present both approaches as the Z test is now widely accepted and discussed.

statistic

Back to our example, Skinner’s claim is that is larger than 100 and its probability of being less than 100 is negligible. Chalmers defines this negligibility as 5% of probability.

We call -value the probability under H0 to have observed our sample. It represents the likelihood of the sample under H0. In our example, the -value is the probability of having for our sampled data.

The following image presents the distribution of (the blue line) given our sample data, its average and the cut at 100, the real average according to H0. The -value is given by the area underlying the blue curve from to .

data frequencies

Computing the probability -value is hence trivial as computing the cumulative distribution function of the first equation, in Python

>>> import scipy.stats as stats
>>> stats.norm(112.5, 2.7386).cdf(100)
2.505e-06

Since -value we can reject H0 and declare that H1 holds with a confidence of 95% (and possibly higher, till 1--value). Skinner rejoices!

Z statistic

A more common way to proceed (but equivalent) with this test is using the Z statistic (the standardization of ). We first compute as Z under the H0 hypothesis:

>>> (100-112.5)*math.sqrt(30)/15
-4.564354645876384

The image below presents this time the distribution of (i.e., the distribution of the sample average given the observed sample data after standardization) which is nothing but the normal distribution with mean and variance , and the cut point which this time is not directly , but its standardization, .

data frequencies

Given and the distribution function we now hence compute the probability which is the area underlying the blue curve in the above image from to .

>> stats.norm(0,1).cdf(-4.56)
2.5051659781952214e-06

Consequently, -value . Again, since -value is less than we reject H0 in favor of H1.

T-test

The T-test has been developed to address the limitation of the Z-test regarding the sample size. It still requires the samples to be IID but it can be safely used whatever value of we have.

Everything works as in the case of the Z-test, but we focus on the statistic instead of the one:

\begin{equation} T = \frac{\mu-\bar{X}}{\sigma/\sqrt{n}} \sim T(n-1) \end{equation}

In fact, is still the distribution of the sample average given the observed sample data after standardization, but we know that for sample variance with it does not distribute anymore like a normal distribution but, instead, like a student distribution (which is also symmetric and bell-shaped).

Here, indicates the sample standard deviation and represents the t-student distribution function with degrees of freedom.

Again, we can compute the cutting value:

Again, as in the latter case of the Z-test, we compute the p-value:

>>> stats.t(29).cdf(-4.56)
4.3005975438384246e-05

(The interested reader might appreciate that for the t-student and Gaussian distributions converge and has been selected as the threshold at which the two distribution are not significantly different anymore. Hence, we can use the normal distribution and the Z-test with sample variance when ). Which yields to the same comparison, -value

Hence, Chalmers has to surrender and recognize Springfield kids result more clever than the average USA student. From here to invest more funding in their education, though, it is all another story.