# How to compute a confidence interval for a mean difference (matched pairs) (in Python, using NumPy and SciPy)

See all solutions.

Say we have two sets of data that are not independent of each other and come from a matched-pairs experiment, and we want to construct a confidence interval for the mean difference between these two samples. How do we make this confidence interval? Let’s assume we’ve chosen a confidence level of $\alpha$ = 0.05.

## Solution

We’ll use Numpy and SciPy to do some statistics later.

1
2
import numpy as np
from scipy import stats


This example computes a 95% confidence interval, but you can choose a different level by choosing a different value for $\alpha$.

1
alpha = 0.05


We have two samples of data, $x_1, x_2, x_3, \ldots, x_k$ and $x’_1, x’_2, x’_3, \ldots, x’_k$. We’re going to use some fake data below just as an example; replace it with your real data.

1
2
sample1 = np.array([15, 10,  7, 22, 17, 14])
sample2 = np.array([ 9,  1, 11, 13,  3,  6])


And now the computations:

1
2
3
4
5
6
7
diff_samples = sample1 - sample2                        # differences between the samples
n = len(sample1)                                        # number of observations per sample
diff_mean = np.mean(diff_samples)                       # mean of the differences
diff_variance = np.var( diff_samples, ddof=1 )          # variance of the differences
critical_val = stats.t.ppf(q = 1-alpha/2, df = n - 1)   # critical value

1

Our 95% confidence interval for the mean difference is $[0.70338, 13.2966]$.