Created
May 11, 2014 03:50
-
-
Save ducalpha/aaa6e4f45d37d0ccb318 to your computer and use it in GitHub Desktop.
compute t-distribution confidence interval
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
| from scipy.stats import t | |
| import numpy | |
| def sampleStdev(samples): | |
| mean = numpy.mean(samples) | |
| squareSamples = [ numpy.power(x - mean, 2) for x in samples] | |
| sumSqr = numpy.sum(squareSamples) | |
| sampleStdev = numpy.sqrt(sumSqr / (len(samples) - 1)) | |
| return sampleStdev | |
| def tConfidenceInterval(alpha, sampleSize, stdev): | |
| degreeOfFreedom = sampleSize - 1 | |
| tCritical = t.ppf(1 - alpha/2, degreeOfFreedom) | |
| confidenceInterval = tCritical * stdev / numpy.sqrt(sampleSize) | |
| return confidenceInterval | |
| def computeTConfidenceInterval(alpha, samples): | |
| stdev = sampleStdev(samples) | |
| print stdev | |
| return tConfidenceInterval(alpha, len(samples), stdev) | |
| samples = [1.5, 2, 3, 2.5, 2.75, 2.25, 2.25, 2, 1.75, 2, 1.5, 3, 1.99, 2.22, 2.77, 3.1] | |
| print computeTConfidenceInterval(0.05, samples) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment