How to do a two-sided hypothesis test for a sample mean
Description
Say we have a population whose mean $\mu$ is known. We take a sample $x_1,\ldots,x_n$ and compute its mean, $\bar x$. We then ask whether this sample is significantly different from the population at large, that is, is $\mu=\bar x$?
Related tasks:
- How to compute a confidence interval for a population mean
- How to do a two-sided hypothesis test for two sample means
- How to do a one-sided hypothesis test for two sample means
- How to do a hypothesis test for a mean difference (matched pairs)
- How to do a hypothesis test for a population proportion
Solution, in Julia
This is a two-sided test with the null hypothesis $H_0:\mu=\bar x$. We choose a value $0\leq\alpha\leq1$ as the probability of a Type I error (false positive, finding we should reject $H_0$ when it’s actually true).
1
2
3
4
5
6
7
8
9
10
# Replace these first three lines with the values from your situation.
alpha = 0.05
pop_mean = 10
sample = [ 9, 12, 14, 8, 13 ]
# The following code runs the test for your chosen alpha:
using HypothesisTests
p_value = pvalue( OneSampleTTest( sample, pop_mean ) )
reject_H0 = p_value < alpha
alpha, p_value, reject_H0
1
(0.05, 0.35845634462296455, false)
In this case, the $p$-value was larger than $\alpha$, so the sample does not give us enough information to reject the null hypothesis. We would continue to assume that the sample is like the population, $\mu=\bar x$.
When you are using the most common value for $\alpha$, which is $0.05$ for the $95\%$ confidence interval, you can simply print out the test itself and get a detailed printout with all the information you need, thus saving a few lines of code.
1
OneSampleTTest( sample, pop_mean )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
One sample t-test
-----------------
Population details:
parameter of interest: Mean
value under h_0: 10
point estimate: 11.2
95% confidence interval: (7.986, 14.41)
Test summary:
outcome with 95% confidence: fail to reject h_0
two-sided p-value: 0.3585
Details:
number of observations: 5
t-statistic: 1.0366421106976316
degrees of freedom: 4
empirical standard error: 1.1575836902790224
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Using SciPy, in Python
This is a two-sided test with the null hypothesis $H_0:\mu=\bar x$. We choose a value $0\leq\alpha\leq1$ as the probability of a Type I error (false positive, finding we should reject $H_0$ when it’s actually true).
1
2
3
4
5
6
7
8
9
10
11
12
from scipy import stats
# Replace these first three lines with the values from your situation.
alpha = 0.05
pop_mean = 10
sample = [ 9, 12, 14, 8, 13 ]
# Run a one-sample t-test and print out alpha, the p value,
# and whether the comparison says to reject the null hypothesis.
t_statistic, p_value = stats.ttest_1samp( sample, pop_mean )
reject_H0 = p_value < alpha
alpha, p_value, reject_H0
1
(0.05, 0.35845634462296455, False)
In this case, the sample does not give us enough information to reject the null hypothesis. We would continue to assume that the sample is like the population, $\mu=\bar x$.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Solution, in R
This is a two-sided test with the null hypothesis $H_0:\mu=\bar x$. We choose a value $0\leq\alpha\leq1$ as the probability of a Type I error (false positive, finding we should reject $H_0$ when it’s actually true).
1
2
3
4
5
6
7
8
# Replace these first three lines with the values from your situation.
alpha <- 0.05
pop.mean <- 10
sample <- c( 9, 12, 14, 8, 13 )
# Run a one-sample t-test and print out alpha, the p value,
# and whether the comparison says to reject the null hypothesis.
t.test( sample, mu=pop.mean, conf.level=1-alpha )
1
2
3
4
5
6
7
8
9
10
One Sample t-test
data: sample
t = 1.0366, df = 4, p-value = 0.3585
alternative hypothesis: true mean is not equal to 10
95 percent confidence interval:
7.986032 14.413968
sample estimates:
mean of x
11.2
Although we can deduce the answer to our question from the above output, by comparing the $p$ value with $\alpha$ manually, we can also ask R to do it.
1
2
3
# Is there enough evidence to reject the null hypothesis?
result <- t.test( sample, mu=pop.mean, conf.level=1-alpha )
result$p.value < alpha
1
[1] FALSE
In this case, the sample does not give us enough information to reject the null hypothesis. We would continue to assume that the sample is like the population, $\mu=\bar x$.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Topics that include this task
Opportunities
This website does not yet contain a solution for this task in any of the following software packages.
- Excel
If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.