Hypothesis tests

Author
Affiliation

Beniamino Sartini

University of Bologna

Published

May 1, 2024

Modified

June 16, 2024

Setup
library(dplyr)
library(ggplot2)
# ================== Setups ==================
n <- 500 # number of simulations 
set.seed(1) # random seed 
mu_0 <- 2.4 # H0 mean 
mu_true <- 2 # true mean
alpha <- 0.05 # confidence level
# ============================================
# Simulated random variable 
Xn <- rnorm(n, mean = mu_true, sd = 4)
# Grid of points for pdf
x <- seq(-4, 4, 0.01)
x_breaks <- seq(min(x), max(x), 1)

A statistical hypothesis test is a method of statistical inference used to decide whether the data sufficiently support a particular hypothesis. An hypothesis test typically involves a calculation of a test statistic, that under the null hypothesis H0 can assume a certain distribution. Then a decision is made, either by comparing the test statistic to a critical value or equivalently by evaluating a p-value computed from the test statistic. Then, the purpose of a test is to reject or not the null hypothesis at a fixed a confidence level α. Note that an hypothesis is never accepted, but always non-rejected. In general, two kind of tests are available:

Let’s consider a ttest for the mean of a sample Xn=(x1,,xi,,xn), i.e.  T(Xn)=μ(Xn)μ0σ(Xn)nH0tn1nN(0,1) where μ(X) is the sample mean, σ(X) is an unbiased estimator of the population standard deviation and μ0 is the mean under the null hypothesis H0. The statistic test under H0 follows a student-t distribution with n1 degrees of freedom. Notably, for large samples the statistic converges to a normal random variable.

1 Two-tail test

For example, let’s simulate a sample Xn of n=500 observations from a normal distribution (i.e. XnN(2,42)). Then, let’s consider the hypothesis: H0:μ(X)=2.4H1:μ(X)2.4 and the statistic test is then defined as: T(Xn)=500μ(Xn)2.4σ(Xn)H0t(499).

Since it is a two-tailed test the critical value at a confidence level α, denoted as tα, is such that: α=P([T(Xn)<tα/2][T(Xn)>tα/2])tα/2=P1(P(T(Xn)>tα/2)), where P1 and P are respectively the quantile and distribution functions of a Student-t. Hence, with α=0.05 if 1.9604<T(Xn)<1.9604 we do not reject the null hypothesis, i.e. μ(Xn) is equal to μ0=2.4, otherwise we reject it, i.e. the two means are different.

Two-tailed test
# Statistic T
z <- sqrt(n)*(mean(Xn) - mu_0)/sd(Xn)
pdf <- dt(x, df = n-1)
# Critical value left 
z_left <- c(qt(alpha/2, df = n-1), dt(qt(alpha/2, df = n-1), df = n-1))
# Critical value right 
z_right <- c(qt(1-alpha/2, df = n-1), dt(qt(1-alpha/2, df = n-1), df = n-1))
ggplot()+
  geom_segment(aes(x = z_left[1], xend = z_left[1], y = 0, yend = z_left[2]), color = "red")+
  geom_segment(aes(x = z_right[1], xend = z_right[1], y = 0, yend = z_right[2]), color = "red")+
  geom_ribbon(aes(x = x[x < z_left[1]], ymin = 0, ymax = dt(x[x < z_left[1]], df = n-1), fill = "rej"), alpha = 0.3)+
  geom_ribbon(aes(x = x[x > z_right[1]], ymin = 0, ymax = dt(x[x > z_right[1]], df = n-1), fill = "rej"), alpha = 0.3)+
  geom_ribbon(aes(x = x[x > z_left[1] & x < z_right[1]], ymin = 0, ymax = dt(x[x > z_left[1] & x < z_right[1]], df = n-1), 
                  fill = "norej"), alpha = 0.3)+
  geom_line(aes(x, pdf))+
  geom_point(aes(z, 0), color = "black")+
  scale_fill_manual(values = c(rej = "red", norej = "green"), 
                    labels = c(rej = "Rejection Area", norej = "Non Rejection Area")) + 
  scale_x_continuous(breaks = x_breaks) +
  labs(y = "", x = "x", fill = NULL)+
  theme_bw()+
  theme(
    legend.position = "top",
    panel.grid = element_blank()
  )
Figure 1: Two-tailed test on the mean.

2 Left-tailed test

For example, let’s consider another the hypothesis: H0:μ(X)2.4H1:μ(X)<2.4 The statistic test T(Xn) do not changes, however it is a left-tailed test. Hence, the critical value is tα is such that P(x<tα)=0.05. Applying the quantile function P1 of a student-t we obtain: α=P(T(Xn)<tα)tα=P1(P(T(Xn)<tα)), where P1 and P are respectively the quantile and distribution functions of a Student-t. Hence, for α=0.05 if T(Xn)<1.6451 we do not reject the null hypothesis, i.e. μ(Xn) is greater than μ0, otherwise we reject it and μ(Xn) is lower than μ0.

Left-tailed test
# Critical value left 
z_left <- c(qt(alpha, df = n-1), dt(qt(alpha, df = n-1), df = n-1))
ggplot()+
  geom_segment(aes(x = z_left[1], xend = z_left[1], y = 0, yend = z_left[2]), color = "red")+
  geom_ribbon(aes(x = x[x < z_left[1]], ymin = 0, ymax = dt(x[x < z_left[1]], df = n-1), fill = "rej"), alpha = 0.3)+
  geom_ribbon(aes(x = x[x > z_left[1]], ymin = 0, ymax = dt(x[x > z_left[1]], df = n-1), 
                  fill = "norej"), alpha = 0.3)+
  geom_line(aes(x, pdf))+
  geom_point(aes(z, 0), color = "black")+
  scale_fill_manual(values = c(rej = "red", norej = "green"), 
                    labels = c(rej = "Rejection Area", norej = "Non Rejection Area")) + 
  scale_x_continuous(breaks = x_breaks) +
  labs(y = "", x = "x", fill = NULL)+
  theme_bw()+
  theme(
    legend.position = "top",
    panel.grid = element_blank()
  )
Figure 2: Left-tailed test on the mean.

In this case we reject the null hyphotesis, hence μ(Xn) is lower than μ0.

3 Right-tailed test

Let’s consider the other case, i.e. H0:μ(X)2.4H1:μ(X)>2.4 It is always one-side test, but in this case is right-tailed. Hence, the critical value tα is such that 1α=P(T(Xn)<tα)tα=P1(P(T(Xn)<tα)), where P1 and P are respectively the quantile and distribution functions of a Student-t. Hence, for α=0.05 if T(Xn)>1.6451 we do not reject the null hypothesis, i.e. μ(Xn) is lower than μ0, otherwise we reject it and μ(Xn) is greater than μ0.

Right-tailed test
# Critical value right 
z_right <- c(qt(1-alpha, df = n-1), dt(qt(1-alpha, df = n-1), df = n-1))
ggplot()+
  geom_segment(aes(x = z_right[1], xend = z_right[1], y = 0, yend = z_right[2]), color = "red")+
  geom_ribbon(aes(x = x[x > z_right[1]], ymin = 0, ymax = dt(x[x > z_right[1]], df = n-1), fill = "rej"), alpha = 0.3)+
  geom_ribbon(aes(x = x[x < z_right[1]], ymin = 0, ymax = dt(x[x < z_right[1]], df = n-1), 
                  fill = "norej"), alpha = 0.3)+
  geom_line(aes(x, pdf))+
  geom_point(aes(z, 0), color = "black")+
  scale_fill_manual(values = c(rej = "red", norej = "green"), 
                    labels = c(rej = "Rejection Area", norej = "Non Rejection Area")) + 
  scale_x_continuous(breaks = x_breaks) +
  labs(y = "", x = "x", fill = NULL)+
  theme_bw()+
  theme(
    legend.position = "top",
    panel.grid = element_blank()
  )
Figure 3: Right-tailed test on the mean.

Coherently with the previous test in this case we don’t reject H0, hence μ(Xn) is lower than μ0=2.4.

Back to top

Citation

BibTeX citation:
@online{sartini2024,
  author = {Sartini, Beniamino},
  title = {Hypothesis Tests},
  date = {2024-05-01},
  url = {https://greenfin.it/statistics/tests/hypothesis-tests.html},
  langid = {en}
}
For attribution, please cite this work as:
Sartini, Beniamino. 2024. “Hypothesis Tests.” May 1, 2024. https://greenfin.it/statistics/tests/hypothesis-tests.html.