Skip to content

Instantly share code, notes, and snippets.

@db5023
Last active January 24, 2018 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save db5023/76f1e4d494a5b5ccb604ee03a29a0ec8 to your computer and use it in GitHub Desktop.
Save db5023/76f1e4d494a5b5ccb604ee03a29a0ec8 to your computer and use it in GitHub Desktop.
Single Sample Z-Test for a Population Proportion
import scipy.stats as stats
import math
def one_sample_ztest_pop_proportion(tail, p, pbar, n, alpha):
#Calculate test stat
sigma = math.sqrt((p*(1-p))/(n))
z = round((pbar - p) / sigma, 2)
if tail == 'lower':
pval = round(stats.norm(p, sigma).cdf(pbar),4)
print("Results for a lower tailed z-test: ")
elif tail == 'upper':
pval = round(1 - stats.norm(p, sigma).cdf(pbar),4)
print("Results for an upper tailed z-test: ")
elif tail == 'two':
pval = round(stats.norm(p, sigma).cdf(pbar)*2,4)
print("Results for a two tailed z-test: ")
#Print test results
print("Test statistic = {}".format(z))
print("P-value = {}".format(pval))
print("Confidence = {}".format(alpha))
#Compare p-value to confidence level
if pval <= alpha:
print("{} <= {}. Reject the null hypothesis.".format(pval, alpha))
else:
print("{} > {}. Do not reject the null hypothesis.".format(pval, alpha))
#one_sample_ztest_pop_proportion('upper', .20, .25, 400, .05)
#one_sample_ztest_pop_proportion('two', .64, .52, 100, .05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment