Is the mean pH of the population 8.2 with a 95% CL?
Loading the Data
import numpy as np
import math
#sample data
data = [8.18, 8.21,8.17,8.22,8.16,8.16,8.17,8.19,8.15,8.18]
sample_mean = round(np.mean(data),3)
n = len(data)
pop_mean_hyp = 8.2
print("Sample mean is {}".format(sample_mean))
Sample mean is 8.179
Sample Std. Deviation
def sample_std_dev(data_set):
'''Calculates denominator s1 in sample std. dev. formula'''
sample_mean = np.mean(data_set)
s1 = 0
for i in data:
s1 += (i-sample_mean)**2
return s1
s1 = sample_std_dev(data)
s = round(math.sqrt(s1/(n-1)),5)
print("Sample Std. Dev. using formula is {}".format(s))
Sample Std. Dev. using formula is 0.02234
Two-tailed t-test
alpha = .05
dof = n-1
import scipy.stats
t_table = round(scipy.stats.t.ppf(q=alpha/2,df=dof),3)
print("t table value is {}".format(t_table))
t_critical = round((sample_mean - pop_mean_hyp)/(s/math.sqrt(n)),3)
print(f"t critical value is {t_critical}")
p_value = scipy.stats.t.sf(abs(t_critical),df=dof)
p_value = round(p_value*2,5)
print(f"The p-value is {p_value}")
print(f"{p_value} <= {alpha}")
if p_value <= alpha:
print("Reject Null Hypothesis")
else:
print("Fail to Reject Null Hypothesis")
t table value is -2.262
t critical value is -2.973
The p-value is 0.01563
0.01563 <= 0.05
Reject Null Hypothesis
Confidence Interval
CI1 = round(sample_mean - abs(t_table*(s/math.sqrt(n))),3)
CI2 = round(sample_mean + abs(t_table*(s/math.sqrt(n))),3)
print("The confidence interval for 95% is [{}, {}]".format(CI1,CI2))
The confidence interval for 95% is [8.163, 8.195]