# HG changeset patch # User Nicolas Saunier # Date 1382414320 14400 # Node ID f738fa1b69f0bedb5255c1f71cd899ea34d4f919 # Parent 67c7ff5d6b2670a2b6884c09e9fc9d2bda612faf added sample size and Student distribution diff -r 67c7ff5d6b26 -r f738fa1b69f0 python/utils.py --- 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))