// RollingAccumulator stores and reports statistics // over N most recent samples. // // T is assumed to be an int, long, double or float. template <typename T> class RollingAccumulator { public:
explicit RollingAccumulator(size_t max_count) : samples_(max_count) {
RTC_DCHECK(max_count > 0);
Reset();
}
~RollingAccumulator() {}
T ComputeMax() const {
if (max_stale_) {
RTC_DCHECK(count() > 0)
<< "It shouldn't be possible for max_stale_ && count() == 0";
max_ = samples_[next_index_];
for (size_t i = 1u; i < count(); i++) {
max_ = std::max(max_, samples_[(next_index_ + i) % max_count()]);
}
max_stale_ = false;
} return max_;
}
T ComputeMin() const {
if (min_stale_) {
RTC_DCHECK(count() > 0)
<< "It shouldn't be possible for min_stale_ && count() == 0";
min_ = samples_[next_index_];
for (size_t i = 1u; i < count(); i++) {
min_ = std::min(min_, samples_[(next_index_ + i) % max_count()]);
}
min_stale_ = false;
} return min_;
}
// O(n) time complexity. // Weights nth sample with weight (learning_rate)^n. Learning_rate should be // between (0.0, 1.0], otherwise the non-weighted mean is returned. double ComputeWeightedMean(double learning_rate) const {
if (count() < 1 || learning_rate <= 0.0 || learning_rate >= 1.0) { return ComputeMean();
} double weighted_mean = 0.0; double current_weight = 1.0; double weight_sum = 0.0; const size_t max_size = max_count();
for (size_t i = 0; i < count(); ++i) {
current_weight *= learning_rate;
weight_sum += current_weight; // Add max_size to prevent underflow.
size_t index = (next_index_ + max_size - i - 1) % max_size;
weighted_mean += current_weight * samples_[index];
} return weighted_mean / weight_sum;
}
// Compute estimated variance. Estimation is more accurate // as the number of samples grows. double ComputeVariance() const { return stats_.GetVariance().value_or(0); }
private:
webrtc_impl::RunningStatistics<T> stats_;
size_t next_index_;
mutable T max_;
mutable bool max_stale_;
mutable T min_;
mutable bool min_stale_;
std::vector<T> samples_;
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.