Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created December 3, 2014 21:02
Show Gist options
  • Save goldsborough/39d41174ce451c617349 to your computer and use it in GitHub Desktop.
Save goldsborough/39d41174ce451c617349 to your computer and use it in GitHub Desktop.
Statistical functions
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