2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.distribution;
19
import junit.framework.TestCase;
22
* Abstract base class for {@link IntegerDistribution} tests.
24
* To create a concrete test class for an integer distribution implementation,
25
* implement makeDistribution() to return a distribution instance to use in
26
* tests and each of the test data generation methods below. In each case, the
27
* test points and test values arrays returned represent parallel arrays of
28
* inputs and expected values for the distribution returned by makeDistribution().
30
* makeDensityTestPoints() -- arguments used to test probability density calculation
31
* makeDensityTestValues() -- expected probability densities
32
* makeCumulativeTestPoints() -- arguments used to test cumulative probabilities
33
* makeCumulativeTestValues() -- expected cumulative probabilites
34
* makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf evaluation
35
* makeInverseCumulativeTestValues() -- expected inverse cdf values
37
* To implement additional test cases with different distribution instances and test data,
38
* use the setXxx methods for the instance data in test cases and call the verifyXxx methods
41
* @version $Revision: 762118 $ $Date: 2009-04-05 12:55:59 -0400 (Sun, 05 Apr 2009) $
43
public abstract class IntegerDistributionAbstractTest extends TestCase {
45
//-------------------- Private test instance data -------------------------
46
/** Discrete distribution instance used to perform tests */
47
private IntegerDistribution distribution;
49
/** Tolerance used in comparing expected and returned values */
50
private double tolerance = 1E-4;
52
/** Arguments used to test probability density calculations */
53
private int[] densityTestPoints;
55
/** Values used to test probability density calculations */
56
private double[] densityTestValues;
58
/** Arguments used to test cumulative probability density calculations */
59
private int[] cumulativeTestPoints;
61
/** Values used to test cumulative probability density calculations */
62
private double[] cumulativeTestValues;
64
/** Arguments used to test inverse cumulative probability density calculations */
65
private double[] inverseCumulativeTestPoints;
67
/** Values used to test inverse cumulative probability density calculations */
68
private int[] inverseCumulativeTestValues;
70
//-------------------------------------------------------------------------
73
* Constructor for IntegerDistributionAbstractTest.
76
public IntegerDistributionAbstractTest(String name) {
80
//-------------------- Abstract methods -----------------------------------
82
/** Creates the default discrete distribution instance to use in tests. */
83
public abstract IntegerDistribution makeDistribution();
85
/** Creates the default probability density test input values */
86
public abstract int[] makeDensityTestPoints();
88
/** Creates the default probability density test expected values */
89
public abstract double[] makeDensityTestValues();
91
/** Creates the default cumulative probability density test input values */
92
public abstract int[] makeCumulativeTestPoints();
94
/** Creates the default cumulative probability density test expected values */
95
public abstract double[] makeCumulativeTestValues();
97
/** Creates the default inverse cumulative probability test input values */
98
public abstract double[] makeInverseCumulativeTestPoints();
100
/** Creates the default inverse cumulative probability density test expected values */
101
public abstract int[] makeInverseCumulativeTestValues();
103
//-------------------- Setup / tear down ----------------------------------
106
* Setup sets all test instance data to default values
109
protected void setUp() throws Exception {
111
distribution = makeDistribution();
112
densityTestPoints = makeDensityTestPoints();
113
densityTestValues = makeDensityTestValues();
114
cumulativeTestPoints = makeCumulativeTestPoints();
115
cumulativeTestValues = makeCumulativeTestValues();
116
inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
117
inverseCumulativeTestValues = makeInverseCumulativeTestValues();
121
* Cleans up test instance data
124
protected void tearDown() throws Exception {
127
densityTestPoints = null;
128
densityTestValues = null;
129
cumulativeTestPoints = null;
130
cumulativeTestValues = null;
131
inverseCumulativeTestPoints = null;
132
inverseCumulativeTestValues = null;
135
//-------------------- Verification methods -------------------------------
138
* Verifies that probability density calculations match expected values
139
* using current test instance data
141
protected void verifyDensities() throws Exception {
142
for (int i = 0; i < densityTestPoints.length; i++) {
143
assertEquals("Incorrect density value returned for " + densityTestPoints[i],
144
densityTestValues[i],
145
distribution.probability(densityTestPoints[i]), tolerance);
150
* Verifies that cumulative probability density calculations match expected values
151
* using current test instance data
153
protected void verifyCumulativeProbabilities() throws Exception {
154
for (int i = 0; i < cumulativeTestPoints.length; i++) {
155
assertEquals("Incorrect cumulative probability value returned for " + cumulativeTestPoints[i],
156
cumulativeTestValues[i],
157
distribution.cumulativeProbability(cumulativeTestPoints[i]), tolerance);
163
* Verifies that inverse cumulative probability density calculations match expected values
164
* using current test instance data
166
protected void verifyInverseCumulativeProbabilities() throws Exception {
167
for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
168
assertEquals("Incorrect inverse cumulative probability value returned for "
169
+ inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
170
distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]));
174
//------------------------ Default test cases -----------------------------
177
* Verifies that probability density calculations match expected values
178
* using default test instance data
180
public void testDensities() throws Exception {
185
* Verifies that cumulative probability density calculations match expected values
186
* using default test instance data
188
public void testCumulativeProbabilities() throws Exception {
189
verifyCumulativeProbabilities();
193
* Verifies that floating point arguments are correctly handled by
194
* cumulativeProbablility(-,-)
197
public void testFloatingPointArguments() throws Exception {
198
for (int i = 0; i < cumulativeTestPoints.length; i++) {
199
double arg = cumulativeTestPoints[i];
201
"Incorrect cumulative probability value returned for " +
202
cumulativeTestPoints[i],
203
cumulativeTestValues[i],
204
distribution.cumulativeProbability(arg), tolerance);
205
if (i < cumulativeTestPoints.length - 1) {
206
double arg2 = cumulativeTestPoints[i + 1];
207
assertEquals("Inconsistent probability for discrete range " +
208
"[ " + arg + "," + arg2 + " ]",
209
distribution.cumulativeProbability(
210
cumulativeTestPoints[i],
211
cumulativeTestPoints[i + 1]),
212
distribution.cumulativeProbability(arg, arg2), tolerance);
213
arg = arg - Math.random();
214
arg2 = arg2 + Math.random();
215
assertEquals("Inconsistent probability for discrete range " +
216
"[ " + arg + "," + arg2 + " ]",
217
distribution.cumulativeProbability(
218
cumulativeTestPoints[i],
219
cumulativeTestPoints[i + 1]),
220
distribution.cumulativeProbability(arg, arg2), tolerance);
229
assertEquals(distribution.cumulativeProbability(one, two),
230
distribution.cumulativeProbability(oned, twod), tolerance);
231
assertEquals(distribution.cumulativeProbability(one, two),
232
distribution.cumulativeProbability(oned - tolerance,
233
twod + 0.9), tolerance);
234
assertEquals(distribution.cumulativeProbability(two, ten),
235
distribution.cumulativeProbability(twod, tend), tolerance);
236
assertEquals(distribution.cumulativeProbability(two, ten),
237
distribution.cumulativeProbability(twod - tolerance,
238
tend + 0.9), tolerance);
242
* Verifies that inverse cumulative probability density calculations match expected values
243
* using default test instance data
245
public void testInverseCumulativeProbabilities() throws Exception {
246
verifyInverseCumulativeProbabilities();
250
* Verifies that illegal arguments are correctly handled
252
public void testIllegalArguments() throws Exception {
254
distribution.cumulativeProbability(1, 0);
255
fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
256
} catch (IllegalArgumentException ex) {
260
distribution.inverseCumulativeProbability(-1);
261
fail("Expecting IllegalArgumentException for p = -1");
262
} catch (IllegalArgumentException ex) {
266
distribution.inverseCumulativeProbability(2);
267
fail("Expecting IllegalArgumentException for p = 2");
268
} catch (IllegalArgumentException ex) {
273
//------------------ Getters / Setters for test instance data -----------
275
* @return Returns the cumulativeTestPoints.
277
protected int[] getCumulativeTestPoints() {
278
return cumulativeTestPoints;
282
* @param cumulativeTestPoints The cumulativeTestPoints to set.
284
protected void setCumulativeTestPoints(int[] cumulativeTestPoints) {
285
this.cumulativeTestPoints = cumulativeTestPoints;
289
* @return Returns the cumulativeTestValues.
291
protected double[] getCumulativeTestValues() {
292
return cumulativeTestValues;
296
* @param cumulativeTestValues The cumulativeTestValues to set.
298
protected void setCumulativeTestValues(double[] cumulativeTestValues) {
299
this.cumulativeTestValues = cumulativeTestValues;
303
* @return Returns the densityTestPoints.
305
protected int[] getDensityTestPoints() {
306
return densityTestPoints;
310
* @param densityTestPoints The densityTestPoints to set.
312
protected void setDensityTestPoints(int[] densityTestPoints) {
313
this.densityTestPoints = densityTestPoints;
317
* @return Returns the densityTestValues.
319
protected double[] getDensityTestValues() {
320
return densityTestValues;
324
* @param densityTestValues The densityTestValues to set.
326
protected void setDensityTestValues(double[] densityTestValues) {
327
this.densityTestValues = densityTestValues;
331
* @return Returns the distribution.
333
protected IntegerDistribution getDistribution() {
338
* @param distribution The distribution to set.
340
protected void setDistribution(IntegerDistribution distribution) {
341
this.distribution = distribution;
345
* @return Returns the inverseCumulativeTestPoints.
347
protected double[] getInverseCumulativeTestPoints() {
348
return inverseCumulativeTestPoints;
352
* @param inverseCumulativeTestPoints The inverseCumulativeTestPoints to set.
354
protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
355
this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
359
* @return Returns the inverseCumulativeTestValues.
361
protected int[] getInverseCumulativeTestValues() {
362
return inverseCumulativeTestValues;
366
* @param inverseCumulativeTestValues The inverseCumulativeTestValues to set.
368
protected void setInverseCumulativeTestValues(int[] inverseCumulativeTestValues) {
369
this.inverseCumulativeTestValues = inverseCumulativeTestValues;
373
* @return Returns the tolerance.
375
protected double getTolerance() {
380
* @param tolerance The tolerance to set.
382
protected void setTolerance(double tolerance) {
383
this.tolerance = tolerance;