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.ranking;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.random.JDKRandomGenerator;
27  import org.hipparchus.random.RandomGenerator;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertThrows;
31  import static org.junit.jupiter.api.Assertions.fail;
32  
33  
34  /**
35   * Test cases for NaturalRanking class
36   *
37   */
38  class NaturalRankingTest {
39  
40      private final double[] exampleData = { 20, 17, 30, 42.3, 17, 50,
41              Double.NaN, Double.NEGATIVE_INFINITY, 17 };
42      private final double[] tiesFirst = { 0, 0, 2, 1, 4 };
43      private final double[] tiesLast = { 4, 4, 1, 0 };
44      private final double[] multipleNaNs = { 0, 1, Double.NaN, Double.NaN };
45      private final double[] multipleTies = { 3, 2, 5, 5, 6, 6, 1 };
46      private final double[] allSame = { 0, 0, 0, 0 };
47  
48      @Test
49      void testDefault() { // Ties averaged, NaNs failed
50          NaturalRanking ranking = new NaturalRanking();
51          double[] ranks;
52  
53          try {
54              ranks = ranking.rank(exampleData);
55              fail("expected MathIllegalArgumentException due to NaNStrategy.FAILED");
56          } catch (MathIllegalArgumentException e) {
57              // expected
58          }
59  
60          ranks = ranking.rank(tiesFirst);
61          double[] correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 };
62          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
63          ranks = ranking.rank(tiesLast);
64          correctRanks = new double[] { 3.5, 3.5, 2, 1 };
65          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
66  
67          try {
68              ranks = ranking.rank(multipleNaNs);
69              fail("expected MathIllegalArgumentException due to NaNStrategy.FAILED");
70          } catch (MathIllegalArgumentException e) {
71              // expected
72          }
73  
74          ranks = ranking.rank(multipleTies);
75          correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 };
76          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
77          ranks = ranking.rank(allSame);
78          correctRanks = new double[] { 2.5, 2.5, 2.5, 2.5 };
79          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
80      }
81  
82      @Test
83      void testNaNsMaximalTiesMinimum() {
84          NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL, TiesStrategy.MINIMUM);
85          double[] ranks = ranking.rank(exampleData);
86          double[] correctRanks = { 5, 2, 6, 7, 2, 8, 9, 1, 2 };
87          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
88          ranks = ranking.rank(tiesFirst);
89          correctRanks = new double[] { 1, 1, 4, 3, 5 };
90          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
91          ranks = ranking.rank(tiesLast);
92          correctRanks = new double[] { 3, 3, 2, 1 };
93          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
94          ranks = ranking.rank(multipleNaNs);
95          correctRanks = new double[] { 1, 2, 3, 3 };
96          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
97          ranks = ranking.rank(multipleTies);
98          correctRanks = new double[] { 3, 2, 4, 4, 6, 6, 1 };
99          UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
100         ranks = ranking.rank(allSame);
101         correctRanks = new double[] { 1, 1, 1, 1 };
102         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
103     }
104 
105     @Test
106     void testNaNsRemovedTiesSequential() {
107         NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED,
108                 TiesStrategy.SEQUENTIAL);
109         double[] ranks = ranking.rank(exampleData);
110         double[] correctRanks = { 5, 2, 6, 7, 3, 8, 1, 4 };
111         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
112         ranks = ranking.rank(tiesFirst);
113         correctRanks = new double[] { 1, 2, 4, 3, 5 };
114         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
115         ranks = ranking.rank(tiesLast);
116         correctRanks = new double[] { 3, 4, 2, 1 };
117         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
118         ranks = ranking.rank(multipleNaNs);
119         correctRanks = new double[] { 1, 2 };
120         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
121         ranks = ranking.rank(multipleTies);
122         correctRanks = new double[] { 3, 2, 4, 5, 6, 7, 1 };
123         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
124         ranks = ranking.rank(allSame);
125         correctRanks = new double[] { 1, 2, 3, 4 };
126         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
127     }
128 
129     @Test
130     void testNaNsMinimalTiesMaximum() {
131         NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL,
132                 TiesStrategy.MAXIMUM);
133         double[] ranks = ranking.rank(exampleData);
134         double[] correctRanks = { 6, 5, 7, 8, 5, 9, 2, 2, 5 };
135         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
136         ranks = ranking.rank(tiesFirst);
137         correctRanks = new double[] { 2, 2, 4, 3, 5 };
138         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
139         ranks = ranking.rank(tiesLast);
140         correctRanks = new double[] { 4, 4, 2, 1 };
141         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
142         ranks = ranking.rank(multipleNaNs);
143         correctRanks = new double[] { 3, 4, 2, 2 };
144         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
145         ranks = ranking.rank(multipleTies);
146         correctRanks = new double[] { 3, 2, 5, 5, 7, 7, 1 };
147         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
148         ranks = ranking.rank(allSame);
149         correctRanks = new double[] { 4, 4, 4, 4 };
150         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
151     }
152 
153     @Test
154     void testNaNsMinimalTiesAverage() {
155         NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL);
156         double[] ranks = ranking.rank(exampleData);
157         double[] correctRanks = { 6, 4, 7, 8, 4, 9, 1.5, 1.5, 4 };
158         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
159         ranks = ranking.rank(tiesFirst);
160         correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 };
161         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
162         ranks = ranking.rank(tiesLast);
163         correctRanks = new double[] { 3.5, 3.5, 2, 1 };
164         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
165         ranks = ranking.rank(multipleNaNs);
166         correctRanks = new double[] { 3, 4, 1.5, 1.5 };
167         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
168         ranks = ranking.rank(multipleTies);
169         correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 };
170         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
171         ranks = ranking.rank(allSame);
172         correctRanks = new double[] { 2.5, 2.5, 2.5, 2.5 };
173         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
174     }
175 
176     @Test
177     void testNaNsFixedTiesRandom() {
178         RandomGenerator randomGenerator = new JDKRandomGenerator();
179         randomGenerator.setSeed(1000);
180         NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED,
181                 randomGenerator);
182         double[] ranks = ranking.rank(exampleData);
183         double[][] correctRanks = { {5}, {2, 3, 4}, {6}, {7}, {2, 3, 4}, {8}, {Double.NaN}, {1}, {2, 3, 4} };
184         UnitTestUtils.customAssertContains(correctRanks, ranks, 0d);
185         ranks = ranking.rank(tiesFirst);
186         correctRanks = new double[][] { {1, 2}, {1, 2}, {4}, {3}, {5} };
187         UnitTestUtils.customAssertContains(correctRanks, ranks, 0d);
188         ranks = ranking.rank(tiesLast);
189         correctRanks = new double[][] { {3, 4}, {3, 4}, {2}, {1} };
190         UnitTestUtils.customAssertContains(correctRanks, ranks, 0d);
191         ranks = ranking.rank(multipleNaNs);
192         UnitTestUtils.customAssertEquals(new double[] { 1, 2, Double.NaN, Double.NaN }, ranks, 0d);
193         ranks = ranking.rank(multipleTies);
194         correctRanks = new double[][] { {3}, {2}, {4, 5}, {4, 5}, {6, 7}, {6, 7}, {1} };
195         UnitTestUtils.customAssertContains(correctRanks, ranks, 0d);
196         ranks = ranking.rank(allSame);
197         correctRanks = new double[][] { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} };
198         UnitTestUtils.customAssertContains(correctRanks, ranks, 0d);
199     }
200 
201     @Test
202     void testNaNsAndInfs() {
203         double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN,
204                 Double.NEGATIVE_INFINITY };
205         NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL);
206         double[] ranks = ranking.rank(data);
207         double[] correctRanks = new double[] { 2, 3.5, 3.5, 1 };
208         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
209         ranking = new NaturalRanking(NaNStrategy.MINIMAL);
210         ranks = ranking.rank(data);
211         correctRanks = new double[] { 3, 4, 1.5, 1.5 };
212         UnitTestUtils.customAssertEquals(correctRanks, ranks, 0d);
213     }
214 
215     @Test
216     void testNaNsFailed() {
217         assertThrows(MathIllegalArgumentException.class, () -> {
218             double[] data = {0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY};
219             NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
220             ranking.rank(data);
221         });
222     }
223 
224     @Test
225     void testNoNaNsFailed() {
226         double[] data = { 1, 2, 3, 4 };
227         NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED);
228         double[] ranks = ranking.rank(data);
229         UnitTestUtils.customAssertEquals(data, ranks, 0d);
230     }
231 
232 }