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.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
34
35
36 class WilcoxonSignedRankTestTest {
37
38 protected WilcoxonSignedRankTest testStatistic = new WilcoxonSignedRankTest();
39
40 @Test
41 void testWilcoxonSignedRankSimple() {
42
43
44
45
46
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
57
58
59
60
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
69
70
71
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
83
84
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
90 assertEquals(0.8125,
91 testStatistic.wilcoxonSignedRankTest(x, y, true),
92 1e-10);
93
94 assertEquals(0.79984610566,
95 testStatistic.wilcoxonSignedRankTest(x, y, false),
96 1e-10);
97 }
98
99
100 @Test
101 void testWilcoxonSignedRankTiesDiscarded() {
102
103
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
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
142
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
149 }
150
151
152
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
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
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
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
188 }
189
190
191
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
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
213 }
214
215
216
217
218 try {
219 testStatistic.wilcoxonSignedRankTest(null, null, true);
220 fail("x and y is null (exact), NullArgumentException expected");
221 } catch (NullArgumentException ex) {
222
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
230 }
231
232
233
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
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
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
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
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 }