Utils: Add stddev support to Statistics
g0dil [Tue, 29 Sep 2009 17:24:31 +0000 (17:24 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1464 270642c3-0616-0410-b53a-bc976706d245

senf/Utils/Statistics.cc
senf/Utils/Statistics.cci
senf/Utils/Statistics.hh
senf/Utils/Statistics.test.cc
senf/Utils/StatisticsTargets.ct
senf/Utils/StatisticsTargets.ih

index cc13424..4351cca 100644 (file)
@@ -27,6 +27,7 @@
 //#include "Statistics.ih"
 
 // Custom includes
+#include <cmath>
 #include <sstream>
 #include <senf/Utils/Console/Console.hh>
 #include "StatisticsTargets.hh"
 ///////////////////////////////////////////////////////////////////////////
 // senf::StatisticsBase
 
-prefix_ void senf::StatisticsBase::enter(float min, float avg, float max)
+prefix_ void senf::StatisticsBase::enter(float min, float avg, float max, float dev)
 {
     min_ = min;
     avg_ = avg;
     max_ = max;
+    dev_ = dev;
     generateOutput();
     signalChildren();
 }
@@ -120,16 +122,22 @@ std::string format_eng( float f)
 prefix_ void senf::StatisticsBase::consoleList(unsigned level, std::ostream & os)
     const
 {
-    os << boost::format("%s%-5d%|15t|  %12s  %12s  %12s\n") 
-        % std::string(2*level,' ') % rank() % format_eng(min()) % format_eng(avg()) % format_eng(max());
+    os << boost::format("%s%-5d%|15t|  %12s  %12s+/-%12s  %12s\n") 
+        % std::string(2*level,' ') 
+        % rank() 
+        % format_eng(min()) 
+        % format_eng(avg()) 
+        % format_eng(dev())
+        % format_eng(max());
     {
         OutputMap::const_iterator i (outputs_.begin());
         OutputMap::const_iterator i_end (outputs_.end());
         for (; i != i_end; ++i)
-            os << boost::format("            %3d  %12s  %12s  %12s\n")
+            os << boost::format("            %3d  %12s  %12s+/-%12s  %12s\n")
                 % i->second.n 
                 % format_eng(i->second.min/i->second.n) 
                 % format_eng(i->second.avg/i->second.n)
+                % format_eng(i->second.dev/i->second.n)
                 % format_eng(i->second.max/i->second.n);
     }
     {
@@ -142,14 +150,14 @@ prefix_ void senf::StatisticsBase::consoleList(unsigned level, std::ostream & os
 
 prefix_ void senf::StatisticsBase::generateOutput()
 {
-    queue_.push_front(QueueEntry(min_, avg_, max_));
+    queue_.push_front(QueueEntry(min_, avg_, max_, dev_));
     while (queue_.size() > maxQueueLen_)
         queue_.pop_back();
 
     OutputMap::iterator i (outputs_.begin());
     OutputMap::iterator const i_end (outputs_.end());
     for (; i != i_end; ++i) {
-        i->second.min = i->second.avg = i->second.max = 0.0f;
+        i->second.min = i->second.avg = i->second.max = i->second.dev = 0.0f;
         Queue::const_iterator j (queue_.begin());
         Queue::const_iterator const j_end (queue_.end());
         unsigned n (0);
@@ -157,8 +165,9 @@ prefix_ void senf::StatisticsBase::generateOutput()
             i->second.min += j->min;
             i->second.avg += j->avg;
             i->second.max += j->max;
+            i->second.dev += j->dev;
         }
-        i->second.signal(i->second.min/n, i->second.avg/n, i->second.max/n);
+        i->second.signal(i->second.min/n, i->second.avg/n, i->second.max/n, i->second.dev/n);
     }
 }
 
@@ -167,7 +176,7 @@ prefix_ void senf::StatisticsBase::signalChildren()
     Children::iterator i (children_.begin());
     Children::iterator const i_end  (children_.end());
     for (; i != i_end; ++i)
-        i->second.enter(min_, avg_, max_);
+        i->second.enter(min_, avg_, max_, dev_);
 }
 
 ///////////////////////////////////////////////////////////////////////////
@@ -188,6 +197,7 @@ prefix_ senf::Statistics::Statistics()
              "    WIN     Size of output average window.\n"
              "    MIN     Last entered minimum value.\n"
              "    AVG     Last entered average value.\n"
+             "    DEV     Standard deviation of average value over the collector rank.\n"
              "    MAX     Last entered maximum value.");
     dir.add("collect", &Statistics::consoleCollect)
         .doc("Add statistics collection groups. The argument gives a sequence of collector\n"
@@ -235,7 +245,7 @@ prefix_ senf::Statistics::Statistics()
 
 prefix_ void senf::Statistics::consoleList(std::ostream & os)
 {
-    os << "RANK        WIN           MIN           AVG           MAX\n";
+    os << "RANK        WIN           MIN           AVG            DEV           MAX\n";
     StatisticsBase::consoleList(0, os);
 }
 
@@ -289,16 +299,22 @@ prefix_ std::string senf::Statistics::v_path()
 ///////////////////////////////////////////////////////////////////////////
 // senf::Collector
 
-prefix_ void senf::Collector::enter(float min, float avg, float max)
+prefix_ void senf::Collector::enter(float min, float avg, float max, float dev)
 {
-    accAvg_ += avg;
+    accSum_ += avg;
+    accSumSq_ += avg*avg + dev*dev;
     if (min < accMin_) accMin_ = min;
     if (max > accMax_) accMax_ = max;
+    std::cerr << "! " << i_ << ' ' << avg << ' ' << dev << ' ' << accSum_ << ' ' << accSumSq_ 
+              << std::endl;
     if (++i_ >= rank_) {
-        StatisticsBase::enter(accMin_, accAvg_ / i_, accMax_);
+        float accAvg (accSum_ / i_);
+        float accDev (std::sqrt(accSumSq_ / i_ - accAvg*accAvg));
+        StatisticsBase::enter(accMin_, accAvg, accMax_, accDev);
         i_ = 0;
         accMin_ = FLT_MAX;
-        accAvg_ = 0.0f;
+        accSum_ = 0.0f;
+        accSumSq_ = 0.0f;
         accMax_ = -FLT_MAX;
     }
 }
index 1f109db..d58a9bd 100644 (file)
@@ -46,19 +46,19 @@ senf::StatisticsBase::Transform::operator()(first_argument_type i)
 // senf::StatisticsBase::OutputEntry
 
 prefix_ senf::StatisticsBase::OutputEntry::OutputEntry()
-    : n(), min(), avg(), max() 
+    : n(), min(), avg(), max(), dev()
 {
     initDir();
 }
 
 prefix_ senf::StatisticsBase::OutputEntry::OutputEntry(unsigned n_)
-    : n(n_), min(), avg(), max() 
+    : n(n_), min(), avg(), max(), dev()
 {
     initDir();
 }
 
 prefix_ senf::StatisticsBase::OutputEntry::OutputEntry(const OutputEntry& other)
-    : n(other.n), min(other.min), avg(other.avg), max(other.max) 
+    : n(other.n), min(other.min), avg(other.avg), max(other.max), dev(other.dev)
 {
     initDir();
 }
@@ -76,6 +76,7 @@ senf::StatisticsBase::OutputEntry::operator=(const OutputEntry& other)
     min = other.min;
     avg = other.avg;
     max = other.max;
+    dev = other.dev;
     return *this;
 }
 
@@ -120,6 +121,12 @@ prefix_ float senf::StatisticsBase::max()
     return max_;
 }
 
+prefix_ float senf::StatisticsBase::dev()
+    const
+{
+    return dev_;
+}
+
 prefix_ unsigned senf::StatisticsBase::rank()
     const
 {
@@ -141,7 +148,7 @@ prefix_ std::string senf::StatisticsBase::path()
 // senf::Collector
 
 prefix_ senf::Collector::Collector(StatisticsBase * owner, unsigned rank)
-    : rank_ (rank), i_ (0u), accMin_ (FLT_MAX), accAvg_ (0.0f), accMax_ (-FLT_MAX),
+    : rank_ (rank), i_ (0u), accMin_ (FLT_MAX), accSum_ (0.0f), accSumSq_ (0.0f), accMax_ (-FLT_MAX),
       owner_ (owner)
 {}
 
@@ -161,14 +168,14 @@ senf::Collector::output(unsigned n)
 ///////////////////////////////////////////////////////////////////////////
 // senf::Statistics
 
-prefix_ void senf::Statistics::operator()(float min, float avg, float max)
+prefix_ void senf::Statistics::operator()(float min, float avg, float max, float dev)
 {
-    enter(min, avg, max);
+    enter(min, avg, max, dev);
 }
 
-prefix_ void senf::Statistics::operator()(float value)
+prefix_ void senf::Statistics::operator()(float value, float dev)
 {
-    enter(value, value, value);
+    enter(value, value, value, dev);
 }
 
 prefix_ senf::StatisticsBase::OutputProxy<senf::Statistics>
index 25ebb59..f95f8bf 100644 (file)
@@ -111,7 +111,7 @@ namespace senf {
             // Function object
             struct Collector
             {
-                void operator()(float min, float avg, float max) 
+                void operator()(float min, float avg, float max, float dev) 
                     { ... }
             };
             \endcode
@@ -153,6 +153,7 @@ namespace senf {
         float min() const;              ///< Last min value entered
         float avg() const;              ///< Last avg value entered
         float max() const;              ///< Last max value entered
+        float dev() const;              ///< Last dev value entered
 
         virtual unsigned rank() const;  ///< Return collectors rank value
                                         /**< \returns number of basic values collected into each new
@@ -227,7 +228,7 @@ namespace senf {
     protected:
         StatisticsBase();
         virtual ~StatisticsBase();
-        void enter(float min, float avg, float max);
+        void enter(float min, float avg, float max, float dev);
 
     private:
         virtual Statistics & v_base() = 0;
@@ -239,15 +240,17 @@ namespace senf {
         float min_;
         float avg_;
         float max_;
+        float dev_;
         Children children_;
         
         struct QueueEntry {
             float min;
             float avg;
             float max;
-            QueueEntry() : min(), avg(), max() {}
-            QueueEntry(float min_, float avg_, float max_)
-                : min(min_), avg(avg_), max(max_) {}
+            float dev;
+            QueueEntry() : min(), avg(), max(), dev() {}
+            QueueEntry(float min_, float avg_, float max_, float dev_)
+                : min(min_), avg(avg_), max(max_), dev(dev_) {}
         };
         typedef std::deque<QueueEntry> Queue;
         Queue queue_;
@@ -282,8 +285,9 @@ namespace senf {
             float min;
             float avg;
             float max;
+            float dev;
 
-            boost::signal<void(float,float,float)> signal;
+            boost::signal<void(float,float,float,float)> signal;
             boost::ptr_vector<TargetBase> targets_;
 
             senf::console::ScopedDirectory<> dir;
@@ -416,7 +420,7 @@ namespace senf {
 
         Statistics();
 
-        void operator()(float min, float avg, float max);
+        void operator()(float min, float avg, float max, float dev=0.0f);
                                         ///< Enter new data
                                         /**< This member must be called whenever a new data value is
                                              available. It is important to call this member \e
@@ -429,9 +433,11 @@ namespace senf {
 
                                              \param[in] min minimal data value since last call
                                              \param[in] avg average data value since last call
-                                             \param[in] max maximal data values since last call */
+                                             \param[in] max maximal data values since last call
+                                             \param[in] dev standard deviation of avg value */
 
-        void operator()(float value);   ///< Same as enter() with \a min == \a avg == \a max
+        void operator()(float value, float dev=0.0f); 
+                                        ///< Same as enter() with \a min == \a avg == \a max
                                         /**< Provided so a Statistics instance can be directly used
                                              as a signal target. */
 
@@ -463,14 +469,15 @@ namespace senf {
         
     private:
         Collector(StatisticsBase * owner, unsigned rank);
-        void enter(float min, float avg, float max);
+        void enter(float min, float avg, float max, float dev);
         Statistics & v_base();
         std::string v_path() const;
 
         unsigned rank_;
         unsigned i_;
         float accMin_;
-        float accAvg_;
+        float accSum_;
+        float accSumSq_;
         float accMax_;
         StatisticsBase * owner_;
 
index 99c31e3..638783b 100644 (file)
@@ -109,61 +109,64 @@ BOOST_AUTO_UNIT_TEST(statistics)
     BOOST_CHECK_CLOSE( stats.min(), 0.4f, .1f );
     BOOST_CHECK_CLOSE( stats.avg(), 1.2f, .1f );
     BOOST_CHECK_CLOSE( stats.max(), 4.0f, .1f );
+    BOOST_CHECK_CLOSE( stats.dev(), 0.0f, .1f );
 
     BOOST_CHECK_CLOSE( stats[4].min(), -2.0f, .1f );
     BOOST_CHECK_CLOSE( stats[4].avg(), -0.05f, .1f );
     BOOST_CHECK_CLOSE( stats[4].max(), 1.1f, .1f );
+    BOOST_CHECK_CLOSE( stats[4].dev(), 1.08282f, .1f );
 
     BOOST_CHECK_CLOSE( stats[4][3].min(), -2.0f, .1f );
     BOOST_CHECK_CLOSE( stats[4][3].avg(), 1.15f, .1f );
     BOOST_CHECK_CLOSE( stats[4][3].max(),  3.8f, .1f );
 
     BOOST_CHECK_EQUAL( statslog.str(), 
-                       "level0 -1 2.3 2.5\n"
-                       "level0 -0.35 2.35 3.15\n"
-                       "level0 -0.4 1.05 1.9\n"
-                       "level0 -0.7 1.45 1.65\n"
-                       "level1 -1.1 1.9 3.8\n"
-                       "averaged1 -1.1 1.9 3.8\n"
-                       "level0 0.35 2.15 2.2\n"
-                       "level0 0.75 0.8 0.8\n"
-                       "level0 0.25 0.25 0.25\n"
-                       "level0 -1 -0.9 -0.5\n"
-                       "level1 -2 -0.05 1.1\n"
-                       "averaged1 -1.55 0.925 2.45\n"
-                       "level0 -1 0.25 0.7\n"
-                       "level0 0.2 1.75 2.2\n"
-                       "level0 -0.3 1.75 2.25\n"
-                       "level0 -0.35 2.35 3.15\n"
-                       "level1 -1 2.05 3.8\n"
-                       "averaged1 -1.36667 1.3 2.9\n"
-                       "level0 -0.4 1.05 1.9\n"
-                       "level0 -0.7 1.45 1.65\n"
-                       "level0 0.35 2.15 2.2\n"
-                       "level0 0.75 0.8 0.8\n"
-                       "level1 -1.1 1.125 3.3\n"
-                       "averaged1 -1.36667 1.04167 2.73333\n"
-                       "level0 0.25 0.25 0.25\n"
-                       "level0 -1 -0.9 -0.5\n"
-                       "level0 -1 0.25 0.7\n"
-                       "level0 0.2 1.75 2.2\n"
-                       "level1 -2 0.425 2.4\n"
-                       "averaged1 -1.36667 1.2 3.16667\n"
-                       "level0 -0.3 1.75 2.25\n"
-                       "level0 -0.35 2.35 3.15\n"
-                       "level0 -0.4 1.05 1.9\n"
-                       "level0 -0.7 1.45 1.65\n"
-                       "level1 -1.1 1.9 3.8\n"
-                       "averaged1 -1.4 1.15 3.16667\n"
-                       "level3 -2 1.225 3.8\n"
-                       "level0 0.35 2.15 2.2\n"
-                       "level0 0.75 0.8 0.8\n"
-                       "level0 0.25 0.25 0.25\n"
-                       "level0 -1 -0.9 -0.5\n"
-                       "level1 -2 -0.05 1.1\n"
-                       "averaged1 -1.7 0.758333 2.43333\n"
-                       "level0 -1 0.25 0.7\n"
-                       "level0 0.2 1.75 3.2\n" );
+                       "level0 -1 2.3 2.5 0\n"
+                       "level0 -0.35 2.35 3.15 0\n"
+                       "level0 -0.4 1.05 1.9 0\n"
+                       "level0 -0.7 1.45 1.65 0\n"
+                       "level1 -1.1 1.9 3.8 1.31719\n"
+                       "averaged1 -1.1 1.9 3.8 1.31719\n"
+                       "level0 0.35 2.15 2.2 0\n"
+                       "level0 0.75 0.8 0.8 0\n"
+                       "level0 0.25 0.25 0.25 0\n"
+                       "level0 -1 -0.9 -0.5 0\n"
+                       "level1 -2 -0.05 1.1 1.08282\n"
+                       "averaged1 -1.55 0.925 2.45 1.20001\n"
+                       "level0 -1 0.25 0.7 0\n"
+                       "level0 0.2 1.75 2.2 0\n"
+                       "level0 -0.3 1.75 2.25 0\n"
+                       "level0 -0.35 2.35 3.15 0\n"
+                       "level1 -1 2.05 3.8 0.492442\n"
+                       "averaged1 -1.36667 1.3 2.9 0.964152\n"
+                       "level0 -0.4 1.05 1.9 0\n"
+                       "level0 -0.7 1.45 1.65 0\n"
+                       "level0 0.35 2.15 2.2 0\n"
+                       "level0 0.75 0.8 0.8 0\n"
+                       "level1 -1.1 1.125 3.3 1.29687\n"
+                       "averaged1 -1.36667 1.04167 2.73333 0.957378\n"
+                       "level0 0.25 0.25 0.25 0\n"
+                       "level0 -1 -0.9 -0.5 0\n"
+                       "level0 -1 0.25 0.7 0\n"
+                       "level0 0.2 1.75 2.2 0\n"
+                       "level1 -2 0.425 2.4 1.52049\n"
+                       "averaged1 -1.36667 1.2 3.16667 1.10327\n"
+                       "level0 -0.3 1.75 2.25 0\n"
+                       "level0 -0.35 2.35 3.15 0\n"
+                       "level0 -0.4 1.05 1.9 0\n"
+                       "level0 -0.7 1.45 1.65 0\n"
+                       "level1 -1.1 1.9 3.8 1.31719\n"
+                       "averaged1 -1.4 1.15 3.16667 1.37818\n"
+                       "level3 -2 1.225 3.8 1.45752\n"
+                       "level0 0.35 2.15 2.2 0\n"
+                       "level0 0.75 0.8 0.8 0\n"
+                       "level0 0.25 0.25 0.25 0\n"
+                       "level0 -1 -0.9 -0.5 0\n"
+                       "level1 -2 -0.05 1.1 1.08282\n"
+                       "averaged1 -1.7 0.758333 2.43333 1.30683\n"
+                       "level0 -1 0.25 0.7 0\n"
+                       "level0 0.2 1.75 3.2 0\n" );
+
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
index 9bb386a..881ed18 100644 (file)
 template <class Stream, class Area, class Level>
 prefix_ void senf::detail::StatisticsLogger<Stream,Area,Level>::operator()(float min,
                                                                            float avg,
-                                                                           float max)
+                                                                           float max,
+                                                                           float dev)
 {
-    SENF_LOG_TPL((StatisticsStream)(Stream)(Area)(Level)(label << min << " " << avg << " " << max));
+    SENF_LOG_TPL((StatisticsStream)(Stream)(Area)(Level)(
+                     label << min << ' ' << avg << ' ' << max << ' ' << dev));
 }
 
 ///////////////////////////////ct.e////////////////////////////////////////
index 54c5d00..fcca853 100644 (file)
@@ -65,7 +65,7 @@ namespace detail {
         : boost::noncopyable
     {
         StatisticsLogger(std::string const & label_);
-        void operator()(float min, float avg, float max);
+        void operator()(float min, float avg, float max, float dev);
         
         std::string label;
     };