blob: e7d5b06226c79c7fa46dd1aa2893c1ca00649295 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ROLLINGACCUMULATOR_H_
12#define RTC_BASE_ROLLINGACCUMULATOR_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <algorithm>
15#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/constructormagic.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20namespace rtc {
21
22// RollingAccumulator stores and reports statistics
23// over N most recent samples.
24//
25// T is assumed to be an int, long, double or float.
26template<typename T>
27class RollingAccumulator {
28 public:
29 explicit RollingAccumulator(size_t max_count)
30 : samples_(max_count) {
31 Reset();
32 }
33 ~RollingAccumulator() {
34 }
35
36 size_t max_count() const {
37 return samples_.size();
38 }
39
40 size_t count() const {
41 return count_;
42 }
43
44 void Reset() {
45 count_ = 0U;
46 next_index_ = 0U;
47 sum_ = 0.0;
48 sum_2_ = 0.0;
49 max_ = T();
50 max_stale_ = false;
51 min_ = T();
52 min_stale_ = false;
53 }
54
55 void AddSample(T sample) {
56 if (count_ == max_count()) {
57 // Remove oldest sample.
58 T sample_to_remove = samples_[next_index_];
59 sum_ -= sample_to_remove;
60 sum_2_ -= static_cast<double>(sample_to_remove) * sample_to_remove;
61 if (sample_to_remove >= max_) {
62 max_stale_ = true;
63 }
64 if (sample_to_remove <= min_) {
65 min_stale_ = true;
66 }
67 } else {
68 // Increase count of samples.
69 ++count_;
70 }
71 // Add new sample.
72 samples_[next_index_] = sample;
73 sum_ += sample;
74 sum_2_ += static_cast<double>(sample) * sample;
75 if (count_ == 1 || sample >= max_) {
76 max_ = sample;
77 max_stale_ = false;
78 }
79 if (count_ == 1 || sample <= min_) {
80 min_ = sample;
81 min_stale_ = false;
82 }
83 // Update next_index_.
84 next_index_ = (next_index_ + 1) % max_count();
85 }
86
87 T ComputeSum() const {
88 return static_cast<T>(sum_);
89 }
90
91 double ComputeMean() const {
92 if (count_ == 0) {
93 return 0.0;
94 }
95 return sum_ / count_;
96 }
97
98 T ComputeMax() const {
99 if (max_stale_) {
100 RTC_DCHECK(count_ > 0) <<
101 "It shouldn't be possible for max_stale_ && count_ == 0";
102 max_ = samples_[next_index_];
103 for (size_t i = 1u; i < count_; i++) {
104 max_ = std::max(max_, samples_[(next_index_ + i) % max_count()]);
105 }
106 max_stale_ = false;
107 }
108 return max_;
109 }
110
111 T ComputeMin() const {
112 if (min_stale_) {
113 RTC_DCHECK(count_ > 0) <<
114 "It shouldn't be possible for min_stale_ && count_ == 0";
115 min_ = samples_[next_index_];
116 for (size_t i = 1u; i < count_; i++) {
117 min_ = std::min(min_, samples_[(next_index_ + i) % max_count()]);
118 }
119 min_stale_ = false;
120 }
121 return min_;
122 }
123
124 // O(n) time complexity.
125 // Weights nth sample with weight (learning_rate)^n. Learning_rate should be
126 // between (0.0, 1.0], otherwise the non-weighted mean is returned.
127 double ComputeWeightedMean(double learning_rate) const {
128 if (count_ < 1 || learning_rate <= 0.0 || learning_rate >= 1.0) {
129 return ComputeMean();
130 }
131 double weighted_mean = 0.0;
132 double current_weight = 1.0;
133 double weight_sum = 0.0;
134 const size_t max_size = max_count();
135 for (size_t i = 0; i < count_; ++i) {
136 current_weight *= learning_rate;
137 weight_sum += current_weight;
138 // Add max_size to prevent underflow.
139 size_t index = (next_index_ + max_size - i - 1) % max_size;
140 weighted_mean += current_weight * samples_[index];
141 }
142 return weighted_mean / weight_sum;
143 }
144
145 // Compute estimated variance. Estimation is more accurate
146 // as the number of samples grows.
147 double ComputeVariance() const {
148 if (count_ == 0) {
149 return 0.0;
150 }
151 // Var = E[x^2] - (E[x])^2
152 double count_inv = 1.0 / count_;
153 double mean_2 = sum_2_ * count_inv;
154 double mean = sum_ * count_inv;
155 return mean_2 - (mean * mean);
156 }
157
158 private:
159 size_t count_;
160 size_t next_index_;
161 double sum_; // Sum(x) - double to avoid overflow
162 double sum_2_; // Sum(x*x) - double to avoid overflow
163 mutable T max_;
164 mutable bool max_stale_;
165 mutable T min_;
166 mutable bool min_stale_;
167 std::vector<T> samples_;
168
169 RTC_DISALLOW_COPY_AND_ASSIGN(RollingAccumulator);
170};
171
172} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200174#endif // RTC_BASE_ROLLINGACCUMULATOR_H_