1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.stat;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.BufferedReader;
31 import java.io.StringReader;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36 import org.hipparchus.UnitTestUtils;
37 import org.junit.Test;
38
39
40
41
42 public final class FrequencyTest {
43 private static final long ONE_LONG = 1L;
44 private static final long TWO_LONG = 2L;
45 private static final long THREE_LONG = 3L;
46 private static final int ONE = 1;
47 private static final int TWO = 2;
48 private static final int THREE = 3 ;
49 private static final char CHAR_A = 'a';
50
51 private static final double TOLERANCE = 10E-15d;
52
53
54 @Test
55 public void testCounts() {
56 Frequency<Long> f = new Frequency<>();
57
58 assertEquals("total count", 0, f.getSumFreq());
59 f.addValue(ONE_LONG);
60 f.addValue(TWO_LONG);
61 f.addValue(1L);
62 f.addValue((long) ONE);
63 assertEquals("one frequency count", 3, f.getCount(1L));
64 assertEquals("two frequency count", 1, f.getCount(2L));
65 assertEquals("three frequency count", 0, f.getCount(3L));
66 assertEquals("total count", 4, f.getSumFreq());
67 assertEquals("zero cumulative frequency", 0, f.getCumFreq(0L));
68 assertEquals("one cumulative frequency", 3, f.getCumFreq(1L));
69 assertEquals("two cumulative frequency", 4, f.getCumFreq(2L));
70 assertEquals("five cumulative frequency", 4, f.getCumFreq(5L));
71
72 f.clear();
73 assertEquals("total count", 0, f.getSumFreq());
74 }
75
76 @Test
77 public void testCountsString() {
78 Frequency<String> f = new Frequency<>();
79
80 f.addValue("one");
81 f.addValue("One");
82 f.addValue("oNe");
83 f.addValue("Z");
84
85 assertEquals("one cumulative frequency", 1, f.getCount("one"));
86 assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"), TOLERANCE);
87 assertEquals("z cumulative pct", 1.0, f.getCumPct("z"), TOLERANCE);
88 assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), TOLERANCE);
89 f.clear();
90 }
91
92 @Test
93 public void testCountsInteger() {
94 Frequency<Integer> f = new Frequency<>();
95
96 f.addValue(1);
97 f.addValue(Integer.valueOf(1));
98 f.addValue(Long.valueOf(1).intValue());
99 f.addValue(2);
100 f.addValue(Integer.valueOf(-1));
101
102 assertEquals("1 count", 3, f.getCount(1));
103 assertEquals("1 count", 3, f.getCount(Integer.valueOf(1)));
104 assertEquals("0 cum pct", 0.2, f.getCumPct(0), TOLERANCE);
105 assertEquals("1 pct", 0.6, f.getPct(Integer.valueOf(1)), TOLERANCE);
106 assertEquals("-2 cum pct", 0, f.getCumPct(-2), TOLERANCE);
107 assertEquals("10 cum pct", 1, f.getCumPct(10), TOLERANCE);
108 }
109
110 @Test
111 public void testCountsComparator() {
112 Frequency<String> f = new Frequency<>(String.CASE_INSENSITIVE_ORDER);
113
114 f.addValue("one");
115 f.addValue("One");
116 f.addValue("oNe");
117 f.addValue("Z");
118 assertEquals("one count", 3 , f.getCount("one"));
119 assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), TOLERANCE);
120 assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), TOLERANCE);
121 }
122
123 @Test
124 public void testCountsCharacter() {
125 Frequency<Character> f = new Frequency<>();
126
127 assertEquals(0L, f.getCount('a'));
128 assertEquals(0L, f.getCumFreq('b'));
129 UnitTestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0);
130 UnitTestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0);
131 f.addValue('a');
132 f.addValue('b');
133 f.addValue('c');
134 f.addValue('d');
135 assertEquals(1L, f.getCount('a'));
136 assertEquals(2L, f.getCumFreq('b'));
137 assertEquals(0.25, f.getPct('a'), 0.0);
138 assertEquals(0.5, f.getCumPct('b'), 0.0);
139 assertEquals(1.0, f.getCumPct('e'), 0.0);
140 }
141
142
143 @Test
144 public void testPcts() {
145 Frequency<Long> f = new Frequency<>();
146
147 f.addValue(ONE_LONG);
148 f.addValue(TWO_LONG);
149 f.addValue((long) ONE);
150 f.addValue((long) TWO);
151 f.addValue(THREE_LONG);
152 f.addValue(THREE_LONG);
153 f.addValue(3L);
154 f.addValue((long) THREE);
155 assertEquals("one pct", 0.25, f.getPct(1L), TOLERANCE);
156 assertEquals("two pct", 0.25, f.getPct(Long.valueOf(2)), TOLERANCE);
157 assertEquals("three pct", 0.5, f.getPct(THREE_LONG), TOLERANCE);
158 assertEquals("five pct", 0, f.getPct(5L), TOLERANCE);
159 assertEquals("one cum pct", 0.25, f.getCumPct(1L), TOLERANCE);
160 assertEquals("two cum pct", 0.50, f.getCumPct(Long.valueOf(2)), TOLERANCE);
161 assertEquals("three cum pct", 1.0, f.getCumPct(THREE_LONG), TOLERANCE);
162 assertEquals("five cum pct", 1.0, f.getCumPct(5L), TOLERANCE);
163 assertEquals("zero cum pct", 0.0, f.getCumPct(0L), TOLERANCE);
164 }
165
166
167 @Test
168 public void testEmptyTable() {
169 final Frequency<Integer> freq = new Frequency<>();
170
171 assertEquals("freq sum, empty table", 0, freq.getSumFreq());
172 assertEquals("count, empty table", 0, freq.getCount(0));
173 assertEquals("count, empty table",0, freq.getCount(Integer.valueOf(0)));
174 assertEquals("cum freq, empty table", 0, freq.getCumFreq(0));
175 assertEquals("cum freq, empty table", 0, freq.getCumFreq(2));
176
177 assertTrue("pct, empty table", Double.isNaN(freq.getPct(0)));
178 assertTrue("pct, empty table", Double.isNaN(freq.getPct(Integer.valueOf(0))));
179 assertTrue("cum pct, empty table", Double.isNaN(freq.getCumPct(0)));
180 assertTrue("cum pct, empty table", Double.isNaN(freq.getCumPct(Integer.valueOf(0))));
181 }
182
183 @Test
184 public void testCumPct() {
185 Frequency<Integer> f = new Frequency<>();
186 f.addValue(3);
187 f.addValue(4);
188 f.addValue(5);
189 f.addValue(6);
190
191 assertEquals("cum freq, single entry", 0.25d, f.getCumPct(3), TOLERANCE);
192 assertEquals("cum freq, single entry", 1.0d, f.getCumPct(6), TOLERANCE);
193
194 assertEquals("cum freq, single entry", 0.0d, f.getCumPct(1), TOLERANCE);
195 assertEquals("cum freq, single entry", 1.0d, f.getCumPct(10), TOLERANCE);
196 }
197
198
199
200
201 @Test
202 public void testToString() throws Exception {
203 Frequency<Long> f = new Frequency<>();
204
205 f.addValue(ONE_LONG);
206 f.addValue(TWO_LONG);
207 f.addValue((long) ONE);
208 f.addValue((long) TWO);
209
210 String s = f.toString();
211
212 assertNotNull(s);
213 BufferedReader reader = new BufferedReader(new StringReader(s));
214 String line = reader.readLine();
215 assertNotNull(line);
216
217 line = reader.readLine();
218 assertNotNull(line);
219
220 line = reader.readLine();
221 assertNotNull(line);
222
223 line = reader.readLine();
224 assertNull(line);
225 }
226
227 @Test
228 public void testIntegerValues() {
229 Frequency<Integer> f = new Frequency<>();
230
231 Integer obj1 = null;
232 obj1 = Integer.valueOf(1);
233 Integer int1 = Integer.valueOf(1);
234 f.addValue(obj1);
235 f.addValue(int1);
236 f.addValue(2);
237 f.addValue(Long.valueOf(2).intValue());
238 assertEquals("Integer 1 count", 2, f.getCount(1));
239 assertEquals("Integer 1 count", 2, f.getCount(Integer.valueOf(1)));
240 assertEquals("Integer 1 count", 2, f.getCount(Long.valueOf(1).intValue()));
241 assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), TOLERANCE);
242 assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Long.valueOf(1).intValue()), TOLERANCE);
243 assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Integer.valueOf(1)), TOLERANCE);
244
245 f.incrementValue(ONE, -2);
246 f.incrementValue(THREE, 5);
247
248 assertEquals("Integer 1 count", 0, f.getCount(1));
249 assertEquals("Integer 3 count", 5, f.getCount(3));
250
251 Iterator<?> it = f.valuesIterator();
252 while (it.hasNext()) {
253 assertTrue(it.next() instanceof Integer);
254 }
255 }
256
257 @Test
258 public void testSerial() {
259 Frequency<Long> f = new Frequency<>();
260
261 f.addValue(ONE_LONG);
262 f.addValue(TWO_LONG);
263 f.addValue((long) ONE);
264 f.addValue((long) TWO);
265 assertEquals(f, UnitTestUtils.serializeAndRecover(f));
266 }
267
268 @Test
269 public void testGetUniqueCount() {
270 Frequency<Long> f = new Frequency<>();
271
272 assertEquals(0, f.getUniqueCount());
273 f.addValue(ONE_LONG);
274 assertEquals(1, f.getUniqueCount());
275 f.addValue(ONE_LONG);
276 assertEquals(1, f.getUniqueCount());
277 f.addValue((long) TWO);
278 assertEquals(2, f.getUniqueCount());
279 }
280
281 @Test
282 public void testIncrement() {
283 Frequency<Long> f = new Frequency<>();
284
285 assertEquals(0, f.getUniqueCount());
286 f.incrementValue(ONE_LONG, 1);
287 assertEquals(1, f.getCount(ONE_LONG));
288
289 f.incrementValue(ONE_LONG, 4);
290 assertEquals(5, f.getCount(ONE_LONG));
291
292 f.incrementValue(ONE_LONG, -5);
293 assertEquals(0, f.getCount(ONE_LONG));
294
295 Frequency<Character> f2 = new Frequency<>();
296 f2.incrementValue(CHAR_A, 2);
297
298 assertEquals(2, f2.getCount(CHAR_A));
299
300 f2.incrementValue(CHAR_A, 3);
301 assertEquals(5, f2.getCount(CHAR_A));
302 }
303
304 @Test
305 public void testMerge() {
306 Frequency<Long> f = new Frequency<>();
307
308 assertEquals(0, f.getUniqueCount());
309 f.addValue(ONE_LONG);
310 f.addValue(TWO_LONG);
311 f.addValue((long) ONE);
312 f.addValue((long) TWO);
313
314 assertEquals(2, f.getUniqueCount());
315 assertEquals(2, f.getCount((long) ONE));
316 assertEquals(2, f.getCount((long) TWO));
317
318 Frequency<Long> g = new Frequency<>();
319 g.addValue(ONE_LONG);
320 g.addValue(THREE_LONG);
321 g.addValue((long) THREE);
322
323 assertEquals(2, g.getUniqueCount());
324 assertEquals(1, g.getCount((long) ONE));
325 assertEquals(2, g.getCount((long) THREE));
326
327 f.merge(g);
328
329 assertEquals(3, f.getUniqueCount());
330 assertEquals(3, f.getCount((long) ONE));
331 assertEquals(2, f.getCount((long) TWO));
332 assertEquals(2, f.getCount((long) THREE));
333 }
334
335 @Test
336 public void testMergeCollection() {
337 Frequency<Long> f = new Frequency<>();
338
339 assertEquals(0, f.getUniqueCount());
340 f.addValue(ONE_LONG);
341
342 assertEquals(1, f.getUniqueCount());
343 assertEquals(1, f.getCount((long) ONE));
344 assertEquals(0, f.getCount((long) TWO));
345
346 Frequency<Long> g = new Frequency<>();
347 g.addValue(TWO_LONG);
348
349 Frequency<Long> h = new Frequency<>();
350 h.addValue(THREE_LONG);
351
352 List<Frequency<Long>> coll = new ArrayList<>();
353 coll.add(g);
354 coll.add(h);
355 f.merge(coll);
356
357 assertEquals(3, f.getUniqueCount());
358 assertEquals(1, f.getCount((long) ONE));
359 assertEquals(1, f.getCount((long) TWO));
360 assertEquals(1, f.getCount((long) THREE));
361 }
362
363 @Test
364 public void testMode() {
365 Frequency<String> f = new Frequency<>();
366
367 List<String> mode;
368 mode = f.getMode();
369 assertEquals(0, mode.size());
370
371 f.addValue("3");
372 mode = f.getMode();
373 assertEquals(1, mode.size());
374 assertEquals("3", mode.get(0));
375
376 f.addValue("2");
377 mode = f.getMode();
378 assertEquals(2, mode.size());
379 assertEquals("2", mode.get(0));
380 assertEquals("3",mode.get(1));
381
382 f.addValue("2");
383 mode = f.getMode();
384 assertEquals(1, mode.size());
385 assertEquals("2", mode.get(0));
386 assertFalse(mode.contains("1"));
387 assertTrue(mode.contains("2"));
388 }
389
390 @Test
391 public void testModeDoubleNan() {
392 Frequency<Double> f = new Frequency<>();
393
394 List<Double> mode;
395 f.addValue(Double.valueOf(Double.NaN));
396 f.addValue(Double.valueOf(Double.NaN));
397 f.addValue(Double.valueOf(Double.NaN));
398 f.addValue(Double.valueOf(Double.NEGATIVE_INFINITY));
399 f.addValue(Double.valueOf(Double.POSITIVE_INFINITY));
400 f.addValue(Double.valueOf(Double.NEGATIVE_INFINITY));
401 f.addValue(Double.valueOf(Double.POSITIVE_INFINITY));
402 f.addValue(Double.valueOf(Double.NEGATIVE_INFINITY));
403 f.addValue(Double.valueOf(Double.POSITIVE_INFINITY));
404 mode = f.getMode();
405 assertEquals(3, mode.size());
406 assertEquals(Double.valueOf(Double.NEGATIVE_INFINITY), mode.get(0));
407 assertEquals(Double.valueOf(Double.POSITIVE_INFINITY), mode.get(1));
408 assertEquals(Double.valueOf(Double.NaN), mode.get(2));
409 }
410
411 @Test
412 public void testModeFloatNan() {
413 Frequency<Float> f = new Frequency<>();
414
415 List<Float> mode;
416 f.addValue(Float.valueOf(Float.NaN));
417 f.addValue(Float.valueOf(Float.NaN));
418 f.addValue(Float.valueOf(Float.NaN));
419 f.addValue(Float.valueOf(Float.NEGATIVE_INFINITY));
420 f.addValue(Float.valueOf(Float.POSITIVE_INFINITY));
421 f.addValue(Float.valueOf(Float.NEGATIVE_INFINITY));
422 f.addValue(Float.valueOf(Float.POSITIVE_INFINITY));
423 f.addValue(Float.valueOf(Float.NEGATIVE_INFINITY));
424 f.addValue(Float.valueOf(Float.POSITIVE_INFINITY));
425 mode = f.getMode();
426 assertEquals(3, mode.size());
427 assertEquals(Float.valueOf(Float.NEGATIVE_INFINITY), mode.get(0));
428 assertEquals(Float.valueOf(Float.POSITIVE_INFINITY), mode.get(1));
429 assertEquals(Float.valueOf(Float.NaN), mode.get(2));
430 }
431
432 }