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 org.apache.commons.math.MathException;
20 import org.apache.commons.math.stat.descriptive.summary.Sum;
21 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
22
23 import org.apache.commons.math.distribution.FDistribution;
24 import org.apache.commons.math.distribution.FDistributionImpl;
25
26 import java.util.Collection;
27 import java.util.Iterator;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class OneWayAnovaImpl implements OneWayAnova {
50
51
52
53
54 public OneWayAnovaImpl() {
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68 public double anovaFValue(Collection categoryData)
69 throws IllegalArgumentException, MathException {
70 AnovaStats a = anovaStats(categoryData);
71 return a.F;
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public double anovaPValue(Collection categoryData)
85 throws IllegalArgumentException, MathException {
86 AnovaStats a = anovaStats(categoryData);
87 FDistribution fdist = new FDistributionImpl(a.dfbg, a.dfwg);
88 return 1.0 - fdist.cumulativeProbability(a.F);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 public boolean anovaTest(Collection categoryData, double alpha)
103 throws IllegalArgumentException, MathException {
104 if ((alpha <= 0) || (alpha > 0.5)) {
105 throw new IllegalArgumentException("bad significance level: " + alpha);
106 }
107 return (anovaPValue(categoryData) < alpha);
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 private AnovaStats anovaStats(Collection categoryData)
122 throws IllegalArgumentException, MathException {
123
124
125 if (categoryData.size() < 2) {
126 throw new IllegalArgumentException(
127 "ANOVA: two or more categories required");
128 }
129
130
131 for (Iterator iterator = categoryData.iterator(); iterator.hasNext();) {
132 double[] array;
133 try {
134 array = (double[])iterator.next();
135 } catch (ClassCastException ex) {
136 throw new IllegalArgumentException(
137 "ANOVA: categoryData contains non-double[] elements.");
138 }
139 if (array.length <= 1) {
140 throw new IllegalArgumentException(
141 "ANOVA: one element of categoryData has fewer than 2 values.");
142 }
143 }
144
145 int dfwg = 0;
146 double sswg = 0;
147 Sum totsum = new Sum();
148 SumOfSquares totsumsq = new SumOfSquares();
149 int totnum = 0;
150
151 for (Iterator iterator = categoryData.iterator(); iterator.hasNext();) {
152 double[] data = (double[])iterator.next();
153
154 Sum sum = new Sum();
155 SumOfSquares sumsq = new SumOfSquares();
156 int num = 0;
157
158 for (int i = 0; i < data.length; i++) {
159 double val = data[i];
160
161
162 num++;
163 sum.increment(val);
164 sumsq.increment(val);
165
166
167 totnum++;
168 totsum.increment(val);
169 totsumsq.increment(val);
170 }
171 dfwg += num - 1;
172 double ss = sumsq.getResult() - sum.getResult() * sum.getResult() / num;
173 sswg += ss;
174 }
175 double sst = totsumsq.getResult() - totsum.getResult() *
176 totsum.getResult()/totnum;
177 double ssbg = sst - sswg;
178 int dfbg = categoryData.size() - 1;
179 double msbg = ssbg/dfbg;
180 double mswg = sswg/dfwg;
181 double F = msbg/mswg;
182
183 return new AnovaStats(dfbg, dfwg, F);
184 }
185
186
187
188
189
190 private static class AnovaStats {
191 private int dfbg;
192 private int dfwg;
193 private double F;
194
195
196
197
198
199
200
201 AnovaStats(int dfbg, int dfwg, double F) {
202 this.dfbg = dfbg;
203 this.dfwg = dfwg;
204 this.F = F;
205 }
206 }
207
208 }