1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.math.distribution;
17
18 import junit.framework.TestCase;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public abstract class IntegerDistributionAbstractTest extends TestCase {
43
44
45
46 private IntegerDistribution distribution;
47
48
49 private double tolerance = 1E-4;
50
51
52 private int[] densityTestPoints;
53
54
55 private double[] densityTestValues;
56
57
58 private int[] cumulativeTestPoints;
59
60
61 private double[] cumulativeTestValues;
62
63
64 private double[] inverseCumulativeTestPoints;
65
66
67 private int[] inverseCumulativeTestValues;
68
69
70
71
72
73
74
75 public IntegerDistributionAbstractTest(String name) {
76 super(name);
77 }
78
79
80
81
82 public abstract IntegerDistribution makeDistribution();
83
84
85 public abstract int[] makeDensityTestPoints();
86
87
88 public abstract double[] makeDensityTestValues();
89
90
91 public abstract int[] makeCumulativeTestPoints();
92
93
94 public abstract double[] makeCumulativeTestValues();
95
96
97 public abstract double[] makeInverseCumulativeTestPoints();
98
99
100 public abstract int[] makeInverseCumulativeTestValues();
101
102
103
104
105
106
107 protected void setUp() throws Exception {
108 super.setUp();
109 distribution = makeDistribution();
110 densityTestPoints = makeDensityTestPoints();
111 densityTestValues = makeDensityTestValues();
112 cumulativeTestPoints = makeCumulativeTestPoints();
113 cumulativeTestValues = makeCumulativeTestValues();
114 inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
115 inverseCumulativeTestValues = makeInverseCumulativeTestValues();
116 }
117
118
119
120
121 protected void tearDown() throws Exception {
122 super.tearDown();
123 distribution = null;
124 densityTestPoints = null;
125 densityTestValues = null;
126 cumulativeTestPoints = null;
127 cumulativeTestValues = null;
128 inverseCumulativeTestPoints = null;
129 inverseCumulativeTestValues = null;
130 }
131
132
133
134
135
136
137
138 protected void verifyDensities() throws Exception {
139 for (int i = 0; i < densityTestPoints.length; i++) {
140 assertEquals("Incorrect density value returned for " + densityTestPoints[i],
141 densityTestValues[i],
142 distribution.probability(densityTestPoints[i]), tolerance);
143 }
144 }
145
146
147
148
149
150 protected void verifyCumulativeProbabilities() throws Exception {
151 for (int i = 0; i < cumulativeTestPoints.length; i++) {
152 assertEquals("Incorrect cumulative probability value returned for " + cumulativeTestPoints[i],
153 cumulativeTestValues[i],
154 distribution.cumulativeProbability(cumulativeTestPoints[i]), tolerance);
155 }
156 }
157
158
159
160
161
162 protected void verifyInverseCumulativeProbabilities() throws Exception {
163 for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
164 assertEquals("Incorrect inverse cumulative probability value returned for "
165 + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
166 distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]));
167 }
168 }
169
170
171
172
173
174
175
176 public void testDensities() throws Exception {
177 verifyDensities();
178 }
179
180
181
182
183
184 public void testCumulativeProbabilities() throws Exception {
185 verifyCumulativeProbabilities();
186 }
187
188
189
190
191
192 public void testInverseCumulativeProbabilities() throws Exception {
193 verifyInverseCumulativeProbabilities();
194 }
195
196
197
198
199 public void testIllegalArguments() throws Exception {
200 try {
201 distribution.cumulativeProbability(1, 0);
202 fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
203 } catch (IllegalArgumentException ex) {
204
205 }
206 try {
207 distribution.inverseCumulativeProbability(-1);
208 fail("Expecting IllegalArgumentException for p = -1");
209 } catch (IllegalArgumentException ex) {
210
211 }
212 try {
213 distribution.inverseCumulativeProbability(2);
214 fail("Expecting IllegalArgumentException for p = 2");
215 } catch (IllegalArgumentException ex) {
216
217 }
218 }
219
220
221
222
223
224 protected int[] getCumulativeTestPoints() {
225 return cumulativeTestPoints;
226 }
227
228
229
230
231 protected void setCumulativeTestPoints(int[] cumulativeTestPoints) {
232 this.cumulativeTestPoints = cumulativeTestPoints;
233 }
234
235
236
237
238 protected double[] getCumulativeTestValues() {
239 return cumulativeTestValues;
240 }
241
242
243
244
245 protected void setCumulativeTestValues(double[] cumulativeTestValues) {
246 this.cumulativeTestValues = cumulativeTestValues;
247 }
248
249
250
251
252 protected int[] getDensityTestPoints() {
253 return densityTestPoints;
254 }
255
256
257
258
259 protected void setDensityTestPoints(int[] densityTestPoints) {
260 this.densityTestPoints = densityTestPoints;
261 }
262
263
264
265
266 protected double[] getDensityTestValues() {
267 return densityTestValues;
268 }
269
270
271
272
273 protected void setDensityTestValues(double[] densityTestValues) {
274 this.densityTestValues = densityTestValues;
275 }
276
277
278
279
280 protected IntegerDistribution getDistribution() {
281 return distribution;
282 }
283
284
285
286
287 protected void setDistribution(IntegerDistribution distribution) {
288 this.distribution = distribution;
289 }
290
291
292
293
294 protected double[] getInverseCumulativeTestPoints() {
295 return inverseCumulativeTestPoints;
296 }
297
298
299
300
301 protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
302 this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
303 }
304
305
306
307
308 protected int[] getInverseCumulativeTestValues() {
309 return inverseCumulativeTestValues;
310 }
311
312
313
314
315 protected void setInverseCumulativeTestValues(int[] inverseCumulativeTestValues) {
316 this.inverseCumulativeTestValues = inverseCumulativeTestValues;
317 }
318
319
320
321
322 protected double getTolerance() {
323 return tolerance;
324 }
325
326
327
328
329 protected void setTolerance(double tolerance) {
330 this.tolerance = tolerance;
331 }
332
333 }