#!/usr/local/bin/python # $Id: moments.python,v 1.4 2001/05/09 00:58:40 doug Exp $ # http://www.bagley.org/~doug/shootout/ import sys, string, math, operator def main(): sum = 0 nums = [] atof = string.atof nums = map(atof, sys.stdin.readlines()) sum = reduce(operator.add, nums) n = len(nums) mean = sum/n average_deviation = 0 standard_deviation = 0 variance = 0 skew = 0 kurtosis = 0 for num in nums: deviation = num - mean average_deviation += abs(deviation) variance += deviation**2; skew += deviation**3; kurtosis += deviation**4 average_deviation /= n variance /= (n - 1) standard_deviation = math.sqrt(variance) if variance > 0.0: skew /= (n * variance * standard_deviation) kurtosis = kurtosis/(n * variance * variance) - 3.0 nums.sort() mid = n / 2 if (n % 2) == 0: median = (nums[mid] + nums[mid-1])/2 else: median = nums[mid] print "n: %d" % n print "median: %f" % median print "mean: %f" % mean print "average_deviation: %f" % average_deviation print "standard_deviation: %f" % standard_deviation print "variance: %f" % variance print "skew: %f" % skew print "kurtosis: %f" % kurtosis main()