Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/src/test/java/com/thealgorithms/streaming/AdwinTest.java at master · datpp/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
datpp
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java
/
src
/
test
/
java
/
com
/
thealgorithms
/
streaming
/
AdwinTest.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
212 lines (174 loc) · 6.84 KB
master
Breadcrumbs
Java
/
src
/
test
/
java
/
com
/
thealgorithms
/
streaming
/
AdwinTest.java
Copy path
Top
File metadata and controls
Code
Blame
212 lines (174 loc) · 6.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.thealgorithms.streaming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Random;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class AdwinTest {
private static double mean(double[] values) {
double sum = 0.0;
for (double value : values) {
sum += value;
}
return sum / values.length;
}
private static double variance(double[] values) {
double mean = mean(values);
double sum = 0.0;
for (double value : values) {
sum += (value - mean) * (value - mean);
}
return sum / values.length;
}
@ParameterizedTest
@ValueSource(doubles = {0.0, 1.0, -0.5, 2.0, Double.NaN})
void rejectsInvalidDeltas(double delta) {
assertThrows(IllegalArgumentException.class, () -> new Adwin(delta));
}
@ParameterizedTest
@ValueSource(doubles = {Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY})
void rejectsNonFiniteSamples(double value) {
Adwin window = new Adwin();
assertThrows(IllegalArgumentException.class, () -> window.accept(value));
}
@Test
void startsEmpty() {
Adwin window = new Adwin();
assertEquals(0, window.width());
assertEquals(0.0, window.estimate());
assertEquals(0.0, window.variance());
assertEquals(0, window.bucketCount());
assertEquals(0, window.changeCount());
assertEquals(0.002, window.delta());
}
@Test
@DisplayName("a constant stream is never cut and the window holds all of it")
void keepsEverythingOnAConstantStream() {
Adwin window = new Adwin();
for (int i = 0; i < 1000; i++) {
assertFalse(window.accept(3.0));
}
assertEquals(1000, window.width());
assertEquals(1000, window.count());
assertEquals(3.0, window.estimate());
assertEquals(0.0, window.variance());
assertEquals(0, window.changeCount());
}
@Test
@DisplayName("the histogram reproduces the mean and the variance of the window exactly")
void agreesWithADirectComputation() {
Adwin window = new Adwin();
Random random = new Random(11L);
double[] data = new double[2000];
for (int i = 0; i < data.length; i++) {
data[i] = random.nextGaussian();
assertFalse(window.accept(data[i]), "stationary noise must not be cut, sample " + i);
}
assertEquals(data.length, window.width());
assertEquals(mean(data), window.estimate(), 1e-9);
assertEquals(variance(data), window.variance(), 1e-9);
}
@Test
@DisplayName("a step in the level cuts the window quickly")
void findsAStep() {
Adwin window = new Adwin();
Random random = new Random(5L);
int firstCut = -1;
for (int i = 0; i < 1200; i++) {
double sample = 0.1 * random.nextGaussian() + (i < 1000 ? 0.0 : 1.0);
boolean cut = window.accept(sample);
if (cut && firstCut < 0) {
firstCut = i;
}
}
assertTrue(firstCut >= 1000, "the window must not be cut before the step, cut at " + firstCut);
assertTrue(firstCut < 1050, "the step should be found quickly, cut at " + firstCut);
assertEquals(200, window.width(), "the window should hold exactly the samples after the step");
assertEquals(1.0, window.estimate(), 0.05);
}
@Test
@DisplayName("the window follows a level that changes several times")
void tracksSeveralRegimes() {
Adwin window = new Adwin();
Random random = new Random(23L);
double[] levels = {0.0, 5.0, -3.0, 10.0};
for (double level : levels) {
for (int i = 0; i < 400; i++) {
window.accept(level + 0.2 * random.nextGaussian());
}
assertEquals(level, window.estimate(), 0.1, "the estimate should have caught up with " + level);
}
assertTrue(window.changeCount() > 0);
}
@Test
@DisplayName("the window never holds more than the stream")
void theWidthNeverExceedsTheCount() {
Adwin window = new Adwin();
Random random = new Random(31L);
for (int i = 0; i < 500; i++) {
window.accept(random.nextGaussian() + i / 100.0);
assertTrue(window.width() <= window.count());
assertTrue(window.width() >= 1);
}
}
@Test
@DisplayName("the number of buckets grows like the logarithm of the width")
void keepsALogarithmicNumberOfBuckets() {
Adwin window = new Adwin();
for (int i = 0; i < 5000; i++) {
window.accept(1.0);
}
int levels = 64 - Long.numberOfLeadingZeros(window.width());
int bound = (Adwin.MAX_BUCKETS + 1) * (levels + 1);
assertEquals(5000, window.width());
assertTrue(window.bucketCount() <= bound, "held " + window.bucketCount() + " buckets, bound was " + bound);
}
@Test
@DisplayName("a smaller delta cuts less eagerly")
void deltaControlsHowEagerlyTheWindowIsCut() {
Adwin eager = new Adwin(0.5);
Adwin careful = new Adwin(1e-9);
Random random = new Random(41L);
for (int i = 0; i < 600; i++) {
double sample = random.nextGaussian() + (i < 300 ? 0.0 : 0.3);
eager.accept(sample);
careful.accept(sample);
}
assertTrue(eager.changeCount() >= careful.changeCount(), "eager cut " + eager.changeCount() + ", careful cut " + careful.changeCount());
}
@Test
void scanReportsOneVerdictPerSample() {
Adwin window = new Adwin();
boolean[] cuts = window.scan(new double[] {1.0, 1.0, 1.0, 1.0});
assertEquals(4, cuts.length);
assertEquals(4, window.count());
}
@Test
void resetEmptiesTheWindow() {
Adwin window = new Adwin();
Random random = new Random(3L);
for (int i = 0; i < 300; i++) {
window.accept(random.nextGaussian());
}
window.reset();
assertEquals(0, window.width());
assertEquals(0, window.count());
assertEquals(0, window.changeCount());
assertEquals(0, window.bucketCount());
assertEquals(0.0, window.estimate());
assertFalse(window.accept(1.0));
assertEquals(1.0, window.estimate());
}
@Test
void toStringMentionsTheState() {
Adwin window = new Adwin();
window.accept(2.0);
String text = window.toString();
assertTrue(text.contains("Adwin"));
assertTrue(text.contains("width=1"));
}
}
You can’t perform that action at this time.