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.inference;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.exception.NullArgumentException;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertThrows;
30  import static org.junit.jupiter.api.Assertions.fail;
31  
32  /**
33   * Test cases for the WilcoxonSignedRangTest class.
34   */
35  
36  class WilcoxonSignedRankTestTest {
37  
38      protected WilcoxonSignedRankTest testStatistic = new WilcoxonSignedRankTest();
39  
40      @Test
41      void testWilcoxonSignedRankSimple() {
42          /*
43           * Hollandar and Wolfe data from R docs.
44           * Target values computed using R version 3.4.4.
45           * x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
46           * y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)
47           */
48          final double[] x = {
49              1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30
50          };
51          final double[] y = {
52              0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29
53          };
54  
55          /*
56           * EXACT:
57           * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE
58           * exact = TRUE, correct = FALSE)
59           * V = 40, p-value = 0.03906
60           * Expected values are from R, version 3.4.4.
61           */
62          assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10);
63          assertEquals(0.03906,
64                              testStatistic.wilcoxonSignedRankTest(x, y, true),
65                              1e-5);
66  
67          /*
68           * ASYMPTOTIC:
69           * wilcox.test(x, y, alternative = "two.sided", mu = 0,
70           * paired = TRUE, exact = FALSE, correct = TRUE)
71           * V = 40, p-value = 0.044010984013
72           */
73          assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10);
74          assertEquals(0.044010984013,
75                              testStatistic.wilcoxonSignedRankTest(x, y, false),
76                              1e-10);
77      }
78  
79      @Test
80      void testWilcoxonSignedRankSimple2() {
81          /*
82           * x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91)
83           * y <- c(1.15, 0.88, 0.90, 0.74, 1.21, 2.0, 1.72)
84           * Expected values are from R, version 3.4.4.
85           */
86          final double[] x = {0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91};
87          final double[] y = {1.15, 0.88, 0.90, 0.74, 1.21, 2.0, 1.72};
88          assertEquals(16,  testStatistic.wilcoxonSignedRank(x, y), 0);
89          // Exact
90          assertEquals(0.8125,
91                              testStatistic.wilcoxonSignedRankTest(x, y, true),
92                              1e-10);
93          // Asymptotic
94          assertEquals(0.79984610566,
95                              testStatistic.wilcoxonSignedRankTest(x, y, false),
96                              1e-10);
97      }
98  
99  
100     @Test
101     void testWilcoxonSignedRankTiesDiscarded() {
102         /*
103          * Verify that tied pairs are discarded.
104          */
105         final double[] x = {
106             1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30
107         };
108         final double[] y = {
109             1.83, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29
110         };
111         final double[] xp = {
112             0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30
113         };
114         final double[] yp = {
115             0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29
116         };
117         assertEquals(testStatistic.wilcoxonSignedRank(xp, yp),
118                             testStatistic.wilcoxonSignedRank(x, y),
119                             0);
120         assertEquals(testStatistic.wilcoxonSignedRankTest(xp, yp, true),
121                             testStatistic.wilcoxonSignedRankTest(x, y, true),
122                             0);
123         assertEquals(testStatistic.wilcoxonSignedRankTest(xp, yp, false),
124                             testStatistic.wilcoxonSignedRankTest(x, y, false),
125                             0);
126     }
127 
128     @Test
129     void testWilcoxonSignedRankInputValidation() {
130         /*
131          * Exact only for sample size <= 30
132          */
133         final double[] x1 = new double[30];
134         final double[] x2 = new double[31];
135         final double[] y1 = new double[30];
136         final double[] y2 = new double[31];
137         for (int i = 0; i < 30; ++i) {
138             x1[i] = x2[i] = y1[i] = y2[i] = i;
139         }
140 
141         // Exactly 30 is okay
142         // testStatistic.wilcoxonSignedRankTest(x1, y1, true);
143 
144         try {
145             testStatistic.wilcoxonSignedRankTest(x2, y2, true);
146             fail("More than 30 samples and exact chosen, MathIllegalArgumentException expected");
147         } catch (MathIllegalArgumentException ex) {
148             // expected
149         }
150 
151         /*
152          * Samples must be present, i.e. length > 0
153          */
154         try {
155             testStatistic.wilcoxonSignedRankTest(new double[] {}, new double[] {
156                 1.0
157             }, true);
158             fail("x does not contain samples (exact), MathIllegalArgumentException expected");
159         } catch (MathIllegalArgumentException ex) {
160             // expected
161         }
162 
163         try {
164             testStatistic.wilcoxonSignedRankTest(new double[] {}, new double[] {
165                 1.0
166             }, false);
167             fail("x does not contain samples (asymptotic), MathIllegalArgumentException expected");
168         } catch (MathIllegalArgumentException ex) {
169             // expected
170         }
171 
172         try {
173             testStatistic.wilcoxonSignedRankTest(new double[] {
174                 1.0
175             }, new double[] {}, true);
176             fail("y does not contain samples (exact), MathIllegalArgumentException expected");
177         } catch (MathIllegalArgumentException ex) {
178             // expected
179         }
180 
181         try {
182             testStatistic.wilcoxonSignedRankTest(new double[] {
183                 1.0
184             }, new double[] {}, false);
185             fail("y does not contain samples (asymptotic), MathIllegalArgumentException expected");
186         } catch (MathIllegalArgumentException ex) {
187             // expected
188         }
189 
190         /*
191          * Samples not same size, i.e. cannot be paired
192          */
193         try {
194             testStatistic.wilcoxonSignedRankTest(new double[] {
195                 1.0, 2.0
196             }, new double[] {
197                 3.0
198             }, true);
199             fail("x and y not same size (exact), MathIllegalArgumentException expected");
200         } catch (MathIllegalArgumentException ex) {
201             // expected
202         }
203 
204         try {
205             testStatistic.wilcoxonSignedRankTest(new double[] {
206                 1.0, 2.0
207             }, new double[] {
208                 3.0
209             }, false);
210             fail("x and y not same size (asymptotic), MathIllegalArgumentException expected");
211         } catch (MathIllegalArgumentException ex) {
212             // expected
213         }
214 
215         /*
216          * x and y is null
217          */
218         try {
219             testStatistic.wilcoxonSignedRankTest(null, null, true);
220             fail("x and y is null (exact), NullArgumentException expected");
221         } catch (NullArgumentException ex) {
222             // expected
223         }
224 
225         try {
226             testStatistic.wilcoxonSignedRankTest(null, null, false);
227             fail("x and y is null (asymptotic), NullArgumentException expected");
228         } catch (NullArgumentException ex) {
229             // expected
230         }
231 
232         /*
233          * x or y is null
234          */
235         try {
236             testStatistic.wilcoxonSignedRankTest(null, new double[] {
237                 1.0
238             }, true);
239             fail("x is null (exact), NullArgumentException expected");
240         } catch (NullArgumentException ex) {
241             // expected
242         }
243 
244         try {
245             testStatistic.wilcoxonSignedRankTest(null, new double[] {
246                 1.0
247             }, false);
248             fail("x is null (asymptotic), NullArgumentException expected");
249         } catch (NullArgumentException ex) {
250             // expected
251         }
252 
253         try {
254             testStatistic.wilcoxonSignedRankTest(new double[] {
255                 1.0
256             }, null, true);
257             fail("y is null (exact), NullArgumentException expected");
258         } catch (NullArgumentException ex) {
259             // expected
260         }
261 
262         try {
263             testStatistic.wilcoxonSignedRankTest(new double[] {
264                 1.0
265             }, null, false);
266             fail("y is null (asymptotic), NullArgumentException expected");
267         } catch (NullArgumentException ex) {
268             // expected
269         }
270     }
271 
272     @Test
273     void testBadInputAllTies() {
274         assertThrows(MathIllegalArgumentException.class, () -> {
275             testStatistic.wilcoxonSignedRankTest(new double[]{
276                 1.0, 2.0, 3.0
277             }, new double[]{
278                 1.0, 2.0, 3.0
279             }, true);
280         });
281     }
282 
283     @Test
284     void testDegenerateOnePair() {
285         final double[] x = {1};
286         final double[] y = {2};
287         assertEquals(1.0, testStatistic.wilcoxonSignedRank(x,y), 0);
288         assertEquals(1.0, testStatistic.wilcoxonSignedRankTest(x,y, true), 0);
289     }
290 
291 }