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 char[] wrongArray = { 'a', 'b', 'c' };
37 private double[] emptyArray = {};
38
39 private double[] classA =
40 {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 };
41 private double[] classB =
42 {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 };
43 private double[] classC =
44 {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 };
45
46 public OneWayAnovaTest(String name) {
47 super(name);
48 }
49
50 public void setUp() {
51 }
52
53 public static Test suite() {
54 TestSuite suite = new TestSuite(OneWayAnovaTest.class);
55 suite.setName("TestStatistic Tests");
56 return suite;
57 }
58
59 public void testAnovaFValue() throws Exception {
60
61 List threeClasses = new ArrayList();
62 threeClasses.add(classA);
63 threeClasses.add(classB);
64 threeClasses.add(classC);
65
66 assertEquals("ANOVA F-value", 24.67361709460624,
67 testStatistic.anovaFValue(threeClasses), 1E-12);
68
69 List twoClasses = new ArrayList();
70 twoClasses.add(classA);
71 twoClasses.add(classB);
72
73 assertEquals("ANOVA F-value", 0.0150579150579,
74 testStatistic.anovaFValue(twoClasses), 1E-12);
75
76
77 List wrongContents = new ArrayList();
78 wrongContents.add(classC);
79 wrongContents.add(wrongArray);
80 try {
81 testStatistic.anovaFValue(wrongContents);
82 fail("non double[] hash value for key classX, IllegalArgumentException expected");
83 } catch (IllegalArgumentException ex) {
84
85 }
86
87 List emptyContents = new ArrayList();
88 emptyContents.add(emptyArray);
89 emptyContents.add(classC);
90 try {
91 testStatistic.anovaFValue(emptyContents);
92 fail("empty array for key classX, IllegalArgumentException expected");
93 } catch (IllegalArgumentException ex) {
94
95 }
96
97 List tooFew = new ArrayList();
98 tooFew.add(classA);
99 try {
100 testStatistic.anovaFValue(tooFew);
101 fail("less than two classes, IllegalArgumentException expected");
102 } catch (IllegalArgumentException ex) {
103
104 }
105 }
106
107
108 public void testAnovaPValue() throws Exception {
109
110 List threeClasses = new ArrayList();
111 threeClasses.add(classA);
112 threeClasses.add(classB);
113 threeClasses.add(classC);
114
115 assertEquals("ANOVA P-value", 6.959446E-06,
116 testStatistic.anovaPValue(threeClasses), 1E-12);
117
118 List twoClasses = new ArrayList();
119 twoClasses.add(classA);
120 twoClasses.add(classB);
121
122 assertEquals("ANOVA P-value", 0.904212960464,
123 testStatistic.anovaPValue(twoClasses), 1E-12);
124
125 }
126
127 public void testAnovaTest() throws Exception {
128
129 List threeClasses = new ArrayList();
130 threeClasses.add(classA);
131 threeClasses.add(classB);
132 threeClasses.add(classC);
133
134 assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01));
135
136 List twoClasses = new ArrayList();
137 twoClasses.add(classA);
138 twoClasses.add(classB);
139
140 assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01));
141 }
142
143 }