Licensing issues have prompted the removal of all source reliant on

Gamma.java.  Gamma.java explicitly stated that it was "based on" an
implementation from Numerical Recipes in C.  Please see
http://www.nr.com/infotop.html - these implementations (and derivatives
of these works) are covered under restrictive licensing terms.  This
code will be removed from the repository until these issues can be
addressed.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140875 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2003-06-02 03:44:13 +00:00
parent 6aa45e2047
commit d23bf18b40
10 changed files with 0 additions and 1206 deletions

View File

@ -1,77 +0,0 @@
/* ====================================================================
* 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 Chi-Squared Distribution defined by
* http://mathworld.wolfram.com/Chi-SquaredDistribution.html.
*
* Instances of ChiSquaredDistribution objects should be created using
* {@link DistributionFactory#createChiSquareDistribution(double)}
*
* @author Brent Worden
*/
public interface ChiSquaredDistribution extends ContinuousDistribution {
/**
* Modify the degrees of freedom.
* @param degreesOfFreedom the new degrees of freedom.
*/
void setDegreesOfFreedom(double degreesOfFreedom);
/**
* Access the degrees of freedom.
* @return the degrees of freedom.
*/
double getDegreesOfFreedom();
}

View File

@ -1,100 +0,0 @@
/* ====================================================================
* 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;
/**
* @author Brent Worden
*/
public class ChiSquaredDistributionImpl
extends AbstractContinuousDistribution
implements ChiSquaredDistribution {
private GammaDistribution gamma;
public ChiSquaredDistributionImpl(double degreesOfFreedom){
super();
setGamma(DistributionFactory.newInstance().createGammaDistribution(degreesOfFreedom / 2.0, 2.0));
}
public void setDegreesOfFreedom(double degreesOfFreedom) {
getGamma().setAlpha(degreesOfFreedom / 2.0);
}
public double getDegreesOfFreedom() {
return getGamma().getAlpha() * 2.0;
}
public double cummulativeProbability(double x) {
return getGamma().cummulativeProbability(x);
}
public double getDomainLowerBound() {
return Double.MIN_VALUE * getGamma().getBeta();
}
public double getDomainUpperBound() {
return Double.MAX_VALUE;
}
public double getMean() {
return getDegreesOfFreedom();
}
private void setGamma(GammaDistribution gamma) {
this.gamma = gamma;
}
private GammaDistribution getGamma() {
return gamma;
}
}

View File

@ -1,108 +0,0 @@
/* ====================================================================
* 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;
/**
* This factory provids the means to create common statistical distributions.
* The following distributions are supported:<ul>
* <li>Chi-Square</li>
* <li>Gamma</li>
* </ul>
*
* Common usage:<pre>
* DistributionFactory factory = DistributionFactory.newInstance();
*
* // create a Chi-Square distribution with 5 degrees of freedom.
* ChiSquaredDistribution chi = factory.createChiSquareDistribution(5.0);
* </pre>
*
* @author Brent Worden
*/
public abstract class DistributionFactory {
/**
* Default constructor.
*/
protected DistributionFactory() {
super();
}
/**
* Create an instance of a <code>DistributionFactory</code>
* @return a new factory.
*/
public static DistributionFactory newInstance() {
// for now, return the only concrete factory.
// later, allow for a plugable implementation, possible using SPI and
// commons-discovery.
return new DistributionFactoryImpl();
}
/**
* Create a new chi-square distribution with the given degrees of freedom.
* @param degreesOfFreedom degrees of freedom.
* @return a new chi-square distribution.
*/
public abstract ChiSquaredDistribution createChiSquareDistribution(
double degreesOfFreedom
);
/**
* Create a new gamma distribution with the given alpha and beta values.
* @param alpha the shape parameter.
* @param beta the scale parameter.
* @return a new gamma distribution.
*/
public abstract GammaDistribution createGammaDistribution(
double alpha, double beta);
}

View File

@ -1,93 +0,0 @@
/* ====================================================================
* 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;
/**
* A concrete distribution factory. This is the default factory used by
* Commons-Math.
*
* @author Brent Worden
*/
public class DistributionFactoryImpl extends DistributionFactory {
/**
* Default constructor. Package scope to prevent unwanted instantiation.
*/
DistributionFactoryImpl() {
super();
}
/**
* Create a new chi-square distribution with the given degrees of freedom.
* @param degreesOfFreedom degrees of freedom.
* @return a new chi-square distribution.
*/
public ChiSquaredDistribution createChiSquareDistribution(
final double degreesOfFreedom) {
return new ChiSquaredDistributionImpl(degreesOfFreedom);
}
/**
* Create a new gamma distribution the given alpha and beta values.
* @param alpha the shape parameter.
* @param beta the scale parameter.
* @return a new gamma distribution.
*/
public GammaDistribution createGammaDistribution(
double alpha, double beta) {
return new GammaDistributionImpl(alpha, beta);
}
}

View File

@ -1,89 +0,0 @@
/* ====================================================================
* 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 Gamma Distribution defined by
* http://mathworld.wolfram.com/GammaDistribution.html.
*
* Instances of GammaDistribution objects should be created using
* {@link DistributionFactory#createGammaDistribution(double, double)}
*
* @author Brent Worden
*/
public interface GammaDistribution extends ContinuousDistribution {
/**
* Modify the shape parameter, alpha.
* @param alpha the new shape parameter.
*/
void setAlpha(double alpha);
/**
* Access the shape parameter, alpha
* @return alpha.
*/
double getAlpha();
/**
* Modify the scale parameter, beta.
* @param beta the new scale parameter.
*/
void setBeta(double beta);
/**
* Access the scale parameter, beta
* @return beta.
*/
double getBeta();
}

View File

@ -1,166 +0,0 @@
/* ====================================================================
* 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.Gamma;
/**
* The Gamma Distribution defined by
* http://mathworld.wolfram.com/GammaDistribution.html.
* <br/>
* Along with the Gamma Distribution, this class can be used as a Chi-Squared
* Distribution defined by
* http://mathworld.wolfram.com/Chi-SquaredDistribution.html
*
* @author Brent Worden
*/
public class GammaDistributionImpl extends AbstractContinuousDistribution
implements GammaDistribution {
/** The shape parameter. */
private double alpha;
/** The scale parameter. */
private double beta;
/**
* Create a new gamma distribution with the given alpha and beta values.
* @param alpha the shape parameter.
* @param beta the scale parameter.
*/
public GammaDistributionImpl(double alpha, double beta) {
super();
setAlpha(alpha);
setBeta(beta);
}
/**
* For this disbution, X, this method returns P(X &lt; x). This
* implementation is based on the formulas found in Abramowitz and Stegun,
* "Handbook of Mathematical Functions."
* @param x the value at which the CDF is evaluated.
* @return CDF for this distribution.
*/
public double cummulativeProbability(double x) {
double ret;
if (x <= 0.0) {
ret = 0.0;
} else {
ret = Gamma.regularizedGammaP(getAlpha(), x / getBeta());
}
return ret;
}
/**
* Modify the shape parameter, alpha.
* @param alpha the new shape parameter.
*/
public void setAlpha(double alpha) {
if (alpha <= 0.0) {
throw new IllegalArgumentException("alpha must be positive");
}
this.alpha = alpha;
}
/**
* Access the shape parameter, alpha
* @return alpha.
*/
public double getAlpha() {
return alpha;
}
/**
* Modify the scale parameter, beta.
* @param beta the new scale parameter.
*/
public void setBeta(double beta) {
if (beta <= 0.0) {
throw new IllegalArgumentException("beta must be positive");
}
this.beta = beta;
}
/**
* Access the scale parameter, beta
* @return beta.
*/
public double getBeta() {
return beta;
}
/**
* @return
*/
public double getDomainLowerBound() {
return Double.MIN_VALUE;
}
/**
* @return
*/
public double getDomainUpperBound() {
return Double.MAX_VALUE;
}
/**
* @return
*/
public double getMean() {
return getAlpha() * getBeta();
}
}

View File

@ -1,180 +0,0 @@
/* ====================================================================
* 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.special;
import org.apache.commons.math.ConvergenceException;
/**
* This is a utility class that provides computation methods related to the
* Gamma family of functions.
*
* @author Brent Worden
*/
public class Gamma {
/** Maximum number of iteration allowed for iterative methods. */
private static final int MAXIMUM_ITERATIONS = 100;
/** Maximum allowed numerical error. */
private static final double EPSILON = 10e-9;
/**
* Default constructor. Prohibit instantiation.
*/
private Gamma() {
super();
}
/**
* Returns the regularized gamma function P(a, x) defined by
* http://mathworld.wolfram.com/RegularizedGammaFunction.html.
*
* This implementation is based on the formulas and descriptions presented
* in Press, et. al. "Numerical Recipes in C." This implementation uses
* the series representation of the regularized gamma function for all
* values a and x. Later on, this method can be improved upon for certain
* values of a and x using continuous fractions as the series converges
* slowly for x >> a.
*
* @param a ???
* @param x ???
* @return the regularized gamma function P(a, x)
*/
public static double regularizedGammaP(double a, double x) {
double ret;
if (a <= 0.0) {
throw new IllegalArgumentException("a must be positive");
} else if (x <= 0.0) {
throw new IllegalArgumentException("x must be non-negative");
} else {
// calculate series
double n = 0.0; // current element index
double an = 1.0 / a; // n-th element in the series
double sum = an; // partial sum
while (Math.abs(an) > EPSILON && n < MAXIMUM_ITERATIONS) {
// compute next element in the series
n = n + 1.0;
an = an * (x / (a + n));
// update partial sum
sum = sum + an;
}
if (n >= MAXIMUM_ITERATIONS) {
throw new ConvergenceException(
"maximum number of iterations reached");
} else {
ret = Math.exp(-x + (a * Math.log(x)) - logGamma(a)) * sum;
}
}
return ret;
}
/**
* Returns the natural logarithm of the gamma function &#915;(x) defined
* by http://mathworld.wolfram.com/GammaFunction.html
*
* This implementation is based on the formulas and descriptions presented
* in Press, et. al. "Numerical Recipes in C" and the Lanczos coefficient
* research conducted by Paul Godfrey.
*
* @param x ???
* @return log(&#915;(x))
*/
public static double logGamma(double x) {
double ret;
if (x <= 0.0) {
throw new IllegalArgumentException(
"x must be non-negative");
} else {
double g = 607.0 / 128.0;
// Lanczos coefficients
double[] c =
{
0.99999999999999709182,
57.156235665862923517,
-59.597960355475491248,
14.136097974741747174,
-0.49191381609762019978,
.33994649984811888699e-4,
.46523628927048575665e-4,
-.98374475304879564677e-4,
.15808870322491248884e-3,
-.21026444172410488319e-3,
.21743961811521264320e-3,
-.16431810653676389022e-3,
.84418223983852743293e-4,
-.26190838401581408670e-4,
.36899182659531622704e-5,
};
double sum = 0.0;
for (int i = 1; i < c.length; ++i) {
sum = sum + (c[i] / (x + i));
}
sum = sum + c[0];
double tmp = x + g + .5;
ret =
(x + .5) * Math.log(tmp)
- tmp
+ (.5 * Math.log(2.0 * Math.PI))
+ Math.log(sum / x);
}
return ret;
}
}

View File

@ -1,129 +0,0 @@
/* ====================================================================
* 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 ChiSquareDistributionTest extends TestCase {
private ChiSquaredDistribution chiSquare;
/**
* Constructor for ChiSquareDistributionTest.
* @param name
*/
public ChiSquareDistributionTest(String name) {
super(name);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
chiSquare = DistributionFactory.newInstance().createChiSquareDistribution(5.0);
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
chiSquare = null;
super.tearDown();
}
public void testLowerTailProbability(){
testProbability( .210, .001);
testProbability( .554, .010);
testProbability( .831, .025);
testProbability(1.145, .050);
testProbability(1.610, .100);
}
public void testUpperTailProbability(){
testProbability(20.515, .999);
testProbability(15.086, .990);
testProbability(12.833, .975);
testProbability(11.070, .950);
testProbability( 9.236, .900);
}
public void testLowerTailValues(){
testValue(.001, .210);
testValue(.010, .554);
testValue(.025, .831);
testValue(.050, 1.145);
testValue(.100, 1.610);
}
public void testUpperTailValues(){
testValue(.999, 20.515);
testValue(.990, 15.086);
testValue(.975, 12.833);
testValue(.950, 11.070);
testValue(.900, 9.236);
}
private void testProbability(double x, double expected){
double actual = chiSquare.cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
}
private void testValue(double p, double expected){
double actual = chiSquare.inverseCummulativeProbability(p);
assertEquals("value for " + p, expected, actual, 10e-4);
}
}

View File

@ -1,183 +0,0 @@
/* ====================================================================
* 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 DistributionFactoryImplTest extends TestCase {
/** */
private DistributionFactory factory;
/**
* Constructor for DistributionFactoryImplTest.
* @param name
*/
public DistributionFactoryImplTest(String name) {
super(name);
}
/**
* @throws java.lang.Exception
*/
protected void setUp() throws Exception {
super.setUp();
factory = new DistributionFactoryImpl();
}
/**
* @throws java.lang.Exception
*/
protected void tearDown() throws Exception {
factory = null;
super.tearDown();
}
public void testCreateChiSquareDistributionNegative(){
try {
factory.createChiSquareDistribution(-1.0);
fail("negative degrees of freedom. IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
public void testCreateChiSquareDistributionZero(){
try {
factory.createChiSquareDistribution(0.0);
fail("zero degrees of freedom. IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
public void testCreateChiSquareDistributionPositive(){
try {
factory.createChiSquareDistribution(1.0);
} catch (IllegalArgumentException ex) {
fail("positive degrees of freedom. IllegalArgumentException is not expected");
}
}
public void testCreateGammaDistributionNegativePositive(){
try {
factory.createGammaDistribution(-1.0, 1.0);
fail("negative alpha. IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
public void testCreateGammaDistributionZeroPositive(){
try {
factory.createGammaDistribution(0.0, 1.0);
fail("zero alpha. IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
public void testCreateGammaDistributionPositiveNegative(){
try {
factory.createGammaDistribution(1.0, -1.0);
fail("negative beta. IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
public void testCreateGammaDistributionPositiveZero(){
try {
factory.createGammaDistribution(1.0, 0.0);
fail("zero beta. IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
;
}
}
public void testCreateGammaDistributionPositivePositive(){
try {
factory.createGammaDistribution(1.0, 1.0);
} catch (IllegalArgumentException ex) {
fail("positive alpah and beta. IllegalArgumentException is not expected");
}
}
//
// public void testCreateTDistributionNegative(){
// try {
// factory.createTDistribution(-1.0);
// fail("negative degrees of freedom. IllegalArgumentException expected");
// } catch (IllegalArgumentException ex) {
// ;
// }
// }
//
// public void testCreateTDistributionZero(){
// try {
// factory.createTDistribution(0.0);
// fail("zero degrees of freedom. IllegalArgumentException expected");
// } catch (IllegalArgumentException ex) {
// ;
// }
// }
//
// public void testCreateTDistributionPositive(){
// try {
// factory.createTDistribution(1.0);
// } catch (IllegalArgumentException ex) {
// fail("positive degrees of freedom. IllegalArgumentException is not expected");
// }
// }
}

View File

@ -1,81 +0,0 @@
/* ====================================================================
* 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 GammaDistributionTest extends TestCase {
/**
* Constructor for ChiSquareDistributionTest.
* @param name
*/
public GammaDistributionTest(String name) {
super(name);
}
public void testProbabilities(){
testProbability(15.5, 4.0, 2.0, .9499);
testProbability( 0.5, 4.0, 1.0, .0018);
testProbability(10.0, 1.0, 2.0, .9933);
testProbability( 5.0, 2.0, 2.0, .7127);
}
private void testProbability(double x, double a, double b, double expected){
double actual = DistributionFactory.newInstance().createGammaDistribution(a, b).cummulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
}
}