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