How to do a hypothesis test for the mean with known standard deviation (in Python, using SciPy)
Task
Let’s say we are measuring a variable over a population, and we know its standard
deviation
Related tasks:
- How to compute a confidence interval for a population mean
- How to do a hypothesis test for a mean difference (matched pairs)
- How to do a hypothesis test for a population proportion
- How to do a hypothesis test for population variance
- How to do a hypothesis test for the difference between means when both population variances are known
- How to do a hypothesis test for the difference between two proportions
- How to do a hypothesis test for the ratio of two population variances
- How to do a hypothesis test of a coefficient’s significance
- How to do a one-sided hypothesis test for two sample means
- How to do a two-sided hypothesis test for a sample mean
- How to do a two-sided hypothesis test for two sample means
Solution
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
Two-tailed test
In a two-tailed test, we compare the unknown population mean to a hypothesized
value
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
0.007290358091535614
The
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,
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
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
0.003645179045767807
The
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,
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
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
0.9963548209542322
The
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by:
- Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)
- Nathan Carter (ncarter@bentley.edu)