diff 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
line wrap: on
line diff
--- a/python/utils.py	Mon Oct 21 18:16:33 2013 -0400
+++ b/python/utils.py	Mon Oct 21 23:58:40 2013 -0400
@@ -28,10 +28,21 @@
 # simple statistics
 #########################
 
-def confidenceInterval(mean, stdev, nSamples, percentConfidence, printLatex = False):
-    from math import sqrt
+def sampleSize(stdev, tolerance, percentConfidence, 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 printLatex:
+        print('${0}^2\\frac{{{1}^2}}{{{2}^2}}$'.format(k, stdev, tolerance))
+    return (k*stdev/tolerance)**2
+
+def confidenceInterval(mean, stdev, nSamples, percentConfidence, trueStd = True, printLatex = False):
+    'if trueStd, use normal distribution, otherwise, Student'
+    from math import sqrt
+    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.
+    else: # use Student
+         k = round(t.ppf(0.5+percentConfidence/200., nSamples-1)*100)/100.
     e = k*stdev/sqrt(nSamples)
     if printLatex:
         print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples))