Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 83995 invoked from network); 19 Aug 2005 03:14:25 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 19 Aug 2005 03:14:25 -0000 Received: (qmail 93017 invoked by uid 500); 19 Aug 2005 03:14:23 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 92726 invoked by uid 500); 19 Aug 2005 03:14:21 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 92713 invoked by uid 500); 19 Aug 2005 03:14:21 -0000 Received: (qmail 92710 invoked by uid 99); 19 Aug 2005 03:14:21 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 18 Aug 2005 20:14:21 -0700 Received: (qmail 83964 invoked by uid 65534); 19 Aug 2005 03:14:21 -0000 Message-ID: <20050819031421.83963.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r233412 - in /jakarta/commons/proper/math/branches/MATH_1_1: src/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java xdocs/changes.xml Date: Fri, 19 Aug 2005 03:14:20 -0000 To: commons-cvs@jakarta.apache.org From: brentworden@apache.org X-Mailer: svnmailer-1.0.3 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: brentworden Date: Thu Aug 18 20:14:16 2005 New Revision: 233412 URL: http://svn.apache.org/viewcvs?rev=233412&view=rev Log: PR 36215: Added upper tail cumulative probability method to HypergeometricDistributionImpl. Note, this new method was not added to the HypergeometricDistribution interface to avoid binary incompatiblities with older versions. Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java?rev=233412&r1=233411&r2=233412&view=diff ============================================================================== --- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java (original) +++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java Thu Aug 18 20:14:16 2005 @@ -18,7 +18,6 @@ import java.io.Serializable; -import org.apache.commons.math.MathException; import org.apache.commons.math.util.MathUtils; /** @@ -54,7 +53,8 @@ super(); if (numberOfSuccesses > populationSize) { throw new IllegalArgumentException( - "number of successes must be less than or equal to population size"); + "number of successes must be less than or equal to " + + "population size"); } if (sampleSize > populationSize) { throw new IllegalArgumentException( @@ -69,10 +69,8 @@ * For this disbution, 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. */ - public double cumulativeProbability(int x) throws MathException{ + public double cumulativeProbability(int x) { double ret; int n = getPopulationSize(); @@ -84,11 +82,10 @@ ret = 0.0; } else if(x >= domain[1]) { ret = 1.0; + } else if (x - domain[0] < domain[1] - x) { + ret = lowerCumulativeProbability(domain[0], x, n, m, k); } else { - ret = 0.0; - for (int i = domain[0]; i <= x; ++i){ - ret += probability(n, m, k, i); - } + ret = 1.0 - upperCumulativeProbability(x + 1, domain[1], n, m, k); } return ret; @@ -182,6 +179,28 @@ } /** + * For this disbution, X, this method returns P(x0 ≤ X ≤ x1). This + * probability is computed by summing the point probabilities for the values + * x0, x0 + 1, x0 + 2, ..., x1, in that order. + * @param x0 the inclusive, lower bound + * @param x1 the inclusive, upper bound + * @param n the population size. + * @param m number of successes in the population. + * @param k the sample size. + * @return P(x0 ≤ X ≤ x1). + */ + private double lowerCumulativeProbability( + int x0, int x1, int n, int m, int k) + { + double ret; + ret = 0.0; + for (int i = x0; i <= x1; ++i) { + ret += probability(n, m, k, i); + } + return ret; + } + + /** * For this disbution, X, this method returns P(X = x). * * @param x the value at which the PMF is evaluated. @@ -203,7 +222,7 @@ return ret; } - + /** * For the disbution, X, defined by the given hypergeometric distribution * parameters, this method returns P(X = x). @@ -219,7 +238,7 @@ MathUtils.binomialCoefficientLog(n - m, k - x) - MathUtils.binomialCoefficientLog(n, k)); } - + /** * Modify the number of successes. * @param num the new number of successes. @@ -245,8 +264,8 @@ } populationSize = size; } - - /** + + /** * Modify the sample size. * @param size the new sample size. * @throws IllegalArgumentException if size is negative. @@ -258,4 +277,52 @@ } sampleSize = size; } + + /** + * For this disbution, X, this method returns P(X ≥ x). + * @param x the value at which the CDF is evaluated. + * @return upper tail CDF for this distribution. + */ + public double upperCumulativeProbability(int x) { + double ret; + + int n = getPopulationSize(); + int m = getNumberOfSuccesses(); + int k = getSampleSize(); + + int[] domain = getDomain(n, m, k); + if (x < domain[0]) { + ret = 1.0; + } else if(x >= domain[1]) { + ret = 0.0; + } else if (x - domain[0] < domain[1] - x) { + ret = 1.0 - lowerCumulativeProbability(domain[0], x - 1, n, m, k); + } else { + ret = upperCumulativeProbability(x, domain[1], n, m, k); + } + + return ret; + } + + /** + * For this disbution, X, this method returns P(x0 ≤ X ≤ x1). This + * probability is computed by summing the point probabilities for the values + * x1, x1 - 1, x1 - 2, ..., x0, in that order. + * @param x0 the inclusive, lower bound + * @param x1 the inclusive, upper bound + * @param n the population size. + * @param m number of successes in the population. + * @param k the sample size. + * @return P(x0 ≤ X ≤ x1). + */ + private double upperCumulativeProbability( + int x0, int x1, int n, int m, int k) + { + double ret = 0.0; + for (int i = x1; i >= x0; --i) { + ret += probability(n, m, k, i); + } + return ret; + } + } Modified: jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml?rev=233412&r1=233411&r2=233412&view=diff ============================================================================== --- jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml (original) +++ jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml Thu Aug 18 20:14:16 2005 @@ -45,6 +45,9 @@ and numerical utilities, and a PRNG pluggability framework making it possible to replace the JDK-supplied random number generator in commons-math (and elsewhere) with alternative PRNG implementations."> + + Added upper tail cumulative probability method to HypergeometricDistributionImpl. + Added better handling of numerical overflow and division by zero in Complex calculations. --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org