Multivariate data analysis (STAT-H400)

2. Hypothesis testing for univariate data

Adrien Foucart

Introduction

What is this module about ?

The problem with randomness

The key question when looking at a sample:

…is it random, or does it show some nonrandom phenomenon?

The problem with randomness

One way to look at the problem: let’s take a bunch of random samples of the same size and compare them to the one we observed. Is our observation similar to the random samples?

The problem with randomness

Statistical tests try to give a quantitative measure of how (un)likely it is to observe a sample, given some assumptions about the data.

Example: if the data comes from a uniform discrete distribution \(U \in [10, 15]\), and we have a sample size of 30, then we expect on average to see 5 observations for each value.

Let’s define a distance measure \(d=\sum_{i=10}^{15} |n_i-5|\), where \(n_i\) is the number of items with value \(i\). How does the observed sample compare with the random samples? Is it likely to also be random?

The problem with randomness

Statistical tests try to give a quantitative measure of how (un)likely it is to observe a sample, given some assumptions about the data.

Drawing lots of random samples from a known distribution is easy, so let’s try to see how often \(d_{random} < d_{obs}\). In our sample, \(d_{obs} = 8\):

d_obs = 8
n_lower = 0
for i in range(1_000_000):
    sample = np.random.randint(10, 16, size=30)
    n_per_bin = [sum(sample==v) 
                for v in range(10, 16)]
    d = sum(np.abs(n-5) for n in n_per_bin)
    if d < d_obs:
        n_lower += 1
print(n_lower/1_000_000)

Result: 22% of random samples are closer to the “expected value” than our observed sample.

The problem with randomness

Result: 22% of random samples are closer to the “expected value” than our observed sample.

What should we conclude?

  1. This proves that our sample comes from a random uniform distribution.
  2. This proves that our sample doesn’t come from a random uniform distribution.
  3. This shows that it is likely that our sample comes from a random uniform distribution.
  4. This shows that it is possible that our sample comes from a random uniform distribution.

We can rarely prove things with this kind of “hypothesis testing”.

What we’ve shown is that we cannot reject the hypothesis that the sample comes from a uniform distribution, as most random samples in our simulations diverge more from the theoretical distribution than the one we observed.

We cannot say, however, that it is likely that our sample comes from such a distribution, at least without additional information. There could be many other distributions from which this sample could come.

So the best answer would be 4: it is possible that the sample comes from a random uniform distribution or, to put it another way, it is not unlikely.

Chebyshev’s Inequality

This example was a “naive” implementation of the idea of measuring the difference between a sample and a distribution. If we want to arrive at something more formal, we need to go back to some important concepts in probabilities, starting with Chebyshev’s Inequality.

Given a random variable \(X\) from a population with finite mean \(\mu = E[X]\) and variance \(\sigma^2 = Var[X]\)… how likely is it that our observation deviates from the mean by a certain amount \(c > 0\)?

\(\rightarrow\) Chebyshev’s Inequality:

\[P(|X-\mu| \geq c) \leq \frac{\sigma^2}{c^2}\]

Consequence: Weak Law of Large Numbers: if we observe independent and identically distributed random variables \(X_1, X_2, ..., X_n\) from a distribution with finite mean \(\mu\) and variance \(\sigma^2\), then \(\bar{X} = \frac{1}{n}\sum_{i=1}^n X_i \approx \mu\) if n is large.

Chebyshev’s Inequality

Consequence: Weak Law of Large Numbers: if we observe independent and identically distributed random variables \(X_1, X_2, ..., X_n\) from a distribution with finite mean \(\mu\) and variance \(\sigma^2\), then \(\bar{X} = \frac{1}{n}\sum_{i=1}^n X_i \approx \mu\) if n is large.

Proof:

\(E[\bar{X}] = \frac{1}{n} \sum_{i=1}^n E[X_i]\)

\(E[\bar{X}] = \frac{1}{n} \sum_{i=1}^n \mu\)

\(E[\bar{X}] = \mu\)

\(Var[\bar{X}] = Var[\frac{1}{n}\sum_{i=1}^n X_i]\)
\(Var[\bar{X}] = \frac{1}{n^2} Var[\sum_{i=1}^n X_i]\)
\(Var[\bar{X}] = \frac{1}{n^2} \sum_{i=1}^n Var[X_i]\)
\(Var[\bar{X}] = \frac{1}{n^2} \sum_{i=1}^n \sigma^2\)
\(Var[\bar{X}] = \frac{n \sigma^2}{n^2}\)
\(Var[\bar{X}] = \frac{\sigma^2}{n}\)

From Chebyshev: \(P(|\bar{X}-\mu| \geq c) \leq \frac{\sigma^2}{n c^2} \rightarrow 0\) as \(n \rightarrow \infty\)

Why is \(Var[aX] = a^2 Var[X]\) ?

\(Var[aX] = E[(aX)^2]-(E[ax])^2\)
\(Var[aX] = a^2(E[X^2]-E[x^2]) = a^2 Var[X]\)

This first important result gives us a mathematical justification for the intuition we saw in the experiment at the end of module 1: as we increase the sample size, the distribution of the sample mean \(\bar{X}\) becomes narrower, as there is a smaller probability that the sample mean deviates from the population mean.

Chebyshev’s Inequality

\[P(|X-\mu| \geq c) \leq \frac{\sigma^2}{c^2}\]

If we set \(c = k \sigma\), then: \(P(|X-\mu| \geq k\sigma) \leq \frac{1}{k^2}\)

Upper bound on the proportion of any distribution that is not included in an interval of \(k\) times the standard deviation around the mean:

k \(P(|X-\mu| \geq k \sigma)\)
1 \(\leq 1\)
2 \(\leq 0.25\)
3 \(\leq \frac{1}{9}\)
4 \(\leq \frac{1}{16}\)

\(\Rightarrow\) The probability to observe a value not in the interval \(\mu \pm 3 \sigma\) for any distribution is less than \(\frac{1}{9}\).

Chebyshev’s Inequality

Putting the two results together, we can infer bounds on the probability that the value of the sample mean deviates from the population mean that are valid for any distribution with finite mean and variance:

\(P(|\bar{X}-\mu| \geq c) \leq \frac{\sigma^2}{n c^2}\)

\(P(|\bar{X}-\mu| \geq k\sigma) \leq \frac{\sigma^2}{n k^2 \sigma^2}\)

\(P(|\bar{X}-\mu| \geq k\sigma) \leq \frac{1}{n k^2}\)

k \ n 10 100 1000
1 \(\leq 0.1\) \(\leq 0.01\) \(\leq 0.001\)
2 \(\leq 0.025\) \(\leq 0.0025\) \(\leq 0.00025\)
3 \(\leq \frac{1}{90}\) \(\leq \frac{1}{900}\) \(\leq \frac{1}{9000}\)

This is also useful if we take the problem the other way around: what sample size do we need to be reasonably certain that the sample mean will be close to the population mean?

We can see here that, with just a sample size of 100, we have a very low probability (\(< 1\%\)) of finding a sample mean further than one standard deviation from the population mean.

Distributions

Gaussian / Normal distribution

\(N(63, 7^2)\), with the mean and standard deviation marked.

As a reminder, the probability density function (PDF) \(f_X(x)\) of a random variable \(X\) with a continuous distribution describes the probability that a sample drawn from that distribution has a value that falls into a given interval, with \(P(a \leq X \leq b) = \int_a^bf_X(x)dx\).

The cumulative density function (CDF) \(F(a)\) gives us the probability that a sample has a value below \(a\), with \(F_X(a) = P(X \leq a) = \int_{-\infty}^af_X(x)dx\).

Sampling distribution of the mean

Remember the experiment with the means of samples drawn from a Gaussian ?

\(N(63, 7^2)\) with histogram of means of 10.000 samples of 20 randomly drawn items.

\(N(63, 7^2)\) with histogram of means of 10.000 samples of 2000 randomly drawn items.

Sampling distribution of the mean

The distribution of \(\bar{X}\) is called the sampling distribution of the mean: the distribution of the means of random samples of size \(n\) drawn from a population distribution. If that distribution is normal (like in our example) \(\sim N(\mu, \sigma^2)\), then the sampling distribution will also be normal, with the same mean \(\mu\) and with variance \(\frac{\sigma^2}{n}\).

N \(\sigma^2\) population \(\frac{\sigma^2}{n}\) theory \(\sigma^2\) sampling observed
20 \(7^2\) 2.45 2.44
2000 \(7^2\) 0.0245 0.0246

What if the original distribution is not normal ?

Experiment

One sample from continuous uniform distribution: \(U([60, 67])\):

Size 20 sample from a uniform distribution

Size 2000 sample from a uniform distribution

Experiment

Sampling distribution of the mean:

10.000 sample means, size 20 sample from a uniform distribution, with normal distribution in blue.

10.000 sample means, size 2000 sample from a uniform distribution, with normal distribution in blue.

Experiment

Variance of a continuous uniform distribution:
\(Var[X \sim U_{a,b}] = \sigma^2 = \frac{(b-a)^2}{12} \rightarrow Var[X \sim U_{60, 67}] = \frac{49}{12} = 4.083\)

N \(\sigma^2\) population \(\frac{\sigma^2}{n}\) theory \(\sigma^2\) sampling observed
20 4.083 0.2042 0.2106
2000 4.083 0.0020 0.0020

Even though the original distribution is no longer normal, the sampling distribution is still normal, and the variance is still \(\frac{\sigma^2}{n}\) !

Central Limit Theorem

If the sample size \(n\) is large enough, then the sampling distribution of the mean \(\bar{X}\) approximates a normal distribution, whatever the population distribution \(X\). More formally:

Let \(X_1, X_2, ..., X_n\) be a random sample from an arbitrary distribution: \(\frac{\bar{X}-E[\bar{X}]}{Var[\bar{X}]}\) converges to \(N(0, 1^2)\) for \(n \rightarrow \infty\).

Where \(\frac{\bar{X}-E[\bar{X}]}{Var[\bar{X}]}\) is the standardized sample mean. As a consequence, we can consider that \(\bar{X} \sim N(E[\bar{X}], Var[\bar{X}]) = N(\mu, \frac{\sigma^2}{n})\) if \(n\) is large enough.

Quick quizz

We are doing an experiment where we take \(n\) patients from the general population and measure their inspiratory reserve volume (IRV). If we have a large number of patients \(n\)…

  1. We can expect the variance of our IRV sample to become very small.
  2. We can expect the IRV distribution in our sample to follow a normal shape.
  3. Both of the above.
  4. None of the above.

Quick quizz

We are doing an experiment where we take \(n\) patients from the general population and measure their inspiratory reserve volume (IRV). If we have a large number of patients \(n\)…

None of the above.

  1. The variance of the IRV sample will approach the variance of the population.
  2. The IRV distribution in the sample will approach the distribution shape in the population.

What the CLT tells us is that the mean value of our sample can be seen as drawn from a normal distribution \(N(\mu, \frac{\sigma^2}{n})\) where \(\mu\) is the population mean and \(\sigma^2\) the population variance.

Sampling distribution of the variance

One sample of size 20 drawn from \(N(63, 7^2)\). Sample variance \(s^2 = 35.4\).

What if, instead of looking at the sampling distribution of the means, we look at the sampling distribution of the variance ?

For one sample \(x\) of size \(n\): \(s^2 = \frac{\sum_{i=1}^n(x_i - \bar{x})^2}{n-1}\)

Sampling distribution of the variance

Distribution of the sample variance for samples of size 20 and 2000 drawn from \(N(63, 7^2)\).

Population variance: \(\sigma^2 = 7^2 = 49\). Like with the sample mean, increasing \(n\) gives us a narrower distribution centered on the population value. But is the shape of the distribution normal ?

Chi-Square distribution

The distribution of \(S^2\) if \(X_i\) are normal (and identical and independently distributed) is related to the chi-square distribution at \(\nu\) degrees of freedom: \(\chi_{\nu}^2 = \sum_{i=1}^{\nu} Z_i^2\) where \(Z \sim N(0, 1^2)\):

\[\frac{(n-1)S^2}{\sigma^2} \sim \chi_{n-1}^2\]

Family of \(\chi^2\) functions for different degrees of freedom.

Chi-Square distribution

Distribution of the sample variance \(S^2\) for samples of size 20 and 2000 drawn from \(N(63, 7^2)\).

Distribution of \(S^2\) for samples of size 20 and 2000 drawn from \(N(63, 7^2)\), with the probability density function of \(\chi_{n-1}^2\) (scaled by \(\frac{\sigma^2}{n-1}\)).

We could see the same thing by looking at the distributions of \(\frac{n-1}{\sigma^2}S^2\), which will directly follow the \(\chi^2_{n-1}\) distributions.

To get the diagram on the right, we need to plot \(g(x) = \frac{n-1}{\sigma^2} f(\frac{n-1}{\sigma^2}x)\) where \(f(x)\) is the probability density function (pdf) of the \(\chi^2_{n-1}\) distribution and \(g(x)\) is the pdf of \(S^2\).

This comes from the change of variables formula:

If \(f(x)\) is the pdf of random variable \(X\) and \(g(y)\) is the pdf of random variable \(Y = r(X)\), then \(g(y) = f(r^{-1}(y))|\frac{d}{dy}r^{-1}(y)|\), with \(g^{-1}\) the inverse function of \(g\).

In our case: \(Y = S^2 = \frac{\sigma^2}{n-1}\chi^2_{n-1}\)

\(\Rightarrow r(X) = \frac{\sigma^2}{n-1}X\)
\(\Rightarrow r^{-1}(Y) = \frac{n-1}{\sigma^2}Y\)
\(\Rightarrow \frac{d}{dy}r^{-1}(y) = \frac{n-1}{\sigma^2}\)
\(\Rightarrow g(y) = f(\frac{n-1}{\sigma^2}y)|\frac{n-1}{\sigma^2}|\)

Chi-Square distribution

Properties of the \(\chi^2\) distribution:

Family of \(\chi^2\) distributions for different degrees of freedom.

Why is \(E[\chi^2_\nu] = \nu\)?

From the definition of the distributino: \(E[\chi^2_\nu] = E[\sum_{i=1}^\nu Z_i^2]\)

\(\Rightarrow E[\chi^2_\nu] = \sum_{i=1}^\nu E[Z_i^2]\)

\(\Rightarrow E[\chi^2_\nu] = \sum_{i=1}^\nu 1 = \nu\)

Student’s t distribution

We’ve previously shown that the sampling distribution of the mean \(\bar{X}\), for a random variable \(X\) with mean \(\mu\) and variance \(\sigma^2\), follows a normal distribution with mean \(\mu\) and variance \(\frac{\sigma^2}{n}\).

Or, to put it another way, that \(\frac{\bar{X}-\mu}{\sigma/\sqrt{n}} \sim N(0, 1^2)\)…

…but we typically don’t know the population variance \(\sigma^2\), so the question is: what happens when we use the sample variance \(s^2\) instead ?

Student’s t distribution

\(\frac{\bar{X}-\mu}{S/\sqrt{n}} \sim t_{n-1}\), where \(t_\nu\) is Student’s t distribution with \(\nu\) degrees of freedom.

It can be reformulated as: \(t_\nu = \frac{Z}{\sqrt{V_\nu/\nu}} = Z\frac{\sqrt{\nu}}{V_\nu}\) with \(Z \sim N(0, 1^2), V_\nu \sim \chi_\nu^2\).

Family of Student’s t distributions for different degrees of freedom.

We will see later in the hypothesis testing part why this is important. If we use the sample variance as a proxy for the population variance when trying to make inference about the population from the sample, and we assume that the sample mean follows a normal rather than a Student distribution, we may come to incorrect conclusions.

Snedecor-Fisher’s F-distribution

Arises in situations when we want to compare two unknown variances.

Let \(U \sim \chi_m^2\) and \(V \sim \chi_n^2\), the F distribution is defined as:

\[F(m, n) = \frac{U/m}{V/n}\]

With \((m,n)\) degrees of freedom.

Family of F distributions for different degrees of freedom.

This distribution is shown for reference because it arises in the variance tests, but we shouldn’t spend too much time on it.

Key lessons

Estimators and Confidence Intervals

Inference

A sample gives us a truncated view of the population… but it’s the best view we have.

Statistical inference is the process of making statements about a population based on a sample drawn from that population. It typically comes in two flavours: estimating an unknown parameter of the population, and testing a hypothesis about an unknown parameter of the population.

Inference

Estimation of a parameter

Testing a hypothesis

These statements can often be made “informally” from “just looking at the data”… but if we want some measure of confidence in the correctness of these statements, then we need something better !

Point estimators

An estimator is a statistic that gives the approximate value to a parameter of a population, using a sample. There can be several different estimators for the same population parameter.

Example: the sample median and the sample mean may both be used as estimators of \(\mu\), the population mean.

Point estimators

Two important properties of an estimator: the bias and the variance. Let \(\hat{\theta}\) be an estimator of the population parameter \(\theta\). We have:

\[Bias(\hat{\theta}) = E[\hat{\theta}] - \theta\]

\[Var[\hat{\theta}] = E[\hat{\theta}-E(\hat{\theta})]^2\]

The lower the bias, the more accurate the estimator is. The lower the variance, the more precise (or reliable) it is.

Note that we previously talked about the accuracy and precision of measurements. Here we have the same concept, but applied to estimators.

The concepts are the same, but we are indeed talking about two different things. We could have perfectly precise and accurante measurements (i.e. the values in the sample are perfectly measured), but a biased estimator (the statistic that we compute is biased relative to the true value of the parameter).

Confidence intervals

Practically, we only have a single sample to work with, so we only have a single estimated value for a parameter… which doesn’t tell us much about the reliability of that measure.

Instead of a single point, it’s therefore very useful to determine an interval \([L, U]\) such that the interval includes \(\theta\) with a probability of at least \(1-\alpha\):

\[P(L \leq \theta \leq U) \geq 1-\alpha\]

Where \(L, U\) are statistics calculated from the sample \(X_1, X_2, ..., X_n\). \([L, U]\) is called the \(100(1-\alpha)\%\) confidence interval (CI) and \(1-\alpha\) is the confidence level.

Confidence intervals

\[P(L \leq \theta \leq U) \geq 1-\alpha\]

How do we determine \(L\) and \(U\)? We need to make some assumptions about the sampling distribution of the estimated parameter.

Example: if we are using the sample mean \(\bar{X}\) as an estimator for the parameter \(\mu\) of a distribution with finite variance and mean, we know that \(\bar{X} \sim N(\mu, \frac{\sigma^2}{n})\) if \(n\) is large.

Therefore: \(Z := \frac{\bar{X}-\mu}{\sigma/\sqrt{n}} \sim N(0, 1^2)\)

Confidence intervals

Therefore: \(Z := \frac{\bar{X}-\mu}{\sigma/\sqrt{n}} \sim N(0, 1^2)\)

From the properties of the standard normal distribution, we know that 95% of the values are in the interval \([-1.96, 1.96]\). So:

\[P(-1.96 \leq Z \leq 1.96) = 0.95\] \[P(-1.96 \leq \frac{\bar{X}-\mu}{\sigma/\sqrt{n}} \leq 1.96) = 0.95\] \[P(\bar{X}-1.96\frac{\sigma}{\sqrt{n}} \leq \mu \leq \bar{X}+1.96\frac{\sigma}{\sqrt{n}}) = 0.95\]

\[CI_{95} = [\bar{X} \pm 1.96 \frac{\sigma}{\sqrt{n}}]\]

Confidence intervals

Example: drawing a random sample of size \(n=20\) from \(N(63, 7^2)\), we compute the sample mean \(\bar{X}\) and \(CI_{95} = [\bar{X} \pm 1.96 \frac{\sigma}{\sqrt{n}}] = [\bar{X} \pm 3.07]\):

Sample where the population mean \(\mu\) is outside the CI (happens 5% of the time).

Sample where the population mean \(\mu\) is inside the CI (happens 95% of the time).

Confidence intervals

…but we often don’t know \(\sigma^2\).

Confidence intervals

…but we often don’t know \(\sigma^2\).

If \(n\) is large, however, the sample variance can be used as an accurate estimator of the population variance, so : \(CI_{95} = [\bar{X} \pm 1.96 \frac{s}{\sqrt{n}}]\).

If we can’t make this approximation, however, we have another option, which is to use Student’s t distribution instead:

\[\frac{\bar{X}-\mu}{S/\sqrt{n}} \sim t_{n-1}\]

Confidence intervals

\[\frac{\bar{X}-\mu}{S/\sqrt{n}} \sim t_{n-1}\]

So we are looking for \(c_1, c_2\) such that \(P(c_1 \leq t_{n-1} \leq c_2) = 0.95\). Since \(t_{n_1}\) is symmetrical and is a known distribution, we can look up the values (in tables or using statistical libraries such as scipy).

Keeping our example, if \(n = 20\), we have \(\frac{\bar{X}-\mu}{S/\sqrt{n}} \sim t_{19}\), and from the known properties of \(t\): \(P(-2.093 \leq t_{19} \leq 2.093) = 0.95\).

And therefore: \(CI_{95} = [\bar{X} \pm 2.093 \frac{S}{\sqrt{n}}]\).

Why is the CI wider than in our previous estimate?

In our previous estimate, we knew the population variance, so we could be more precise about our estimation of the sample mean distribution.

Here, we add uncertainty about the variance itself, so we need to make the CI bigger to take that uncertainty into account.

Confidence intervals

Important note about the interpretation of the confidence interval:

It is incorrect to say that, after computing a 95% CI, “there is 95% probability that the true value is within the CI”.

What the “95% CI” means is that, if we repeated the same experiment an infinite amount of time, drawing samples of size \(n\) and using always the same method for computing the CI… then 95% of the time the CI would include the true value. We therefore say that “we are 95% confident that the CI contains the true value”.

Confidence interval

Example simulation: looking at how often the “true mean” is outside the CI using (a) a normal distribution, knowing the true variance, (b) a normal distribution, using the estimated sample variance, (c) a student distribution, using the estimated sample variance.

distribution = norm(loc=63, scale=7)
n = 20
oks_normal_true_s = []
oks_normal_s = []
oks_student_s = []
for _ in range(100_000):
    X = distribution.rvs(n)
    m = np.mean(X)
    s = np.std(X, ddof=1)
    
    cia = (m-1.96*(7/np.sqrt(n)), m+1.96*(7/np.sqrt(n)))
    cib = (m-1.96*(s/np.sqrt(n)), m+1.96*(s/np.sqrt(n)))
    cic = (m-2.093*(s/np.sqrt(n)) , m+2.093*(s/np.sqrt(n)) )
    
    oks_normal_true_s.append(63 >= cia[0] and 63 <= cia[1])
    oks_normal_s.append(63 >= cib[0] and 63 <= cib[1])
    oks_student_s.append(63 >= cic[0] and 63 <= cic[1])

print(f"Normal (true s): {sum(oks_normal_true_s)/100_000}")
print(f"Normal (estimated s): {sum(oks_normal_s)/100_000}")
print(f"Student (estimated s): {sum(oks_student_s)/100_000}")
Normal (true s): 0.95064
Normal (estimated s): 0.93629
Student (estimated s): 0.95097

Reading

From Verma et al., 2021: results of the MoNuSAC challenge.

Based on the reported CI, would you consider that we can be confident that TIA-Lab’s results are better than those of SJTU-426?

Those results tell us that there is a very small overlap between the two CIs, with TIA-Lab between [0.595-0.629] and SJTU-426 between [0.560-0.598].

For SJTU to be, in reality, equal or better than TIA, it would however require both their scores to be close to the edge of their respective CI, which becomes very unlikely.

To really answer the question, however, we would be better off using a statistical test, which will more directly answer: is TIA better than SJTU.

Key lessons

Hypothesis testing

Hypothesis tests

A hypothesis in this context is a statement or a claim that we want to assess based on sample data.

Examples of hypotheses:

Hypothesis tests

With each hypothesis comes an alternative hypothesis, or counterclaim. The two competing hypothesis are called the null hypothesis (or \(H_0\)) and the alternative hypothesis (or \(H_1\)).

The typical process of hypothesis testing is:

Hypothesis tests

\(H_0\) and \(H_1\) should (ideally) be fixed before collecting the sample(s). They are determined based on a theory, a model, an understanding of the studied process, an objective to be attained (e.g. target specification of a product)…

Hypothesis tests can never disprove entirely \(H_0\), but they can show that it is very unlikely to be true (and therefore \(H_1\) is very likely to be true).

To accept or reject the hypothesis, we have to define a test statistic and critical constants, such that the test rejects \(H_0\) when the statistic value is outside the region bounded by the critical constants.

Practically, \(H_0\) and \(H_1\) are often determined while analyzing the data, with hypotheses made during a preliminary visual examination of the data, and then tested more rigorously before drawing conclusions.

Proceeding like that, however, weakens the conclusions that can be made with the tests. At the very least, it’s important to have some idea of the type of things we want to test beforehand. Otherwise, we may run into the pitfall of “playing with the data until we find something significant”, which is dangerous: if we look at enough different things, we will eventually find something “statistically significant”, as we’ll establish later when talking about p-values.

Experiment

We are observing a phenomenon where the value in the population, in reality, follows a normal distribution: \(X \sim N(63, 7^2)\)… but we don’t know that. Current knowledge of the domain hypothesizes that the average value is 65, with unknown variance (\(H_0\) conditions).

We take a random sample of size 50 and observe \(\bar{x} = 62.8\), \(s = 6.9\). How (un)likely is it to get such a sample under our \(H_0\) ?

Experiment

Under our \(H_0\), \(\frac{\bar{x}-\mu}{s/\sqrt{n}} \sim t_{n-1}\), with \(\mu = 65\), \(\bar{x} = 62.8\), \(s = 6.9\), \(n=50\).

So the observed value of the statistic is \(\frac{62.8-65}{6.8/\sqrt{50}} = -2.2\).

As \(t_{n-1}\) is a known symmetric distribution, we can determine the probability to encounter a value as far from the mean as 2.2:

\(P(|v| \geq 2.2) + P(-|v| \leq -2.2) = 2P(-|v| \leq -2.2) = 0.029\)

distribution = norm(loc=63, scale=7)
t_dist = t(df=49)
hypothetic_m = 65
sample = distribution.rvs(size=50, random_state=1)
m = np.mean(sample)
s = np.std(sample)
v = (m-hypothetic_m)/(s/np.sqrt(50))
p = 2*t_dist.cdf(-np.abs(v))

This tells us that there is a very low probability to observe a sample mean of 62.8 under \(H_0\) with a sample size of \(50\).

However, it doesn’t tell us anything about what the true mean is. We could trivially make a “new hypothesis” that \(\mu = 62.8\), and run the same statistical test to show that we “can’t reject \(H_0\)”, but that wouldn’t prove that \(\mu = 62.8\). This is why it’s important for \(H_0\) to be determined in advance, so that we don’t pollute our hypothesis with knowledge of the sample.

Experiment

Isn’t this the same as what we did with the Confidence Interval ? It’s related, but not exactly the same.

With the confidence interval, we measure an estimator and then compute an interval, based on the sample statistics, where we are likely to find the value we’re trying to estimate.

With the hypothesis test, we start by assuming an hypothesis, then test how (un)likely it would be to observe the sample statistics given that hypothesis.

Type I and Type II errors

There are two types of errors that we can make when using a hypothesis test:

  1. Rejecting \(H_0\) even though it is true.
  2. Not rejecting \(H_0\) even though it is false

These errors are called Type I and Type II errors:

\(H_0\) is True \(H_0\) is False (\(H_1\))
Accept \(H_0\) correct Type II error
Reject \(H_0\) Type I error correct

Type I and Type II errors

P \(H_0\) is True \(H_0\) is False (\(H_1\))
Accept \(H_0\) \(P(H_0 accepted | H_0)\) \(P(H_0 accepted | H_1)\)
Reject \(H_0\) \(P(H_0 rejected | H_0) \leq \alpha\) \(P(H_0 rejected | H_1)\)

We define the level of significance \(\alpha\) of the test as the maximum type I error that we are ready to make by rejecting \(H_0\): to pass, the test must satisfy \(P(H_0 rejected | H_0) \leq \alpha\).

Type I and Type II errors

P \(H_0\) is True \(H_0\) is False (\(H_1\))
Accept \(H_0\) \(P(H_0 accepted | H_0)\) \(P(H_0 accepted | H_1)\)
Reject \(H_0\) \(P(H_0 rejected | H_0)\) \(P(H_0 rejected | H_1) = \pi\)

We can also define the power of the test \(\pi = 1 - P(Type II) = P(H_0 rejected | H_1)\), the probability that the test correctly rejects \(H_0\).

If a test has a high power and doesn’t reject \(H_0\), then it’s very likely that \(H_0\) is correct.

p-value

The p-value of a test is the observed level of significance, i.e. the probability under \(H_0\) of obtaining a value for the test statistic at least as extreme as the observed value.

Therefore, we can reject \(H_0\) with a level of significance \(\alpha\) if \(pval < \alpha\).

\(\alpha\) is an arbitrary choice. By convention, \(0.05\) is often used, but sometimes \(0.10\) is used for research claims where the need for certainty is more “relaxed”, or \(0.01\) when it is more important.

Bilateral vs Unilateral test

So far, we’ve considered the bilateral version of the hypothesis test, where the acceptance zone for the observed statistic \(t\) lies between \(t_0 - a\) and \(t_0 + a\) such that \(P(T < t_0 - a) = P(T > t_0 + a) = \frac{\alpha}{2}\). If the distribution of the statistic \(T\) is symmetrical, we further have \(\alpha = 2 \times P(T > t_0 + a)\).

In some cases, we are only interested in one side of the distribution. Then, we might have \(H_0: t \leq t_0\) and \(H_1: t > t_0\). The acceptance zone will therefore be bounded only on one side by \(t_0 + a\) such that \(P(T > t_0 + a) = \alpha\).

Bilateral vs Unilateral test

\(H_0\) is rejected if the observed value is outside of \([t_0 \pm a]\)

\(H_0\) is rejected if the observed value is larger than \(t_0 + a\) (or smaller than \(t_0 - a\), depending on the side we chose)

Interpretation of the p-value

Just like the CI doesn’t prove that the true value is within its bound, the hypothesis tests don’t prove that \(H_0\) is true (or not).

A small p-value shows that it is very unlikely that \(H_0\) is true and that we observe this sample. But it doesn’t tell us what causes this discrepancy, and in practical situations p-values may be very difficult to interpret.

Interpretation of the p-value

Things that are not covered by the p-value:

Also… a 5% chance is not that rare ! If we do measurements on lots of variables in different conditions, we are very likely to find some results where \(H_0\) is rejected due to random chance.

Practical or statistical significance

When a study has a p-value under the target significance level (such as 0.05), it is said to be statistically significant. This, however, does not necessarily mean it is practically significant.

Practical significance exists outside of statistics and is domain-dependant.

Example: if the sample size is large enough, even very small differences in the observed values will be “statistically significant”. But for the target application, this difference may be completely meaningless.

Quick quizz

We are manufacturing an EEG sensor, and we want to produce sensors that have an average input impedance \(> 100 G\Omega\) with a standard deviation \(< 10G\Omega\). To control the production line, we randomly sample 10 sensors in every batch and measure their impedance \(Z\). Assuming that we first want to focus on the average impedance \(\bar{Z}\), we should…

  1. Measure \(\bar{Z}\) and compute a CI around it.
  2. Make an hypothesis test under \(H_0: \bar{Z} \leq 100G\Omega\).
  3. Compute a CI around \(100 G\Omega\) and check that \(\bar{Z}\) is outside.
  4. Make an hypothesis test under \(H_0: \bar{Z} \geq 100G\Omega\).
  1. Not really what we want: we don’t care about the range of values for \(\bar{Z}\), just that it is \(>100G\Omega\).
  2. Yes.
  3. That would be equivalent to a two-sided test on \(H_0: \bar{Z} = 100G\Omega\), so not what we are after here.
  4. No, that would mean we are trying to “prove” H_0.

We could afterwards perform a \(\chi^2\) test of variance on \(Var[Z]\) with \(H_0: Var[Z] > 10G\Omega\).

Power and sample size

P \(H_0\) is True \(H_0\) is False (\(H_1\))
Accept \(H_0\) \(1-\alpha\) \(\beta\)
Reject \(H_0\) \(\alpha\) \(\pi = 1-\beta\)

Trade-off: if \(\alpha \uparrow\), \(\beta \downarrow\) (and \(\pi \uparrow\)) \(\rightarrow\) increasing the power of the test (probability of correctly rejecting \(H_0\)) will also increase the Type I error (probability that we reject \(H_0\) even though it’s true).

Example: power function for different \(\alpha\) values and “true” values of \(\mu\) for \(H_0: \mu_0 = 68\), if \(T \sim N(\mu, \frac{7^2}{\sqrt{10}})\).

Power and sample size

For a test on the mean of a normal distribution with known variance \(\sigma^2\), if we fix a target \(\alpha\), \(\pi\), and a difference from the mean \(\delta = |\mu_0-\mu_1|\) that we want to be able to detect, we can compute the minimum sample size for the test to respect those conditions. Let’s look at the unilateral test: \(H_0: \mu = \mu_0, H_1: \mu = \mu_1 > \mu_0\).

Under \(H_0\), \(\bar{X} \sim N(\mu_0, \frac{\sigma^2}{n})\) or \(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} \sim N(0, 1) = Z\).
Under \(H_1\), \(\bar{X} \sim N(\mu_1, \frac{\sigma^2}{n})\) or \(\frac{\bar{X}-\mu_1}{\sigma/\sqrt{n}} \sim N(0, 1) = Z\).

For a given \(\alpha\), we can compute the critical values for rejecting \(H_0\). Let’s note \(Z_k\) the 100\(k\)th percentile of a distribution \(Z\), which we can either find in a table of Z scores: https://www.ztable.net/, or using scipy.stats.norm:

def z(k):
    return norm(loc=0, scale=1).ppf(k)
Percentile \(k\) 0.01 0.05 0.25 0.5 0.75 0.95 0.99
\(Z_k\) -2.33 -1.64 -0.67 0.00 0.67 1.64 2.33

Power and sample size

Percentile \(k\) 0.01 0.05 0.25 0.5 0.75 0.95 0.99
\(Z_k\) -2.33 -1.64 -0.67 0.00 0.67 1.64 2.33
\(\Phi(Z_k)\) 0.01 0.05 0.25 0.5 0.75 0.95 0.99

\(\Phi(x) = P(X \leq x)\) with \(X \sim N(0,1)\) is the cumulative distribution function of \(N(0,1)\), so that \(\Phi(Z_k) = k\).

Power and sample size

Let’s look at the unilateral test: \(H_0: \mu \leq \mu_0, H_1: \mu = \mu_1 > \mu_0\).

Under \(H_0\), \(\bar{X} \sim N(\mu_0, \frac{\sigma^2}{n})\) or \(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} \sim N(0, 1) = Z\).
Under \(H_1\), \(\bar{X} \sim N(\mu_1, \frac{\sigma^2}{n})\) or \(\frac{\bar{X}-\mu_1}{\sigma/\sqrt{n}} \sim N(0, 1) = Z\).

Critical value: \(Z_{1-\alpha}\) \(\rightarrow\) we reject \(H_0\) if \(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} > Z_{1-\alpha}\).

Power: \(\pi = P(Reject H_0 | H_1) = P(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} > Z_{1-\alpha} | H_1)\)


Power and sample size

Under \(H_1\), \(\bar{X} \sim N(\mu_1, \frac{\sigma^2}{n})\) or \(\frac{\bar{X}-\mu_1}{\sigma/\sqrt{n}} \sim N(0, 1) = Z\).

\(\pi = P(Reject H_0 | H_1) = P(\frac{\bar{X}-\mu_0}{\sigma/\sqrt{n}} > Z_{1-\alpha} | H_1)\)

\(\Rightarrow \pi = P(\bar{X} > \mu_0 + Z_{1-\alpha}\frac{\sigma}{\sqrt{n}} | H_1)\)

\(\Rightarrow \pi = 1 - P(\bar{X} \leq \mu_0 + Z_{1-\alpha}\frac{\sigma}{\sqrt{n}} | H_1)\) (because \(P(X > x) = 1 - P(X \leq x)\))

\(\Rightarrow \pi = 1 - P(\frac{\bar{X}-\mu_1}{\sigma/\sqrt{n}} \leq \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}} + Z_{1-\alpha} | H_1)\)

\(\Rightarrow \pi = 1 - P(Z \leq \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}} + Z_{1-\alpha})\) (if \(H_1\) is true)

Power and sample size

Under \(H_1\), \(\bar{X} \sim N(\mu_1, \frac{\sigma^2}{n})\) or \(\frac{\bar{X}-\mu_1}{\sigma/\sqrt{n}} \sim N(0, 1) = Z\).

\(\Rightarrow \pi = 1 - P(Z \leq \frac{\mu_0-\mu_1}{\sigma/\sqrt{n}} + Z_{1-\alpha})\)

\(\Rightarrow \pi = 1 - \Phi(\frac{\mu_0-\mu_1}{\sigma/\sqrt{n}} + Z_{1-\alpha})\)

\(\Rightarrow \pi = \Phi(\frac{\mu_1-\mu_0}{\sigma/\sqrt{n}} - Z_{1-\alpha})\) (because \(\Phi(-x) = 1 - \Phi(x)\)).

And \(\pi = \Phi(Z_\pi)\).

So if we fix a target \(\pi\), we want \(Z_\pi = \frac{\mu_1-\mu_0}{\sigma/\sqrt{n}} - Z_{1-\alpha}\)

\(Z_\pi\) and \(Z_{1-\alpha}\) are critical values found in tables or software. \(\mu_0\) and \(\mu_1\) are in our hypotheses. \(\sigma\) is known. So we can compute \(n\)!

Power and sample size

With \(\delta = \mu_1-\mu_0\), we have:

\(Z_\pi = \frac{\delta}{\sigma/\sqrt{n}} - Z_{1-\alpha}\)

\(\Rightarrow Z_\pi+Z_{1-\alpha} = \frac{\delta}{\sigma/\sqrt{n}}\)

\(\Rightarrow \frac{\sigma (Z_\pi+Z_{1-\alpha})}{\delta} = \sqrt{n}\)

\(\Rightarrow n = (\frac{\sigma (Z_\pi+Z_{1-\alpha})}{\delta})^2\)

And we can only have integer sample sizes, so:

\(n = \left \lceil \frac{\sigma^2(Z_{\pi}+Z_{1-\alpha})^2}{\delta^2} \right \rceil\)

Example: In a population with a known variance of 49, we have \(H_0: \mu_0 \leq 68\), and we want to detect a \(\delta\) of 3 with \(\alpha = 0.05\) and \(\pi = 0.9\). In other words: we want to ensure that, if the true \(\mu\) is larger than 71, we have at least 90% probability of correctly rejecting \(H_0\) and at most 5% probability of rejecting \(H_0\) if, in fact, \(\mu = \mu_0\).

\(n = \left \lceil \frac{7^2(Z_{0.9}+Z_{0.95})^2}{3^2} \right \rceil = \left \lceil \frac{7^2(1.28+1.64)^2}{3^2} \right \rceil\)

\(n = \left \lceil 46.63 \right \rceil = 47\)

We need at least a sample size of 47.



Power function for \(n=10\)

Power function for \(n=47\)

Power and sample size

These results can be generalized to the two-sided test. The minimum sample size for a given \(\alpha\), \(\pi\) and \(\delta\) are therefore:

Unilateral (\(\mu_1 > \mu_0+\delta\) or \(\mu_1 < \mu_0-\delta\)):

\[n = \left \lceil \frac{\sigma^2(Z_{\pi}+Z_{1-\alpha})^2}{\delta^2} \right \rceil\]

Bilateral (\(|\mu_1-\mu_0| > \delta\)):

\[n = \left \lceil \frac{\sigma^2(Z_{\pi}+Z_{1-\frac{\alpha}{2}})^2}{\delta^2} \right \rceil\]

Parametric vs nonparametric tests

The hypothesis tests we’ve presented so far relied on the estimation of population parameters using sample statistics. Such tests, called parametric tests, generally make strong assumptions about the nature of the population distribution (such as assumption of normality).

When we cannot make these assumptions (either because the population doesn’t follow the distribution or because our sample size is too small to verify that it does), we have to fall back on another type of tests: nonparameteric tests.

Parametric vs nonparametric tests

Parametric tests are generally more powerful than their nonparametric counterpart, so they are preferred… when they are applicable !

Nonparametric tests are often based on ranks rather than values.

Parametric vs nonparametric tests

Sample \(X\): \(X_1, X_2, ..., X_n\) with unknown variance.

Parametric

\(H_0: \bar{X} = \mu_0\)

One-sample t-test:

Test statistic: \(\frac{\bar{X}-\mu_0}{S/\sqrt{n}}\)

Follows distribution: \(t_{n-1}\)

Assumes \(\bar{X}\) to be normally distributed (CLT \(\rightarrow\) should be verified if \(n\) is large enough even if \(X\) is non-normal).

Nonparametric

\(H_0: Median[X] = m_0\)

Wilcoxon one-sample signed-rank test:

Compute \(|X_1-m_0|, |X_2-m_0|, ..., |X_n-m_0|\)
Compute ranks: \(R_1, R_2, ..., R_n\)
Compute signs: \(sgn(X_1-m_0), sgn(X_2-m_0), ..., sgn(X_n-m_0)\)

Test statistic: \(T = \sum_{i=1}^N sgn(X_i-m_0)R_i\)

Follows a known distribution which has a normal approximation if \(n\) is large enough (usually \(>50\) is used).

Assumes \(X_i\) is symmetric.

Variance test

We’ve seen before that the sampling distribution of the variance for a random variable \(X \sim N(\mu, \sigma^2)\) can be described using the \(\chi^2_\nu\) distribution: \(\frac{(n-1)s^2}{\sigma^2} \sim \chi^2_{n-1}\).

Similar to the test on the mean, we can have the null hypothesis \(H_0: \sigma = \sigma_0 \Rightarrow C^2 = \frac{(n-1)s^2}{\sigma_0^2} \sim \chi_{n-1}^2\)

As \(\chi^2_{n-1}\) is a known distribution, we can compute (or look-up in tables) values such that \(100\alpha\)% of the values of the distribution are outside \([\chi_{n-1,\alpha/2}^2, \chi_{n-1, 1-\alpha/2}^2]\) (or use the same principle to compute a CI for an observed variance).

Note that this test is very sensitive to the normality assumption (not applicable if \(X \sim N(\mu, \sigma^2)\) is not verified).

Variance test

Example: for \(X \sim N(63, 7^2)\), we can take samples of size \(n=30\) and verify that, with the measured variance \(s^2\), we get \(\frac{(n-1)s^2}{\sigma_0^2} \sim \chi_{n-1}^2\).

Using \(\alpha = 0.05\), we can determine the two-sided acceptance zone for \(H_0: \sigma = 7\).

\(s^2_{low} = \frac{\chi^2_{29, 0.025}7^2}{29} = 27.11\)
\(s^2_{high} = \frac{\chi^2_{29, 0.975}7^2}{29} = 77.25\)

(using tables or scipy.stats.chi2 for the \(\chi^2\) values)

Distribution fit

So how do we verify normality ? Different methods for “goodness of fit” estimation:

Q-Q Plot

For a size 50 sample drawn from \(N(63, 7^2)\). If normal, should be aligned.

Test: regression fit (for later).

Statistical tests:

\(\rightarrow\) based on comparing observed vs expected frequencies under \(H_0\) (“distributions are equal”)

Kolmogorov-Smirnov goodness-of-fit test

Given a sample of size \(n\) of random variable \(X\), with \(H_0: X \sim D\):

The KS statistic is: \(C = max(max_{i=1...n}(F(X_i)-\frac{i-1}{n}),max_{i=1...n}(\frac{i}{n}-F(X_i))\) (that is the maximum distance between the theoretical and observed cumulative frequency curves).

It follows a known distribution, so critical values can be found in tables (or software) !

Bscan for Wikimedia

Key lessons

Reading

How is the statistical significance of the results communicated in Fig. 2 ? What test do these p-values correspond to ?