1 16 package org.apache.commons.math.distribution; 17 18 import junit.framework.TestCase; 19 import org.apache.commons.math.TestUtils; 20 21 43 public abstract class ContinuousDistributionAbstractTest extends TestCase { 44 45 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 66 70 public ContinuousDistributionAbstractTest(String name) { 71 super(name); 72 } 73 74 76 77 public abstract ContinuousDistribution makeDistribution(); 78 79 80 public abstract double[] makeCumulativeTestPoints(); 81 82 83 public abstract double[] makeCumulativeTestValues(); 84 85 87 88 public double[] makeInverseCumulativeTestPoints() { 89 return makeCumulativeTestValues(); 90 } 91 92 93 public double[] makeInverseCumulativeTestValues() { 94 return makeCumulativeTestPoints(); 95 } 96 97 99 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 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 125 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 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 153 157 public void testCumulativeProbabilities() throws Exception { 158 verifyCumulativeProbabilities(); 159 } 160 161 165 public void testInverseCumulativeProbabilities() throws Exception { 166 verifyInverseCumulativeProbabilities(); 167 } 168 169 172 public void testConsistency() throws Exception { 173 for (int i=1; i < cumulativeTestPoints.length; i++) { 174 175 TestUtils.assertEquals(0d, 177 distribution.cumulativeProbability 178 (cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance); 179 180 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 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 } 201 try { 202 distribution.inverseCumulativeProbability(-1); 203 fail("Expecting IllegalArgumentException for p = -1"); 204 } catch (IllegalArgumentException ex) { 205 } 207 try { 208 distribution.inverseCumulativeProbability(2); 209 fail("Expecting IllegalArgumentException for p = 2"); 210 } catch (IllegalArgumentException ex) { 211 } 213 } 214 215 219 protected double[] getCumulativeTestPoints() { 220 return cumulativeTestPoints; 221 } 222 223 226 protected void setCumulativeTestPoints(double[] cumulativeTestPoints) { 227 this.cumulativeTestPoints = cumulativeTestPoints; 228 } 229 230 233 protected double[] getCumulativeTestValues() { 234 return cumulativeTestValues; 235 } 236 237 240 protected void setCumulativeTestValues(double[] cumulativeTestValues) { 241 this.cumulativeTestValues = cumulativeTestValues; 242 } 243 244 247 protected ContinuousDistribution getDistribution() { 248 return distribution; 249 } 250 251 254 protected void setDistribution(ContinuousDistribution distribution) { 255 this.distribution = distribution; 256 } 257 258 261 protected double[] getInverseCumulativeTestPoints() { 262 return inverseCumulativeTestPoints; 263 } 264 265 268 protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) { 269 this.inverseCumulativeTestPoints = inverseCumulativeTestPoints; 270 } 271 272 275 protected double[] getInverseCumulativeTestValues() { 276 return inverseCumulativeTestValues; 277 } 278 279 282 protected void setInverseCumulativeTestValues(double[] inverseCumulativeTestValues) { 283 this.inverseCumulativeTestValues = inverseCumulativeTestValues; 284 } 285 286 289 protected double getTolerance() { 290 return tolerance; 291 } 292 293 296 protected void setTolerance(double tolerance) { 297 this.tolerance = tolerance; 298 } 299 300 } 301 | Popular Tags |