1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.apache.commons.math4.legacy.stat.inference;
18  
19  import org.apache.commons.math4.legacy.exception.NoDataException;
20  import org.apache.commons.math4.legacy.exception.NullArgumentException;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  
25  
26  
27  
28  
29  
30  public class MannWhitneyUTestTest {
31  
32      protected MannWhitneyUTest testStatistic = new MannWhitneyUTest();
33  
34      @Test
35      public void testMannWhitneyUSimple() {
36          
37  
38  
39  
40  
41  
42          final double x[] = {19, 22, 16, 29, 24};
43          final double y[] = {20, 11, 17, 12};
44  
45          Assert.assertEquals(3, testStatistic.mannWhitneyU(x, y), 1e-10);
46          Assert.assertEquals(0.08641, testStatistic.mannWhitneyUTest(x, y), 1e-5);
47      }
48  
49  
50      @Test
51      public void testMannWhitneyUInputValidation() {
52          
53  
54          try {
55              testStatistic.mannWhitneyUTest(new double[] { }, new double[] { 1.0 });
56              Assert.fail("x does not contain samples (exact), NoDataException expected");
57          } catch (NoDataException ex) {
58              
59          }
60  
61          try {
62              testStatistic.mannWhitneyUTest(new double[] { 1.0 }, new double[] { });
63              Assert.fail("y does not contain samples (exact), NoDataException expected");
64          } catch (NoDataException ex) {
65              
66          }
67  
68          
69  
70  
71          try {
72              testStatistic.mannWhitneyUTest(null, null);
73              Assert.fail("x and y is null (exact), NullArgumentException expected");
74          } catch (NullArgumentException ex) {
75              
76          }
77  
78          try {
79              testStatistic.mannWhitneyUTest(null, null);
80              Assert.fail("x and y is null (asymptotic), NullArgumentException expected");
81          } catch (NullArgumentException ex) {
82              
83          }
84  
85          
86  
87  
88          try {
89              testStatistic.mannWhitneyUTest(null, new double[] { 1.0 });
90              Assert.fail("x is null (exact), NullArgumentException expected");
91          } catch (NullArgumentException ex) {
92              
93          }
94  
95          try {
96              testStatistic.mannWhitneyUTest(new double[] { 1.0 }, null);
97              Assert.fail("y is null (exact), NullArgumentException expected");
98          } catch (NullArgumentException ex) {
99              
100         }
101     }
102 
103     @Test
104     public void testBigDataSet() {
105         double[] d1 = new double[1500];
106         double[] d2 = new double[1500];
107         for (int i = 0; i < 1500; i++) {
108             d1[i] = 2 * i;
109             d2[i] = 2 * i + 1;
110         }
111         double result = testStatistic.mannWhitneyUTest(d1, d2);
112         Assert.assertTrue(result > 0.1);
113     }
114 
115     @Test
116     public void testBigDataSetOverflow() {
117         
118         double[] d1 = new double[110000];
119         double[] d2 = new double[110000];
120         for (int i = 0; i < 110000; i++) {
121             d1[i] = i;
122             d2[i] = i;
123         }
124         double result = testStatistic.mannWhitneyUTest(d1, d2);
125         Assert.assertEquals(1.0, result, 0.0);
126     }
127 }