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 java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
23  import org.apache.commons.math4.legacy.stat.descriptive.SummaryStatistics;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  
28  /**
29   * Test cases for the OneWayAnovaImpl class.
30   *
31   */
32  
33  public class OneWayAnovaTest {
34  
35      protected OneWayAnova testStatistic = new OneWayAnova();
36  
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      @Test
47      public void testAnovaFValue() {
48          // Target comparison values computed using R version 2.6.0 (Linux version)
49          List<double[]> threeClasses = new ArrayList<>();
50          threeClasses.add(classA);
51          threeClasses.add(classB);
52          threeClasses.add(classC);
53  
54          Assert.assertEquals("ANOVA F-value",  24.67361709460624,
55                   testStatistic.anovaFValue(threeClasses), 1E-12);
56  
57          List<double[]> twoClasses = new ArrayList<>();
58          twoClasses.add(classA);
59          twoClasses.add(classB);
60  
61          Assert.assertEquals("ANOVA F-value",  0.0150579150579,
62                   testStatistic.anovaFValue(twoClasses), 1E-12);
63  
64          List<double[]> emptyContents = new ArrayList<>();
65          emptyContents.add(emptyArray);
66          emptyContents.add(classC);
67          try {
68              testStatistic.anovaFValue(emptyContents);
69              Assert.fail("empty array for key classX, MathIllegalArgumentException expected");
70          } catch (MathIllegalArgumentException ex) {
71              // expected
72          }
73  
74          List<double[]> tooFew = new ArrayList<>();
75          tooFew.add(classA);
76          try {
77              testStatistic.anovaFValue(tooFew);
78              Assert.fail("less than two classes, MathIllegalArgumentException expected");
79          } catch (MathIllegalArgumentException ex) {
80              // expected
81          }
82      }
83  
84  
85      @Test
86      public void testAnovaPValue() {
87          // Target comparison values computed using R version 2.6.0 (Linux version)
88          List<double[]> threeClasses = new ArrayList<>();
89          threeClasses.add(classA);
90          threeClasses.add(classB);
91          threeClasses.add(classC);
92  
93          Assert.assertEquals("ANOVA P-value", 6.959446E-06,
94                   testStatistic.anovaPValue(threeClasses), 1E-12);
95  
96          List<double[]> twoClasses = new ArrayList<>();
97          twoClasses.add(classA);
98          twoClasses.add(classB);
99  
100         Assert.assertEquals("ANOVA P-value",  0.904212960464,
101                  testStatistic.anovaPValue(twoClasses), 1E-12);
102     }
103 
104     @Test
105     public void testAnovaPValueSummaryStatistics() {
106         // Target comparison values computed using R version 2.6.0 (Linux version)
107         List<SummaryStatistics> threeClasses = new ArrayList<>();
108         SummaryStatistics statsA = new SummaryStatistics();
109         for (double a : classA) {
110             statsA.addValue(a);
111         }
112         threeClasses.add(statsA);
113         SummaryStatistics statsB = new SummaryStatistics();
114         for (double b : classB) {
115             statsB.addValue(b);
116         }
117         threeClasses.add(statsB);
118         SummaryStatistics statsC = new SummaryStatistics();
119         for (double c : classC) {
120             statsC.addValue(c);
121         }
122         threeClasses.add(statsC);
123 
124         Assert.assertEquals("ANOVA P-value", 6.959446E-06,
125                  testStatistic.anovaPValue(threeClasses, true), 1E-12);
126 
127         List<SummaryStatistics> twoClasses = new ArrayList<>();
128         twoClasses.add(statsA);
129         twoClasses.add(statsB);
130 
131         Assert.assertEquals("ANOVA P-value",  0.904212960464,
132                  testStatistic.anovaPValue(twoClasses, false), 1E-12);
133     }
134 
135     @Test
136     public void testAnovaTest() {
137         // Target comparison values computed using R version 2.3.1 (Linux version)
138         List<double[]> threeClasses = new ArrayList<>();
139         threeClasses.add(classA);
140         threeClasses.add(classB);
141         threeClasses.add(classC);
142 
143         Assert.assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01));
144 
145         List<double[]> twoClasses = new ArrayList<>();
146         twoClasses.add(classA);
147         twoClasses.add(classB);
148 
149         Assert.assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01));
150     }
151 }