# stats functions from math import sqrt 'test data' hp = (16,11,23,17,7,19,15,9,21,16,18,19,14,20,11,17,17) lp = (12,8,10,4,6,17,14,8,13,13,9,15,14,7,10,12,11,5,9) f = (53.4,53.2,23.7,37.8,72.5,46.8,27.2,32.0,25.2) u = (61.4,56.9,28.1,36.2,79.2,45.6,34.3,38.8,25.0) def sum(l): return sigmaOfX(l) def sigmaOfX(l): 'returns the sum of all elements in a list' tmp = 0 for eachNo in l: tmp = tmp + eachNo return tmp def mean(l): 'returns the mean if all elements in a list' return float(sigmaOfX(l)) / float(n(l)) def sigmaOfXSquared(l): 'returns the sum of the squared elements in a list' tmp = 0 for eachNo in l: tmp = tmp + eachNo ** 2 return tmp def n(l): 'returns the number of elements in a list' return len(l) def SS(l): 'returns the sum of squares' return sigmaOfXSquared(l) - ((sigmaOfX(l) ** 2) / n(l)) def sampleVariance(l): 'returns the sample variance' return float(SS(l)) / float(n(l)-1) def std(l): 'returns the standard deviation' return sqrt(sampleVariance(l)) def se(l): 'returns the standard error' return std(l) / sqrt(n(l)) 'comparison between two Population Means' def se2(l,m): 'returns the standard error of mean(l) - mean(m)' return sqrt((sampleVariance(l)/n(l) + (sampleVariance(m)/n(m)))) def diffPopMean(l,m): 'returns an estimate of the difference in the population mean' return mean(l) - mean(m) def approx95conf(l,m): 'returns the approximate 95% confidence interval' dpm = diffPopMean(l,m) err = 2 * se2(l,m) return dpm - err, dpm + err def approx90conf(l,m): 'returns the approximate 90% confidence interval' dpm = diffPopMean(l,m) err = 1.645 * se2(l,m) return dpm - err, dpm + err 'point estimates' def p(l, threshold): """returns a point estimate of P, where threshold is the minimum number which denotes a success""" count = 0 for eachElement in l: if eachElement >= threshold: count = count + 1 return float(count) / float(n(l)) def pminusp(l,m,threshold): """returns an estimate of P - P, where threshold is the minimum number which denotes a success""" p1 = p(l,threshold) p2 = p(m,threshold) return p1 - p2 def se3(l,m,threshold): """returns the standard error of the estimate of P - P, where threshold is the minimun number which denotes a success""" p1 = p(l,threshold) p2 = p(m,threshold) a = p1 * (1 - p1) / n(l) b = p2 * (1 - p2) / n(m) return sqrt(a+b)