// -*- mode: c++ -*- // $Id // http://www.bagley.org/~doug/shootout/ // Calculate statistical moments of a region, from Bill Lear // [dada] 2001-09-19 had to change power to pow for VC++ #include <iostream> #include <vector> #include <numeric> #include <iterator> #include <algorithm> #include <cstdio> #include <math.h> using namespace std; template <class T> struct moments { public: template <class InputIterator> moments(InputIterator begin, InputIterator end) : median(0.0), mean(0.0), average_deviation(0.0), standard_deviation(0.0), variance(0.0), skew(0.0), kurtosis(0.0) { T sum = accumulate(begin, end, 0.0); size_t N = end - begin; mean = sum / N; for (InputIterator i = begin; i != end; ++i) { T deviation = *i - mean; average_deviation += fabs(deviation); variance += pow(deviation, 2); skew += pow(deviation, 3); kurtosis += pow(deviation, 4); } average_deviation /= N; variance /= (N - 1); standard_deviation = sqrt(variance); if (variance) { skew /= (N * variance * standard_deviation); kurtosis = kurtosis/(N * variance * variance) - 3.0; } sort(begin, end); size_t mid = N/2; median = N % 2 ? *(begin+mid) : (*(begin+mid) + *(begin+mid-1))/2; } T median; T mean; T average_deviation; T standard_deviation; T variance; T skew; T kurtosis; }; int main() { vector<double> v; char line[8]; ios_base::sync_with_stdio(false); cin.tie(0); while (cin.getline(line, 8)) { v.push_back(atof(line)); } moments<double> m(v.begin(), v.end()); int n = v.end() - v.begin(); printf("n: %d\n", n); printf("median: %f\n", m.median); printf("mean: %f\n", m.mean); printf("average_deviation: %f\n", m.average_deviation); printf("standard_deviation: %f\n", m.standard_deviation); printf("variance: %f\n", m.variance); printf("skew: %f\n", m.skew); printf("kurtosis: %f\n", m.kurtosis); }