Created
August 10, 2014 17:58
-
-
Save gcardone/05276131b6dc7232dbaf to your computer and use it in GitHub Desktop.
How to calculate confidence interval for means with unknown standard deviation using the Student t distribution. Needs numpy and scipy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from scipy.stats import t | |
from numpy import average, std | |
from math import sqrt | |
if __name__ == '__main__': | |
# data we want to evaluate: average height of 30 one year old male and | |
# female toddlers. Interestingly, at this age height is not bimodal yet | |
data = [63.5, 81.3, 88.9, 63.5, 76.2, 67.3, 66.0, 64.8, 74.9, 81.3, 76.2, | |
72.4, 76.2, 81.3, 71.1, 80.0, 73.7, 74.9, 76.2, 86.4, 73.7, 81.3, | |
68.6, 71.1, 83.8, 71.1, 68.6, 81.3, 73.7, 74.9] | |
mean = average(data) | |
# evaluate sample variance by setting delta degrees of freedom (ddof) to | |
# 1. The degree used in calculations is N - ddof | |
stddev = std(data, ddof=1) | |
# Get the endpoints of the range that contains 95% of the distribution | |
t_bounds = t.interval(0.95, len(data) - 1) | |
# sum mean to the confidence interval | |
ci = [mean + critval * stddev / sqrt(len(data)) for critval in t_bounds] | |
print "Mean: %f" % mean | |
print "Confidence Interval 95%%: %f, %f" % (ci[0], ci[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment