comparison python/utils.py @ 859:a8de3c93f6b7

minor modifications to helper stat functions
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 21 Oct 2016 16:02:46 -0400
parents e310577cc0b8
children c7e72d758049
comparison
equal deleted inserted replaced
858:2faabcbde2c4 859:a8de3c93f6b7
49 def fitLogNormal(x): 49 def fitLogNormal(x):
50 'returns the fitted location and scale of the lognormal (general definition)' 50 'returns the fitted location and scale of the lognormal (general definition)'
51 shape, loc, scale = lognorm.fit(x, floc=0.) 51 shape, loc, scale = lognorm.fit(x, floc=0.)
52 return log(scale), shape 52 return log(scale), shape
53 53
54 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False): 54 def sampleSize(stdev, tolerance, percentConfidence, nRoundingDigits = None, printLatex = False):
55 from scipy.stats.distributions import norm 55 from scipy.stats.distributions import norm
56 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. 56 if nRoundingDigits is None:
57 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1), 2) # 1.-(100-percentConfidence)/200.
58 else:
59 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1), nRoundingDigits)
60 stdev = round(stdev, nRoundingDigits)
61 tolerance = round(tolerance, nRoundingDigits)
57 if printLatex: 62 if printLatex:
58 print('${0}^2\\frac{{{1}^2}}{{{2}^2}}$'.format(k, stdev, tolerance)) 63 print('$z_{{{}}}^2\\frac{{s^2}}{{e^2}}={}^2\\frac{{{}^2}}{{{}^2}}$'.format(0.5+percentConfidence/200.,k, stdev, tolerance))
59 return (k*stdev/tolerance)**2 64 return (k*stdev/tolerance)**2
60 65
61 def confidenceInterval(mean, stdev, nSamples, percentConfidence, trueStd = True, printLatex = False): 66 def confidenceInterval(mean, stdev, nSamples, percentConfidence, trueStd = True, printLatex = False):
62 '''if trueStd, use normal distribution, otherwise, Student 67 '''if trueStd, use normal distribution, otherwise, Student
63 68
65 ex: norm.interval(0.95, loc = 0., scale = 2.3/sqrt(11)) 70 ex: norm.interval(0.95, loc = 0., scale = 2.3/sqrt(11))
66 t.interval(0.95, 10, loc=1.2, scale = 2.3/sqrt(nSamples)) 71 t.interval(0.95, 10, loc=1.2, scale = 2.3/sqrt(nSamples))
67 loc is mean, scale is sigma/sqrt(n) (for Student, 10 is df)''' 72 loc is mean, scale is sigma/sqrt(n) (for Student, 10 is df)'''
68 from scipy.stats.distributions import norm, t 73 from scipy.stats.distributions import norm, t
69 if trueStd: 74 if trueStd:
70 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. 75 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1), 2)
71 else: # use Student 76 else: # use Student
72 k = round(t.ppf(0.5+percentConfidence/200., nSamples-1)*100)/100. 77 k = round(t.ppf(0.5+percentConfidence/200., nSamples-1), 2)
73 e = k*stdev/sqrt(nSamples) 78 e = k*stdev/sqrt(nSamples)
74 if printLatex: 79 if printLatex:
75 print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples)) 80 print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples))
76 return mean-e, mean+e 81 return mean-e, mean+e
77 82