You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oe...@apache.org on 2015/09/20 21:06:49 UTC
[6/8] [math] optimization of probability, logProbability,
and cumulativeProbability methods in GeometricDistribution by
precalculation of log(1-p) and log(p)
optimization of probability, logProbability, and cumulativeProbability methods in GeometricDistribution by precalculation of log(1-p) and log(p)
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/6c53783e
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/6c53783e
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/6c53783e
Branch: refs/heads/MATH_3_X
Commit: 6c53783ea0c0185d2da589037b1a2170aafc7e3d
Parents: ecb5299
Author: Otmar Ertl <ot...@gmail.com>
Authored: Sun Sep 20 20:20:16 2015 +0200
Committer: Otmar Ertl <ot...@gmail.com>
Committed: Sun Sep 20 20:49:38 2015 +0200
----------------------------------------------------------------------
.../math3/distribution/GeometricDistribution.java | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/6c53783e/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java
----------------------------------------------------------------------
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 19797c5..20ff7bc 100644
--- a/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java
+++ b/src/main/java/org/apache/commons/math3/distribution/GeometricDistribution.java
@@ -35,6 +35,10 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
private static final long serialVersionUID = 20130507L;
/** The probability of success. */
private final double probabilityOfSuccess;
+ /** {@code log(p)} where p is the probability of success. */
+ private final double logProbabilityOfSuccess;
+ /** {@code log(1 - p)} where p is the probability of success. */
+ private final double log1mProbabilityOfSuccess;
/**
* Create a geometric distribution with the given probability of success.
@@ -68,6 +72,8 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
}
probabilityOfSuccess = p;
+ logProbabilityOfSuccess = FastMath.log(p);
+ log1mProbabilityOfSuccess = FastMath.log1p(-p);
}
/**
@@ -84,7 +90,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
if (x < 0) {
return 0.0;
} else {
- return FastMath.pow(1 - probabilityOfSuccess, x) * probabilityOfSuccess;
+ return FastMath.exp(log1mProbabilityOfSuccess * x) * probabilityOfSuccess;
}
}
@@ -94,7 +100,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
if (x < 0) {
return Double.NEGATIVE_INFINITY;
} else {
- return x * FastMath.log1p(-probabilityOfSuccess) + FastMath.log(probabilityOfSuccess);
+ return x * log1mProbabilityOfSuccess + logProbabilityOfSuccess;
}
}
@@ -103,7 +109,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
if (x < 0) {
return 0.0;
} else {
- return 1.0 - FastMath.pow(1 - probabilityOfSuccess, x + 1);
+ return -FastMath.expm1(log1mProbabilityOfSuccess * (x + 1));
}
}