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    *      http://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  package org.apache.commons.math4.legacy.stat.inference;
18  
19  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
20  import org.apache.commons.math4.legacy.exception.NoDataException;
21  import org.apache.commons.math4.legacy.exception.NullArgumentException;
22  import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  
27  /**
28   * Test cases for the WilcoxonSignedRangTest class.
29   *
30   */
31  
32  public class WilcoxonSignedRankTestTest {
33  
34      protected WilcoxonSignedRankTest testStatistic = new WilcoxonSignedRankTest();
35  
36      @Test
37      public void testWilcoxonSignedRankSimple() {
38          /* Target values computed using R version 2.11.1
39           * x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
40           * y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)
41           */
42          final double x[] = {1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30};
43          final double y[] = {0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29};
44  
45          /* EXACT:
46           * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE, exact = TRUE, correct = FALSE)
47           * V = 40, p-value = 0.03906
48           *
49           * Corresponds to the value obtained in R.
50           */
51          Assert.assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10);
52          Assert.assertEquals(0.03906, testStatistic.wilcoxonSignedRankTest(x, y, true), 1e-5);
53  
54          /* ASYMPTOTIC:
55           * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE, exact = FALSE, correct = FALSE)
56           * V = 40, p-value = 0.03815
57           *
58           * This is not entirely the same due to different corrects,
59           * e.g. http://mlsc.lboro.ac.uk/resources/statistics/wsrt.pdf
60           * and src/library/stats/R/wilcox.test.R in the R source
61           */
62          Assert.assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10);
63          Assert.assertEquals(0.0329693812, testStatistic.wilcoxonSignedRankTest(x, y, false), 1e-10);
64      }
65  
66      @Test
67      public void testWilcoxonSignedRankInputValidation() {
68          /*
69           * Exact only for sample size <= 30
70           */
71          final double[] x1 = new double[30];
72          final double[] x2 = new double[31];
73          final double[] y1 = new double[30];
74          final double[] y2 = new double[31];
75          for (int i = 0; i < 30; ++i) {
76              x1[i] = x2[i] = y1[i] = y2[i] = i;
77          }
78  
79          // Exactly 30 is okay
80          //testStatistic.wilcoxonSignedRankTest(x1, y1, true);
81  
82          try {
83              testStatistic.wilcoxonSignedRankTest(x2, y2, true);
84              Assert.fail("More than 30 samples and exact chosen, NumberIsTooLargeException expected");
85          } catch (NumberIsTooLargeException ex) {
86              // expected
87          }
88  
89          /* Samples must be present, i.e. length > 0
90           */
91          try {
92              testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, true);
93              Assert.fail("x does not contain samples (exact), NoDataException expected");
94          } catch (NoDataException ex) {
95              // expected
96          }
97  
98          try {
99              testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, false);
100             Assert.fail("x does not contain samples (asymptotic), NoDataException expected");
101         } catch (NoDataException ex) {
102             // expected
103         }
104 
105         try {
106             testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, true);
107             Assert.fail("y does not contain samples (exact), NoDataException expected");
108         } catch (NoDataException ex) {
109             // expected
110         }
111 
112         try {
113             testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, false);
114             Assert.fail("y does not contain samples (asymptotic), NoDataException expected");
115         } catch (NoDataException ex) {
116             // expected
117         }
118 
119         /* Samples not same size, i.e. cannot be paired
120          */
121         try {
122             testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, true);
123             Assert.fail("x and y not same size (exact), DimensionMismatchException expected");
124         } catch (DimensionMismatchException ex) {
125             // expected
126         }
127 
128         try {
129             testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, false);
130             Assert.fail("x and y not same size (asymptotic), DimensionMismatchException expected");
131         } catch (DimensionMismatchException ex) {
132             // expected
133         }
134 
135         /*
136          * x and y is null
137          */
138         try {
139             testStatistic.wilcoxonSignedRankTest(null, null, true);
140             Assert.fail("x and y is null (exact), NullArgumentException expected");
141         } catch (NullArgumentException ex) {
142             // expected
143         }
144 
145         try {
146             testStatistic.wilcoxonSignedRankTest(null, null, false);
147             Assert.fail("x and y is null (asymptotic), NullArgumentException expected");
148         } catch (NullArgumentException ex) {
149             // expected
150         }
151 
152         /*
153          * x or y is null
154          */
155         try {
156             testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, true);
157             Assert.fail("x is null (exact), NullArgumentException expected");
158         } catch (NullArgumentException ex) {
159             // expected
160         }
161 
162         try {
163             testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, false);
164             Assert.fail("x is null (asymptotic), NullArgumentException expected");
165         } catch (NullArgumentException ex) {
166             // expected
167         }
168 
169         try {
170             testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, true);
171             Assert.fail("y is null (exact), NullArgumentException expected");
172         } catch (NullArgumentException ex) {
173             // expected
174         }
175 
176         try {
177             testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, false);
178             Assert.fail("y is null (asymptotic), NullArgumentException expected");
179         } catch (NullArgumentException ex) {
180             // expected
181         }
182     }
183 }