View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.junit.jupiter.api.Test;
26  
27  import java.io.BufferedReader;
28  import java.io.StringReader;
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertFalse;
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  import static org.junit.jupiter.api.Assertions.assertNull;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  /**
40   * Test cases for the {@link Frequency} class.
41   */
42  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      /** test freq counts */
54      @Test
55      void testCounts() {
56          Frequency<Long> f = new Frequency<>();
57  
58          assertEquals(0, f.getSumFreq(), "total count");
59          f.addValue(ONE_LONG);
60          f.addValue(TWO_LONG);
61          f.addValue(1L);
62          f.addValue((long) ONE);
63          assertEquals(3, f.getCount(1L), "one frequency count");
64          assertEquals(1, f.getCount(2L), "two frequency count");
65          assertEquals(0, f.getCount(3L), "three frequency count");
66          assertEquals(4, f.getSumFreq(), "total count");
67          assertEquals(0, f.getCumFreq(0L), "zero cumulative frequency");
68          assertEquals(3,  f.getCumFreq(1L),  "one cumulative frequency");
69          assertEquals(4,  f.getCumFreq(2L),  "two cumulative frequency");
70          assertEquals(4, f.getCumFreq(5L), "five cumulative frequency");
71  
72          f.clear();
73          assertEquals(0, f.getSumFreq(), "total count");
74      }
75  
76      @Test
77      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(1, f.getCount("one"), "one cumulative frequency");
86          assertEquals(0.5, f.getCumPct("Z"), TOLERANCE, "Z cumulative pct");
87          assertEquals(1.0, f.getCumPct("z"), TOLERANCE, "z cumulative pct");
88          assertEquals(0.25, f.getCumPct("Ot"), TOLERANCE, "Ot cumulative pct");
89          f.clear();
90      }
91  
92      @Test
93      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(3, f.getCount(1), "1 count");
103         assertEquals(3, f.getCount(Integer.valueOf(1)), "1 count");
104         assertEquals(0.2, f.getCumPct(0), TOLERANCE, "0 cum pct");
105         assertEquals(0.6, f.getPct(Integer.valueOf(1)), TOLERANCE, "1 pct");
106         assertEquals(0, f.getCumPct(-2), TOLERANCE, "-2 cum pct");
107         assertEquals(1, f.getCumPct(10), TOLERANCE, "10 cum pct");
108     }
109 
110     @Test
111     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(3 ,  f.getCount("one"),  "one count");
119         assertEquals(1 ,  f.getCumPct("Z"), TOLERANCE, "Z cumulative pct -- case insensitive");
120         assertEquals(1 ,  f.getCumPct("z"), TOLERANCE, "z cumulative pct -- case insensitive");
121     }
122 
123     @Test
124     void testCountsCharacter() {
125         Frequency<Character> f = new Frequency<>();
126 
127         assertEquals(0L, f.getCount('a'));
128         assertEquals(0L, f.getCumFreq('b'));
129         UnitTestUtils.customAssertEquals(Double.NaN, f.getPct('a'), 0.0);
130         UnitTestUtils.customAssertEquals(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     /** test pcts */
143     @Test
144     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(0.25, f.getPct(1L), TOLERANCE, "one pct");
156         assertEquals(0.25, f.getPct(Long.valueOf(2)), TOLERANCE, "two pct");
157         assertEquals(0.5, f.getPct(THREE_LONG), TOLERANCE, "three pct");
158         assertEquals(0, f.getPct(5L), TOLERANCE, "five pct");
159         assertEquals(0.25, f.getCumPct(1L), TOLERANCE, "one cum pct");
160         assertEquals(0.50, f.getCumPct(Long.valueOf(2)), TOLERANCE, "two cum pct");
161         assertEquals(1.0, f.getCumPct(THREE_LONG), TOLERANCE, "three cum pct");
162         assertEquals(1.0, f.getCumPct(5L), TOLERANCE, "five cum pct");
163         assertEquals(0.0, f.getCumPct(0L), TOLERANCE, "zero cum pct");
164     }
165 
166     /** test empty table */
167     @Test
168     void testEmptyTable() {
169         final Frequency<Integer> freq = new Frequency<>();
170 
171         assertEquals(0, freq.getSumFreq(), "freq sum, empty table");
172         assertEquals(0, freq.getCount(0), "count, empty table");
173         assertEquals(0, freq.getCount(Integer.valueOf(0)), "count, empty table");
174         assertEquals(0, freq.getCumFreq(0), "cum freq, empty table");
175         assertEquals(0, freq.getCumFreq(2), "cum freq, empty table");
176 
177         assertTrue(Double.isNaN(freq.getPct(0)), "pct, empty table");
178         assertTrue(Double.isNaN(freq.getPct(Integer.valueOf(0))), "pct, empty table");
179         assertTrue(Double.isNaN(freq.getCumPct(0)), "cum pct, empty table");
180         assertTrue(Double.isNaN(freq.getCumPct(Integer.valueOf(0))), "cum pct, empty table");
181     }
182 
183     @Test
184     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(0.25d, f.getCumPct(3), TOLERANCE, "cum freq, single entry");
192         assertEquals(1.0d,  f.getCumPct(6), TOLERANCE, "cum freq, single entry");
193 
194         assertEquals(0.0d,  f.getCumPct(1), TOLERANCE, "cum freq, single entry");
195         assertEquals(1.0d,  f.getCumPct(10), TOLERANCE, "cum freq, single entry");
196     }
197 
198     /**
199      * Tests toString()
200      */
201     @Test
202     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         //System.out.println(s);
212         assertNotNull(s);
213         BufferedReader reader = new BufferedReader(new StringReader(s));
214         String line = reader.readLine(); // header line
215         assertNotNull(line);
216 
217         line = reader.readLine(); // one's or two's line
218         assertNotNull(line);
219 
220         line = reader.readLine(); // one's or two's line
221         assertNotNull(line);
222 
223         line = reader.readLine(); // no more elements
224         assertNull(line);
225     }
226 
227     @Test
228     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(2, f.getCount(1), "Integer 1 count");
239         assertEquals(2, f.getCount(Integer.valueOf(1)), "Integer 1 count");
240         assertEquals(2, f.getCount(Long.valueOf(1).intValue()), "Integer 1 count");
241         assertEquals(0.5, f.getCumPct(1), TOLERANCE, "Integer 1 cumPct");
242         assertEquals(0.5, f.getCumPct(Long.valueOf(1).intValue()), TOLERANCE, "Integer 1 cumPct");
243         assertEquals(0.5, f.getCumPct(Integer.valueOf(1)), TOLERANCE, "Integer 1 cumPct");
244 
245         f.incrementValue(ONE, -2);
246         f.incrementValue(THREE, 5);
247 
248         assertEquals(0, f.getCount(1), "Integer 1 count");
249         assertEquals(5, f.getCount(3), "Integer 3 count");
250 
251         Iterator<?> it = f.valuesIterator();
252         while (it.hasNext()) {
253             assertTrue(it.next() instanceof Integer);
254         }
255     }
256 
257     @Test
258     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     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     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     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     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     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     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     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 }