From 96d6a416d6a88f925c4f103c378bafc6ab21dd28 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 21 Oct 2013 20:13:52 +0000 Subject: [PATCH] Avoid code duplication in Distribution density/probability methods by using the result of logDensity or logProbability respectively. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1534358 13f79535-47bb-0310-9956-ffa450edef68 --- .../distribution/BinomialDistribution.java | 11 ++-------- .../distribution/ExponentialDistribution.java | 9 ++++---- .../math3/distribution/FDistribution.java | 11 +--------- .../HypergeometricDistribution.java | 21 ++----------------- .../distribution/PoissonDistribution.java | 13 ++---------- .../math3/distribution/TDistribution.java | 8 +------ 6 files changed, 13 insertions(+), 60 deletions(-) diff --git a/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java b/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java index c02dcb8b8..15747e13e 100644 --- a/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java @@ -99,15 +99,8 @@ public class BinomialDistribution extends AbstractIntegerDistribution { /** {@inheritDoc} */ public double probability(int x) { - double ret; - if (x < 0 || x > numberOfTrials) { - ret = 0.0; - } else { - ret = FastMath.exp(SaddlePointExpansion.logBinomialProbability(x, - numberOfTrials, probabilityOfSuccess, - 1.0 - probabilityOfSuccess)); - } - return ret; + final double logProbability = logProbability(x); + return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability); } /** {@inheritDoc} **/ diff --git a/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java b/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java index 9f7c5c592..e7ce77636 100644 --- a/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java @@ -161,15 +161,16 @@ public class ExponentialDistribution extends AbstractRealDistribution { /** {@inheritDoc} */ public double density(double x) { - if (x < 0) { - return 0; - } - return FastMath.exp(-x / mean) / mean; + final double logDensity = logDensity(x); + return logDensity == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logDensity); } /** {@inheritDoc} **/ @Override public double logDensity(double x) { + if (x < 0) { + return Double.NEGATIVE_INFINITY; + } return -x / mean - logMean; } diff --git a/src/main/java/org/apache/commons/math3/distribution/FDistribution.java b/src/main/java/org/apache/commons/math3/distribution/FDistribution.java index f5804e298..2abfabe6a 100644 --- a/src/main/java/org/apache/commons/math3/distribution/FDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/FDistribution.java @@ -142,16 +142,7 @@ public class FDistribution extends AbstractRealDistribution { * @since 2.1 */ public double density(double x) { - final double nhalf = numeratorDegreesOfFreedom / 2; - final double mhalf = denominatorDegreesOfFreedom / 2; - final double logx = FastMath.log(x); - final double logn = FastMath.log(numeratorDegreesOfFreedom); - final double logm = FastMath.log(denominatorDegreesOfFreedom); - final double lognxm = FastMath.log(numeratorDegreesOfFreedom * x + - denominatorDegreesOfFreedom); - return FastMath.exp(nhalf * logn + nhalf * logx - logx + - mhalf * logm - nhalf * lognxm - mhalf * lognxm - - Beta.logBeta(nhalf, mhalf)); + return FastMath.exp(logDensity(x)); } /** {@inheritDoc} **/ diff --git a/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java b/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java index d2cb9abc1..872aad1a3 100644 --- a/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java @@ -193,25 +193,8 @@ public class HypergeometricDistribution extends AbstractIntegerDistribution { /** {@inheritDoc} */ public double probability(int x) { - double ret; - - int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); - if (x < domain[0] || x > domain[1]) { - ret = 0.0; - } else { - double p = (double) sampleSize / (double) populationSize; - double q = (double) (populationSize - sampleSize) / (double) populationSize; - double p1 = SaddlePointExpansion.logBinomialProbability(x, - numberOfSuccesses, p, q); - double p2 = - SaddlePointExpansion.logBinomialProbability(sampleSize - x, - populationSize - numberOfSuccesses, p, q); - double p3 = - SaddlePointExpansion.logBinomialProbability(sampleSize, populationSize, p, q); - ret = FastMath.exp(p1 + p2 - p3); - } - - return ret; + final double logProbability = logProbability(x); + return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability); } /** {@inheritDoc} */ diff --git a/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java b/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java index ee187d0ca..67a913cbe 100644 --- a/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java @@ -161,17 +161,8 @@ public class PoissonDistribution extends AbstractIntegerDistribution { /** {@inheritDoc} */ public double probability(int x) { - double ret; - if (x < 0 || x == Integer.MAX_VALUE) { - ret = 0.0; - } else if (x == 0) { - ret = FastMath.exp(-mean); - } else { - ret = FastMath.exp(-SaddlePointExpansion.getStirlingError(x) - - SaddlePointExpansion.getDeviancePart(x, mean)) / - FastMath.sqrt(MathUtils.TWO_PI * x); - } - return ret; + final double logProbability = logProbability(x); + return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability); } /** {@inheritDoc} */ diff --git a/src/main/java/org/apache/commons/math3/distribution/TDistribution.java b/src/main/java/org/apache/commons/math3/distribution/TDistribution.java index 066355cd0..e3ff58c03 100644 --- a/src/main/java/org/apache/commons/math3/distribution/TDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/TDistribution.java @@ -120,13 +120,7 @@ public class TDistribution extends AbstractRealDistribution { /** {@inheritDoc} */ public double density(double x) { - final double n = degreesOfFreedom; - final double nPlus1Over2 = (n + 1) / 2; - return FastMath.exp(Gamma.logGamma(nPlus1Over2) - - 0.5 * (FastMath.log(FastMath.PI) + - FastMath.log(n)) - - Gamma.logGamma(n / 2) - - nPlus1Over2 * FastMath.log(1 + x * x / n)); + return FastMath.exp(logDensity(x)); } /** {@inheritDoc} */