Avoid code duplication in Distribution density/probability methods by using the result of logDensity or logProbability respectively.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1534358 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-10-21 20:13:52 +00:00
parent 2745587342
commit 96d6a416d6
6 changed files with 13 additions and 60 deletions

View File

@ -99,15 +99,8 @@ public class BinomialDistribution extends AbstractIntegerDistribution {
/** {@inheritDoc} */
public double probability(int x) {
double ret;
if (x < 0 || x > numberOfTrials) {
ret = 0.0;
} else {
ret = FastMath.exp(SaddlePointExpansion.logBinomialProbability(x,
numberOfTrials, probabilityOfSuccess,
1.0 - probabilityOfSuccess));
}
return ret;
final double logProbability = logProbability(x);
return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability);
}
/** {@inheritDoc} **/

View File

@ -161,15 +161,16 @@ public class ExponentialDistribution extends AbstractRealDistribution {
/** {@inheritDoc} */
public double density(double x) {
if (x < 0) {
return 0;
}
return FastMath.exp(-x / mean) / mean;
final double logDensity = logDensity(x);
return logDensity == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logDensity);
}
/** {@inheritDoc} **/
@Override
public double logDensity(double x) {
if (x < 0) {
return Double.NEGATIVE_INFINITY;
}
return -x / mean - logMean;
}

View File

@ -142,16 +142,7 @@ public class FDistribution extends AbstractRealDistribution {
* @since 2.1
*/
public double density(double x) {
final double nhalf = numeratorDegreesOfFreedom / 2;
final double mhalf = denominatorDegreesOfFreedom / 2;
final double logx = FastMath.log(x);
final double logn = FastMath.log(numeratorDegreesOfFreedom);
final double logm = FastMath.log(denominatorDegreesOfFreedom);
final double lognxm = FastMath.log(numeratorDegreesOfFreedom * x +
denominatorDegreesOfFreedom);
return FastMath.exp(nhalf * logn + nhalf * logx - logx +
mhalf * logm - nhalf * lognxm - mhalf * lognxm -
Beta.logBeta(nhalf, mhalf));
return FastMath.exp(logDensity(x));
}
/** {@inheritDoc} **/

View File

@ -193,25 +193,8 @@ public class HypergeometricDistribution extends AbstractIntegerDistribution {
/** {@inheritDoc} */
public double probability(int x) {
double ret;
int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize);
if (x < domain[0] || x > domain[1]) {
ret = 0.0;
} else {
double p = (double) sampleSize / (double) populationSize;
double q = (double) (populationSize - sampleSize) / (double) populationSize;
double p1 = SaddlePointExpansion.logBinomialProbability(x,
numberOfSuccesses, p, q);
double p2 =
SaddlePointExpansion.logBinomialProbability(sampleSize - x,
populationSize - numberOfSuccesses, p, q);
double p3 =
SaddlePointExpansion.logBinomialProbability(sampleSize, populationSize, p, q);
ret = FastMath.exp(p1 + p2 - p3);
}
return ret;
final double logProbability = logProbability(x);
return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability);
}
/** {@inheritDoc} */

View File

@ -161,17 +161,8 @@ public class PoissonDistribution extends AbstractIntegerDistribution {
/** {@inheritDoc} */
public double probability(int x) {
double ret;
if (x < 0 || x == Integer.MAX_VALUE) {
ret = 0.0;
} else if (x == 0) {
ret = FastMath.exp(-mean);
} else {
ret = FastMath.exp(-SaddlePointExpansion.getStirlingError(x) -
SaddlePointExpansion.getDeviancePart(x, mean)) /
FastMath.sqrt(MathUtils.TWO_PI * x);
}
return ret;
final double logProbability = logProbability(x);
return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability);
}
/** {@inheritDoc} */

View File

@ -120,13 +120,7 @@ public class TDistribution extends AbstractRealDistribution {
/** {@inheritDoc} */
public double density(double x) {
final double n = degreesOfFreedom;
final double nPlus1Over2 = (n + 1) / 2;
return FastMath.exp(Gamma.logGamma(nPlus1Over2) -
0.5 * (FastMath.log(FastMath.PI) +
FastMath.log(n)) -
Gamma.logGamma(n / 2) -
nPlus1Over2 * FastMath.log(1 + x * x / n));
return FastMath.exp(logDensity(x));
}
/** {@inheritDoc} */