Untitled
Untitled#
import scipy.stats as stats
import numpy as np
number_n = 120
yes_n = 54
p_hat_n = yes_n / number_n
number_s = 140
yes_s = 77
p_hat_s = yes_s / number_s
center = p_hat_n - p_hat_s
a = stats.norm()
z_star = a.ppf(0.975)
a = p_hat_n*(1-p_hat_n)/number_n
b = p_hat_s*(1-p_hat_s)/number_s
s = np.sqrt(a + b)
left = center - z_star * s
right = center + z_star * s
print([left, right])
[-0.2213019341677507, 0.02130193416775064]
import scipy.stats as stats
import numpy as np
number_n = 120
yes_n = 44
p_hat_n = yes_n / number_n
number_s = 140
yes_s = 77
p_hat_s = yes_s / number_s
center = p_hat_n - p_hat_s
a = p_hat_n*(1-p_hat_n)/number_n
b = p_hat_s*(1-p_hat_s)/number_s
s = np.sqrt(a + b)
test_statistic = center / s
a = stats.norm()
p_value = 2 * a.cdf(test_statistic)
print(p_value)
0.002588980054111791