Link Search Menu Expand Document (external link)

How to compute a confidence interval for the difference between two means when both population variances are known

Description

If we have samples from two independent populations, and both of the population variances are known, how do we construct a confidence interval for the difference between the population means?

Related tasks:

Using NumPy and SciPy, in Python

View this solution alone.

We’re going to use some fake data here to illustrate how to make the confidence interval. Replace our fake data and population variances with your actual data and population variances if you use this code.

1
2
3
4
sample1 = [15, 10, 7, 22, 17, 14]
sample2 = [9, 1, 11, 13, 3, 6]
pop1_variance = 2.3
pop2_variance = 3

We will need the size and mean of each sample.

1
2
3
4
5
import numpy as np
n_sample1 = len(sample1)
n_sample2 = len(sample2)
xbar1 = np.mean(sample1)
xbar2 = np.mean(sample2)

We can then use that data to create the confidence interval.

1
2
3
4
5
6
7
8
9
10
11
# Find the critical value from the normal distribution
from scipy import stats
alpha = 0.05       # replace with your chosen alpha (here, a 95% confidence level)
critical_val = stats.norm.ppf(1-alpha/2)

# Find the lower and upper bounds of the confidence interval
upper_bound = (xbar1 - xbar2) + \
    critical_val*np.sqrt((pop1_variance/n_sample1) + (pop2_variance/n_sample2))
lower_bound = (xbar1 - xbar2) - \
    critical_val*np.sqrt((pop1_variance/n_sample1) + (pop2_variance/n_sample2))
lower_bound, upper_bound
1
(5.15791188458682, 8.842088115413178)

Our 95% confidence interval for the true difference between the population means is $[5.1579, 8.842]$.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Solution, in R

View this solution alone.

We’re going to use some fake data here to illustrate how to make the confidence interval. Replace our fake data and population variances with your actual data and population variances if you use this code.

1
2
3
4
sample.1 <- c(15, 10, 7, 22, 17, 14)
sample.2 <- c(9, 1, 11, 13, 3, 6)
pop1.variance <- 2.3
pop2.variance <- 3

We will need the size and mean of each sample.

1
2
3
4
n.sample1 <- length(sample.1)
n.sample2 <- length(sample.2)
xbar1 <- mean(sample.1)
xbar2 <- mean(sample.2)

We can then use that data to create the confidence interval.

1
2
3
4
5
6
7
8
9
10
# Find the critical value from the normal distribution
alpha <- 0.05       # replace with your chosen alpha (here, a 95% confidence level)
critical.val <- qnorm(p=alpha/2, lower.tail=FALSE)

# Find the lower and upper bounds of the confidence interval
radius <- critical.val*sqrt(pop1.variance/n.sample1 + pop2.variance/n.sample2)
upper.bound <- (xbar1 - xbar2) + radius
lower.bound <- (xbar1 - xbar2) - radius
lower.bound
upper.bound
1
2
3
4
5
[1] 5.157912



[1] 8.842088

Our 95% confidence interval for the true difference between the population means is $[5.1579, 8.842]$.

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
  • Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.