1 /*
2  *  Copyright 2019 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 
11 #include "rtc_base/numerics/event_based_exponential_moving_average.h"
12 
13 #include <cmath>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace {
18 
19 // For a normal distributed value, the 95% double sided confidence interval is
20 // is 1.96 * stddev.
21 constexpr double ninetyfive_percent_confidence = 1.96;
22 
23 }  // namespace
24 
25 namespace rtc {
26 
27 // |half_time| specifies how much weight will be given to old samples,
28 // a sample gets exponentially less weight so that it's 50%
29 // after |half_time| time units has passed.
EventBasedExponentialMovingAverage(int half_time)30 EventBasedExponentialMovingAverage::EventBasedExponentialMovingAverage(
31     int half_time) {
32   SetHalfTime(half_time);
33 }
34 
SetHalfTime(int half_time)35 void EventBasedExponentialMovingAverage::SetHalfTime(int half_time) {
36   tau_ = static_cast<double>(half_time) / log(2);
37   Reset();
38 }
39 
Reset()40 void EventBasedExponentialMovingAverage::Reset() {
41   value_ = std::nan("uninit");
42   sample_variance_ = std::numeric_limits<double>::infinity();
43   estimator_variance_ = 1;
44   last_observation_timestamp_.reset();
45 }
46 
AddSample(int64_t now,int sample)47 void EventBasedExponentialMovingAverage::AddSample(int64_t now, int sample) {
48   if (!last_observation_timestamp_.has_value()) {
49     value_ = sample;
50   } else {
51     // TODO(webrtc:11140): This should really be > (e.g not >=)
52     // but some pesky tests run with simulated clock and let
53     // samples arrive simultaneously!
54     RTC_DCHECK(now >= *last_observation_timestamp_);
55     // Variance gets computed after second sample.
56     int64_t age = now - *last_observation_timestamp_;
57     double e = exp(-age / tau_);
58     double alpha = e / (1 + e);
59     double one_minus_alpha = 1 - alpha;
60     double sample_diff = sample - value_;
61     value_ = one_minus_alpha * value_ + alpha * sample;
62     estimator_variance_ =
63         (one_minus_alpha * one_minus_alpha) * estimator_variance_ +
64         (alpha * alpha);
65     if (sample_variance_ == std::numeric_limits<double>::infinity()) {
66       // First variance.
67       sample_variance_ = sample_diff * sample_diff;
68     } else {
69       double new_variance = one_minus_alpha * sample_variance_ +
70                             alpha * sample_diff * sample_diff;
71       sample_variance_ = new_variance;
72     }
73   }
74   last_observation_timestamp_ = now;
75 }
76 
GetConfidenceInterval() const77 double EventBasedExponentialMovingAverage::GetConfidenceInterval() const {
78   return ninetyfive_percent_confidence *
79          sqrt(sample_variance_ * estimator_variance_);
80 }
81 
82 }  // namespace rtc
83