private void doTestMath753(final double shape,
final double meanNoOF, final double sdNoOF,
final double meanOF, final double sdOF,
final String resourceName) throws IOException {
final GammaDistribution distribution = new GammaDistribution(shape, 1.0);
final SummaryStatistics statOld = new SummaryStatistics();
final SummaryStatistics statNewNoOF = new SummaryStatistics();
final SummaryStatistics statNewOF = new SummaryStatistics();
final InputStream resourceAsStream;
resourceAsStream = this.getClass().getResourceAsStream(resourceName);
Assert.assertNotNull("Could not find resource " + resourceName,
resourceAsStream);
final BufferedReader in;
in = new BufferedReader(new InputStreamReader(resourceAsStream));
try {
for (String line = in.readLine(); line != null; line = in
.readLine()) {
final String[] tokens = line.split(", ");
Assert.assertTrue("expected two floating-point values",
tokens.length == 2);
final double x = Double.parseDouble(tokens[0]);
final String msg = "x = " + x + ", shape = " + shape +
", scale = 1.0";
final double expected = Double.parseDouble(tokens[1]);
final double ulp = FastMath.ulp(expected);
final double actualOld = density(x, shape, 1.0);
final double actualNew = distribution.density(x);
final double errOld, errNew;
errOld = FastMath.abs((actualOld - expected) / ulp);
errNew = FastMath.abs((actualNew - expected) / ulp);
if (Double.isNaN(actualOld) || Double.isInfinite(actualOld)) {
Assert.assertFalse(msg, Double.isNaN(actualNew));
Assert.assertFalse(msg, Double.isInfinite(actualNew));
statNewOF.addValue(errNew);
} else {
statOld.addValue(errOld);
statNewNoOF.addValue(errNew);
}
}
if (statOld.getN() != 0) {
/*
* If no overflow occurs, check that new implementation is
* better than old one.
*/
final StringBuilder sb = new StringBuilder("shape = ");
sb.append(shape);
sb.append(", scale = 1.0\n");
sb.append("Old implementation\n");
sb.append("------------------\n");
sb.append(statOld.toString());
sb.append("New implementation\n");
sb.append("------------------\n");
sb.append(statNewNoOF.toString());
final String msg = sb.toString();
final double oldMin = statOld.getMin();
final double newMin = statNewNoOF.getMin();
Assert.assertTrue(msg, newMin <= oldMin);
final double oldMax = statOld.getMax();
final double newMax = statNewNoOF.getMax();
Assert.assertTrue(msg, newMax <= oldMax);
final double oldMean = statOld.getMean();
final double newMean = statNewNoOF.getMean();
Assert.assertTrue(msg, newMean <= oldMean);
final double oldSd = statOld.getStandardDeviation();
final double newSd = statNewNoOF.getStandardDeviation();
Assert.assertTrue(msg, newSd <= oldSd);
Assert.assertTrue(msg, newMean <= meanNoOF);
Assert.assertTrue(msg, newSd <= sdNoOF);
}
if (statNewOF.getN() != 0) {
final double newMean = statNewOF.getMean();
final double newSd = statNewOF.getStandardDeviation();
final StringBuilder sb = new StringBuilder("shape = ");
sb.append(shape);
sb.append(", scale = 1.0");
sb.append(", max. mean error (ulps) = ");