Login
📚 Python for Introductory Statistics
Chapters ▾

Confidence Interval for μ (raw data given)

To find the confidence interval and margin of error for the population mean when raw data is given, first find the sample mean, std and size using numpy.

Example: In a study of a new pain relieving drag, the measures of pain levels for 15 subjects are recorded below. Use the sample data to construct a 95% confidence interval for the mean pain levels for the population (assume normal).

8.6; 9.4; 7.9; 6.8; 8.3; 7.3; 9.2; 9.6; 8.7; 11.4; 10.3; 5.4; 8.1; 5.5; 6.9

We will use the error_t_mean function above. But first we need the sample std and size.

x = [8.6, 9.4, 7.9, 6.8, 8.3, 7.3, 9.2, 9.6, 8.7, 11.4, 10.3, 5.4, 8.1, 
             5.5, 6.9]

import numpy as np
s = np.std(x,ddof=1)
n = np.size(x)

print('std',s)
print('size',n)
Show expected output
std 1.6722383060978339
size 15
error_t_mean(.95,s,n)  # values of s and n from above
Show expected output
Margin of Error is 0.9260547070143034

Example: In a study of a new pain relieving drag, the measures of pain levels for 15 subjects are recorded below. Use the sample data to construct a 95% confidence interval for the mean pain levels for the population (assume normal).

8.6; 9.4; 7.9; 6.8; 8.3; 7.3; 9.2; 9.6; 8.7; 11.4; 10.3; 5.4; 8.1; 5.5; 6.9

use the conf_t_mean function above but first find the mean, std and n.

x = [8.6, 9.4, 7.9, 6.8, 8.3, 7.3, 9.2, 9.6, 8.7, 11.4, 10.3, 5.4, 8.1, 
             5.5, 6.9]

import numpy as np
m = np.mean(x)
s = np.std(x,ddof=1)
n = np.size(x)

Call the function conf_t_mean with mean = m, std = s using results from the above code.

conf_t_mean(.95,m,s,n) # values of m,s and n from above
Show expected output
mean is 8.226666666666667 and std is 1.6722383060978339
( 7.300611959652363 , 9.15272137368097 )

Interpretation: 7.300611959652363<μ<9.15272137368097

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.