comparison python/utils.py @ 423:f738fa1b69f0

added sample size and Student distribution
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 21 Oct 2013 23:58:40 -0400
parents 4fce27946c60
children d40ad901b272
comparison
equal deleted inserted replaced
422:67c7ff5d6b26 423:f738fa1b69f0
26 26
27 ######################### 27 #########################
28 # simple statistics 28 # simple statistics
29 ######################### 29 #########################
30 30
31 def confidenceInterval(mean, stdev, nSamples, percentConfidence, printLatex = False): 31 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False):
32 from math import sqrt
33 from scipy.stats.distributions import norm 32 from scipy.stats.distributions import norm
34 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. 33 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200.
34 if printLatex:
35 print('${0}^2\\frac{{{1}^2}}{{{2}^2}}$'.format(k, stdev, tolerance))
36 return (k*stdev/tolerance)**2
37
38 def confidenceInterval(mean, stdev, nSamples, percentConfidence, trueStd = True, printLatex = False):
39 'if trueStd, use normal distribution, otherwise, Student'
40 from math import sqrt
41 from scipy.stats.distributions import norm, t
42 if trueStd:
43 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200.
44 else: # use Student
45 k = round(t.ppf(0.5+percentConfidence/200., nSamples-1)*100)/100.
35 e = k*stdev/sqrt(nSamples) 46 e = k*stdev/sqrt(nSamples)
36 if printLatex: 47 if printLatex:
37 print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples)) 48 print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples))
38 return mean-e, mean+e 49 return mean-e, mean+e
39 50