def findLSRline(dp): if !checkDPFormat(dp): print "ERROR: invalid dotplot format in findLSRline" return "invalid dotplot format" r = CorrCoef(dp) x = [i[0] for i in dp] sx = stats.stdDev(x) y = [i[1] for i in dp] sy = stats.stdDev(y) b = (r * sy) / sx a = stats.mean(y) - b * stats.mean(x)
def Std_EnergyOverTime(bouncecumulative): #calc StdDev for energy development over time for n in range(0, g.nbnc + 1): paraAvg[1, n] = stats.stdDev(Bounce[9, n, :], paraAvg[0, n], (g.NUM_OF_JOBS * g.NUM_OF_TRAJ) - bouncecumulative[n]) normAvg[1, n] = stats.stdDev(Bounce[10, n, :], normAvg[0, n], (g.NUM_OF_JOBS * g.NUM_OF_TRAJ) - bouncecumulative[n]) kinAvg[1, n] = stats.stdDev( Bounce[10, n, :] + Bounce[9, n, :], kinAvg[0, n], (g.NUM_OF_JOBS * g.NUM_OF_TRAJ) - bouncecumulative[n])
def calculateDividingLine(gestures, maybeGestures, nonGestures): numFolds = min(TESTING_FOLDS, len(gestures)) allGestureDistances = [] allNonGestureDistances = [] for foldNum in range(numFolds): trainingGestures = [gesture for i, gesture in enumerate(gestures) if i % numFolds != foldNum] testingGestures = [localTimeGestures for i, localTimeGestures in enumerate(maybeGestures) if i % numFolds == foldNum] #print 'train, test #s: ', len(trainingGestures), len(testingGestures) #make a distance calculator based on the subset of hte training data distanceCalculator = gestureDistanceCalculator.GestureDistanceCalculator(trainingGestures) #each localTimeGestures is a list of the closest times to when a gesture was identified in training #since the output can be triggered at slightly different times, we should look for a minimum near where #the gesture is known to have happened, compared to the training gestures gestureDistances = [] #print testingGestures for localTimeGestureSet in testingGestures: closestDistance = min(map(distanceCalculator.getDistance, localTimeGestureSet)) gestureDistances.append(closestDistance) #gestureDistances = map(distanceCalculator.getDistance, testingGestures) #print gestureDistances nonGestureDistances = map(distanceCalculator.getDistance, nonGestures) #print gestureDistances allGestureDistances += gestureDistances allNonGestureDistances += nonGestureDistances #break #print len(allGestureDistances), len(allNonGestureDistances) print 'means: ', stats.mean(allGestureDistances), stats.mean(allNonGestureDistances) print 'std devs: ', stats.stdDev(allGestureDistances), stats.stdDev(allNonGestureDistances) meanGesture = stats.mean(allGestureDistances) meanNon = stats.mean(allNonGestureDistances) devGesture = stats.stdDev(allGestureDistances) devNon = stats.stdDev(allNonGestureDistances) line = (meanGesture * devNon + meanNon * devGesture) / ( devGesture + devNon) #print line return line
def scaleTestMinFinding(): xs = range(10) distances = [] noise = 3.5 n = 1000000 for i in range(n): a = random() b = random() c = random() ys = [x*x*a + x*b + c + random() * noise for x in xs] #print a, b, c, polynomialFit(xs, ys)[::-1] minExp, unc = polynomialFindMinimum(xs, ys, returnErrors = True) minCalc = -b/(2.0*a) dist = (minCalc - minExp) / unc #print minCalc, minExp, unc, dist distances.append(dist) print 'mean: %f' % stats.mean(distances) print 'stdDev: %f' % stats.stdDev(distances) for sigma in [1, 2, 3]: print 'With %d sigma: %f%%' % (sigma, 100.0 * sum([int(abs(d) < sigma) for d in distances]) / n) pylab.hist(distances, bins = 50, range = (-5, 5)) pylab.show()
def CorrCoef(data):#format: [[x,y],[x,y]..] where [x,y] represents 1 individual if !checkDPFormat(data): print "ERROR: invalid dotplot format in CorrCoef" return 2 x = [i[0] for i in data] y = [i[1] for i in data] xb = stats.mean(x) yb = stats.mean(y) if xb == "N/A" or yb == "N/A": print "ERROR: failed to calculate mean in CorrCoef" return 3 sx = stats.stdDev(x) sy = stats.stdDev(y) if sx == -1 or sy == -1: print "ERROR: failed to calculate stdDev in CorrCoef" return 4 s = 0 for i in data: s += ((i[0] - xb) / sx) * ((i[1] - yb) / sy) return s / (len(data) - 1)
def testSingleStdDev(self): # passing only one data point to stdDev() must return zero (no deviation!) self.assertEqual(stats.stdDev([5]), 0.0)
distance, closest, systemOutput = gr.getOutput(input) s = '\t'.join([str(i), str([round(i[0], 2) for i in gestureDistanceCalculator.normalizeData(input)]), str(round(distance, 2))]) if distance < DISTANCE_TO_PRINT: print s fout.write(s + '\n') allDistances.append(distance) #elapsed = time.time() - start #numPoints = len(data) - l1 - l2 #print 'took %s for %s points' % (elapsed, numPoints) avDistance = stats.mean(allDistances) stdDevDistance = stats.stdDev(allDistances) print 'average distance: %s with stdDev %s' % (avDistance, stdDevDistance) fout.close() def testGestureRecognizer(): data = loadData(filename = '../data2.txt') gesturesDict, maybeGesturesDict = realtimeGR1.extractGesturesAndMaybe1(data) gestures = gesturesDict[0] maybeGestures = maybeGesturesDict[0] #print maybeGestures nonGestures = realtimeGR1.extractNonGestures(data)
def GetConvergence(data, plot = True, initialOffset = 0.0): baseInterval = rawSps / (samplesPerSecond + initialOffset) channelIntervals = [baseInterval + rangePercentage * i * 0.02 for i in range(-channels/2+1, channels/2+1)] sampleFrequencies = map(lambda x: rawSps / x, channelIntervals) channelLeakages = [[] for _ in range(channels)] intervalCounters = [0.0] * channels buffers = [[] for _ in range(channels)] lastSample = 0.0 startIndex = int(math.ceil(windowSize * max(channelIntervals))) for index, sample in enumerate(data): #add the sample to channels if necessary for c in range(channels): intervalCounters[c] += 1.0 if intervalCounters[c] > channelIntervals[c]: intervalCounters[c] %= channelIntervals[c] weight0 = min(1.0, intervalCounters[c]) weight1 = max(0.0, 1.0 - intervalCounters[c]) interpolated = weight0 * sample + weight1 * lastSample buffers[c].append(interpolated) if index > startIndex and index % overlap == 0: for c in range(channels): leakage = getToneLeakage(buffers[c][-windowSize:]) channelLeakages[c].append(leakage) if len(channelLeakages[0]) >= measurementCount: print 'total data collection time: %d ms' % (1000 * index / rawSps) break lastSample = sample means = [] stdDevs = [] for c in range(channels): means.append(stats.mean(channelLeakages[c])) stdDevs.append(stats.stdDev(channelLeakages[c])) errors = stdDevs for ys in zip(*channelLeakages): pylab.plot(sampleFrequencies, ys, 'bo') pylab.errorbar(sampleFrequencies, means, yerr=stdDevs, fmt='co') #print '\n'.join([str(x) for x in zip(sampleFrequencies, means, stdDevs)]) #get the fit nextInterval, error = polyFitTest.polynomialFindMinimum(channelIntervals, means, errors = errors, order = 3, returnErrors = True) print nextInterval, error sps = rawSps / nextInterval sigma_sps = rawSps /(nextInterval**2.0) * error print 'Calculated samples per second: %f +/- %f' % (sps, sigma_sps) z = numpy.poly1d(polyFitTest.polynomialFit(sampleFrequencies, means, errors = errors, order = 3)[::-1]) xp = numpy.linspace(min(sampleFrequencies)-2, max(sampleFrequencies)+2, 100) yp = z(xp) pylab.plot(xp, yp, 'g') #mins = [sps, sps-sigma_sps, sps+sigma_sps] pylab.errorbar(sps, z(sps), xerr=[sigma_sps], fmt='ro') pylab.grid(True) pylab.show()
def testDefaultPrecisionStdDev(self): X = [1, 2, -2, 4, -3] self.assertEqual(stats.stdDev(X), 2.577)
import stats from matplotlib import pylab f = open("ukPowerFrequencies.txt") frequencies = [float(line.split("\t")[-1]) for line in f] print "# Points: %s" % len(frequencies) print "mean: %s" % stats.mean(frequencies) print "stdDev: %s" % stats.stdDev(frequencies) print "range: (%s - %s)" % (min(frequencies), max(frequencies)) pylab.hist(frequencies, 50) pylab.show()
import sys sys.path.append(sys.path[0] + "/tools/") import stats import parsers data = parsers.parseCSV(sys.path[0] + "/dataSets/sdintuition.csv") info = data.pop(0) for i in range(0, len(info)): print info[i] + ": " + str(stats.stdDev([j[i] for j in data]))
def GetExpectedError(algorithmClass, n = 1000): import multiprocessing pool = multiprocessing.Pool(7) partialConverge = functools.partial(ConvergeFunc, algorithmClass) offsets = [10*(random() - 0.5) for _ in xrange(n)] leakages = [] for i, leakage in enumerate(pool.imap(partialConverge, offsets), 1): sys.stderr.write('\rdone {0:%}'.format(float(i)/n)) leakages.append(leakage) #print leakages print '\r%s: Average error: %f \t+/-: %f' % (algorithmClass.__name__, stats.mean(leakages), stats.stdDev(leakages))
def testOtherPrecisionStdDev(self): X = [1, 2, -2, 4, -3] self.assertEqual(stats.stdDev(X,4), 2.5768)
def testOtherPrecisionStdDev(self): X = [1, 2, -2, 4, -3] self.assertEqual(stats.stdDev(X, 4), 2.5768)
def main (): data = stats.getNumbers() xbar = stats.mean(data) std = stats.stdDev(data) xbar2, std2 = stats.meanStdDev(data) print (xbar, std, xbar2, std2)