blob: d77280a797e54b4382d8a3250263e9fa59375c40 [file] [log] [blame]
Yves Gerey890f62b2019-04-10 17:18:48 +02001/*
2 * Copyright (c) 2016 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/running_statistics.h"
12
13#include <math.h>
14#include <random>
15#include <vector>
16
17#include "absl/algorithm/container.h"
18#include "test/gtest.h"
19
20// Tests were copied from samples_stats_counter_unittest.cc.
21
22namespace webrtc {
23namespace {
24
25RunningStatistics<double> CreateStatsFilledWithIntsFrom1ToN(int n) {
26 std::vector<double> data;
27 for (int i = 1; i <= n; i++) {
28 data.push_back(i);
29 }
30 absl::c_shuffle(data, std::mt19937(std::random_device()()));
31
32 RunningStatistics<double> stats;
33 for (double v : data) {
34 stats.AddSample(v);
35 }
36 return stats;
37}
38
39// Add n samples drawn from uniform distribution in [a;b].
40RunningStatistics<double> CreateStatsFromUniformDistribution(int n,
41 double a,
42 double b) {
43 std::mt19937 gen{std::random_device()()};
44 std::uniform_real_distribution<> dis(a, b);
45
46 RunningStatistics<double> stats;
47 for (int i = 1; i <= n; i++) {
48 stats.AddSample(dis(gen));
49 }
50 return stats;
51}
52
53class RunningStatisticsTest : public ::testing::TestWithParam<int> {};
54
55constexpr int SIZE_FOR_MERGE = 5;
56
57} // namespace
58
Yves Gerey3dfb6802019-05-13 17:01:32 +020059TEST(RunningStatistics, FullSimpleTest) {
Yves Gerey890f62b2019-04-10 17:18:48 +020060 auto stats = CreateStatsFilledWithIntsFrom1ToN(100);
61
62 EXPECT_DOUBLE_EQ(*stats.GetMin(), 1.0);
63 EXPECT_DOUBLE_EQ(*stats.GetMax(), 100.0);
64 EXPECT_DOUBLE_EQ(*stats.GetMean(), 50.5);
65}
66
67TEST(RunningStatistics, VarianceAndDeviation) {
68 RunningStatistics<int> stats;
69 stats.AddSample(2);
70 stats.AddSample(2);
71 stats.AddSample(-1);
72 stats.AddSample(5);
73
74 EXPECT_DOUBLE_EQ(*stats.GetMean(), 2.0);
75 EXPECT_DOUBLE_EQ(*stats.GetVariance(), 4.5);
76 EXPECT_DOUBLE_EQ(*stats.GetStandardDeviation(), sqrt(4.5));
77}
78
Yves Gerey3dfb6802019-05-13 17:01:32 +020079TEST(RunningStatistics, RemoveSample) {
80 // We check that adding then removing sample is no-op,
81 // or so (due to loss of precision).
82 RunningStatistics<int> stats;
83 stats.AddSample(2);
84 stats.AddSample(2);
85 stats.AddSample(-1);
86 stats.AddSample(5);
87
88 constexpr int iterations = 1e5;
89 for (int i = 0; i < iterations; ++i) {
90 stats.AddSample(i);
91 stats.RemoveSample(i);
92
93 EXPECT_NEAR(*stats.GetMean(), 2.0, 1e-8);
94 EXPECT_NEAR(*stats.GetVariance(), 4.5, 1e-3);
95 EXPECT_NEAR(*stats.GetStandardDeviation(), sqrt(4.5), 1e-4);
96 }
97}
98
99TEST(RunningStatistics, RemoveSamplesSequence) {
100 // We check that adding then removing a sequence of samples is no-op,
101 // or so (due to loss of precision).
102 RunningStatistics<int> stats;
103 stats.AddSample(2);
104 stats.AddSample(2);
105 stats.AddSample(-1);
106 stats.AddSample(5);
107
108 constexpr int iterations = 1e4;
109 for (int i = 0; i < iterations; ++i) {
110 stats.AddSample(i);
111 }
112 for (int i = 0; i < iterations; ++i) {
113 stats.RemoveSample(i);
114 }
115
116 EXPECT_NEAR(*stats.GetMean(), 2.0, 1e-7);
117 EXPECT_NEAR(*stats.GetVariance(), 4.5, 1e-3);
118 EXPECT_NEAR(*stats.GetStandardDeviation(), sqrt(4.5), 1e-4);
119}
120
121TEST(RunningStatistics, VarianceFromUniformDistribution) {
Yves Gerey890f62b2019-04-10 17:18:48 +0200122 // Check variance converge to 1/12 for [0;1) uniform distribution.
123 // Acts as a sanity check for NumericStabilityForVariance test.
124 auto stats = CreateStatsFromUniformDistribution(1e6, 0, 1);
125
126 EXPECT_NEAR(*stats.GetVariance(), 1. / 12, 1e-3);
127}
128
Yves Gerey3dfb6802019-05-13 17:01:32 +0200129TEST(RunningStatistics, NumericStabilityForVariance) {
Yves Gerey890f62b2019-04-10 17:18:48 +0200130 // Same test as VarianceFromUniformDistribution,
131 // except the range is shifted to [1e9;1e9+1).
132 // Variance should also converge to 1/12.
133 // NB: Although we lose precision for the samples themselves, the fractional
134 // part still enjoys 22 bits of mantissa and errors should even out,
135 // so that couldn't explain a mismatch.
136 auto stats = CreateStatsFromUniformDistribution(1e6, 1e9, 1e9 + 1);
137
138 EXPECT_NEAR(*stats.GetVariance(), 1. / 12, 1e-3);
139}
140
Yves Gerey3dfb6802019-05-13 17:01:32 +0200141TEST(RunningStatistics, MinRemainsUnchangedAfterRemove) {
142 // We don't want to recompute min (that's RollingAccumulator's role),
143 // check we get the overall min.
144 RunningStatistics<int> stats;
145 stats.AddSample(1);
146 stats.AddSample(2);
147 stats.RemoveSample(1);
148 EXPECT_EQ(stats.GetMin(), 1);
149}
150
151TEST(RunningStatistics, MaxRemainsUnchangedAfterRemove) {
152 // We don't want to recompute max (that's RollingAccumulator's role),
153 // check we get the overall max.
154 RunningStatistics<int> stats;
155 stats.AddSample(1);
156 stats.AddSample(2);
157 stats.RemoveSample(2);
158 EXPECT_EQ(stats.GetMax(), 2);
159}
160
Yves Gerey890f62b2019-04-10 17:18:48 +0200161TEST_P(RunningStatisticsTest, MergeStatistics) {
162 int data[SIZE_FOR_MERGE] = {2, 2, -1, 5, 10};
163 // Split the data in different partitions.
164 // We have 6 distinct tests:
165 // * Empty merged with full sequence.
166 // * 1 sample merged with 4 last.
167 // * 2 samples merged with 3 last.
168 // [...]
169 // * Full merged with empty sequence.
170 // All must lead to the same result.
171 // I miss QuickCheck so much.
172 RunningStatistics<int> stats0, stats1;
173 for (int i = 0; i < GetParam(); ++i) {
174 stats0.AddSample(data[i]);
175 }
176 for (int i = GetParam(); i < SIZE_FOR_MERGE; ++i) {
177 stats1.AddSample(data[i]);
178 }
179 stats0.MergeStatistics(stats1);
180
181 EXPECT_EQ(stats0.Size(), SIZE_FOR_MERGE);
182 EXPECT_DOUBLE_EQ(*stats0.GetMin(), -1);
183 EXPECT_DOUBLE_EQ(*stats0.GetMax(), 10);
184 EXPECT_DOUBLE_EQ(*stats0.GetMean(), 3.6);
185 EXPECT_DOUBLE_EQ(*stats0.GetVariance(), 13.84);
186 EXPECT_DOUBLE_EQ(*stats0.GetStandardDeviation(), sqrt(13.84));
187}
188
189INSTANTIATE_TEST_SUITE_P(RunningStatisticsTests,
190 RunningStatisticsTest,
191 ::testing::Range(0, SIZE_FOR_MERGE + 1));
192
193} // namespace webrtc