1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.stat.inference;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32 public class OneWayAnovaTest extends TestCase {
33
34 protected OneWayAnova testStatistic = new OneWayAnovaImpl();
35
36 private double[] emptyArray = {};
37
38 private double[] classA =
39 {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 };
40 private double[] classB =
41 {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 };
42 private double[] classC =
43 {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 };
44
45 public OneWayAnovaTest(String name) {
46 super(name);
47 }
48
49 public static Test suite() {
50 TestSuite suite = new TestSuite(OneWayAnovaTest.class);
51 suite.setName("TestStatistic Tests");
52 return suite;
53 }
54
55 public void testAnovaFValue() throws Exception {
56
57 List<double[]> threeClasses = new ArrayList<double[]>();
58 threeClasses.add(classA);
59 threeClasses.add(classB);
60 threeClasses.add(classC);
61
62 assertEquals("ANOVA F-value", 24.67361709460624,
63 testStatistic.anovaFValue(threeClasses), 1E-12);
64
65 List<double[]> twoClasses = new ArrayList<double[]>();
66 twoClasses.add(classA);
67 twoClasses.add(classB);
68
69 assertEquals("ANOVA F-value", 0.0150579150579,
70 testStatistic.anovaFValue(twoClasses), 1E-12);
71
72 List<double[]> emptyContents = new ArrayList<double[]>();
73 emptyContents.add(emptyArray);
74 emptyContents.add(classC);
75 try {
76 testStatistic.anovaFValue(emptyContents);
77 fail("empty array for key classX, IllegalArgumentException expected");
78 } catch (IllegalArgumentException ex) {
79
80 }
81
82 List<double[]> tooFew = new ArrayList<double[]>();
83 tooFew.add(classA);
84 try {
85 testStatistic.anovaFValue(tooFew);
86 fail("less than two classes, IllegalArgumentException expected");
87 } catch (IllegalArgumentException ex) {
88
89 }
90 }
91
92
93 public void testAnovaPValue() throws Exception {
94
95 List<double[]> threeClasses = new ArrayList<double[]>();
96 threeClasses.add(classA);
97 threeClasses.add(classB);
98 threeClasses.add(classC);
99
100 assertEquals("ANOVA P-value", 6.959446E-06,
101 testStatistic.anovaPValue(threeClasses), 1E-12);
102
103 List<double[]> twoClasses = new ArrayList<double[]>();
104 twoClasses.add(classA);
105 twoClasses.add(classB);
106
107 assertEquals("ANOVA P-value", 0.904212960464,
108 testStatistic.anovaPValue(twoClasses), 1E-12);
109
110 }
111
112 public void testAnovaTest() throws Exception {
113
114 List<double[]> threeClasses = new ArrayList<double[]>();
115 threeClasses.add(classA);
116 threeClasses.add(classB);
117 threeClasses.add(classC);
118
119 assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01));
120
121 List<double[]> twoClasses = new ArrayList<double[]>();
122 twoClasses.add(classA);
123 twoClasses.add(classB);
124
125 assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01));
126 }
127
128 }