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