Skip to content

Instantly share code, notes, and snippets.

@db5023
Created January 24, 2018 02:39
Show Gist options
  • Save db5023/afb7b18340741ee897970cc43e1f420b to your computer and use it in GitHub Desktop.
Save db5023/afb7b18340741ee897970cc43e1f420b to your computer and use it in GitHub Desktop.
One Sample T-Test Using Only Summary Statistics
import scipy.stats as stats
import math
def one_sample_ttest_pval(tail, mu, x, s, n, alpha):
#Calculate test statistic and p-value. Provide summary stats and specify 1 or 2 tailed test
t = round((x - mu) / (s / math.sqrt(n)),2)
test_stat = round(stats.t.ppf(q=alpha,
df=n-1),3)
if tail == 1:
pval = round(stats.t.cdf(t,
df= n-1),4)
print("Results for a 1-tailed t-test:")
elif tail == 2:
pval = round((1 - stats.t.cdf(t,
df= n-1))* 2, 4)
print("Results for a 2-tailed t-test:")
#Print test results
print("Test statistic = {}".format(test_stat))
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))
#Sample tests:
#one_sample_ttest_pval(2, 600, 612, 65, 40, .05)
#one_sample_ttest_pval(1, 238, 231, 80, 100, .05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment