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 feb39552dfe2f64a4d909d80db175909155aedba
Author: aherbert <[email protected]>
AuthorDate: Tue Jun 23 15:51:18 2020 +0100

    SaddlePointExpansion is only used with integer values.
    
    Modify the functions to drop support for floating point values. This
    still supports the Poisson and Binomial distributions.
    
    Added test cases for Binomial distribution at the switch point from
    exact Stirling values (x=15) and the next value (x=16).
---
 .../distribution/SaddlePointExpansionUtils.java    | 26 +++++++++-------------
 .../distribution/PoissonDistributionTest.java      | 15 +++++++------
 2 files changed, 18 insertions(+), 23 deletions(-)

diff --git 
a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/SaddlePointExpansionUtils.java
 
b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/SaddlePointExpansionUtils.java
index e0a57e0..73588a2 100644
--- 
a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/SaddlePointExpansionUtils.java
+++ 
b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/SaddlePointExpansionUtils.java
@@ -16,8 +16,6 @@
  */
 package org.apache.commons.statistics.distribution;
 
-import org.apache.commons.numbers.gamma.LogGamma;
-
 /**
  * Utility class used by various distributions to accurately compute their
  * respective probability mass functions. The implementation for this class is
@@ -29,12 +27,10 @@ import org.apache.commons.numbers.gamma.LogGamma;
 final class SaddlePointExpansionUtils {
     /** 2 &pi;. */
     private static final double TWO_PI = 2 * Math.PI;
-    /** 1/2 * log(2 &pi;). */
-    private static final double HALF_LOG_TWO_PI = 0.5 * Math.log(TWO_PI);
     /** 1/10. */
     private static final double ONE_TENTH = 0.1;
     /** The threshold value for switching the method to compute th Stirling 
error. */
-    private static final double STIRLING_ERROR_THRESHOLD = 15.0;
+    private static final int STIRLING_ERROR_THRESHOLD = 15;
 
     /** Exact Stirling expansion error for certain values. */
     private static final double[] EXACT_STIRLING_ERRORS = {
@@ -88,19 +84,15 @@ final class SaddlePointExpansionUtils {
      * </ol>
      * </p>
      *
+     * <p>Note: This function has been modified for integer {@code z}.</p>
+     *
      * @param z the value.
      * @return the Striling's series error.
      */
-    static double getStirlingError(double z) {
+    static double getStirlingError(int z) {
         double ret;
-        if (z < STIRLING_ERROR_THRESHOLD) {
-            final double z2 = 2.0 * z;
-            if (Math.floor(z2) == z2) {
-                ret = EXACT_STIRLING_ERRORS[(int) z2];
-            } else {
-                ret = LogGamma.value(z + 1.0) - (z + 0.5) * Math.log(z) +
-                      z - HALF_LOG_TWO_PI;
-            }
+        if (z <= STIRLING_ERROR_THRESHOLD) {
+            ret = EXACT_STIRLING_ERRORS[2 * z];
         } else {
             final double z2 = z * z;
             ret = (0.083333333333333333333 -
@@ -125,11 +117,13 @@ final class SaddlePointExpansionUtils {
      * </ol>
      * </p>
      *
+     * <p>Note: This function has been modified for integer {@code x}.</p>
+     *
      * @param x the x value.
      * @param mu the average.
      * @return a part of the deviance.
      */
-    static double getDeviancePart(double x, double mu) {
+    static double getDeviancePart(int x, double mu) {
         double ret;
         if (Math.abs(x - mu) < 0.1 * (x + mu)) {
             final double d = x - mu;
@@ -183,7 +177,7 @@ final class SaddlePointExpansionUtils {
                 ret = n * Math.log(p);
             }
         } else {
-            final double nMx = (double) n - x;
+            final int nMx = n - x;
             ret = getStirlingError(n) - getStirlingError(x) -
                   getStirlingError(nMx) - getDeviancePart(x, n * p) -
                   getDeviancePart(nMx, n * q);
diff --git 
a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/PoissonDistributionTest.java
 
b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/PoissonDistributionTest.java
index 64dfac4..f5b69ec 100644
--- 
a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/PoissonDistributionTest.java
+++ 
b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/PoissonDistributionTest.java
@@ -51,7 +51,7 @@ public class PoissonDistributionTest extends 
DiscreteDistributionAbstractTest {
      */
     @Override
     public int[] makeDensityTestPoints() {
-        return new int[] {-1, 0, 1, 2, 3, 4, 5, 10, 20};
+        return new int[] {-1, 0, 1, 2, 3, 4, 5, 10, 15, 16, 20};
     }
 
     /**
@@ -61,8 +61,9 @@ public class PoissonDistributionTest extends 
DiscreteDistributionAbstractTest {
     @Override
     public double[] makeDensityTestValues() {
         return new double[] {0d, 0.0183156388887d,  0.073262555555d,
-                             0.14652511111d, 0.195366814813d, 0.195366814813,
-                             0.156293451851d, 0.00529247667642d, 
8.27746364655e-09};
+                             0.14652511111d, 0.195366814813d, 0.195366814813d,
+                             0.156293451851d, 0.00529247667642d, 
1.503911676283e-05d,
+                             3.759779190708e-06d, 8.27746364655e-09};
     }
 
     /**
@@ -74,7 +75,7 @@ public class PoissonDistributionTest extends 
DiscreteDistributionAbstractTest {
         return new double[] {Double.NEGATIVE_INFINITY, -4.000000000000d,
                              -2.613705638880d, -1.920558458320d, 
-1.632876385868d,
                              -1.632876385868d, -1.856019937183d, 
-5.241468961877d,
-                             -18.609729238356d};
+                             -11.1048559670425d, -12.4911503281624d, 
-18.609729238356d};
     }
 
     /**
@@ -90,9 +91,9 @@ public class PoissonDistributionTest extends 
DiscreteDistributionAbstractTest {
      */
     @Override
     public double[] makeCumulativeTestValues() {
-        return new double[] {0d,  0.0183156388887d, 0.0915781944437d,
-                             0.238103305554d, 0.433470120367d, 0.62883693518,
-                             0.78513038703d,  0.99716023388d, 0.999999998077};
+        return new double[] {0d, 0.0183156388887d, 0.0915781944437d,
+                             0.238103305554d, 0.433470120367d, 0.62883693518d,
+                             0.78513038703d, 0.99716023388d, 0.999999998077};
     }
 
     /**

Reply via email to