1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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  
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          
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              
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              
90          }
91      }
92  
93  
94      @Test
95      void testAnovaPValue() {
96          
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         
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         
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 }