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