commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1534358 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution: BinomialDistribution.java ExponentialDistribution.java FDistribution.java HypergeometricDistribution.java PoissonDistribution.java TDistribution.java
Date Mon, 21 Oct 2013 20:13:52 GMT
Author: tn
Date: Mon Oct 21 20:13:52 2013
New Revision: 1534358

URL: http://svn.apache.org/r1534358
Log:
Avoid code duplication in Distribution density/probability methods by using the result of
logDensity or logProbability respectively.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java?rev=1534358&r1=1534357&r2=1534358&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java
Mon Oct 21 20:13:52 2013
@@ -99,15 +99,8 @@ public class BinomialDistribution extend
 
     /** {@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} **/

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java?rev=1534358&r1=1534357&r2=1534358&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java
Mon Oct 21 20:13:52 2013
@@ -161,15 +161,16 @@ public class ExponentialDistribution ext
 
     /** {@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;
     }
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java?rev=1534358&r1=1534357&r2=1534358&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java
Mon Oct 21 20:13:52 2013
@@ -142,16 +142,7 @@ public class FDistribution extends Abstr
      * @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} **/

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java?rev=1534358&r1=1534357&r2=1534358&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java
Mon Oct 21 20:13:52 2013
@@ -193,25 +193,8 @@ public class HypergeometricDistribution 
 
     /** {@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} */

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java?rev=1534358&r1=1534357&r2=1534358&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java
Mon Oct 21 20:13:52 2013
@@ -161,17 +161,8 @@ public class PoissonDistribution extends
 
     /** {@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} */

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java?rev=1534358&r1=1534357&r2=1534358&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java
Mon Oct 21 20:13:52 2013
@@ -120,13 +120,7 @@ public class TDistribution extends Abstr
 
     /** {@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} */



Mime
View raw message