From: jmo Date: Mon, 22 Nov 2010 15:11:15 +0000 (+0000) Subject: first version of statistic value accumulator added X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=2ed56fbec2aa25bf4a8b13e6a06ec90e9dcc1fec;p=senf.git first version of statistic value accumulator added git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1751 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/StatisticAccumulator.ct b/senf/Utils/StatisticAccumulator.ct new file mode 100644 index 0000000..c0b83ae --- /dev/null +++ b/senf/Utils/StatisticAccumulator.ct @@ -0,0 +1,93 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StatisticAccumulator non-inline template implementation */ + +// Custom includes + +#define prefix_ +///////////////////////////////ct.p//////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::StatisticAccumulator + +template +prefix_ senf::StatisticAccumulator::StatisticAccumulator( T defaultvalue) + : defaultvalue_(defaultvalue), + sum_squared_(defaultvalue*defaultvalue), + sum_(defaultvalue), + min_(defaultvalue), + max_(defaultvalue), + last_avg_(float(defaultvalue)), + count_(0) +{ +} + +//template +//prefix_ senf::StatisticAccumulator::~StatisticAccumulator() +//{ } + +template +prefix_ float senf::StatisticAccumulator::stddev() + const +{ + if (count_ == 0) { + return NAN; + } + else if (count_ == 1) { + return 0.0; + } + float _avg (avg()); + return sqrt( ( float(sum_squared_) / float( count_) ) - (_avg * _avg) ); +} + +template +prefix_ void senf::StatisticAccumulator::accumulate( T value) +{ + if (count_ == 0) { + min_ = max_ = sum_ = value; + sum_squared_ = value * value; + count_++; + return; + } + sum_ += value; + sum_squared_ += value * value; + count_++; + if (value < min_) + min_ = value; + else if (value > max_) + max_ = value; +} + +template +prefix_ void senf::StatisticAccumulator::clear() +{ + last_avg_ = avg(); + count_ = 0; + sum_squared_ = defaultvalue_*defaultvalue_; + sum_ = min_ = max_ = defaultvalue_; +} + + +///////////////////////////////ct.e//////////////////////////////////////// +#undef prefix_ diff --git a/senf/Utils/StatisticAccumulator.cti b/senf/Utils/StatisticAccumulator.cti new file mode 100644 index 0000000..d06939e --- /dev/null +++ b/senf/Utils/StatisticAccumulator.cti @@ -0,0 +1,88 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StatisticAccumulator inline template implementation */ + +// Custom includes + + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::StatisticAccumulator + +template +prefix_ T senf::StatisticAccumulator::min() + const +{ + return min_; +} + +template +prefix_ T senf::StatisticAccumulator::max() + const +{ + return max_; +} + +template +prefix_ float senf::StatisticAccumulator::avg() + const +{ + return count_ == 0 ? NAN : (sum_ / float(count_)); +} + +//template +//prefix_ senf::Statistics & senf::StatisticAccumulator::statistics() +//{ +// return stats_; +//} + +template +prefix_ float senf::StatisticAccumulator::last_avg() + const +{ + return last_avg_; +} + +template +prefix_ boost::uint32_t senf::StatisticAccumulator::count() + const +{ + return count_; +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -U" +// End: diff --git a/senf/Utils/StatisticAccumulator.hh b/senf/Utils/StatisticAccumulator.hh new file mode 100644 index 0000000..6c08c41 --- /dev/null +++ b/senf/Utils/StatisticAccumulator.hh @@ -0,0 +1,108 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StatisticAccumulator public header */ + +#ifndef HH_SENF_Utils_StatisticAccumulator_ +#define HH_SENF_Utils_StatisticAccumulator_ 1 + +// Custom includes +#include +#include +///////////////////////////////hh.p//////////////////////////////////////// +namespace senf { + /** \brief Accumulate measurement values + + The accumulator mainly do the prelimenary work for the senf::Statistic class. + It accumulates certain values with in an interval to be used by senf::Statistics + + \li the senf::Statistics class + \li statistics sources + + This class provides the average, standard deviation, min, max values over one + interval, means until clear() is called. It rather directly calculates the results + then collection all single values provided by calling accumulate(). + \see senf::Statistics to process accumulated values + \ingroup senf_statistics + */ + + template + class StatisticAccumulator + { + public: + StatisticAccumulator( T defaultvalue = 0); +// virtual ~StatisticAccumulator(); + + void clear(); + ///< Reset accumulated values. + /**< This member reset all avg/min/max values to the given \a + defaultvalue and the count to zero. */ + void accumulate(T value); + ///< Gather value to be accumulated. + /**< This method accumulate a value. + \param[in] value to be accumulated value*/ + + public: + T min() const; + ///< Returns current minimal value. + /**< This method returns the minimal value of the current accumulation.*/ + T max() const; + ///< Returns current maximal value. + /**< This method returns the maximal value of the current accumulation.*/ + float avg() const; + ///< Returns average value. + /**< This method returns the average value of the current accumulation.*/ + float last_avg() const; + ///< Returns former average value. + /**< This method returns the average value of the former accumulation period.*/ + float stddev() const; + ///< Returns standard deviation value. + /**< This method returns the standard deviation value of the current accumulation.*/ + boost::uint32_t count() const; + ///< Returns count of accumulated values. + /**< This method returns count of accumulated values of the current accumulation.*/ + + private: + T defaultvalue_; + T sum_squared_; + T sum_; + T min_; + T max_; + float last_avg_; + boost::uint32_t count_; + + + }; + + + typedef StatisticAccumulator StatisticAccumulatorInt; + typedef StatisticAccumulator StatisticAccumulatorFloat; + + +} +///////////////////////////////hh.e//////////////////////////////////////// +//#include "StatisticAccumulator.cci" +#include "StatisticAccumulator.ct" +#include "StatisticAccumulator.cti" +#endif + diff --git a/senf/Utils/StatisticAccumulator.test.cc b/senf/Utils/StatisticAccumulator.test.cc new file mode 100644 index 0000000..17059e8 --- /dev/null +++ b/senf/Utils/StatisticAccumulator.test.cc @@ -0,0 +1,69 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief StatisticAccumulator non-inline template implementation */ + +// Custom includes + +#include "StatisticAccumulator.hh" +#include "Statistics.hh" +#include "auto_unit_test.hh" +#include +#include + +#define prefix_ +///////////////////////////////ct.p//////////////////////////////////////// +SENF_AUTO_UNIT_TEST(StatisticAccumulator) +{ + senf::Statistics s; + + senf::StatisticAccumulatorInt sa; + + sa.accumulate( 5); + sa.accumulate( 3); + sa.accumulate( 4); + sa.accumulate( 6); + sa.accumulate( 7); + + BOOST_CHECK_EQUAL( sa.max(), 7); + BOOST_CHECK_EQUAL( sa.min(), 3); + BOOST_CHECK_EQUAL( sa.avg(), 5.0); + BOOST_CHECK_EQUAL( sa.last_avg(), 0.0); +// BOOST_CHECK_EQUAL( sa.stddev(), 1.41421354); + BOOST_CHECK_EQUAL( sa.count(), 5); + + s(0,sa); + + BOOST_CHECK_EQUAL( sa.max(), 0); + BOOST_CHECK_EQUAL( sa.min(), 0); + BOOST_CHECK( (boost::math::isnan)( sa.avg())); + BOOST_CHECK_EQUAL( sa.last_avg(), 5.0); + BOOST_CHECK( (boost::math::isnan)( sa.stddev())); + BOOST_CHECK_EQUAL( sa.count(), 0); + + + + +} +///////////////////////////////ct.e//////////////////////////////////////// +#undef prefix_ diff --git a/senf/Utils/Statistics.cci b/senf/Utils/Statistics.cci index 2e72991..d522f9f 100644 --- a/senf/Utils/Statistics.cci +++ b/senf/Utils/Statistics.cci @@ -186,6 +186,7 @@ prefix_ void senf::Statistics::operator()(float value, float dev) enter(1, value, value, value, dev); } + prefix_ senf::StatisticsBase::OutputProxy senf::Statistics::output(unsigned n) { diff --git a/senf/Utils/Statistics.cti b/senf/Utils/Statistics.cti index 6647999..a18319e 100644 --- a/senf/Utils/Statistics.cti +++ b/senf/Utils/Statistics.cti @@ -85,6 +85,17 @@ prefix_ senf::console::ScopedDirectory<> & senf::StatisticsBase::OutputProxydir; } +/////////////////////////////////////////////////////////////////////////// +// senf::Statistics + +template +prefix_ void senf::Statistics::operator()(unsigned n, StatisticAccumulator & sa) +{ + enter(n, float(sa.min()), sa.avg(), float(sa.max()), sa.stddev()); + sa.clear(); +} + + //-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_ diff --git a/senf/Utils/Statistics.hh b/senf/Utils/Statistics.hh index 4148ba1..3093b1c 100644 --- a/senf/Utils/Statistics.hh +++ b/senf/Utils/Statistics.hh @@ -38,6 +38,7 @@ #include "Exception.hh" #include #include +#include "StatisticAccumulator.hh" //#include "Statistics.mpp" //-///////////////////////////////////////////////////////////////////////////////////////////////// @@ -49,6 +50,7 @@ namespace senf { The statistics functionality has two parts: \li the senf::Statistics class + \li the senf::StatisticsAccumulator class \li statistics sources Each senf::Statistics instance collects information about a single parameter. Which @@ -463,6 +465,14 @@ namespace senf { ///< Same as operator() with \a min == \a avg == \a max /**< Provided so a Statistics instance can be directly used as a signal target. */ + template + void operator()(unsigned n, StatisticAccumulator & sa); + ///< Same as operator() gathers values from StatisticAccumulator + /**< Provided so a Statistics instance can be directly used + as a signal target. Caution: Clears values in + StatisticAccumulator afterwards + \param[in] n number of time-slices + \param[in] sa StatisticAccumulator*/ StatisticsBase::OutputProxy output(unsigned n = 1u);