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;
21
import org.apache.commons.math.TestUtils;
24
* Abstract base class for {@link ContinuousDistribution} tests.
26
* To create a concrete test class for a continuous distribution
27
* implementation, first implement makeDistribution() to return a distribution
28
* instance to use in tests. Then implement each of the test data generation
29
* methods below. In each case, the test points and test values arrays
30
* returned represent parallel arrays of inputs and expected values for the
31
* distribution returned by makeDistribution(). Default implementations
32
* are provided for the makeInverseXxx methods that just invert the mapping
33
* defined by the arrays returned by the makeCumulativeXxx methods.
35
* makeCumulativeTestPoints() -- arguments used to test cumulative probabilities
36
* makeCumulativeTestValues() -- expected cumulative probabilites
37
* makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf
38
* makeInverseCumulativeTestValues() -- expected inverse cdf values
40
* To implement additional test cases with different distribution instances and
41
* test data, use the setXxx methods for the instance data in test cases and
42
* call the verifyXxx methods to verify results.
44
* Error tolerance can be overriden by implementing getTolerance().
46
* Test data should be validated against reference tables or other packages
47
* where possible, and the source of the reference data and/or validation
48
* should be documented in the test cases. A framework for validating
49
* distribution data against R is included in the /src/test/R source tree.
51
* See {@link NormalDistributionTest} and {@link ChiSquareDistributionTest}
54
* @version $Revision: 545192 $ $Date: 2007-06-07 07:35:04 -0700 (Thu, 07 Jun 2007) $
56
public abstract class ContinuousDistributionAbstractTest extends TestCase {
58
//-------------------- Private test instance data -------------------------
59
/** Distribution instance used to perform tests */
60
private ContinuousDistribution distribution;
62
/** Tolerance used in comparing expected and returned values */
63
private double tolerance = 1E-4;
65
/** Arguments used to test cumulative probability density calculations */
66
private double[] cumulativeTestPoints;
68
/** Values used to test cumulative probability density calculations */
69
private double[] cumulativeTestValues;
71
/** Arguments used to test inverse cumulative probability density calculations */
72
private double[] inverseCumulativeTestPoints;
74
/** Values used to test inverse cumulative probability density calculations */
75
private double[] inverseCumulativeTestValues;
77
//-------------------------------------------------------------------------
80
* Constructor for ContinuousDistributionAbstractTest.
83
public ContinuousDistributionAbstractTest(String name) {
87
//-------------------- Abstract methods -----------------------------------
89
/** Creates the default continuous distribution instance to use in tests. */
90
public abstract ContinuousDistribution makeDistribution();
92
/** Creates the default cumulative probability density test input values */
93
public abstract double[] makeCumulativeTestPoints();
95
/** Creates the default cumulative probability density test expected values */
96
public abstract double[] makeCumulativeTestValues();
98
//---- Default implementations of inverse test data generation methods ----
100
/** Creates the default inverse cumulative probability test input values */
101
public double[] makeInverseCumulativeTestPoints() {
102
return makeCumulativeTestValues();
105
/** Creates the default inverse cumulative probability density test expected values */
106
public double[] makeInverseCumulativeTestValues() {
107
return makeCumulativeTestPoints();
110
//-------------------- Setup / tear down ----------------------------------
113
* Setup sets all test instance data to default values
115
protected void setUp() throws Exception {
117
distribution = makeDistribution();
118
cumulativeTestPoints = makeCumulativeTestPoints();
119
cumulativeTestValues = makeCumulativeTestValues();
120
inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
121
inverseCumulativeTestValues = makeInverseCumulativeTestValues();
125
* Cleans up test instance data
127
protected void tearDown() throws Exception {
130
cumulativeTestPoints = null;
131
cumulativeTestValues = null;
132
inverseCumulativeTestPoints = null;
133
inverseCumulativeTestValues = null;
136
//-------------------- Verification methods -------------------------------
139
* Verifies that cumulative probability density calculations match expected values
140
* using current test instance data
142
protected void verifyCumulativeProbabilities() throws Exception {
143
for (int i = 0; i < cumulativeTestPoints.length; i++) {
144
TestUtils.assertEquals("Incorrect cumulative probability value returned for "
145
+ cumulativeTestPoints[i], cumulativeTestValues[i],
146
distribution.cumulativeProbability(cumulativeTestPoints[i]),
152
* Verifies that inverse cumulative probability density calculations match expected values
153
* using current test instance data
155
protected void verifyInverseCumulativeProbabilities() throws Exception {
156
for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
157
TestUtils.assertEquals("Incorrect inverse cumulative probability value returned for "
158
+ inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
159
distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]),
164
//------------------------ Default test cases -----------------------------
167
* Verifies that cumulative probability density calculations match expected values
168
* using default test instance data
170
public void testCumulativeProbabilities() throws Exception {
171
verifyCumulativeProbabilities();
175
* Verifies that inverse cumulative probability density calculations match expected values
176
* using default test instance data
178
public void testInverseCumulativeProbabilities() throws Exception {
179
verifyInverseCumulativeProbabilities();
183
* Verifies that probability computations are consistent
185
public void testConsistency() throws Exception {
186
for (int i=1; i < cumulativeTestPoints.length; i++) {
188
// check that cdf(x, x) = 0
189
TestUtils.assertEquals(0d,
190
distribution.cumulativeProbability
191
(cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance);
193
// check that P(a < X < b) = P(X < b) - P(X < a)
194
double upper = Math.max(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
195
double lower = Math.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]);
196
double diff = distribution.cumulativeProbability(upper) -
197
distribution.cumulativeProbability(lower);
198
double direct = distribution.cumulativeProbability(lower, upper);
199
TestUtils.assertEquals("Inconsistent cumulative probabilities for ("
200
+ lower + "," + upper + ")", diff, direct, tolerance);
205
* Verifies that illegal arguments are correctly handled
207
public void testIllegalArguments() throws Exception {
209
distribution.cumulativeProbability(1, 0);
210
fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
211
} catch (IllegalArgumentException ex) {
215
distribution.inverseCumulativeProbability(-1);
216
fail("Expecting IllegalArgumentException for p = -1");
217
} catch (IllegalArgumentException ex) {
221
distribution.inverseCumulativeProbability(2);
222
fail("Expecting IllegalArgumentException for p = 2");
223
} catch (IllegalArgumentException ex) {
228
//------------------ Getters / Setters for test instance data -----------
230
* @return Returns the cumulativeTestPoints.
232
protected double[] getCumulativeTestPoints() {
233
return cumulativeTestPoints;
237
* @param cumulativeTestPoints The cumulativeTestPoints to set.
239
protected void setCumulativeTestPoints(double[] cumulativeTestPoints) {
240
this.cumulativeTestPoints = cumulativeTestPoints;
244
* @return Returns the cumulativeTestValues.
246
protected double[] getCumulativeTestValues() {
247
return cumulativeTestValues;
251
* @param cumulativeTestValues The cumulativeTestValues to set.
253
protected void setCumulativeTestValues(double[] cumulativeTestValues) {
254
this.cumulativeTestValues = cumulativeTestValues;
258
* @return Returns the distribution.
260
protected ContinuousDistribution getDistribution() {
265
* @param distribution The distribution to set.
267
protected void setDistribution(ContinuousDistribution distribution) {
268
this.distribution = distribution;
272
* @return Returns the inverseCumulativeTestPoints.
274
protected double[] getInverseCumulativeTestPoints() {
275
return inverseCumulativeTestPoints;
279
* @param inverseCumulativeTestPoints The inverseCumulativeTestPoints to set.
281
protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
282
this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
286
* @return Returns the inverseCumulativeTestValues.
288
protected double[] getInverseCumulativeTestValues() {
289
return inverseCumulativeTestValues;
293
* @param inverseCumulativeTestValues The inverseCumulativeTestValues to set.
295
protected void setInverseCumulativeTestValues(double[] inverseCumulativeTestValues) {
296
this.inverseCumulativeTestValues = inverseCumulativeTestValues;
300
* @return Returns the tolerance.
302
protected double getTolerance() {
307
* @param tolerance The tolerance to set.
309
protected void setTolerance(double tolerance) {
310
this.tolerance = tolerance;