Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cmath>

#include <algorithm>
#include <deque>
#include <limits>
#include <mutex>
#include <numeric>
Expand Down Expand Up @@ -52,6 +53,9 @@ class MovingAverageStatistics
LIBSTATISTICS_COLLECTOR_PUBLIC
MovingAverageStatistics() = default;

LIBSTATISTICS_COLLECTOR_PUBLIC
explicit MovingAverageStatistics(std::size_t window_size);

LIBSTATISTICS_COLLECTOR_PUBLIC
~MovingAverageStatistics() = default;

Expand Down Expand Up @@ -131,6 +135,8 @@ class MovingAverageStatistics
double max_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = std::numeric_limits<double>::lowest();
double sum_of_square_diff_from_mean_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = 0;
uint64_t count_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = 0;
std::size_t window_size_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = 0;
std::deque<double> window_buffer_;
};

} // namespace moving_average_statistics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ namespace libstatistics_collector
namespace moving_average_statistics
{

MovingAverageStatistics::MovingAverageStatistics(std::size_t window_size)
: window_size_{window_size} {}

double MovingAverageStatistics::Average() const
{
return GetStatistics().average;
Expand Down Expand Up @@ -76,16 +79,35 @@ void MovingAverageStatistics::Reset()
max_ = std::numeric_limits<double>::lowest();
sum_of_square_diff_from_mean_ = 0;
count_ = 0;
window_buffer_.clear();
}

void MovingAverageStatistics::AddMeasurement(const double item)
{
std::lock_guard<std::mutex> guard{mutex_};

if (!std::isnan(item)) {
count_++;
const double previous_average = average_;
average_ = previous_average + (item - previous_average) / count_;
if (window_size_ == 0 || count_ < window_size_) {
count_++;
average_ = previous_average + (item - previous_average) / count_;
if (window_size_ != 0) {
window_buffer_.push_back(item);
}
} else if (window_size_ > 0) {
const double old_item = window_buffer_.front();
window_buffer_.pop_front();
average_ = previous_average + (item - old_item) / window_size_;
sum_of_square_diff_from_mean_ = sum_of_square_diff_from_mean_ - (old_item - average_) *
(old_item - previous_average);
if (std::abs(old_item - min_) < std::numeric_limits<double>::epsilon()) {
min_ = *std::min_element(window_buffer_.begin(), window_buffer_.end());
}
if (std::abs(old_item - max_) < std::numeric_limits<double>::epsilon()) {
max_ = *std::max_element(window_buffer_.begin(), window_buffer_.end());
}
window_buffer_.push_back(item);
}
min_ = std::min(min_, item);
max_ = std::max(max_, item);
sum_of_square_diff_from_mean_ = sum_of_square_diff_from_mean_ + (item - previous_average) *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,49 @@ TEST(MovingAverageStatisticsTest, TestPrettyPrinting) {
libstatistics_collector::moving_average_statistics::StatisticsDataToString(
stats.GetStatistics()));
}

TEST_F(MovingAverageStatisticsTestFixture, TestWindowSizeInitialization) {
constexpr size_t window_size = 5;
MovingAverageStatistics windowed_stats(window_size);

const auto data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

const double kExpectedAverage = 8.0;
const double kExpectedMinimum = 6.0;
const double kExpectedMaximum = 10.0;
const double kExpectedStd = 1.4142135623730951;
const int kExpectedSize = window_size;

for (auto d : data) {
windowed_stats.AddMeasurement(static_cast<double>(d));
}

EXPECT_EQ(windowed_stats.GetCount(), kExpectedSize);
EXPECT_DOUBLE_EQ(windowed_stats.Average(), kExpectedAverage);
EXPECT_DOUBLE_EQ(windowed_stats.Min(), kExpectedMinimum);
EXPECT_DOUBLE_EQ(windowed_stats.Max(), kExpectedMaximum);
EXPECT_DOUBLE_EQ(windowed_stats.StandardDeviation(), kExpectedStd);
}

TEST_F(MovingAverageStatisticsTestFixture, TestWindowSizeInitializationWithFewerData) {
constexpr size_t window_size = 5;
MovingAverageStatistics windowed_stats(window_size);

const auto data = {1, 2, 3};

const double kExpectedAverage = 2.0;
const double kExpectedMinimum = 1.0;
const double kExpectedMaximum = 3.0;
const double kExpectedStd = 0.816496580927726;
const int kExpectedSize = 3;

for (auto d : data) {
windowed_stats.AddMeasurement(static_cast<double>(d));
}

EXPECT_EQ(windowed_stats.GetCount(), kExpectedSize);
EXPECT_DOUBLE_EQ(windowed_stats.Average(), kExpectedAverage);
EXPECT_DOUBLE_EQ(windowed_stats.Min(), kExpectedMinimum);
EXPECT_DOUBLE_EQ(windowed_stats.Max(), kExpectedMaximum);
EXPECT_DOUBLE_EQ(windowed_stats.StandardDeviation(), kExpectedStd);
}
Loading