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: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
30   */
31  
32  public class OneWayAnovaTest extends TestCase {
33  
34      protected OneWayAnova testStatistic = new OneWayAnovaImpl();
35      
36      private double[] emptyArray = {};
37  
38      private double[] classA =
39              {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 };
40      private double[] classB =
41              {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 };
42      private double[] classC =
43              {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 };
44  
45      public OneWayAnovaTest(String name) {
46          super(name);
47      }
48  
49      public static Test suite() {
50          TestSuite suite = new TestSuite(OneWayAnovaTest.class);
51          suite.setName("TestStatistic Tests");
52          return suite;
53      }
54  
55      public void testAnovaFValue() throws Exception {
56          // Target comparison values computed using R version 2.6.0 (Linux version)
57          List<double[]> threeClasses = new ArrayList<double[]>();
58          threeClasses.add(classA);
59          threeClasses.add(classB);
60          threeClasses.add(classC);
61  
62          assertEquals("ANOVA F-value",  24.67361709460624,
63                   testStatistic.anovaFValue(threeClasses), 1E-12);
64  
65          List<double[]> twoClasses = new ArrayList<double[]>();
66          twoClasses.add(classA);
67          twoClasses.add(classB);
68          
69          assertEquals("ANOVA F-value",  0.0150579150579,
70                   testStatistic.anovaFValue(twoClasses), 1E-12);
71  
72          List<double[]> emptyContents = new ArrayList<double[]>();
73          emptyContents.add(emptyArray);
74          emptyContents.add(classC);
75          try {
76              testStatistic.anovaFValue(emptyContents);
77              fail("empty array for key classX, IllegalArgumentException expected");
78          } catch (IllegalArgumentException ex) {
79              // expected
80          }  
81  
82          List<double[]> tooFew = new ArrayList<double[]>();
83          tooFew.add(classA);
84          try {
85              testStatistic.anovaFValue(tooFew);
86              fail("less than two classes, IllegalArgumentException expected");
87          } catch (IllegalArgumentException ex) {
88              // expected
89          }  
90      }
91      
92  
93      public void testAnovaPValue() throws Exception {
94          // Target comparison values computed using R version 2.6.0 (Linux version)
95          List<double[]> threeClasses = new ArrayList<double[]>();
96          threeClasses.add(classA);
97          threeClasses.add(classB);
98          threeClasses.add(classC);
99  
100         assertEquals("ANOVA P-value", 6.959446E-06,
101                  testStatistic.anovaPValue(threeClasses), 1E-12);
102 
103         List<double[]> twoClasses = new ArrayList<double[]>();
104         twoClasses.add(classA);
105         twoClasses.add(classB);
106         
107         assertEquals("ANOVA P-value",  0.904212960464,
108                  testStatistic.anovaPValue(twoClasses), 1E-12);
109 
110     }
111 
112     public void testAnovaTest() throws Exception {
113         // Target comparison values computed using R version 2.3.1 (Linux version)
114         List<double[]> threeClasses = new ArrayList<double[]>();
115         threeClasses.add(classA);
116         threeClasses.add(classB);
117         threeClasses.add(classC);
118 
119         assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01));
120 
121         List<double[]> twoClasses = new ArrayList<double[]>();
122         twoClasses.add(classA);
123         twoClasses.add(classB);
124         
125         assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01));
126     }
127 
128 }