Calculate various measures of dispersion.
- Naive range of data
- Quartile deviation
- Mean deviation
- Variance
- Standard deviation
- Weighted variance
- Weighted standard deviation
- Coefficient of variation
Needs averages.py
Calculate various measures of dispersion.
Needs averages.py
#needs averages.py (https://gist.github.com/shotahorii/8206127) | |
#python version 3.3.3 | |
#assume that all parameters satisfy pre-conditions. | |
#programs do not deal with wrong parameters. | |
import averages as avg | |
import math | |
#calculates raw range of data. | |
#l: list of numbers | |
def naiveRange(l): | |
return max(l)-min(l) | |
#calculates quartile deviation. | |
#l: list of numbers | |
def quartileDev(l): | |
return (avg.percentile(l,.75)-avg.percentile(l,.25))/2.0 | |
#calculates mean deviation. | |
#l: list of numbers | |
def meanDev(l): | |
m = avg.arithmetic(l) | |
return sum([math.fabs(l[i]-m) for i in range(len(l))])/len(l) | |
#calculates variance. | |
#l: list of numbers | |
def variance(l): | |
m = avg.arithmetic(l) | |
return sum([(l[i]-m)**2 for i in range(len(l))])/len(l) | |
#calculates standard deviation. | |
#l: list of numbers | |
def stdDev(l): | |
return variance(l)**0.5 | |
#calculates weighted variance. | |
#l: list of numbers(data), w: list of numbers(weight) | |
def weightedVariance(l,w): | |
m = avg.weighted(l,w) | |
return sum([w[i]*((l[i]-m)**2) for i in range(len(l))])/sum(w) | |
#calculates weighted standard deviation. | |
#l: list of numbers(data), w: list of numbers(weight) | |
def weightedStdDev(l,w): | |
return weightedVariance(l,w)**0.5 | |
#calculates coefficient of variation | |
#l: list of numbers | |
def coef(l): | |
return stdDev(l)/avg.arithmetic(l) |