[MATH-1276] Improved performance of sampling and inverse cumulative
probability calculation for geometric distributions.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/2fd6c8fa
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/2fd6c8fa
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/2fd6c8fa

Branch: refs/heads/master
Commit: 2fd6c8fa1e392f985ee860d8ee154a612ddde569
Parents: 4158323
Author: Otmar Ertl <[email protected]>
Authored: Sun Sep 20 20:33:41 2015 +0200
Committer: Otmar Ertl <[email protected]>
Committed: Sun Sep 20 20:33:41 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                            |  4 ++++
 .../math4/distribution/GeometricDistribution.java  | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/2fd6c8fa/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b1284e1..90f3ee3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,10 @@ If the output is not quite correct, check for invisible 
trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="oertl" type="update" issue="MATH-1276"> <!-- backported to 
3.6 -->
+        Improved performance of sampling and inverse cumulative probability 
calculation
+        for geometric distributions.
+      </action>
       <action dev="oertl" type="fix" issue="MATH-1277" due-to="Marc Rosen"> 
<!-- backported to 3.6 -->
         Fixed incorrect Kendall's tau coefficient calculation due to internal 
integer overflow.
       </action>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/2fd6c8fa/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java
 
b/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java
index 2723fd0..011c21f 100644
--- 
a/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java
+++ 
b/src/main/java/org/apache/commons/math4/distribution/GeometricDistribution.java
@@ -172,4 +172,21 @@ public class GeometricDistribution extends 
AbstractIntegerDistribution {
     public boolean isSupportConnected() {
         return true;
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int inverseCumulativeProbability(double p) throws 
OutOfRangeException {
+        if (p < 0 || p > 1) {
+            throw new OutOfRangeException(p, 0, 1);
+        }
+        if (p == 1) {
+            return Integer.MAX_VALUE;
+        }
+        if (p == 0) {
+            return 0;
+        }
+        return Math.max(0, (int) 
Math.ceil((FastMath.log1p(-p)/log1mProbabilityOfSuccess-1)));
+    }
 }

Reply via email to