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

Private GIT Repository
Use online algorithms for statistics.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 25 Jan 2011 18:36:31 +0000 (19:36 +0100)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 25 Jan 2011 18:36:31 +0000 (19:36 +0100)
Makefile
main.cpp
statistics.cpp [deleted file]
statistics.h

index 2b0df87c2ccef2fa39d0860bb79a0cdf4cf99a9e..276053128325715e7761bb88b7c0245a81846888 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,6 @@ SRC.loba := main.cpp          \
        neighbor.cpp            \
        options.cpp             \
        process.cpp             \
        neighbor.cpp            \
        options.cpp             \
        process.cpp             \
-       statistics.cpp          \
        version.cpp
 
 SRC.simple_async := simple_async.cpp
        version.cpp
 
 SRC.simple_async := simple_async.cpp
index 4e8864cd941484c3b333c2f477ac4391275333d8..67af3feb393943f76da81e6974988b1e872d9869 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -88,7 +88,7 @@ static void check_for_lost_load()
 #define PR_STATS(descr, st)                                             \
     INFO5("| %.*s: %g / %g / %g", 39,                                   \
           descr " total/avg./stddev. at exit.........................", \
 #define PR_STATS(descr, st)                                             \
     INFO5("| %.*s: %g / %g / %g", 39,                                   \
           descr " total/avg./stddev. at exit.........................", \
-          st.get_sum(), st.get_avg(), st.get_stddev())
+          st.get_sum(), st.get_mean(), st.get_stddev())
 
 int main(int argc, char* argv[])
 {
 
 int main(int argc, char* argv[])
 {
diff --git a/statistics.cpp b/statistics.cpp
deleted file mode 100644 (file)
index 77f415f..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#include <algorithm>
-#include <cmath>
-#include <tr1/functional>
-#include <numeric>
-
-#include "statistics.h"
-
-statistics::statistics()
-    : up2date(false)
-{
-}
-
-void statistics::reset()
-{
-    values.clear();
-    up2date = false;
-}
-
-void statistics::push(double val)
-{
-    values.push_back(val);
-    up2date = false;
-}
-
-size_t statistics::get_count()
-{
-    return values.size();
-}
-
-double statistics::get_sum()
-{
-    update();
-    return sum;
-}
-
-double statistics::get_avg()
-{
-    update();
-    return avg;
-}
-
-double statistics::get_stddev()
-{
-    update();
-    return stddev;
-}
-
-void statistics::update()
-{
-    if (up2date)
-        return;
-
-    if (values.empty()) {
-        sum = 0.0;
-        avg = stddev = 0.0 / 0.0;
-    }
-
-    using std::tr1::bind;
-    using std::tr1::placeholders::_1;
-
-    unsigned n = values.size();
-    sum = std::accumulate(values.begin(), values.end(), 0.0);
-    avg = sum / n;
-
-    std::vector<double> diff(values);
-    std::transform(diff.begin(), diff.end(), diff.begin(),
-                   bind(std::minus<double>(), _1, avg));
-    double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0);
-    double square_sum = std::inner_product(diff.begin(), diff.end(),
-                                           diff.begin(), 0.0);
-    double variance = (square_sum - (epsilon * epsilon) / n) / n;
-    stddev = sqrt(variance);
-
-    up2date = true;
-}
index 2978747901e4d7ea5f3e4008f5ff307a92718206..7e3fb2ead86d5b70f48430a6d662710c3b44bd9c 100644 (file)
@@ -1,29 +1,37 @@
 #ifndef STATISTICS_H
 #define STATISTICS_H
 
 #ifndef STATISTICS_H
 #define STATISTICS_H
 
+#include <cmath>
 #include <vector>
 
 class statistics {
 public:
 #include <vector>
 
 class statistics {
 public:
-    statistics();
-
-    void reset();
-    void push(double value);
-
-    size_t get_count();
-    double get_sum();
-    double get_avg();
-    double get_stddev();
+    statistics()
+        : count(0)
+        , sum(0.0)
+        , mean(0.0)
+        , sqdiff_sum(0.0)
+    { }
+
+    void push(double x) {
+        double delta = x - mean;
+        ++count;
+        sum += x;
+        mean = sum / count;
+        sqdiff_sum += delta * (x - mean);
+    }
+
+    unsigned get_count() const  { return count;                }
+    double get_sum() const      { return sum;                  }
+    double get_mean() const     { return mean;                 }
+    double get_variance() const { return sqdiff_sum / count;   }
+    double get_stddev() const   { return sqrt(get_variance()); }
 
 private:
 
 private:
-    bool up2date;
-
-    std::vector<double> values;
-    double sum;
-    double avg;
-    double stddev;
-
-    void update();
+    int count;
+    double sum;                 // sum of x_i
+    double mean;                // mean of x_i
+    double sqdiff_sum;          // sum of (x_i - mean)^2
 };
 
 #endif // !STATISTICS_H
 };
 
 #endif // !STATISTICS_H