optimization of probability, logProbability, and cumulativeProbability

methods in GeometricDistribution by precalculation of log(1-p) and
log(p)
This commit is contained in:
Otmar Ertl 2015-09-20 20:20:16 +02:00
parent 73351b6adb
commit 079a07fe18
1 changed files with 9 additions and 3 deletions

View File

@ -35,6 +35,10 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
private static final long serialVersionUID = 20130507L; private static final long serialVersionUID = 20130507L;
/** The probability of success. */ /** The probability of success. */
private final double probabilityOfSuccess; 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. * Create a geometric distribution with the given probability of success.
@ -68,6 +72,8 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
} }
probabilityOfSuccess = p; probabilityOfSuccess = p;
logProbabilityOfSuccess = FastMath.log(p);
log1mProbabilityOfSuccess = FastMath.log1p(-p);
} }
/** /**
@ -85,7 +91,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
if (x < 0) { if (x < 0) {
return 0.0; return 0.0;
} else { } else {
return FastMath.pow(1 - probabilityOfSuccess, x) * probabilityOfSuccess; return FastMath.exp(log1mProbabilityOfSuccess * x) * probabilityOfSuccess;
} }
} }
@ -95,7 +101,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
if (x < 0) { if (x < 0) {
return Double.NEGATIVE_INFINITY; return Double.NEGATIVE_INFINITY;
} else { } else {
return x * FastMath.log1p(-probabilityOfSuccess) + FastMath.log(probabilityOfSuccess); return x * log1mProbabilityOfSuccess + logProbabilityOfSuccess;
} }
} }
@ -105,7 +111,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
if (x < 0) { if (x < 0) {
return 0.0; return 0.0;
} else { } else {
return 1.0 - FastMath.pow(1 - probabilityOfSuccess, x + 1); return -FastMath.expm1(log1mProbabilityOfSuccess * (x + 1));
} }
} }