Skip to content

Instantly share code, notes, and snippets.

@sheymann
Created May 16, 2012 08:10
Show Gist options
  • Save sheymann/2708599 to your computer and use it in GitHub Desktop.
Save sheymann/2708599 to your computer and use it in GitHub Desktop.
Mean, Standard deviation and Skewness functions
import sys
import math
myvalues = [-3, -2, -1, -1, 0, 1, 2, 3, 7]
def moy(l):
sommeval = reduce(lambda somme, val: somme+val, l, 0)
return (float(sommeval)/len(l))
def stddev(l):
mu = moy(l)
n = len(l)
cur_est = 0
for x in l:
cur_est += (float(x)-mu)*(float(x)-mu)
return (float(cur_est)/(n-1))
def skew(l):
mu = moy(l)
sigma = math.sqrt(stddev(l))
n = len(l)
cur_est = 0
for x in l:
cur_est += ((float(x)-mu)/sigma)**3
return (cur_est*n)/((n-1)*(n-2))
sys.stdout.write("%f %f\n"%(skew(myvalues), moy(myvalues)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment