3 # $Id: moments.ruby,v 1.2 2005-06-10 00:57:22 igouy-guest Exp $
4 # http://www.bagley.org/~doug/shootout/
6 # throw away unused parameter sent by benchmark framework
14 for line
in STDIN.readlines()
23 average_deviation
= 0.0
24 standard_deviation
= 0.0
30 deviation
= num
- mean
31 average_deviation
+= deviation
.abs()
32 variance
+= deviation
**2;
34 kurtosis
+= deviation
**4
36 average_deviation
/= n
38 standard_deviation
= Math
.sqrt(variance
)
41 skew
/= (n
* variance
* standard_deviation
)
42 kurtosis
= kurtosis
/(n
* variance
* variance
) - 3.0
49 median
= (nums
.at(mid
) + nums
.at(mid-1
))/2
55 printf("median: %f\n", median
)
56 printf("mean: %f\n", mean
)
57 printf("average_deviation: %f\n", average_deviation
)
58 printf("standard_deviation: %f\n", standard_deviation
)
59 printf("variance: %f\n", variance
)
60 printf("skew: %f\n", skew
)
61 printf("kurtosis: %f\n", kurtosis
)