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 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
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
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
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
86 }
87 }
88
89
90 @Test
91 public void testAnovaPValue() {
92
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
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
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 }