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.math.stat.inference;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Test cases for the OneWayAnovaImpl class.
28   *
29   * @version $Revision$ $Date$
30   */
31  
32  public class OneWayAnovaTest extends TestCase {
33  
34      protected OneWayAnova testStatistic = new OneWayAnovaImpl();
35      
36      private char[] wrongArray = { 'a', 'b', 'c' };
37      private double[] emptyArray = {};
38  
39      private double[] classA =
40              {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 };
41      private double[] classB =
42              {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 };
43      private double[] classC =
44              {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 };
45  
46      public OneWayAnovaTest(String name) {
47          super(name);
48      }
49  
50      public void setUp() {
51      }
52  
53      public static Test suite() {
54          TestSuite suite = new TestSuite(OneWayAnovaTest.class);
55          suite.setName("TestStatistic Tests");
56          return suite;
57      }
58  
59      public void testAnovaFValue() throws Exception {
60          // Target comparison values computed using R version 2.6.0 (Linux version)
61          List threeClasses = new ArrayList();
62          threeClasses.add(classA);
63          threeClasses.add(classB);
64          threeClasses.add(classC);
65  
66          assertEquals("ANOVA F-value",  24.67361709460624,
67                   testStatistic.anovaFValue(threeClasses), 1E-12);
68  
69          List twoClasses = new ArrayList();
70          twoClasses.add(classA);
71          twoClasses.add(classB);
72          
73          assertEquals("ANOVA F-value",  0.0150579150579,
74                   testStatistic.anovaFValue(twoClasses), 1E-12);
75  
76          // now try some input hashes which should fail
77          List wrongContents = new ArrayList();
78          wrongContents.add(classC);
79          wrongContents.add(wrongArray);
80          try {
81              testStatistic.anovaFValue(wrongContents);
82              fail("non double[] hash value for key classX, IllegalArgumentException expected");
83          } catch (IllegalArgumentException ex) {
84              // expected
85          }  
86  
87          List emptyContents = new ArrayList();
88          emptyContents.add(emptyArray);
89          emptyContents.add(classC);
90          try {
91              testStatistic.anovaFValue(emptyContents);
92              fail("empty array for key classX, IllegalArgumentException expected");
93          } catch (IllegalArgumentException ex) {
94              // expected
95          }  
96  
97          List tooFew = new ArrayList();
98          tooFew.add(classA);
99          try {
100             testStatistic.anovaFValue(tooFew);
101             fail("less than two classes, IllegalArgumentException expected");
102         } catch (IllegalArgumentException ex) {
103             // expected
104         }  
105     }
106     
107 
108     public void testAnovaPValue() throws Exception {
109         // Target comparison values computed using R version 2.6.0 (Linux version)
110         List threeClasses = new ArrayList();
111         threeClasses.add(classA);
112         threeClasses.add(classB);
113         threeClasses.add(classC);
114 
115         assertEquals("ANOVA P-value", 6.959446E-06,
116                  testStatistic.anovaPValue(threeClasses), 1E-12);
117 
118         List twoClasses = new ArrayList();
119         twoClasses.add(classA);
120         twoClasses.add(classB);
121         
122         assertEquals("ANOVA P-value",  0.904212960464,
123                  testStatistic.anovaPValue(twoClasses), 1E-12);
124 
125     }
126 
127     public void testAnovaTest() throws Exception {
128         // Target comparison values computed using R version 2.3.1 (Linux version)
129         List threeClasses = new ArrayList();
130         threeClasses.add(classA);
131         threeClasses.add(classB);
132         threeClasses.add(classC);
133 
134         assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01));
135 
136         List twoClasses = new ArrayList();
137         twoClasses.add(classA);
138         twoClasses.add(classB);
139         
140         assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01));
141     }
142 
143 }