Skip to content

Instantly share code, notes, and snippets.

@ducalpha
Created May 11, 2014 03:50
Show Gist options
  • Select an option

  • Save ducalpha/aaa6e4f45d37d0ccb318 to your computer and use it in GitHub Desktop.

Select an option

Save ducalpha/aaa6e4f45d37d0ccb318 to your computer and use it in GitHub Desktop.
compute t-distribution confidence interval
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