# HG changeset patch # User Nicolas Saunier # Date 1477080166 14400 # Node ID a8de3c93f6b7d51ac345498b2167b0ad9914340f # Parent 2faabcbde2c44ac36703cc9bcf9ca1b2611f8f48 minor modifications to helper stat functions diff -r 2faabcbde2c4 -r a8de3c93f6b7 python/utils.py --- a/python/utils.py Tue Oct 11 17:57:50 2016 -0400 +++ b/python/utils.py Fri Oct 21 16:02:46 2016 -0400 @@ -51,11 +51,16 @@ shape, loc, scale = lognorm.fit(x, floc=0.) return log(scale), shape -def sampleSize(stdev, tolerance, percentConfidence, printLatex = False): +def sampleSize(stdev, tolerance, percentConfidence, nRoundingDigits = None, printLatex = False): from scipy.stats.distributions import norm - k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. + if nRoundingDigits is None: + k = round(norm.ppf(0.5+percentConfidence/200., 0, 1), 2) # 1.-(100-percentConfidence)/200. + else: + k = round(norm.ppf(0.5+percentConfidence/200., 0, 1), nRoundingDigits) + stdev = round(stdev, nRoundingDigits) + tolerance = round(tolerance, nRoundingDigits) if printLatex: - print('${0}^2\\frac{{{1}^2}}{{{2}^2}}$'.format(k, stdev, tolerance)) + print('$z_{{{}}}^2\\frac{{s^2}}{{e^2}}={}^2\\frac{{{}^2}}{{{}^2}}$'.format(0.5+percentConfidence/200.,k, stdev, tolerance)) return (k*stdev/tolerance)**2 def confidenceInterval(mean, stdev, nSamples, percentConfidence, trueStd = True, printLatex = False): @@ -67,9 +72,9 @@ loc is mean, scale is sigma/sqrt(n) (for Student, 10 is df)''' from scipy.stats.distributions import norm, t if trueStd: - k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. + k = round(norm.ppf(0.5+percentConfidence/200., 0, 1), 2) else: # use Student - k = round(t.ppf(0.5+percentConfidence/200., nSamples-1)*100)/100. + k = round(t.ppf(0.5+percentConfidence/200., nSamples-1), 2) e = k*stdev/sqrt(nSamples) if printLatex: print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples))