You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2022/01/26 20:04:17 UTC
[commons-statistics] 02/06: Updated PMF vs log PMF assertion at the support bound
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 32776c58d64a031f051d1a608ca93873915e58e9
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Jan 26 01:21:30 2022 +0000
Updated PMF vs log PMF assertion at the support bound
---
.../distribution/BaseDiscreteDistributionTest.java | 34 +++++++++++++++++++---
1 file changed, 30 insertions(+), 4 deletions(-)
diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDiscreteDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDiscreteDistributionTest.java
index a10e731..ccbe909 100644
--- a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDiscreteDistributionTest.java
+++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/BaseDiscreteDistributionTest.java
@@ -884,7 +884,7 @@ abstract class BaseDiscreteDistributionTest
// the CDF is 1.0 as they may be truncated.
Assertions.assertEquals(1.0, dist.cumulativeProbability(hi), "cdf(upper)");
Assertions.assertEquals(0.0, dist.survivalProbability(hi), "sf(upper)");
- TestUtils.assertEquals(dist.probability(hi), dist.survivalProbability(hi - 1), tolerance, "sf(upper - 1)");
+ TestUtils.assertEquals(dist.probability(hi), dist.survivalProbability(hi - 1), tolerance, () -> "pmf(upper - 1) != sf(upper - 1) for " + hi);
final int above = hi + 1;
Assertions.assertEquals(0.0, dist.probability(above), "pmf(x > upper)");
@@ -894,10 +894,36 @@ abstract class BaseDiscreteDistributionTest
}
// Test the logProbability at the support bound. This hits edge case coverage for logProbability.
+ assertPmfAndLogPmfAtBound(dist, lo, tolerance, "lower");
+ assertPmfAndLogPmfAtBound(dist, hi, tolerance, "upper");
+ }
+
+ /**
+ * Assert the PMF and log PMF are consistent at the named point.
+ * This method asserts either:
+ * <pre>
+ * log(pmf) == logpmf
+ * pmf == exp(logpmf)
+ * </pre>
+ *
+ * @param dist Distribution
+ * @param x Point
+ * @param tolerance Test tolerance
+ * @param name Point name
+ */
+ private static void assertPmfAndLogPmfAtBound(DiscreteDistribution dist, int x,
+ DoubleTolerance tolerance, String name) {
// It is assumed the log probability may support a value when the plain probability will be zero.
- // So do not test Math.log(dist.probability(x)) == dist.logProbability(x)
- TestUtils.assertEquals(dist.probability(lo), Math.exp(dist.logProbability(lo)), tolerance, "pmf(lower) != exp(logpmf(lower))");
- TestUtils.assertEquals(dist.probability(hi), Math.exp(dist.logProbability(hi)), tolerance, "pmf(upper) != exp(logpmf(upper))");
+ // Only assert Math.log(dist.probability(x)) == dist.logProbability(x) for normal values.
+ final double p = dist.probability(x);
+ final double logp = dist.logProbability(x);
+ if (p > Double.MIN_NORMAL) {
+ TestUtils.assertEquals(Math.log(p), logp, tolerance,
+ () -> String.format("%s: log(pmf(%d)) != logpmf(%d)", name, x, x));
+ } else {
+ TestUtils.assertEquals(p, Math.exp(logp), tolerance,
+ () -> String.format("%s: pmf(%d) != exp(logpmf(%d))", name, x, x));
+ }
}
/**