]> AND Private Git Repository - loba.git/blob - statistics.h
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Tracing API has changed. Disable it until its usage is fixed.
[loba.git] / statistics.h
1 #ifndef STATISTICS_H
2 #define STATISTICS_H
3
4 #include <cmath>
5 #include <limits>
6
7 class statistics {
8 public:
9     statistics()
10         : count(0)
11         , min(std::numeric_limits<double>::infinity())
12         , max(-std::numeric_limits<double>::infinity())
13         , sum(0.0)
14         , mean(0.0)
15         , sqdiff_sum(0.0)
16     { }
17
18     void push(double x) {
19         double delta = x - mean;
20         ++count;
21         if (x < min)
22             min = x;
23         if (x > max)
24             max = x;
25         sum += x;
26         mean = sum / count;
27         sqdiff_sum += delta * (x - mean);
28     }
29
30     unsigned get_count() const  { return count;                     }
31     double get_min() const      { return min;                       }
32     double get_max() const      { return max;                       }
33     double get_sum() const      { return sum;                       }
34     double get_mean() const     { return mean;                      }
35     double get_variance() const { return sqdiff_sum / count;        }
36     double get_stddev() const   { return std::sqrt(get_variance()); }
37
38 private:
39     int count;
40     double min;                 // min of x_i
41     double max;                 // max of x_i
42     double sum;                 // sum of x_i
43     double mean;                // mean of x_i
44     double sqdiff_sum;          // sum of (x_i - mean)^2
45 };
46
47 #endif // !STATISTICS_H
48
49 // Local variables:
50 // mode: c++
51 // End: