# How to do a hypothesis test for the mean with known standard deviation

## Description

Let’s say we are measuring a variable over a population, and we know its standard deviation $\sigma$ is known, and assume that the variable is normally distributed. We take a sample, $x_1, x_2, x_3, \ldots, x_k$, and compute its mean $\bar{x}$. We want to determine if the sample mean is significantly different from, greater than, or less than some hypothesized value, such as a hypothesized population mean. How do we formulate an appropriate hypothesis test?

## Using SciPy, in Python

View this solution alone.

We will use the following fake data, but you can insert your actual data in its place. We have a sample of just 5 values and an assumed population standard deviation of 3.

1
2
sample = [31, 44, 28, 25, 40]  # sample data
pop_std = 3                    # population standard deviation


We also choose a value $0 \le \alpha \le 1$ as our Type I error rate. We’ll let $\alpha$ be 0.05 here, but you can change that in the code below.

### Two-tailed test

In a two-tailed test, we compare the unknown population mean to a hypothesized value $m$ using the null hypothesis $H_0: \mu=m$. Here we’ll use $m=30$, but you can replace that value in the code below with the hypothesis relevant for your situation.

1
2
3
4
5
6
7
from scipy import stats
import numpy as np
m = 30                                         # hypothesized mean
n = len(sample)                                # number of observations
xbar = np.mean(sample)                         # sample mean
test_stat = (xbar - m) / (pop_std/np.sqrt(n))  # test statistic
2*stats.norm.sf(abs(test_stat))                # two-tailed p-value

1
0.007290358091535614


The $p$-value, 0.00729, is less than $\alpha$, so we have evidence to reject the null hypothesis and conclude that the actual population mean $\mu$ is significantly different from the hypothesized value of $m=30$.

### Right-tailed test

In a right-tailed hypothesis test, the null hypothesis is that the population mean is greater than or equal to a chosen value, $H_0: \mu \ge m$.

Most of the code below is the same as above, but we repeat it to make it easy to copy and paste. Only the computation of the $p$-value changes.

1
2
3
4
5
6
7
from scipy import stats
import numpy as np
m = 30                                         # hypothesized mean
n = len(sample)                                # number of observations
xbar = np.mean(sample)                         # sample mean
test_stat = (xbar - m) / (pop_std/np.sqrt(n))  # test statistic
stats.norm.sf(abs(test_stat))                  # right-tailed p-value

1
0.003645179045767807


The $p$-value, 0.003645, is less than $\alpha$, so we have evidence to reject the null hypothesis and conclude that the actual population mean $\mu$ is significantly less than the hypothesized value of $m=30$.

### Left-tailed test

In a left-tailed hypothesis test, the null hypothesis is that the population mean is less than or equal to a chosen value, $H_0: \mu \le m$.

Most of the code below is the same as above, but we repeat it to make it easy to copy and paste. Only the computation of the $p$-value changes.

1
2
3
4
5
6
7
from scipy import stats
import numpy as np
m = 30                                         # hypothesized mean
n = len(sample)                                # number of observations
xbar = np.mean(sample)                         # sample mean
test_stat = (xbar - m) / (pop_std/np.sqrt(n))  # test statistic
stats.norm.sf(-abs(test_stat))                 # left-tailed p-value

1
0.9963548209542322


The $p$-value, 0.99635, is greater than $\alpha$, so wwe do not have sufficient evidence to conclude that $\mu>m$ and should continue to accept the null hypothesis, that $\mu\le m$.

See a problem? Tell us or edit the source.

## Solution, in R

View this solution alone.

We will use the following fake data, but you can insert your actual data in its place. We have a sample of just 5 values and an assumed population standard deviation of 3.

1
2
sample <- c(31, 44, 28, 25, 40)  # sample data
pop.std <- 3                     # population standard deviation


We also choose a value $0 \le \alpha \le 1$ as our Type I error rate. We’ll let $\alpha$ be 0.05 here, but you can change that in the code below.

### Two-tailed test

In a two-tailed test, we compare the unknown population mean to a hypothesized value $m$ using the null hypothesis $H_0: \mu=m$. Here we’ll use $m=30$, but you can replace that value in the code below with the hypothesis relevant for your situation.

1
2
3
4
5
m <- 30                                           # hypothesized mean
n <- length(sample)                               # number of observations
xbar <- mean(sample)                              # sample mean
test.stat <- (xbar - m) / (pop.std/sqrt(n))       # test statistic
2*pnorm(abs(test.stat), 0, 1, lower.tail=FALSE)   # two-tailed p-value

1
[1] 0.007290358


The $p$-value, 0.00729, is less than $\alpha$, so we have evidence to reject the null hypothesis and conclude that the actual population mean $\mu$ is significantly different from the hypothesized value of $m=30$.

### Right-tailed test

In a right-tailed hypothesis test, the null hypothesis is that the population mean is greater than or equal to a chosen value, $H_0: \mu \ge m$.

Most of the code below is the same as above, but we repeat it to make it easy to copy and paste. Only the computation of the $p$-value changes.

1
2
3
4
5
m <- 30                                      # hypothesized mean
n <- length(sample)                          # number of observations
xbar <- mean(sample)                         # sample mean
test.stat <- (xbar - m) / (pop.std/sqrt(n))  # test statistic
pnorm(test.stat, 0, 1, lower.tail=FALSE)     # right-tailed p-value

1
[1] 0.003645179


The $p$-value, 0.003645, is less than $\alpha$, so we have evidence to reject the null hypothesis and conclude that the actual population mean $\mu$ is significantly less than the hypothesized value of $m=30$.

### Left-tailed test

In a left-tailed hypothesis test, the null hypothesis is that the population mean is less than or equal to a chosen value, $H_0: \mu \le m$.

Most of the code below is the same as above, but we repeat it to make it easy to copy and paste. Only the computation of the $p$-value changes.

1
2
3
4
5
m <- 30                                      # hypothesized mean
n <- length(sample)                          # number of observations
xbar <- mean(sample)                         # sample mean
test.stat <- (xbar - m) / (pop.std/sqrt(n))  # test statistic
pnorm(test.stat, 0, 1, lower.tail=TRUE)      # left-tailed p-value

1
[1] 0.9963548


The $p$-value, 0.99635, is greater than $\alpha$, so wwe do not have sufficient evidence to conclude that $\mu>m$ and should continue to accept the null hypothesis, that $\mu\le m$.