Added classes for binominal distribution and some infrastructure for other

discrete distributions.
Contributed by Brent Worden (brent@worden.org)


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140993 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joerg Pietschmann 2003-08-16 17:06:15 +00:00
parent 026c120649
commit 606b836061
8 changed files with 775 additions and 3 deletions

View File

@ -0,0 +1,160 @@
/* ====================================================================
* 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;
/**
* Base class for various discrete distributions. It provides default
* implementations for some of the methods that do not vary from distribution
* to distribution.
*
* @version $Revision: 1.1 $ $Date: 2003/08/16 17:06:15 $
*/
public abstract class AbstractDiscreteDistribution
implements DiscreteDistribution {
/**
* Default constructor.
*/
protected AbstractDiscreteDistribution() {
super();
}
/**
* For this disbution, X, this method returns P(x0 &le; X &le; x1).
* @param x0 the inclusive, lower bound
* @param x1 the inclusive, upper bound
* @return the cummulative probability.
*/
public double cummulativeProbability(int x0, int x1) {
return cummulativeProbability(x1) -
cummulativeProbability(x0 - 1);
}
/**
* For this distribution, X, this method returns the critical point x, such
* that P(X &le; x) &le; <code>p</code>.
*
* @param p the desired probability
* @return x, such that P(X &lt; x) = <code>p</code>
*/
public int inverseCummulativeProbability(final double p) {
if (p < 0.0 || p > 1.0) {
throw new IllegalArgumentException(
"p must be between 0.0 and 1.0, inclusive.");
}
// by default, do simple bisection.
// subclasses can override if there is a better method.
int x0 = getDomainLowerBound(p);
int x1 = getDomainUpperBound(p);
double pm;
while (x0 < x1) {
int xm = x0 + (x1 - x0) / 2;
pm = cummulativeProbability(xm);
if (pm > p) {
// update x1
if (xm == x1) {
// this can happen with integer division
// simply decrement x1
--x1;
} else {
// update x1 normally
x1 = xm;
}
} else {
// update x0
if (xm == x0) {
// this can happen with integer division
// simply increment x0
++x0;
} else {
// update x0 normally
x0 = xm;
}
}
}
// insure x0 is the correct critical point
pm = cummulativeProbability(x0);
while (pm > p) {
--x0;
pm = cummulativeProbability(x0);
}
return x0;
}
/**
* Access the domain value lower bound, based on <code>p</code>, used to
* bracket a PDF root. This method is used by
* {@link #inverseCummulativeProbability(int)} to find critical values.
*
* @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 abstract int getDomainLowerBound(double p);
/**
* Access the domain value upper bound, based on <code>p</code>, used to
* bracket a PDF root. This method is used by
* {@link #inverseCummulativeProbability(int)} to find critical values.
*
* @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 abstract int getDomainUpperBound(double p);
}

View File

@ -0,0 +1,94 @@
/* ====================================================================
* 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 Binomial Distribution.
*
* Instances of BinomialDistribution objects should be created using
* {@link DistributionFactory#createBinomailDistribution(int, double)}.
*
* References:
* <ul>
* <li><a href="http://mathworld.wolfram.com/BinomialDistribution.html">
* Binomial Distribution</a></li>
* </ul>
*
* @version $Revision: 1.1 $ $Date: 2003/08/16 17:06:15 $
*/
public interface BinomialDistribution extends DiscreteDistribution {
/**
* Access the number of trials for this distribution.
* @return the number of trials.
*/
int getNumberOfTrials();
/**
* Access the probability of success for this distribution.
* @return the probability of success.
*/
double getProbabilityOfSuccess();
/**
* Change the number of trials for this distribution.
* @param trials the new number of trials.
*/
void setNumberOfTrials(int trials);
/**
* Change the probability of success for this distribution.
* @param p the new probability of success.
*/
void setProbabilityOfSuccess(double p);
}

View File

@ -0,0 +1,184 @@
/* ====================================================================
* 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.special.Beta;
import org.apache.commons.math.util.MathUtils;
/**
* The default implementation of {@link BinomialDistribution}.
*
* @version $Revision: 1.1 $ $Date: 2003/08/16 17:06:15 $
*/
public class BinomialDistributionImpl extends AbstractDiscreteDistribution
implements BinomialDistribution {
/** The number of trials. */
private int numberOfTrials;
/** The probability of success. */
private double probabilityOfSuccess;
/**
* Create a binomial distribution with the given number of trials and
* probability of success.
* @param trials the number of trials.
* @param p the probability of success.
*/
public BinomialDistributionImpl(int trials, double p) {
super();
setNumberOfTrials(trials);
setProbabilityOfSuccess(p);
}
/**
* Access the number of trials for this distribution.
* @return the number of trials.
*/
public int getNumberOfTrials() {
return numberOfTrials;
}
/**
* Access the probability of success for this distribution.
* @return the probability of success.
*/
public double getProbabilityOfSuccess() {
return probabilityOfSuccess;
}
/**
* Change the number of trials for this distribution.
* @param trials the new number of trials.
*/
public void setNumberOfTrials(int trials) {
if (trials < 0) {
throw new IllegalArgumentException(
"number of trials must be non-negative.");
}
numberOfTrials = trials;
}
/**
* Change the probability of success for this distribution.
* @param p the new probability of success.
*/
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.");
}
probabilityOfSuccess = p;
}
/**
* 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 -1;
}
/**
* 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 getNumberOfTrials();
}
/**
* 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;
if (x < 0) {
ret = 0.0;
} else if (x >= getNumberOfTrials()) {
ret = 1.0;
} else {
ret = 1.0 - Beta.regularizedBeta(getProbabilityOfSuccess(),
x + 1.0, getNumberOfTrials() - x);
}
return ret;
}
/**
* 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;
if (x < 0 || x > getNumberOfTrials()) {
ret = 0.0;
} else {
ret = MathUtils.binomialCoefficientDouble(getNumberOfTrials(), x) *
Math.pow(getProbabilityOfSuccess(), x) *
Math.pow(1.0 - getProbabilityOfSuccess(),
getNumberOfTrials() - x);
}
return ret;
}
}

View File

@ -0,0 +1,90 @@
/* ====================================================================
* 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;
/**
* Base interface for various discrete distributions.
*
* @version $Revision: 1.1 $ $Date: 2003/08/16 17:06:15 $
*/
public interface DiscreteDistribution {
/**
* 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.
*/
double probability(int x);
/**
* 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.
*/
double cummulativeProbability(int x);
/**
* For this disbution, X, this method returns P(x0 &le; X &le; x1).
* @param x0 the inclusive, lower bound
* @param x1 the inclusive, upper bound
* @return the cummulative probability.
*/
double cummulativeProbability(int x0, int x1);
/**
* For this disbution, X, this method returns x such that P(X &le; x) <= p.
* @param p the cummulative probability.
* @return x.
*/
int inverseCummulativeProbability(double p);
}

View File

@ -57,7 +57,9 @@ package org.apache.commons.math.stat.distribution;
* This factory provids the means to create common statistical distributions.
* The following distributions are supported:
* <ul>
* <li>Binomial</li>
* <li>Chi-Squared</li>
* <li>Exponential</li>
* <li>F</li>
* <li>Gamma</li>
* <li>Student's t</li>
@ -70,7 +72,7 @@ package org.apache.commons.math.stat.distribution;
* ChiSquaredDistribution chi = factory.createChiSquareDistribution(5.0);
* </pre>
*
* @version $Revision: 1.9 $ $Date: 2003/07/30 21:58:11 $
* @version $Revision: 1.10 $ $Date: 2003/08/16 17:06:15 $
*/
public abstract class DistributionFactory {
/**
@ -89,7 +91,17 @@ public abstract class DistributionFactory {
public static DistributionFactory newInstance() {
return new DistributionFactoryImpl();
}
/**
* Create a binomial distribution with the given number of trials and
* probability of success.
* @param numberOfTrials the number of trials.
* @param probabilityOfSuccess the probability of success.
* @return a new binomial distribution.
*/
public abstract BinomialDistribution createBinomailDistribution(
int numberOfTrials, double probabilityOfSuccess);
/**
* Create a new chi-square distribution with the given degrees of freedom.
* @param degreesOfFreedom degrees of freedom.

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.7 $ $Date: 2003/07/09 20:03:23 $
* @version $Revision: 1.8 $ $Date: 2003/08/16 17:06:15 $
*/
public class DistributionFactoryImpl extends DistributionFactory {
/**
@ -122,4 +122,17 @@ public class DistributionFactoryImpl extends DistributionFactory {
return new ExponentialDistributionImpl(mean);
}
/**
* Create a binomial distribution with the given number of trials and
* probability of success.
* @param numberOfTrials the number of trials.
* @param probabilityOfSuccess the probability of success.
* @return a new binomial distribution.
*/
public BinomialDistribution createBinomailDistribution(
int numberOfTrials, double probabilityOfSuccess) {
return new BinomialDistributionImpl(numberOfTrials,
probabilityOfSuccess);
}
}

View File

@ -0,0 +1,163 @@
/* ====================================================================
* 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;
/**
* @author Brent Worden
*/
public class BinomialDistributionTest extends TestCase {
private BinomialDistribution b;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
*/
public BinomialDistributionTest(String name) {
super(name);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
b = DistributionFactory.newInstance().createBinomailDistribution(10, 0.70);
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
b = null;
super.tearDown();
}
public void testInverseCummulativeProbability001() {
testValue(1, .001);
}
public void testInverseCumulativeProbability010() {
testValue(2, .010);
}
public void testInverseCumulativeProbability025() {
testValue(3, .025);
}
public void testInverseCumulativeProbability050() {
testValue(4, .050);
}
public void testInverseCumulativeProbability100() {
testValue(4, .100);
}
public void testInverseCummulativeProbability999() {
testValue(9, .999);
}
public void testInverseCumulativeProbability990() {
testValue(9, .990);
}
public void testInverseCumulativeProbability975() {
testValue(9, .975);
}
public void testInverseCumulativeProbability950() {
testValue(8, .950);
}
public void testInverseCumulativeProbability900() {
testValue(8, .900);
}
public void testCummulativeProbability1() {
testProbability(1, .00014);
}
public void testCumulativeProbability2() {
testProbability(2, .00159);
}
public void testCumulativeProbability3() {
testProbability(3, .01059);
}
public void testCumulativeProbability4() {
testProbability(4, .04735);
}
public void testCumulativeProbability9() {
testProbability(9, .97175);
}
public void testCummulativeProbability8() {
testProbability(8, .85069);
}
private void testProbability(int x, double expected){
double actual = b.cummulativeProbability(x);
assertEquals(expected, actual, 10e-4);
}
private void testValue(int expected, double p){
int actual = b.inverseCummulativeProbability(p);
assertEquals(expected, actual);
assertTrue(b.cummulativeProbability(actual) <= p);
assertTrue(b.cummulativeProbability(actual + 1) >= p);
}
}

View File

@ -197,4 +197,60 @@ public class DistributionFactoryImplTest extends TestCase {
fail("positive degrees of freedom. IllegalArgumentException is not expected");
}
}
public void testBinomialDistributionNegativePositive(){
try {
factory.createBinomailDistribution(-1, 0.5);
fail("negative number of trials. IllegalArgumentException expected");
} catch (IllegalArgumentException ex ) {
}
}
public void testBinomialDistributionZeroPositive(){
try {
factory.createBinomailDistribution(0, 0.5);
} catch (IllegalArgumentException ex ) {
fail("zero number of trials. IllegalArgumentException is not expected");
}
}
public void testBinomialDistributionPositivePositive(){
try {
factory.createBinomailDistribution(10, 0.5);
} catch (IllegalArgumentException ex ) {
fail("positive number of trials. IllegalArgumentException is not expected");
}
}
public void testBinomialDistributionPositiveNegative(){
try {
factory.createBinomailDistribution(10, -0.5);
fail("negative probability of success. IllegalArgumentException expected");
} catch (IllegalArgumentException ex ) {
}
}
public void testBinomialDistributionPositiveZero(){
try {
factory.createBinomailDistribution(10, 0.0);
} catch (IllegalArgumentException ex ) {
fail("zero probability of success. IllegalArgumentException is not expected");
}
}
public void testBinomialDistributionPositiveOne(){
try {
factory.createBinomailDistribution(10, 1.0);
} catch (IllegalArgumentException ex ) {
fail("valid probability of success. IllegalArgumentException is not expected");
}
}
public void testBinomialDistributionPositiveTwo(){
try {
factory.createBinomailDistribution(10, 2.0);
fail("high probability of success. IllegalArgumentException expected");
} catch (IllegalArgumentException ex ) {
}
}
}