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