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.NoDataException;
20  import org.apache.commons.math4.legacy.exception.NullArgumentException;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  
25  /**
26   * Test cases for the MannWhitneyUTestImpl class.
27   *
28   */
29  
30  public class MannWhitneyUTestTest {
31  
32      protected MannWhitneyUTest testStatistic = new MannWhitneyUTest();
33  
34      @Test
35      public void testMannWhitneyUSimple() {
36          /* Target values computed using R version 2.11.1
37           * x <- c(19, 22, 16, 29, 24)
38           * y <- c(20, 11, 17, 12)
39           * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = FALSE, exact = FALSE, correct = FALSE)
40           * W = 17, p-value = 0.08641
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          /* Samples must be present, i.e. length > 0
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              // expected
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              // expected
66          }
67  
68          /*
69           * x and y is null
70           */
71          try {
72              testStatistic.mannWhitneyUTest(null, null);
73              Assert.fail("x and y is null (exact), NullArgumentException expected");
74          } catch (NullArgumentException ex) {
75              // expected
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              // expected
83          }
84  
85          /*
86           * x or y is null
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              // expected
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              // expected
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         // MATH-1145
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 }