You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/09/17 21:19:09 UTC
cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util BeanTransformer.java NumberTransformer.java
mdiggory 2003/09/17 12:19:09
Modified: math/src/java/org/apache/commons/math/stat/distribution
BinomialDistribution.java DistributionFactory.java
DistributionFactoryImpl.java
math/src/test/org/apache/commons/math/stat/distribution
BinomialDistributionTest.java
DistributionFactoryImplTest.java
math/src/java/org/apache/commons/math/stat Univariate.java
math/src/java/org/apache/commons/math/util
BeanTransformer.java NumberTransformer.java
Added: math/src/java/org/apache/commons/math/stat/distribution
HypergeometricDistribution.java
HypergeometricDistributionImpl.java
math/src/test/org/apache/commons/math/stat/distribution
HypergeometricDistributionTest.java
math/src/java/org/apache/commons/math/stat Applyable.java
Log:
PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=23010
Submitted by: Brent Worden
Reviewed by: Mark Diggory
Revision Changes Path
1.2 +2 -2 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/BinomialDistribution.java
Index: BinomialDistribution.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/BinomialDistribution.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BinomialDistribution.java 16 Aug 2003 17:06:15 -0000 1.1
+++ BinomialDistribution.java 17 Sep 2003 19:19:08 -0000 1.2
@@ -57,7 +57,7 @@
* The Binomial Distribution.
*
* Instances of BinomialDistribution objects should be created using
- * {@link DistributionFactory#createBinomailDistribution(int, double)}.
+ * {@link DistributionFactory#createBinomialDistribution(int, double)}.
*
* References:
* <ul>
1.11 +15 -2 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactory.java
Index: DistributionFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactory.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- DistributionFactory.java 16 Aug 2003 17:06:15 -0000 1.10
+++ DistributionFactory.java 17 Sep 2003 19:19:08 -0000 1.11
@@ -51,6 +51,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
+
package org.apache.commons.math.stat.distribution;
/**
@@ -99,7 +100,7 @@
* @param probabilityOfSuccess the probability of success.
* @return a new binomial distribution.
*/
- public abstract BinomialDistribution createBinomailDistribution(
+ public abstract BinomialDistribution createBinomialDistribution(
int numberOfTrials, double probabilityOfSuccess);
/**
@@ -142,4 +143,16 @@
* @return a new t distribution.
*/
public abstract TDistribution createTDistribution(double degreesOfFreedom);
+
+ /**
+ * Create a new hypergeometric distribution with the given the population
+ * size, the number of successes in the population, and the sample size.
+ * @param populationSize the population size.
+ * @param numberOfSuccesses number of successes in the population.
+ * @param sampleSize the sample size.
+ * @return a new hypergeometric desitribution.
+ */
+ public abstract HypergeometricDistribution
+ createHypergeometricDistribution(int populationSize,
+ int numberOfSuccesses, int sampleSize);
}
1.9 +19 -2 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactoryImpl.java
Index: DistributionFactoryImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactoryImpl.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DistributionFactoryImpl.java 16 Aug 2003 17:06:15 -0000 1.8
+++ DistributionFactoryImpl.java 17 Sep 2003 19:19:08 -0000 1.9
@@ -129,10 +129,27 @@
* @param probabilityOfSuccess the probability of success.
* @return a new binomial distribution.
*/
- public BinomialDistribution createBinomailDistribution(
+ public BinomialDistribution createBinomialDistribution(
int numberOfTrials, double probabilityOfSuccess) {
return new BinomialDistributionImpl(numberOfTrials,
probabilityOfSuccess);
+ }
+
+ /**
+ * Create a new hypergeometric distribution with the given the population
+ * size, the number of successes in the population, and the sample size.
+ * @param populationSize the population size.
+ * @param numberOfSuccesses number of successes in the population.
+ * @param sampleSize the sample size.
+ * @return a new hypergeometric desitribution.
+ */
+ public HypergeometricDistribution createHypergeometricDistribution(
+ int populationSize,
+ int numberOfSuccesses,
+ int sampleSize)
+ {
+ return new HypergeometricDistributionImpl(populationSize,
+ numberOfSuccesses, sampleSize);
}
}
1.1 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/HypergeometricDistribution.java
Index: HypergeometricDistribution.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math.stat.distribution;
/**
* The Hypergeometric Distribution.
*
* Instances of HypergeometricDistribution objects should be created using
* {@link DistributionFactory#createHypergeometricDistribution(int, int, int)}.
*
* References:
* <ul>
* <li><a href="http://mathworld.wolfram.com/HypergeometricDistribution.html">
* Hypergeometric Distribution</a></li>
* </ul>
*
* @version $Revision: 1.1 $ $Date: 2003/09/17 19:19:08 $
*/
public interface HypergeometricDistribution extends DiscreteDistribution {
/**
* Access the number of successes.
* @return the number of successes.
*/
public abstract int getNumberOfSuccesses();
/**
* Access the population size.
* @return the population size.
*/
public abstract int getPopulationSize();
/**
* Access the sample size.
* @return the sample size.
*/
public abstract int getSampleSize();
/**
* Modify the number of successes.
* @param num the new number of successes.
*/
public abstract void setNumberOfSuccesses(int num);
/**
* Modify the population size.
* @param size the new population size.
*/
public abstract void setPopulationSize(int size);
/**
* Modify the sample size.
* @param size the new sample size.
*/
public abstract void setSampleSize(int size);
}
1.1 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/HypergeometricDistributionImpl.java
Index: HypergeometricDistributionImpl.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math.stat.distribution;
import org.apache.commons.math.util.MathUtils;
/**
* The default implementation of {@link HypergeometricDistribution}.
*
* @version $Revision: 1.1 $ $Date: 2003/09/17 19:19:08 $
*/
public class HypergeometricDistributionImpl extends AbstractDiscreteDistribution
implements HypergeometricDistribution
{
/** The number of successes in the population. */
private int numberOfSuccesses;
/** The population size. */
private int populationSize;
/** The sample size. */
private int sampleSize;
/**
* Construct a new hypergeometric distribution with the given the population
* size, the number of successes in the population, and the sample size.
* @param populationSize the population size.
* @param numberOfSuccesses number of successes in the population.
* @param sampleSize the sample size.
*/
public HypergeometricDistributionImpl(int populationSize,
int numberOfSuccesses, int sampleSize)
{
super();
setPopulationSize(populationSize);
setSampleSize(sampleSize);
setNumberOfSuccesses(numberOfSuccesses);
}
/**
* 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.
*/
public double cummulativeProbability(int x) {
double ret;
int n = getPopulationSize();
int m = getNumberOfSuccesses();
int k = getSampleSize();
int[] domain = getDomain(n, m, k);
if (x < domain[0]) {
ret = 0.0;
} else if(x >= domain[1]) {
ret = 1.0;
} else {
ret = 0.0;
for (int i = domain[0]; i <= x; ++i){
ret += probability(n, m, k, i);
}
}
return ret;
}
/**
* Return the domain for the given hypergeometric distribution parameters.
* @param n the population size.
* @param m number of successes in the population.
* @param k the sample size.
* @return a two element array containing the lower and upper bounds of the
* hypergeometric distribution.
*/
private int[] getDomain(int n, int m, int k){
return new int[]{
getLowerDomain(n, m, k),
getUpperDomain(m, k)
};
}
/**
* Access the domain value lower bound, based on <code>p</code>, used to
* bracket a PDF root.
*
* @param p the desired probability for the critical value
* @return domain value lower bound, i.e.
* P(X < <i>lower bound</i>) < <code>p</code>
*/
protected int getDomainLowerBound(double p) {
return getLowerDomain(getPopulationSize(), getNumberOfSuccesses(),
getSampleSize());
}
/**
* Access the domain value upper bound, based on <code>p</code>, used to
* bracket a PDF root.
*
* @param p the desired probability for the critical value
* @return domain value upper bound, i.e.
* P(X < <i>upper bound</i>) > <code>p</code>
*/
protected int getDomainUpperBound(double p) {
return getUpperDomain(getSampleSize(), getNumberOfSuccesses());
}
/**
* Return the lowest domain value for the given hypergeometric distribution
* parameters.
* @param n the population size.
* @param m number of successes in the population.
* @param k the sample size.
* @return the lowest domain value of the hypergeometric distribution.
*/
private int getLowerDomain(int n, int m, int k) {
return Math.max(0, m - (n - k));
}
/**
* Access the number of successes.
* @return the number of successes.
*/
public int getNumberOfSuccesses() {
return numberOfSuccesses;
}
/**
* Access the population size.
* @return the population size.
*/
public int getPopulationSize() {
return populationSize;
}
/**
* Access the sample size.
* @return the sample size.
*/
public int getSampleSize() {
return sampleSize;
}
/**
* Return the highest domain value for the given hypergeometric distribution
* parameters.
* @param m number of successes in the population.
* @param k the sample size.
* @return the highest domain value of the hypergeometric distribution.
*/
private int getUpperDomain(int m, int k){
return Math.min(k, m);
}
/**
* For this disbution, X, this method returns P(X = x).
* @param x the value at which the PMF is evaluated.
* @return PMF for this distribution.
*/
public double probability(int x) {
double ret;
int n = getPopulationSize();
int m = getNumberOfSuccesses();
int k = getSampleSize();
int[] domain = getDomain(n, m, k);
if(x < domain[0] || x > domain[1]){
ret = 0.0;
} else {
ret = probability(n, m, k, x);
}
return ret;
}
/**
* For the disbution, X, defined by the given hypergeometric distribution
* parameters, this method returns P(X = x).
* @param n the population size.
* @param m number of successes in the population.
* @param k the sample size.
* @param x the value at which the PMF is evaluated.
* @return PMF for the distribution.
*/
private double probability(int n, int m, int k, int x) {
return Math.exp(MathUtils.binomialCoefficientLog(m, x) +
MathUtils.binomialCoefficientLog(n - m, k - x) -
MathUtils.binomialCoefficientLog(n, k));
}
/**
* Modify the number of successes.
* @param num the new number of successes.
*/
public void setNumberOfSuccesses(int num) {
if(num < 0){
throw new IllegalArgumentException(
"number of successes must be non-negative.");
}
numberOfSuccesses = num;
}
/**
* Modify the population size.
* @param size the new population size.
*/
public void setPopulationSize(int size) {
if(size <= 0){
throw new IllegalArgumentException(
"population size must be positive.");
}
populationSize = size;
}
/**
* Modify the sample size.
* @param size the new sample size.
*/
public void setSampleSize(int size) {
if(size < 0){
throw new IllegalArgumentException(
"sample size must be non-negative.");
}
sampleSize = size;
}
}
1.2 +1 -1 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/BinomialDistributionTest.java
Index: BinomialDistributionTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/BinomialDistributionTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BinomialDistributionTest.java 16 Aug 2003 17:06:15 -0000 1.1
+++ BinomialDistributionTest.java 17 Sep 2003 19:19:09 -0000 1.2
@@ -74,7 +74,7 @@
*/
protected void setUp() throws Exception {
super.setUp();
- b = DistributionFactory.newInstance().createBinomailDistribution(10, 0.70);
+ b = DistributionFactory.newInstance().createBinomialDistribution(10, 0.70);
}
/*
1.8 +55 -7 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/DistributionFactoryImplTest.java
Index: DistributionFactoryImplTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/DistributionFactoryImplTest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DistributionFactoryImplTest.java 16 Aug 2003 17:06:15 -0000 1.7
+++ DistributionFactoryImplTest.java 17 Sep 2003 19:19:09 -0000 1.8
@@ -200,7 +200,7 @@
public void testBinomialDistributionNegativePositive(){
try {
- factory.createBinomailDistribution(-1, 0.5);
+ factory.createBinomialDistribution(-1, 0.5);
fail("negative number of trials. IllegalArgumentException expected");
} catch (IllegalArgumentException ex ) {
}
@@ -208,7 +208,7 @@
public void testBinomialDistributionZeroPositive(){
try {
- factory.createBinomailDistribution(0, 0.5);
+ factory.createBinomialDistribution(0, 0.5);
} catch (IllegalArgumentException ex ) {
fail("zero number of trials. IllegalArgumentException is not expected");
}
@@ -216,7 +216,7 @@
public void testBinomialDistributionPositivePositive(){
try {
- factory.createBinomailDistribution(10, 0.5);
+ factory.createBinomialDistribution(10, 0.5);
} catch (IllegalArgumentException ex ) {
fail("positive number of trials. IllegalArgumentException is not expected");
}
@@ -224,7 +224,7 @@
public void testBinomialDistributionPositiveNegative(){
try {
- factory.createBinomailDistribution(10, -0.5);
+ factory.createBinomialDistribution(10, -0.5);
fail("negative probability of success. IllegalArgumentException expected");
} catch (IllegalArgumentException ex ) {
}
@@ -232,7 +232,7 @@
public void testBinomialDistributionPositiveZero(){
try {
- factory.createBinomailDistribution(10, 0.0);
+ factory.createBinomialDistribution(10, 0.0);
} catch (IllegalArgumentException ex ) {
fail("zero probability of success. IllegalArgumentException is not expected");
}
@@ -240,7 +240,7 @@
public void testBinomialDistributionPositiveOne(){
try {
- factory.createBinomailDistribution(10, 1.0);
+ factory.createBinomialDistribution(10, 1.0);
} catch (IllegalArgumentException ex ) {
fail("valid probability of success. IllegalArgumentException is not expected");
}
@@ -248,9 +248,57 @@
public void testBinomialDistributionPositiveTwo(){
try {
- factory.createBinomailDistribution(10, 2.0);
+ factory.createBinomialDistribution(10, 2.0);
fail("high probability of success. IllegalArgumentException expected");
} catch (IllegalArgumentException ex ) {
+ }
+ }
+
+ public void testHypergeometricDistributionNegativePositivePositive(){
+ try {
+ factory.createHypergeometricDistribution(-1, 10, 10);
+ fail("negative population size. IllegalArgumentException expected");
+ } catch(IllegalArgumentException ex) {
+ }
+ }
+
+ public void testHypergeometricDistributionZeroPositivePositive(){
+ try {
+ factory.createHypergeometricDistribution(0, 10, 10);
+ fail("zero population size. IllegalArgumentException expected");
+ } catch(IllegalArgumentException ex) {
+ }
+ }
+
+ public void testHypergeometricDistributionPositiveNegativePositive(){
+ try {
+ factory.createHypergeometricDistribution(20, -1, 10);
+ fail("negative number of successes. IllegalArgumentException expected");
+ } catch(IllegalArgumentException ex) {
+ }
+ }
+
+ public void testHypergeometricDistributionPositiveZeroPositive(){
+ try {
+ factory.createHypergeometricDistribution(20, 0, 10);
+ } catch(IllegalArgumentException ex) {
+ fail("valid number of successes. IllegalArgumentException is not expected");
+ }
+ }
+
+ public void testHypergeometricDistributionPositivePositiveNegative(){
+ try {
+ factory.createHypergeometricDistribution(20, 10, -1);
+ fail("negative sample size. IllegalArgumentException expected");
+ } catch(IllegalArgumentException ex) {
+ }
+ }
+
+ public void testHypergeometricDistributionPositivePositiveZero(){
+ try {
+ factory.createHypergeometricDistribution(20, 10, 0);
+ } catch(IllegalArgumentException ex) {
+ fail("valid sample size. IllegalArgumentException is not expected");
}
}
}
1.1 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/HypergeometricDistributionTest.java
Index: HypergeometricDistributionTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math.stat.distribution;
import junit.framework.TestCase;
/**
* @version $Revision: 1.1 $ $Date: 2003/09/17 19:19:09 $
*/
public class HypergeometricDistributionTest extends TestCase {
private HypergeometricDistribution h;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
*/
public HypergeometricDistributionTest(String name) {
super(name);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
h = DistributionFactory.newInstance().createHypergeometricDistribution(10, 5, 5);
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
h = null;
super.tearDown();
}
public void testInverseCummulativeProbability001() {
testValue(-1, .001);
}
public void testInverseCumulativeProbability010() {
testValue(0, .010);
}
public void testInverseCumulativeProbability025() {
testValue(0, .025);
}
public void testInverseCumulativeProbability050() {
testValue(0, .050);
}
public void testInverseCumulativeProbability100() {
testValue(0, .100);
}
public void testInverseCummulativeProbability999() {
testValue(4, .999);
}
public void testInverseCumulativeProbability990() {
testValue(3, .990);
}
public void testInverseCumulativeProbability975() {
testValue(3, .975);
}
public void testInverseCumulativeProbability950() {
testValue(3, .950);
}
public void testInverseCumulativeProbability900() {
testValue(3, .900);
}
public void testCummulativeProbability0() {
testProbability(0, .00400);
}
public void testCummulativeProbability1() {
testProbability(1, .10318);
}
public void testCumulativeProbability2() {
testProbability(2, .50000);
}
public void testCumulativeProbability3() {
testProbability(3, .89683);
}
public void testCumulativeProbability4() {
testProbability(4, .99603);
}
public void testCumulativeProbability5() {
testProbability(5, 1.00000);
}
private void testProbability(int x, double expected){
double actual = h.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
}
private void testValue(int expected, double p){
int actual = h.inverseCummulativeProbability(p);
assertEquals(expected, actual);
assertTrue(h.cummulativeProbability(actual) <= p);
assertTrue(h.cummulativeProbability(actual + 1) >= p);
}
}
1.9 +2 -2 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/Univariate.java
Index: Univariate.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/Univariate.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Univariate.java 9 Jul 2003 21:45:23 -0000 1.8
+++ Univariate.java 17 Sep 2003 19:19:09 -0000 1.9
@@ -71,7 +71,7 @@
* in all computations.
* @version $Revision$ $Date$
*/
-public interface Univariate {
+public interface Univariate extends Applyable{
/**
* A LEPTOKURTIC set has a positive kurtosis (a high peak)
*/
1.1 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/Applyable.java
Index: Applyable.java
===================================================================
/*
* Created on Jul 15, 2003
*
*/
package org.apache.commons.math.stat;
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
/**
* Applyable.java
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*
*/
public interface Applyable {
/**
* Applies a UnivariateStatistic object against this object
* and returns the result.
* @param stat The stat to apply.
* @return The result value of the application.
*/
double apply(UnivariateStatistic stat);
}
1.5 +6 -5 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/BeanTransformer.java
Index: BeanTransformer.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/BeanTransformer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- BeanTransformer.java 9 Aug 2003 04:03:41 -0000 1.4
+++ BeanTransformer.java 17 Sep 2003 19:19:09 -0000 1.5
@@ -55,6 +55,7 @@
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.math.MathException;
/**
* Uses PropertyUtils to map a Bean getter to a double value.
@@ -85,18 +86,18 @@
/**
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
*/
- public double transform(final Object o) {
+ public double transform(final Object o) throws MathException {
double d = Double.NaN;
try {
d =
((Number) PropertyUtils.getProperty(o, propertyName))
.doubleValue();
} catch (IllegalAccessException e) {
- e.printStackTrace();
+ throw new MathException(e.getMessage(),e);
} catch (InvocationTargetException e) {
- e.printStackTrace();
+ throw new MathException(e.getMessage(),e);
} catch (NoSuchMethodException e) {
- e.printStackTrace();
+ throw new MathException(e.getMessage(),e);
}
return d;
}
1.4 +4 -2 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/NumberTransformer.java
Index: NumberTransformer.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/NumberTransformer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- NumberTransformer.java 9 Jul 2003 20:04:12 -0000 1.3
+++ NumberTransformer.java 17 Sep 2003 19:19:09 -0000 1.4
@@ -53,6 +53,8 @@
*/
package org.apache.commons.math.util;
+import org.apache.commons.math.MathException;
+
/**
* Subclasses implementing this interface can transform Objects to doubles.
* @version $Revision$ $Date$
@@ -66,5 +68,5 @@
* @param o the Object to be transformed.
* @return the double value of the Object.
*/
- double transform(Object o);
+ double transform(Object o) throws MathException;
}