Submitted by:	 Brent Worden 
Reviewed by:	Mark Diggory


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140995 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-09-17 19:19:09 +00:00
parent ffa6aac264
commit 4b7bef4b26
12 changed files with 680 additions and 23 deletions

View File

@ -57,7 +57,7 @@ package org.apache.commons.math.stat.distribution;
* The Binomial Distribution.
*
* Instances of BinomialDistribution objects should be created using
* {@link DistributionFactory#createBinomailDistribution(int, double)}.
* {@link DistributionFactory#createBinomialDistribution(int, double)}.
*
* References:
* <ul>
@ -65,7 +65,7 @@ package org.apache.commons.math.stat.distribution;
* Binomial Distribution</a></li>
* </ul>
*
* @version $Revision: 1.1 $ $Date: 2003/08/16 17:06:15 $
* @version $Revision: 1.2 $ $Date: 2003/09/17 19:19:08 $
*/
public interface BinomialDistribution extends DiscreteDistribution {
/**

View File

@ -51,6 +51,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.math.stat.distribution;
/**
@ -72,7 +73,7 @@ package org.apache.commons.math.stat.distribution;
* ChiSquaredDistribution chi = factory.createChiSquareDistribution(5.0);
* </pre>
*
* @version $Revision: 1.10 $ $Date: 2003/08/16 17:06:15 $
* @version $Revision: 1.11 $ $Date: 2003/09/17 19:19:08 $
*/
public abstract class DistributionFactory {
/**
@ -99,7 +100,7 @@ public abstract class DistributionFactory {
* @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 @@ public abstract class DistributionFactory {
* @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);
}

View File

@ -58,7 +58,7 @@ package org.apache.commons.math.stat.distribution;
* A concrete distribution factory. This is the default factory used by
* Commons-Math.
*
* @version $Revision: 1.8 $ $Date: 2003/08/16 17:06:15 $
* @version $Revision: 1.9 $ $Date: 2003/09/17 19:19:08 $
*/
public class DistributionFactoryImpl extends DistributionFactory {
/**
@ -129,10 +129,27 @@ public class DistributionFactoryImpl extends DistributionFactory {
* @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);
}
}

View File

@ -0,0 +1,107 @@
/* ====================================================================
* 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);
}

View File

@ -0,0 +1,279 @@
/* ====================================================================
* 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 &le; 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 &lt; <i>lower bound</i>) &lt; <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 &lt; <i>upper bound</i>) &gt; <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;
}
}

View File

@ -0,0 +1,26 @@
/*
* 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);
}

View File

@ -69,9 +69,9 @@ package org.apache.commons.math.stat;
* reported statistics will be based on these values<p>
* The default windowSize is "infinite" -- i.e., all values added are included
* in all computations.
* @version $Revision: 1.8 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.9 $ $Date: 2003/09/17 19:19:09 $
*/
public interface Univariate {
public interface Univariate extends Applyable{
/**
* A LEPTOKURTIC set has a positive kurtosis (a high peak)
*/

View File

@ -55,10 +55,11 @@ package org.apache.commons.math.util;
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.
* @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
* @version $Revision: 1.5 $ $Date: 2003/09/17 19:19:09 $
*/
public class BeanTransformer implements NumberTransformer {
@ -85,18 +86,18 @@ public class BeanTransformer implements NumberTransformer {
/**
* @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;
}

View File

@ -53,9 +53,11 @@
*/
package org.apache.commons.math.util;
import org.apache.commons.math.MathException;
/**
* Subclasses implementing this interface can transform Objects to doubles.
* @version $Revision: 1.3 $ $Date: 2003/07/09 20:04:12 $
* @version $Revision: 1.4 $ $Date: 2003/09/17 19:19:09 $
*/
public interface NumberTransformer {
@ -66,5 +68,5 @@ public interface NumberTransformer {
* @param o the Object to be transformed.
* @return the double value of the Object.
*/
double transform(Object o);
double transform(Object o) throws MathException;
}

View File

@ -74,7 +74,7 @@ public class BinomialDistributionTest extends TestCase {
*/
protected void setUp() throws Exception {
super.setUp();
b = DistributionFactory.newInstance().createBinomailDistribution(10, 0.70);
b = DistributionFactory.newInstance().createBinomialDistribution(10, 0.70);
}
/*

View File

@ -200,7 +200,7 @@ public class DistributionFactoryImplTest extends TestCase {
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 class DistributionFactoryImplTest extends TestCase {
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 class DistributionFactoryImplTest extends TestCase {
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 class DistributionFactoryImplTest extends TestCase {
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 class DistributionFactoryImplTest extends TestCase {
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 class DistributionFactoryImplTest extends TestCase {
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 class DistributionFactoryImplTest extends TestCase {
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");
}
}
}

View File

@ -0,0 +1,164 @@
/* ====================================================================
* 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);
}
}