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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.inference;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.hipparchus.exception.MathIllegalArgumentException;
28  import org.hipparchus.stat.descriptive.StreamingStatistics;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  
33  /**
34   * Test cases for the OneWayAnovaImpl class.
35   *
36   */
37  
38  public class OneWayAnovaTest {
39  
40      protected OneWayAnova testStatistic = new OneWayAnova();
41  
42      private double[] emptyArray = {};
43  
44      private double[] classA =
45              {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 };
46      private double[] classB =
47              {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 };
48      private double[] classC =
49              {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 };
50  
51      @Test
52      public void testAnovaFValue() {
53          // Target comparison values computed using R version 2.6.0 (Linux version)
54          List<double[]> threeClasses = new ArrayList<double[]>();
55          threeClasses.add(classA);
56          threeClasses.add(classB);
57          threeClasses.add(classC);
58  
59          Assert.assertEquals("ANOVA F-value",  24.67361709460624,
60                   testStatistic.anovaFValue(threeClasses), 1E-12);
61  
62          List<double[]> twoClasses = new ArrayList<double[]>();
63          twoClasses.add(classA);
64          twoClasses.add(classB);
65  
66          Assert.assertEquals("ANOVA F-value",  0.0150579150579,
67                   testStatistic.anovaFValue(twoClasses), 1E-12);
68  
69          List<double[]> emptyContents = new ArrayList<double[]>();
70          emptyContents.add(emptyArray);
71          emptyContents.add(classC);
72          try {
73              testStatistic.anovaFValue(emptyContents);
74              Assert.fail("empty array for key classX, MathIllegalArgumentException expected");
75          } catch (MathIllegalArgumentException ex) {
76              // expected
77          }
78  
79          List<double[]> tooFew = new ArrayList<double[]>();
80          tooFew.add(classA);
81          try {
82              testStatistic.anovaFValue(tooFew);
83              Assert.fail("less than two classes, MathIllegalArgumentException expected");
84          } catch (MathIllegalArgumentException ex) {
85              // expected
86          }
87      }
88  
89  
90      @Test
91      public void testAnovaPValue() {
92          // Target comparison values computed using R version 2.6.0 (Linux version)
93          List<double[]> threeClasses = new ArrayList<double[]>();
94          threeClasses.add(classA);
95          threeClasses.add(classB);
96          threeClasses.add(classC);
97  
98          Assert.assertEquals("ANOVA P-value", 6.959446E-06,
99                   testStatistic.anovaPValue(threeClasses), 1E-12);
100 
101         List<double[]> twoClasses = new ArrayList<double[]>();
102         twoClasses.add(classA);
103         twoClasses.add(classB);
104 
105         Assert.assertEquals("ANOVA P-value",  0.904212960464,
106                  testStatistic.anovaPValue(twoClasses), 1E-12);
107 
108     }
109 
110     @Test
111     public void testAnovaPValueSummaryStatistics() {
112         // Target comparison values computed using R version 2.6.0 (Linux version)
113         List<StreamingStatistics> threeClasses = new ArrayList<StreamingStatistics>();
114         StreamingStatistics statsA = new StreamingStatistics();
115         for (double a : classA) {
116             statsA.addValue(a);
117         }
118         threeClasses.add(statsA);
119         StreamingStatistics statsB = new StreamingStatistics();
120         for (double b : classB) {
121             statsB.addValue(b);
122         }
123         threeClasses.add(statsB);
124         StreamingStatistics statsC = new StreamingStatistics();
125         for (double c : classC) {
126             statsC.addValue(c);
127         }
128         threeClasses.add(statsC);
129 
130         Assert.assertEquals("ANOVA P-value", 6.959446E-06,
131                  testStatistic.anovaPValue(threeClasses, true), 1E-12);
132 
133         List<StreamingStatistics> twoClasses = new ArrayList<StreamingStatistics>();
134         twoClasses.add(statsA);
135         twoClasses.add(statsB);
136 
137         Assert.assertEquals("ANOVA P-value",  0.904212960464,
138                  testStatistic.anovaPValue(twoClasses, false), 1E-12);
139 
140     }
141 
142     @Test
143     public void testAnovaTest() {
144         // Target comparison values computed using R version 2.3.1 (Linux version)
145         List<double[]> threeClasses = new ArrayList<double[]>();
146         threeClasses.add(classA);
147         threeClasses.add(classB);
148         threeClasses.add(classC);
149 
150         Assert.assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01));
151 
152         List<double[]> twoClasses = new ArrayList<double[]>();
153         twoClasses.add(classA);
154         twoClasses.add(classB);
155 
156         Assert.assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01));
157     }
158 
159 }