code cleanup in GeometricDistribution.java as proposed by Gilles:

* removed needless declaration of a local variable "p" with the same
value as the "probabilityOfSuccess" field
* remove needless local variable "ret" ("return" statements can be used
directly in each way of the alternatives)
This commit is contained in:
Otmar Ertl 2015-09-20 20:11:47 +02:00
parent fb0078159d
commit 73351b6adb
1 changed files with 8 additions and 19 deletions

View File

@ -82,40 +82,31 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double probability(int x) { public double probability(int x) {
double ret;
if (x < 0) { if (x < 0) {
ret = 0.0; return 0.0;
} else { } else {
final double p = probabilityOfSuccess; return FastMath.pow(1 - probabilityOfSuccess, x) * probabilityOfSuccess;
ret = FastMath.pow(1 - p, x) * p;
} }
return ret;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double logProbability(int x) { public double logProbability(int x) {
double ret;
if (x < 0) { if (x < 0) {
ret = Double.NEGATIVE_INFINITY; return Double.NEGATIVE_INFINITY;
} else { } else {
final double p = probabilityOfSuccess; return x * FastMath.log1p(-probabilityOfSuccess) + FastMath.log(probabilityOfSuccess);
ret = x * FastMath.log1p(-p) + FastMath.log(p);
} }
return ret;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double cumulativeProbability(int x) { public double cumulativeProbability(int x) {
double ret;
if (x < 0) { if (x < 0) {
ret = 0.0; return 0.0;
} else { } else {
final double p = probabilityOfSuccess; return 1.0 - FastMath.pow(1 - probabilityOfSuccess, x + 1);
ret = 1.0 - FastMath.pow(1 - p, x + 1);
} }
return ret;
} }
/** /**
@ -125,8 +116,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
*/ */
@Override @Override
public double getNumericalMean() { public double getNumericalMean() {
final double p = probabilityOfSuccess; return (1 - probabilityOfSuccess) / probabilityOfSuccess;
return (1 - p) / p;
} }
/** /**
@ -137,8 +127,7 @@ public class GeometricDistribution extends AbstractIntegerDistribution {
*/ */
@Override @Override
public double getNumericalVariance() { public double getNumericalVariance() {
final double p = probabilityOfSuccess; return (1 - probabilityOfSuccess) / (probabilityOfSuccess * probabilityOfSuccess);
return (1 - p) / (p * p);
} }
/** /**