1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
31
32
33 public class WilcoxonSignedRankTestTest {
34
35 protected WilcoxonSignedRankTest testStatistic = new WilcoxonSignedRankTest();
36
37 @Test
38 public void testWilcoxonSignedRankSimple() {
39
40
41
42
43
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
54
55
56
57
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
66
67
68
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
80
81
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
87 Assert.assertEquals(0.8125,
88 testStatistic.wilcoxonSignedRankTest(x, y, true),
89 1e-10);
90
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
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
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
139
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
147 }
148
149
150
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
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
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
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
190 }
191
192
193
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
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
217 }
218
219
220
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
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
236 }
237
238
239
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
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
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
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
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 }