Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/am_utils/am_onlinestatistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace am {
class OnlineStatistics {
public:
OnlineStatistics() : OnlineStatistics(0) {};
OnlineStatistics(size_t N) : ary_(0), cb(N) {};
OnlineStatistics(size_t N) : ary_(0), cb_(N) {};
void addData( double val );
void setSize( size_t N );
void insertion_sort(std::vector<double>& ary);
Expand All @@ -29,11 +29,11 @@ class OnlineStatistics {

private:
size_t N;
boost::circular_buffer<double> cb;
std::vector<double> ary_;
boost::circular_buffer<double> cb_;
double mean_; // mean, median
double m2n_; // sum of squares of differences from current mean
double var_, skew_, kurt_; // variance, skewness, and kurtosis
std::vector<double> ary_;
};


Expand Down
28 changes: 14 additions & 14 deletions src/am_onlinestatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@ namespace am {
*
*/
void OnlineStatistics::addData( double val ) {
if ( cb.size() == 0 ) {
if ( cb_.size() == 0 ) {
mean_ = val;
var_ = NAN;
m2n_ = 0.0;
} else if ( 1 + cb.size() > cb.capacity() ) {
double tmp = cb.back();
} else if ( 1 + cb_.size() > cb_.capacity() ) {
double tmp = cb_.back();
// std::cout << *this << std::endl;
double oldmean = mean_;
mean_ = mean_ + (val - tmp)/cb.size();
mean_ = mean_ + (val - tmp)/cb_.size();
double delta = mean_ - oldmean;
double alpha = val - tmp;
/* Fix this part here */
m2n_ = m2n_ + (delta*delta*cb.size()) + alpha*(alpha+2*tmp-2*mean_ );
var_ = m2n_ / cb.size();
m2n_ = m2n_ + (delta*delta*cb_.size()) + alpha*(alpha+2*tmp-2*mean_ );
var_ = m2n_ / cb_.size();
auto const ip = std::find( ary_.begin(), ary_.end(), tmp );
if ( ip != ary_.end() ) {
ary_.erase(ip);
}

} else {
double oldmean_ = mean_;
mean_ = (val - mean_)/(cb.size()+1) + mean_;
mean_ = (val - mean_)/(cb_.size()+1) + mean_;
m2n_ = m2n_ + ( val - oldmean_)*(val - mean_ );
var_ = m2n_ / (cb.size()+1);
var_ = m2n_ / (cb_.size()+1);
}
ary_.insert(ary_.begin(),val );
insertion_sort(ary_);
cb.push_front(val);
cb_.push_front(val);
// std::cout << *this << std::endl;
}

Expand All @@ -59,14 +59,14 @@ void OnlineStatistics::insertion_sort(std::vector<double>& ary) {

void OnlineStatistics::setSize( size_t N )
{
cb.set_capacity(N);
cb.erase(cb.begin(),cb.end());
cb_.set_capacity(N);
cb_.erase(cb_.begin(),cb_.end());
ary_.clear();
}

void OnlineStatistics::setDelayStats( am_utils::DelayStats &msg )
{
msg.count = cb.size();
msg.count = cb_.size();
msg.mean_delay = getMean();
msg.stddev_delay = sqrt(getVariance());
msg.min_delay = getMin();
Expand All @@ -77,8 +77,8 @@ void OnlineStatistics::setDelayStats( am_utils::DelayStats &msg )
std::ostream& operator<<(std::ostream& os, OnlineStatistics &stats) {
// os << stats.cb;
os << "[ ";
for ( auto it = stats.cb.begin() ; it != stats.cb.end(); ++it ) {
os << *it << ( it + 1 == stats.cb.end() ? "" : ", " );
for ( auto it = stats.cb_.begin() ; it != stats.cb_.end(); ++it ) {
os << *it << ( it + 1 == stats.cb_.end() ? "" : ", " );
}
os << "]";
return os;
Expand Down