From 55b2cc8c039d659da8f4d8af332814043d084a3b Mon Sep 17 00:00:00 2001
From: Brent Worden
* References:
- *
trials
is not positive.
+ * Change the number of successes for this distribution.
+ * @param successes the new number of successes
+ * @throws IllegalArgumentException if successes
is not
+ * positive.
*/
public void setNumberOfSuccesses(int successes) {
if (successes < 0) {
- throw new IllegalArgumentException("number of trials must be non-negative.");
+ throw new IllegalArgumentException(
+ "number of successes must be non-negative.");
}
numberOfSuccesses = successes;
}
/**
* Change the probability of success for this distribution.
- *
* @param p the new probability of success
* @throws IllegalArgumentException if p
is not a valid
* probability.
*/
public void setProbabilityOfSuccess(double p) {
if (p < 0.0 || p > 1.0) {
- throw new IllegalArgumentException("probability of success must be between 0.0 and 1.0, inclusive.");
+ throw new IllegalArgumentException(
+ "probability of success must be between 0.0 and 1.0, inclusive.");
}
probabilityOfSuccess = p;
}
@@ -101,10 +98,9 @@ public class PascalDistributionImpl
/**
* Access the domain value lower bound, based on p
, used to
* bracket a PDF root.
- *
* @param p the desired probability for the critical value
- * @return domain value lower bound, i.e.
- * P(X < lower bound) < p
+ * @return domain value lower bound, i.e. P(X < lower bound) <
+ * p
*/
protected int getDomainLowerBound(double p) {
return -1;
@@ -113,80 +109,76 @@ public class PascalDistributionImpl
/**
* Access the domain value upper bound, based on p
, used to
* bracket a PDF root.
- *
* @param p the desired probability for the critical value
- * @return domain value upper bound, i.e.
- * P(X < upper bound) > p
+ * @return domain value upper bound, i.e. P(X < upper bound) >
+ * p
*/
protected int getDomainUpperBound(double p) {
- // use MAX - 1 because MAX causes loop
- return Integer.MAX_VALUE - 1;
+ // use MAX - 1 because MAX causes loop
+ return Integer.MAX_VALUE - 1;
}
/**
* For this distribution, X, this method returns P(X ≤ x).
- *
* @param x the value at which the PDF is evaluated
* @return PDF for this distribution
- * @throws MathException if the cumulative probability can not be
- * computed due to convergence or other numerical errors
+ * @throws MathException if the cumulative probability can not be computed
+ * due to convergence or other numerical errors
*/
public double cumulativeProbability(int x) throws MathException {
double ret;
if (x < 0) {
ret = 0.0;
} else {
- ret = Beta.regularizedBeta(
- getProbabilityOfSuccess(),
- getNumberOfSuccesses(),
- x + 1);
+ ret = Beta.regularizedBeta(getProbabilityOfSuccess(),
+ getNumberOfSuccesses(), x + 1);
}
return ret;
}
/**
* For this distribution, X, this method returns P(X = x).
- *
* @param x the value at which the PMF is evaluated
- * @return PMF for this distribution
+ * @return PMF for this distribution
*/
public double probability(int x) {
double ret;
if (x < 0) {
ret = 0.0;
} else {
- ret = MathUtils.binomialCoefficientDouble(x + getNumberOfSuccesses() - 1,
- getNumberOfSuccesses() - 1) *
- Math.pow(getProbabilityOfSuccess(), getNumberOfSuccesses()) *
- Math.pow(1.0 - getProbabilityOfSuccess(),
- x);
+ ret = MathUtils.binomialCoefficientDouble(x
+ + getNumberOfSuccesses() - 1, getNumberOfSuccesses() - 1)
+ * Math.pow(getProbabilityOfSuccess(), getNumberOfSuccesses())
+ * Math.pow(1.0 - getProbabilityOfSuccess(), x);
}
return ret;
}
-
+
/**
- * For this distribution, X, this method returns the largest x, such
- * that P(X ≤ x) ≤ p
.
+ * For this distribution, X, this method returns the largest x, such that
+ * P(X ≤ x) ≤ p
.
*
- * Returns -1
for p=0 and Integer.MAX_VALUE
for
- * p=1.
- *
+ * Returns -1
for p=0 and Integer.MAX_VALUE
+ * for p=1.
* @param p the desired probability
* @return the largest x such that P(X ≤ x) <= p
* @throws MathException if the inverse cumulative probability can not be
- * computed due to convergence or other numerical errors.
+ * computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if p < 0 or p > 1
*/
- public int inverseCumulativeProbability(final double p) throws MathException {
+ public int inverseCumulativeProbability(final double p)
+ throws MathException {
+ int ret;
+
// handle extreme values explicitly
if (p == 0) {
- return -1;
- }
- if (p == 1) {
- return Integer.MAX_VALUE;
+ ret = -1;
+ } else if (p == 1) {
+ ret = Integer.MAX_VALUE;
+ } else {
+ ret = super.inverseCumulativeProbability(p);
}
-
- // use default bisection impl
- return super.inverseCumulativeProbability(p);
+
+ return ret;
}
}