-
Notifications
You must be signed in to change notification settings - Fork 2
/
DynamicKDETest.cpp
179 lines (147 loc) · 4.69 KB
/
DynamicKDETest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include "DynamicDensity.pb.h"
#include "DynamicKDE.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using dyden::DynamicKDE;
using dyden::DynamicKDEOpts;
using dyden::Kernel;
using dynamic_density::DensityMap;
using testing::ContainerEq;
using testing::Eq;
using testing::Types;
// From https://en.wikipedia.org/wiki/Geometric_series#Formula
// Args:
// a: First term in series.
// r: Rate. Decay rate is 1 - rate in this formula.
// n: Number of terms in series.
double exponential_sum(double a, double r, int n) {
return a * (1.0 - pow(r, n)) / (1 - r);
}
TEST(KernelTest, addValue) {
Kernel k;
k.addValue(1.0, 1.0);
k.addValue(3.0, 1.0);
k.addValue(5.0, 1.0);
k.addValue(7.0, 1.0);
EXPECT_EQ(k.mean(), 4.0);
EXPECT_EQ(k.variance(), 5.0);
EXPECT_EQ(k.count(), 4.0);
}
TEST(KernelTest, decay) {
Kernel k;
k.addValue(1.0, 1.0);
k.addValue(3.0, 1.0);
k.addValue(5.0, 1.0);
k.addValue(7.0, 1.0);
k.decay(0.9, 5);
EXPECT_EQ(k.mean(), 4.0);
EXPECT_EQ(k.variance(), 5.0);
EXPECT_EQ(k.count(), 3.6);
}
TEST(KernelTest, weightedAddValue) {
Kernel k;
k.addValue(1.0, 0.5);
k.addValue(3.0, 0.5);
k.addValue(5.0, 0.5);
k.addValue(7.0, 0.5);
EXPECT_EQ(k.mean(), 4.0);
EXPECT_EQ(k.variance(), 5.0);
EXPECT_EQ(k.count(), 2.0);
}
TEST(KernelTest, cdf) {
Kernel k;
k.addValue(1.0, 0.5);
k.addValue(3.0, 0.5);
k.addValue(5.0, 0.5);
k.addValue(7.0, 0.5);
EXPECT_EQ(k.cdf(4.0), 0.5);
// In R:
// > pnorm(5, 4, sqrt(5))
// [1] 0.6726396
EXPECT_NEAR(k.cdf(5.0), 0.6726396, 1e-6);
EXPECT_NEAR(k.cdf(-100.0), 0.0, 1e-10);
EXPECT_NEAR(k.cdf(100.0), 1.0, 1e-10);
}
TEST(KernelTest, populateProto) {
Kernel k;
k.addValue(1.0, 0.5);
k.addValue(3.0, 0.5);
k.addValue(5.0, 0.5);
k.addValue(7.0, 0.5);
::dynamic_density::DynamicKDE::Kernel proto;
k.populateProto(&proto);
EXPECT_EQ(proto.coord()[0], 4.0);
EXPECT_EQ(proto.variance()[0], 5.0);
EXPECT_EQ(proto.count(), 2.0);
}
TEST(DynamicKDETest, addNoDecay) {
static constexpr int kNumValues = 100000;
static constexpr double kDecayRate = 0.0;
DynamicKDE uut(DynamicKDEOpts().setNumKernels(31).setDecayRate(kDecayRate));
std::default_random_engine gen;
std::normal_distribution<double> norm(0.0, 1.0);
for (int i = 0; i < kNumValues; i++) {
uut.addValue(norm(gen));
}
EXPECT_NEAR(uut.computeTotalCount(), kNumValues, 1e-9);
EXPECT_NEAR(uut.getQuantileEstimate(0.5), 0.0, 1e-1);
EXPECT_NEAR(uut.getQuantileEstimate(0.05), -1.644854, 1e-1);
EXPECT_NEAR(uut.getQuantileEstimate(0.95), 1.644854, 1e-1);
EXPECT_NEAR(uut.getMean(), 0.0, 1e-1) << uut.debugString();
}
TEST(DynamicKDETest, addWithDecay) {
static constexpr int kNumValues = 100000;
static constexpr double kDecayRate = 0.00001;
DynamicKDE uut(DynamicKDEOpts().setNumKernels(31).setDecayRate(kDecayRate));
std::default_random_engine gen;
std::normal_distribution<double> norm(0.0, 1.0);
for (int i = 0; i < kNumValues; i++) {
uut.addValue(norm(gen));
}
EXPECT_NEAR(uut.computeTotalCount(),
exponential_sum(1, 1 - kDecayRate, kNumValues), 1e-6);
EXPECT_NEAR(uut.getQuantileEstimate(0.5), 0.0, 1e-1);
EXPECT_NEAR(uut.getQuantileEstimate(0.05), -1.644854, 1e-1);
EXPECT_NEAR(uut.getQuantileEstimate(0.95), 1.644854, 1e-1);
EXPECT_NEAR(uut.getMean(), 0.0, 1e-1) << uut.debugString();
}
TEST(DynamicKDETest, asProto) {
DynamicKDE uut(DynamicKDEOpts().setNumKernels(61));
uut.mutableDescription()->setTitle("test");
uut.mutableDescription()->setLabels({"x-value"});
std::normal_distribution<double> norm(10000.0, 1.0);
std::default_random_engine gen;
static constexpr int kNumValues = 1000000;
for (int i = 0; i < kNumValues; i++) {
uut.addValue(norm(gen));
}
DensityMap dm = uut.asProto();
EXPECT_EQ(dm.dynamic_kde().description().title(), "test");
EXPECT_EQ(dm.dynamic_kde().description().labels()[0], "x-value");
size_t size = dm.dynamic_kde().kernels().size();
EXPECT_EQ(size, 61);
EXPECT_LT(dm.dynamic_kde().kernels()[0].coord()[0], 10000.0);
EXPECT_GT(dm.dynamic_kde().kernels()[size - 1].coord()[0], 10000.0);
// std::ofstream myfile("/tmp/DynamicKDE.pbuf");
// ASSERT_TRUE(myfile.is_open());
// ASSERT_TRUE(dm.SerializeToOstream(&myfile));
// myfile.close();
}
TEST(DynamicKDETest, count) {
DynamicKDE uut(DynamicKDEOpts().setNumKernels(61));
std::normal_distribution<double> norm(10000.0, 1.0);
std::default_random_engine gen;
static constexpr int kNumValues = 10000;
for (int i = 0; i < kNumValues; i++) {
uut.addValue(norm(gen));
}
ASSERT_EQ(uut.computeTotalCount(), kNumValues) << uut.debugString();
}