Created
December 3, 2014 21:02
-
-
Save goldsborough/39d41174ce451c617349 to your computer and use it in GitHub Desktop.
Statistical functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def average(values): | |
return sum(values)/len(values) | |
def median(values): | |
values.sort() | |
middle = int(len(values)/2) | |
if len(values) % 2 == 0: | |
return average((values[middle - 1],values[middle])) | |
else: | |
return values[middle] | |
def mode(values): | |
noDups = list(set(values)) | |
counts = [0 for i in noDups] | |
for x in values: | |
counts[noDups.index(x)] += 1 | |
maxCount = max(counts) | |
return [noDups[i] for i,x in enumerate(counts) if x == maxCount] | |
def meanSquare(values): | |
avg = average(values) | |
temp = sum([x**2 for x in values]) | |
temp /= len(values) | |
return temp - (avg**2) | |
def meanRootSquare(values): | |
return math.sqrt(meanSquare(values)) | |
def boxPlot(values): | |
values.sort() | |
minimum = values[0] | |
maximum = values[-1] | |
q2 = median(values) | |
q2Pos = int(len(values)/2) | |
if len(values) % 2 != 0: | |
q2Pos -= 1 | |
q1 = median(values[:q2Pos]) | |
q3 = median(values[q2Pos:]) | |
qDistance = q3 - q1 | |
return { | |
"minimum" : minimum, | |
"maximum" : maximum, | |
"q1" : q1, | |
"q2" : q2, | |
"q3" : q3, | |
"quartile distance" : qDistance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment