henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_ROLLING_ACCUMULATOR_H_ |
| 12 | #define RTC_BASE_ROLLING_ACCUMULATOR_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 20 | #include "rtc_base/numerics/running_statistics.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | // RollingAccumulator stores and reports statistics |
| 25 | // over N most recent samples. |
| 26 | // |
| 27 | // T is assumed to be an int, long, double or float. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | template <typename T> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 29 | class RollingAccumulator { |
| 30 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | explicit RollingAccumulator(size_t max_count) : samples_(max_count) { |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 32 | RTC_DCHECK(max_count > 0); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 33 | Reset(); |
| 34 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | ~RollingAccumulator() {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | size_t max_count() const { return samples_.size(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 38 | |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 39 | size_t count() const { return static_cast<size_t>(stats_.Size()); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 40 | |
| 41 | void Reset() { |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 42 | stats_ = webrtc::RunningStatistics<T>(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | next_index_ = 0U; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 44 | max_ = T(); |
| 45 | max_stale_ = false; |
| 46 | min_ = T(); |
| 47 | min_stale_ = false; |
| 48 | } |
| 49 | |
| 50 | void AddSample(T sample) { |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 51 | if (count() == max_count()) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 52 | // Remove oldest sample. |
| 53 | T sample_to_remove = samples_[next_index_]; |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 54 | stats_.RemoveSample(sample_to_remove); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | if (sample_to_remove >= max_) { |
| 56 | max_stale_ = true; |
| 57 | } |
| 58 | if (sample_to_remove <= min_) { |
| 59 | min_stale_ = true; |
| 60 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | } |
| 62 | // Add new sample. |
| 63 | samples_[next_index_] = sample; |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 64 | if (count() == 0 || sample >= max_) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | max_ = sample; |
| 66 | max_stale_ = false; |
| 67 | } |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 68 | if (count() == 0 || sample <= min_) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | min_ = sample; |
| 70 | min_stale_ = false; |
| 71 | } |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 72 | stats_.AddSample(sample); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | // Update next_index_. |
| 74 | next_index_ = (next_index_ + 1) % max_count(); |
| 75 | } |
| 76 | |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 77 | double ComputeMean() const { return stats_.GetMean().value_or(0); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | |
| 79 | T ComputeMax() const { |
| 80 | if (max_stale_) { |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 81 | RTC_DCHECK(count() > 0) |
| 82 | << "It shouldn't be possible for max_stale_ && count() == 0"; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 83 | max_ = samples_[next_index_]; |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 84 | for (size_t i = 1u; i < count(); i++) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | max_ = std::max(max_, samples_[(next_index_ + i) % max_count()]); |
| 86 | } |
| 87 | max_stale_ = false; |
| 88 | } |
| 89 | return max_; |
| 90 | } |
| 91 | |
| 92 | T ComputeMin() const { |
| 93 | if (min_stale_) { |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 94 | RTC_DCHECK(count() > 0) |
| 95 | << "It shouldn't be possible for min_stale_ && count() == 0"; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 96 | min_ = samples_[next_index_]; |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 97 | for (size_t i = 1u; i < count(); i++) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | min_ = std::min(min_, samples_[(next_index_ + i) % max_count()]); |
| 99 | } |
| 100 | min_stale_ = false; |
| 101 | } |
| 102 | return min_; |
| 103 | } |
| 104 | |
| 105 | // O(n) time complexity. |
| 106 | // Weights nth sample with weight (learning_rate)^n. Learning_rate should be |
| 107 | // between (0.0, 1.0], otherwise the non-weighted mean is returned. |
| 108 | double ComputeWeightedMean(double learning_rate) const { |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 109 | if (count() < 1 || learning_rate <= 0.0 || learning_rate >= 1.0) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | return ComputeMean(); |
| 111 | } |
| 112 | double weighted_mean = 0.0; |
| 113 | double current_weight = 1.0; |
| 114 | double weight_sum = 0.0; |
| 115 | const size_t max_size = max_count(); |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 116 | for (size_t i = 0; i < count(); ++i) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 117 | current_weight *= learning_rate; |
| 118 | weight_sum += current_weight; |
| 119 | // Add max_size to prevent underflow. |
| 120 | size_t index = (next_index_ + max_size - i - 1) % max_size; |
| 121 | weighted_mean += current_weight * samples_[index]; |
| 122 | } |
| 123 | return weighted_mean / weight_sum; |
| 124 | } |
| 125 | |
| 126 | // Compute estimated variance. Estimation is more accurate |
| 127 | // as the number of samples grows. |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 128 | double ComputeVariance() const { return stats_.GetVariance().value_or(0); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 129 | |
| 130 | private: |
Yves Gerey | 3dfb680 | 2019-05-13 17:01:32 +0200 | [diff] [blame] | 131 | webrtc::RunningStatistics<T> stats_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 132 | size_t next_index_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 133 | mutable T max_; |
| 134 | mutable bool max_stale_; |
| 135 | mutable T min_; |
| 136 | mutable bool min_stale_; |
| 137 | std::vector<T> samples_; |
| 138 | |
| 139 | RTC_DISALLOW_COPY_AND_ASSIGN(RollingAccumulator); |
| 140 | }; |
| 141 | |
| 142 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 144 | #endif // RTC_BASE_ROLLING_ACCUMULATOR_H_ |