blob: 806b1e3a1ca583f0559ad94ed607070fdf48e077 [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
59TEST(RunningStatisticsTest, FullSimpleTest) {
60 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
79TEST(RunningStatisticsTest, VarianceFromUniformDistribution) {
80 // Check variance converge to 1/12 for [0;1) uniform distribution.
81 // Acts as a sanity check for NumericStabilityForVariance test.
82 auto stats = CreateStatsFromUniformDistribution(1e6, 0, 1);
83
84 EXPECT_NEAR(*stats.GetVariance(), 1. / 12, 1e-3);
85}
86
87TEST(RunningStatisticsTest, NumericStabilityForVariance) {
88 // Same test as VarianceFromUniformDistribution,
89 // except the range is shifted to [1e9;1e9+1).
90 // Variance should also converge to 1/12.
91 // NB: Although we lose precision for the samples themselves, the fractional
92 // part still enjoys 22 bits of mantissa and errors should even out,
93 // so that couldn't explain a mismatch.
94 auto stats = CreateStatsFromUniformDistribution(1e6, 1e9, 1e9 + 1);
95
96 EXPECT_NEAR(*stats.GetVariance(), 1. / 12, 1e-3);
97}
98
99TEST_P(RunningStatisticsTest, MergeStatistics) {
100 int data[SIZE_FOR_MERGE] = {2, 2, -1, 5, 10};
101 // Split the data in different partitions.
102 // We have 6 distinct tests:
103 // * Empty merged with full sequence.
104 // * 1 sample merged with 4 last.
105 // * 2 samples merged with 3 last.
106 // [...]
107 // * Full merged with empty sequence.
108 // All must lead to the same result.
109 // I miss QuickCheck so much.
110 RunningStatistics<int> stats0, stats1;
111 for (int i = 0; i < GetParam(); ++i) {
112 stats0.AddSample(data[i]);
113 }
114 for (int i = GetParam(); i < SIZE_FOR_MERGE; ++i) {
115 stats1.AddSample(data[i]);
116 }
117 stats0.MergeStatistics(stats1);
118
119 EXPECT_EQ(stats0.Size(), SIZE_FOR_MERGE);
120 EXPECT_DOUBLE_EQ(*stats0.GetMin(), -1);
121 EXPECT_DOUBLE_EQ(*stats0.GetMax(), 10);
122 EXPECT_DOUBLE_EQ(*stats0.GetMean(), 3.6);
123 EXPECT_DOUBLE_EQ(*stats0.GetVariance(), 13.84);
124 EXPECT_DOUBLE_EQ(*stats0.GetStandardDeviation(), sqrt(13.84));
125}
126
127INSTANTIATE_TEST_SUITE_P(RunningStatisticsTests,
128 RunningStatisticsTest,
129 ::testing::Range(0, SIZE_FOR_MERGE + 1));
130
131} // namespace webrtc