Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Last active January 1, 2016 21:49
Show Gist options
  • Save shotahorii/8206127 to your computer and use it in GitHub Desktop.
Save shotahorii/8206127 to your computer and use it in GitHub Desktop.
[statistics] Various Averages

Calculate various averages.

  • arithmetic mean
  • weighted arithmetic mean
  • percentile (including median and quartiles)
  • mode
  • mid-range
  • geometric mean
  • harmonic mean
#python version 3.3.3
#assume that all parameters satisfy pre-conditions.
#programs do not deal with wrong parameters.
import math
import functools
#arithmetic mean
#l: data(list)
def arithmetic(l):
return sum(l)/len(l)
#weighted arithmetic mean
#l: data(list), w: weight(list)
def weighted(l,w):
return sum([l[i]*w[i] for i in range(len(l))])/sum(w)
#percentile/quantile
#l: data(list)
#q: percentage 0<=q<=1
def percentile(l,q):
def helper(sl, i):
hi, lo = int(math.ceil(i)), int(math.floor(i))
return sl[int(i)] if i==int(i) else (hi-i)*sl[lo]+(i-lo)*sl[hi]
return helper(sorted(l), (len(l)-1)*q)
#median
#l: data(list)
def median(l):
return percentile(l,0.5)
#quartile
#l: data(list)
#returns: list of 5 q-quantiles (q=0,q=.25,q=.5,q=.75,q=1)
def quartile(l):
return [percentile(l,0.25*x) for x in range(5)]
#frequency dictionary
#l: data(list), d: dict to store the frequencies
#returns: frecency dictionary(dict)
def freqDict(l,d):
if not l:return d
if l[0] in d.keys():
d[l[0]] +=1
else:
d[l[0]] =1
return freqDict(l[1:],d)
#mode
#l: data(list)
#returns: mode(list). note that if multiple modes, all in the resulting list.
def mode(l):
d = freqDict(l,{})
m = max(d.values())
return [item[0] for item in d.items() if item[1]==m]
#mid-range
#l: data(list)
def midRange(l):
return (max(l)+min(l))/2.0
#geometric mean
#l: data(list)
def geometric(l):
return functools.reduce(lambda x,y:x*y,l)**(1/len(l))
#functools.reduce(lambda x,y:x*y,l) is 'product'
#here, the recursive expression
def product(l):
return 1 if not l else l[0]*product(l[1:])
#harmonic mean
#l: data(list)
def harmonic(l):
return len(l)/sum([1/l[i] for i in range(len(l))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment