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

Private GIT Repository
Bug fix: only send to a lesser loaded neighbor!
[loba.git] / statistics.cpp
1 #include <algorithm>
2 #include <cmath>
3 #include <tr1/functional>
4 #include <numeric>
5
6 #include "statistics.h"
7
8 statistics::statistics()
9     : up2date(false)
10 {
11 }
12
13 void statistics::reset()
14 {
15     values.clear();
16     up2date = false;
17 }
18
19 void statistics::push(double val)
20 {
21     values.push_back(val);
22     up2date = false;
23 }
24
25 size_t statistics::get_count()
26 {
27     return values.size();
28 }
29
30 double statistics::get_sum()
31 {
32     update();
33     return sum;
34 }
35
36 double statistics::get_avg()
37 {
38     update();
39     return avg;
40 }
41
42 double statistics::get_stddev()
43 {
44     update();
45     return stddev;
46 }
47
48 void statistics::update()
49 {
50     if (up2date)
51         return;
52
53     if (values.empty()) {
54         sum = 0.0;
55         avg = stddev = 0.0 / 0.0;
56     }
57
58     using std::tr1::bind;
59     using std::tr1::placeholders::_1;
60
61     unsigned n = values.size();
62     sum = std::accumulate(values.begin(), values.end(), 0.0);
63     avg = sum / n;
64
65     std::vector<double> diff(values);
66     std::transform(diff.begin(), diff.end(), diff.begin(),
67                    bind(std::minus<double>(), _1, avg));
68     double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0);
69     double square_sum = std::inner_product(diff.begin(), diff.end(),
70                                            diff.begin(), 0.0);
71     double variance = (square_sum - (epsilon * epsilon) / n) / n;
72     stddev = sqrt(variance);
73
74     up2date = true;
75 }