/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(x0 ≤ X ≤ x1). * * @param x0 the inclusive, lower bound * @param x1 the inclusive, upper bound * @return the cumulative probability. * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. * @throws IllegalArgumentException if x0 > x1 */ public double cumulativeProbability(int x0, int x1) throws MathException { if (x0 > x1) { throw new IllegalArgumentException ("lower endpoint must be less than or equal to upper endpoint"); } return cumulativeProbability(x1) - cumulativeProbability(x0 - 1); }
/** * For this distribution, X, this method returns the largest x, such that * P(X ≤ x) ≤ <code>p</code>. * <p> * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> * for p=1.</p> * @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. * @throws IllegalArgumentException if p < 0 or p > 1 */ @Override public int inverseCumulativeProbability(final double p) throws MathException { int ret; // handle extreme values explicitly if (p == 0) { ret = -1; } else if (p == 1) { ret = Integer.MAX_VALUE; } else { ret = super.inverseCumulativeProbability(p); } return ret; }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(X = x). In other words, this * method represents the probability mass function, or PMF, for the distribution. * <p> * If <code>x</code> does not represent an integer value, 0 is returned. * * @param x the value at which the probability density function is evaluated * @return the value of the probability density function at x */ public double probability(double x) { double fl = Math.floor(x); if (fl == x) { return this.probability((int) x); } else { return 0; } }
int x0 = getDomainLowerBound(p); int x1 = getDomainUpperBound(p); double pm; while (x0 < x1) { int xm = x0 + (x1 - x0) / 2; pm = cumulativeProbability(xm); if (pm > p) { pm = cumulativeProbability(x0); while (pm > p) { --x0; pm = cumulativeProbability(x0);
int x0 = getDomainLowerBound(p); int x1 = getDomainUpperBound(p); double pm; while (x0 < x1) { int xm = x0 + (x1 - x0) / 2; pm = checkedCumulativeProbability(xm); if (pm > p) { pm = checkedCumulativeProbability(x0); while (pm > p) { --x0; pm = checkedCumulativeProbability(x0);
/** * Generates a random sample from the distribution. The default implementation * generates the sample by calling {@link #sample()} in a loop. * * @param sampleSize number of random values to generate * @since 2.2 * @return an array representing the random sample * @throws MathException if an error occurs generating the sample * @throws IllegalArgumentException if sampleSize is not positive */ public int[] sample(int sampleSize) throws MathException { if (sampleSize <= 0) { MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_POSITIVE_SAMPLE_SIZE, sampleSize); } int[] out = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; }
int x0 = getDomainLowerBound(p); int x1 = getDomainUpperBound(p); double pm; while (x0 < x1) { int xm = x0 + (x1 - x0) / 2; pm = checkedCumulativeProbability(xm); if (pm > p) { pm = checkedCumulativeProbability(x0); while (pm > p) { --x0; pm = checkedCumulativeProbability(x0);
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(X ≤ x). In other words, * this method represents the (cumulative) distribution function, or * CDF, for this distribution. * <p> * If <code>x</code> does not represent an integer value, the CDF is * evaluated at the greatest integer less than x. * * @param x the value at which the distribution function is evaluated. * @return cumulative probability that a random variable with this * distribution takes a value less than or equal to <code>x</code> * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. */ public double cumulativeProbability(double x) throws MathException { return cumulativeProbability((int) Math.floor(x)); }
/** * For this distribution, X, this method returns the largest x, such * that P(X ≤ x) ≤ <code>p</code>. * <p> * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> for * p=1.</p> * * @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. * @throws IllegalArgumentException if p < 0 or p > 1 */ public int inverseCumulativeProbability(final double p) throws MathException { // handle extreme values explicitly if (p == 0) { return -1; } if (p == 1) { return Integer.MAX_VALUE; } // use default bisection impl return super.inverseCumulativeProbability(p); } }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(X = x). In other words, this * method represents the probability mass function, or PMF, for the distribution. * <p> * If <code>x</code> does not represent an integer value, 0 is returned. * * @param x the value at which the probability density function is evaluated * @return the value of the probability density function at x */ public double probability(double x) { double fl = Math.floor(x); if (fl == x) { return this.probability((int) x); } else { return 0; } }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(X ≤ x). In other words, * this method represents the (cumulative) distribution function, or * CDF, for this distribution. * <p> * If <code>x</code> does not represent an integer value, the CDF is * evaluated at the greatest integer less than x. * * @param x the value at which the distribution function is evaluated. * @return cumulative probability that a random variable with this * distribution takes a value less than or equal to <code>x</code> * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. */ public double cumulativeProbability(double x) throws MathException { return cumulativeProbability((int) Math.floor(x)); }
/** * For this distribution, X, this method returns the largest x, such that * P(X ≤ x) ≤ <code>p</code>. * <p> * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> * for p=1.</p> * @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. * @throws IllegalArgumentException if p < 0 or p > 1 */ public int inverseCumulativeProbability(final double p) throws MathException { int ret; // handle extreme values explicitly if (p == 0) { ret = -1; } else if (p == 1) { ret = Integer.MAX_VALUE; } else { ret = super.inverseCumulativeProbability(p); } return ret; } }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(X = x). In other words, this * method represents the probability mass function, or PMF, for the distribution. * <p> * If <code>x</code> does not represent an integer value, 0 is returned. * * @param x the value at which the probability density function is evaluated * @return the value of the probability density function at x */ public double probability(double x) { double fl = FastMath.floor(x); if (fl == x) { return this.probability((int) x); } else { return 0; } }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(x0 ≤ X ≤ x1). * * @param x0 the inclusive, lower bound * @param x1 the inclusive, upper bound * @return the cumulative probability. * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. * @throws IllegalArgumentException if x0 > x1 */ public double cumulativeProbability(int x0, int x1) throws MathException { if (x0 > x1) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1); } return cumulativeProbability(x1) - cumulativeProbability(x0 - 1); }
/** * For this distribution, X, this method returns the largest x, such that * P(X ≤ x) ≤ <code>p</code>. * <p> * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> for * p=1. * </p> * * @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. * @throws IllegalArgumentException if p < 0 or p > 1 */ @Override public int inverseCumulativeProbability(final double p) throws MathException { // handle extreme values explicitly if (p == 0) { return -1; } if (p == 1) { return Integer.MAX_VALUE; } // use default bisection impl return super.inverseCumulativeProbability(p); }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(x0 ≤ X ≤ x1). * * @param x0 the inclusive, lower bound * @param x1 the inclusive, upper bound * @return the cumulative probability. * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. * @throws IllegalArgumentException if x0 > x1 */ public double cumulativeProbability(int x0, int x1) throws MathException { if (x0 > x1) { throw MathRuntimeException.createIllegalArgumentException( WRONG_ORDER_ENDPOINTS_MESSAGE, x0, x1); } return cumulativeProbability(x1) - cumulativeProbability(x0 - 1); }
/** * For this distribution, X, this method returns the largest x, such that * P(X ≤ x) ≤ <code>p</code>. * <p> * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> * for p=1.</p> * @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. * @throws IllegalArgumentException if p < 0 or p > 1 */ @Override public int inverseCumulativeProbability(final double p) throws MathException { int ret; // handle extreme values explicitly if (p == 0) { ret = -1; } else if (p == 1) { ret = Integer.MAX_VALUE; } else { ret = super.inverseCumulativeProbability(p); } return ret; } }
/** * For a random variable X whose values are distributed according * to this distribution, this method returns P(x0 ≤ X ≤ x1). * * @param x0 the (inclusive) lower bound * @param x1 the (inclusive) upper bound * @return the probability that a random variable with this distribution * will take a value between <code>x0</code> and <code>x1</code>, * including the endpoints. * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. * @throws IllegalArgumentException if <code>x0 > x1</code> */ public double cumulativeProbability(double x0, double x1) throws MathException { if (x0 > x1) { throw new IllegalArgumentException ("lower endpoint must be less than or equal to upper endpoint"); } if (Math.floor(x0) < x0) { return cumulativeProbability(((int) Math.floor(x0)) + 1, (int) Math.floor(x1)); // don't want to count mass below x0 } else { // x0 is mathematical integer, so use as is return cumulativeProbability((int) Math.floor(x0), (int) Math.floor(x1)); } }
/** * For this distribution, X, this method returns the largest x, such that * P(X ≤ x) ≤ <code>p</code>. * <p> * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> for * p=1. * </p> * * @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. * @throws IllegalArgumentException if p < 0 or p > 1 */ @Override public int inverseCumulativeProbability(final double p) throws MathException { // handle extreme values explicitly if (p == 0) { return -1; } if (p == 1) { return Integer.MAX_VALUE; } // use default bisection impl return super.inverseCumulativeProbability(p); } }
/** * Computes the cumulative probability function and checks for NaN values returned. * Throws MathException if the value is NaN. Rethrows any MathException encountered * evaluating the cumulative probability function. Throws * MathException if the cumulative probability function returns NaN. * * @param argument input value * @return cumulative probability * @throws MathException if the cumulative probability is NaN */ private double checkedCumulativeProbability(int argument) throws MathException { double result = Double.NaN; result = cumulativeProbability(argument); if (Double.isNaN(result)) { throw new MathException(LocalizedFormats.DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument); } return result; }