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