diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 61240bc6d..e7c9ca108 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces! + + Improved performance of sampling and inverse cumulative probability calculation + for geometric distributions. + Fixed incorrect Kendall's tau coefficient calculation due to internal integer overflow. diff --git a/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java b/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java index 20ff7bccb..89ffcb6c0 100644 --- a/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java @@ -165,4 +165,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))); + } }