//#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();
}
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);
}
{
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);
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);
}
}
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_);
}
///////////////////////////////////////////////////////////////////////////
" 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"
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);
}
///////////////////////////////////////////////////////////////////////////
// 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;
}
}
// 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();
}
min = other.min;
avg = other.avg;
max = other.max;
+ dev = other.dev;
return *this;
}
return max_;
}
+prefix_ float senf::StatisticsBase::dev()
+ const
+{
+ return dev_;
+}
+
prefix_ unsigned senf::StatisticsBase::rank()
const
{
// 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)
{}
///////////////////////////////////////////////////////////////////////////
// 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>
// Function object
struct Collector
{
- void operator()(float min, float avg, float max)
+ void operator()(float min, float avg, float max, float dev)
{ ... }
};
\endcode
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
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;
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_;
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;
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
\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. */
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_;
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////////////////////////////////////////
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////////////////////////////////////////
: 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;
};