changeset 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 2faabcbde2c4
children 07c5eab11eba
files python/utils.py
diffstat 1 files changed, 10 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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))