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