mdiggory 2003/06/05 07:02:29 Modified: math/src/java/org/apache/commons/math/stat/distribution ContinuousDistribution.java AbstractContinuousDistribution.java Added: math/src/java/org/apache/commons/math/stat/distribution ChiSquaredDistribution.java GammaDistribution.java DistributionFactory.java GammaDistributionImpl.java ChiSquaredDistributionImpl.java DistributionFactoryImpl.java Log: PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20488 Submitted by: Brent Worden Initial submission of the distribution library Revision Changes Path 1.2 +83 -98 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ContinuousDistribution.java Index: ContinuousDistribution.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ContinuousDistribution.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ContinuousDistribution.java 29 May 2003 19:34:38 -0000 1.1 +++ ContinuousDistribution.java 5 Jun 2003 14:02:28 -0000 1.2 @@ -1,98 +1,83 @@ -/* ==================================================================== - * 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 [EMAIL PROTECTED] - * - * 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 continuous distributions. - * - * @author Brent Worden - */ -public interface ContinuousDistribution { - /** - * For this disbution, X, this method returns P(X < x). - * @param x the value at which the CDF is evaluated. - * @return CDF for this distribution. - */ - double cummulativeProbability(double x); - - /** - * For this disbution, X, this method returns P(x0 < X < x1). - * @param x0 the lower bound - * @param x1 the upper bound - * @return the cummulative probability. - */ - double cummulativeProbability(double x0, double x1); - - /** - * For this disbution, X, this method returns x such that P(X < x) = p. - * @param p the cummulative probability. - * @return x. - */ - double inverseCummulativeProbability(double p); - - /** - * - */ - double getMean(); - - /** - * - */ - double getDomainLowerBound(); - - /** - * - */ - double getDomainUpperBound(); -} +/* ==================================================================== + * 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 [EMAIL PROTECTED] + * + * 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 continuous distributions. + * + * @author Brent Worden + */ +public interface ContinuousDistribution { + /** + * For this disbution, X, this method returns P(X < x). + * @param x the value at which the CDF is evaluated. + * @return CDF for this distribution. + */ + double cummulativeProbability(double x); + + /** + * For this disbution, X, this method returns P(x0 < X < x1). + * @param x0 the lower bound + * @param x1 the upper bound + * @return the cummulative probability. + */ + double cummulativeProbability(double x0, double x1); + + /** + * For this disbution, X, this method returns x such that P(X < x) = p. + * @param p the cummulative probability. + * @return x. + */ + double inverseCummulativeProbability(double p); +} 1.2 +153 -113 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/AbstractContinuousDistribution.java Index: AbstractContinuousDistribution.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/AbstractContinuousDistribution.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractContinuousDistribution.java 29 May 2003 19:34:38 -0000 1.1 +++ AbstractContinuousDistribution.java 5 Jun 2003 14:02:29 -0000 1.2 @@ -1,113 +1,153 @@ -/* ==================================================================== - * 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 [EMAIL PROTECTED] - * - * 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.RootFinding; -import org.apache.commons.math.UnivariateFunction; - -/** - * Base class for various continuous distributions. It provides default - * implementations for some of the methods that do not vary from distribution - * to distribution. - * - * @author Brent Worden - */ -public abstract class AbstractContinuousDistribution - implements ContinuousDistribution { - - /** - * Default constructor. - */ - protected AbstractContinuousDistribution() { - super(); - } - - /** - * For this disbution, X, this method returns P(x0 < X < x1). This - * is accomplished by using the equality P(x0 < X < x1) = - * P(X < x1) - P(X < x0). - * - * @param x0 the lower bound - * @param x1 the upper bound - * @return the cummulative probability. - */ - public double cummulativeProbability(double x0, double x1) { - return cummulativeProbability(x1) - cummulativeProbability(x0); - } - - /** - * - */ - public double 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 root finding using bracketing and bisection. - // subclasses can overide if there is a better method. - UnivariateFunction rootFindingFunction = new UnivariateFunction() { - public double evaluate(double x) { - return cummulativeProbability(x) - p; - } - }; - - // bracket root - double[] bracket = RootFinding.bracket(rootFindingFunction, getMean(), getDomainLowerBound(), getDomainUpperBound()); - - // find root - double root = RootFinding.bisection(rootFindingFunction, bracket[0], bracket[1]); - - return root; - } -} +/* ==================================================================== + * 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 [EMAIL PROTECTED] + * + * 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.RootFinding; +import org.apache.commons.math.UnivariateFunction; + +/** + * Base class for various continuous distributions. It provides default + * implementations for some of the methods that do not vary from distribution + * to distribution. + * + * @author Brent Worden + */ +public abstract class AbstractContinuousDistribution + implements ContinuousDistribution { + + /** + * Default constructor. + */ + protected AbstractContinuousDistribution() { + super(); + } + + /** + * For this distribution, X, this method returns P(x0 < X < x1). This + * is accomplished by using the equality P(x0 < X < x1) = + * P(X < x1) - P(X < x0). + * + * @param x0 the lower bound + * @param x1 the upper bound + * @return the cummulative probability. + */ + public double cummulativeProbability(double x0, double x1) { + return cummulativeProbability(x1) - cummulativeProbability(x0); + } + + /** + * For this distribution, X, this method returns the critical point x, such + * that P(X < x) = <code>p</code>. + * + * @param p the desired probability + * @return x, such that P(X < x) = <code>p</code> + */ + public double 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 root finding using bracketing and bisection. + // subclasses can overide if there is a better method. + UnivariateFunction rootFindingFunction = new UnivariateFunction() { + public double evaluate(double x) { + return cummulativeProbability(x) - p; + } + }; + + // bracket root + double[] bracket = RootFinding.bracket(rootFindingFunction, + getInitialDomain(p), getDomainLowerBound(p), + getDomainUpperBound(p)); + + // find root + double root = RootFinding.bisection(rootFindingFunction, bracket[0], + bracket[1]); + + return root; + } + + /** + * Access the initial domain value, based on <code>p</code>, used to + * bracket a CDF root. This method is used by + * [EMAIL PROTECTED] #inverseCummulativeProbability(double)} to find critical values. + * + * @param p the desired probability for the critical value + * @return initial domain value + */ + protected abstract double getInitialDomain(double p); + + /** + * Access the domain value lower bound, based on <code>p</code>, used to + * bracket a CDF root. This method is used by + * [EMAIL PROTECTED] #inverseCummulativeProbability(double)} to find critical values. + * + * @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 abstract double getDomainLowerBound(double p); + + /** + * Access the domain value upper bound, based on <code>p</code>, used to + * bracket a CDF root. This method is used by + * [EMAIL PROTECTED] #inverseCummulativeProbability(double)} to find critical values. + * + * @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 abstract double getDomainUpperBound(double p); +} 1.3 +86 -76 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ChiSquaredDistribution.java 1.3 +99 -89 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/GammaDistribution.java 1.3 +113 -108 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactory.java 1.3 +218 -166 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/GammaDistributionImpl.java 1.3 +129 -100 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ChiSquaredDistributionImpl.java 1.3 +93 -93 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactoryImpl.java
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]