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
11 #include "rtc_base/rolling_accumulator.h"
12
13 #include <random>
14
15 #include "test/gtest.h"
16
17 namespace rtc {
18
19 namespace {
20
21 const double kLearningRate = 0.5;
22
23 // Add |n| samples drawn from uniform distribution in [a;b].
FillStatsFromUniformDistribution(RollingAccumulator<double> & stats,int n,double a,double b)24 void FillStatsFromUniformDistribution(RollingAccumulator<double>& stats,
25 int n,
26 double a,
27 double b) {
28 std::mt19937 gen{std::random_device()()};
29 std::uniform_real_distribution<> dis(a, b);
30
31 for (int i = 1; i <= n; i++) {
32 stats.AddSample(dis(gen));
33 }
34 }
35 } // namespace
36
TEST(RollingAccumulatorTest,ZeroSamples)37 TEST(RollingAccumulatorTest, ZeroSamples) {
38 RollingAccumulator<int> accum(10);
39
40 EXPECT_EQ(0U, accum.count());
41 EXPECT_DOUBLE_EQ(0.0, accum.ComputeMean());
42 EXPECT_DOUBLE_EQ(0.0, accum.ComputeVariance());
43 EXPECT_EQ(0, accum.ComputeMin());
44 EXPECT_EQ(0, accum.ComputeMax());
45 }
46
TEST(RollingAccumulatorTest,SomeSamples)47 TEST(RollingAccumulatorTest, SomeSamples) {
48 RollingAccumulator<int> accum(10);
49 for (int i = 0; i < 4; ++i) {
50 accum.AddSample(i);
51 }
52
53 EXPECT_EQ(4U, accum.count());
54 EXPECT_DOUBLE_EQ(1.5, accum.ComputeMean());
55 EXPECT_NEAR(2.26666, accum.ComputeWeightedMean(kLearningRate), 0.01);
56 EXPECT_DOUBLE_EQ(1.25, accum.ComputeVariance());
57 EXPECT_EQ(0, accum.ComputeMin());
58 EXPECT_EQ(3, accum.ComputeMax());
59 }
60
TEST(RollingAccumulatorTest,RollingSamples)61 TEST(RollingAccumulatorTest, RollingSamples) {
62 RollingAccumulator<int> accum(10);
63 for (int i = 0; i < 12; ++i) {
64 accum.AddSample(i);
65 }
66
67 EXPECT_EQ(10U, accum.count());
68 EXPECT_DOUBLE_EQ(6.5, accum.ComputeMean());
69 EXPECT_NEAR(10.0, accum.ComputeWeightedMean(kLearningRate), 0.01);
70 EXPECT_NEAR(9.0, accum.ComputeVariance(), 1.0);
71 EXPECT_EQ(2, accum.ComputeMin());
72 EXPECT_EQ(11, accum.ComputeMax());
73 }
74
TEST(RollingAccumulatorTest,ResetSamples)75 TEST(RollingAccumulatorTest, ResetSamples) {
76 RollingAccumulator<int> accum(10);
77
78 for (int i = 0; i < 10; ++i) {
79 accum.AddSample(100);
80 }
81 EXPECT_EQ(10U, accum.count());
82 EXPECT_DOUBLE_EQ(100.0, accum.ComputeMean());
83 EXPECT_EQ(100, accum.ComputeMin());
84 EXPECT_EQ(100, accum.ComputeMax());
85
86 accum.Reset();
87 EXPECT_EQ(0U, accum.count());
88
89 for (int i = 0; i < 5; ++i) {
90 accum.AddSample(i);
91 }
92
93 EXPECT_EQ(5U, accum.count());
94 EXPECT_DOUBLE_EQ(2.0, accum.ComputeMean());
95 EXPECT_EQ(0, accum.ComputeMin());
96 EXPECT_EQ(4, accum.ComputeMax());
97 }
98
TEST(RollingAccumulatorTest,RollingSamplesDouble)99 TEST(RollingAccumulatorTest, RollingSamplesDouble) {
100 RollingAccumulator<double> accum(10);
101 for (int i = 0; i < 23; ++i) {
102 accum.AddSample(5 * i);
103 }
104
105 EXPECT_EQ(10u, accum.count());
106 EXPECT_DOUBLE_EQ(87.5, accum.ComputeMean());
107 EXPECT_NEAR(105.049, accum.ComputeWeightedMean(kLearningRate), 0.1);
108 EXPECT_NEAR(229.166667, accum.ComputeVariance(), 25);
109 EXPECT_DOUBLE_EQ(65.0, accum.ComputeMin());
110 EXPECT_DOUBLE_EQ(110.0, accum.ComputeMax());
111 }
112
TEST(RollingAccumulatorTest,ComputeWeightedMeanCornerCases)113 TEST(RollingAccumulatorTest, ComputeWeightedMeanCornerCases) {
114 RollingAccumulator<int> accum(10);
115 EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(kLearningRate));
116 EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(0.0));
117 EXPECT_DOUBLE_EQ(0.0, accum.ComputeWeightedMean(1.1));
118
119 for (int i = 0; i < 8; ++i) {
120 accum.AddSample(i);
121 }
122
123 EXPECT_DOUBLE_EQ(3.5, accum.ComputeMean());
124 EXPECT_DOUBLE_EQ(3.5, accum.ComputeWeightedMean(0));
125 EXPECT_DOUBLE_EQ(3.5, accum.ComputeWeightedMean(1.1));
126 EXPECT_NEAR(6.0, accum.ComputeWeightedMean(kLearningRate), 0.1);
127 }
128
TEST(RollingAccumulatorTest,VarianceFromUniformDistribution)129 TEST(RollingAccumulatorTest, VarianceFromUniformDistribution) {
130 // Check variance converge to 1/12 for [0;1) uniform distribution.
131 // Acts as a sanity check for NumericStabilityForVariance test.
132 RollingAccumulator<double> stats(/*max_count=*/0.5e6);
133 FillStatsFromUniformDistribution(stats, 1e6, 0, 1);
134
135 EXPECT_NEAR(stats.ComputeVariance(), 1. / 12, 1e-3);
136 }
137
TEST(RollingAccumulatorTest,NumericStabilityForVariance)138 TEST(RollingAccumulatorTest, NumericStabilityForVariance) {
139 // Same test as VarianceFromUniformDistribution,
140 // except the range is shifted to [1e9;1e9+1).
141 // Variance should also converge to 1/12.
142 // NB: Although we lose precision for the samples themselves, the fractional
143 // part still enjoys 22 bits of mantissa and errors should even out,
144 // so that couldn't explain a mismatch.
145 RollingAccumulator<double> stats(/*max_count=*/0.5e6);
146 FillStatsFromUniformDistribution(stats, 1e6, 1e9, 1e9 + 1);
147
148 EXPECT_NEAR(stats.ComputeVariance(), 1. / 12, 1e-3);
149 }
150 } // namespace rtc
151