fixed spelling error: cummulative -> cumulative.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141100 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a3c8daf76c
commit
70106770ea
|
@ -62,7 +62,7 @@ import org.apache.commons.math.analysis.UnivariateRealSolverUtils;
|
|||
* implementations for some of the methods that do not vary from distribution
|
||||
* to distribution.
|
||||
*
|
||||
* @version $Revision: 1.17 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.18 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public abstract class AbstractContinuousDistribution
|
||||
implements ContinuousDistribution {
|
||||
|
@ -81,11 +81,11 @@ public abstract class AbstractContinuousDistribution
|
|||
*
|
||||
* @param x0 the lower bound
|
||||
* @param x1 the upper bound
|
||||
* @return the cummulative probability.
|
||||
* @return the cumulative probability.
|
||||
*/
|
||||
public double cummulativeProbability(double x0, double x1)
|
||||
public double cumulativeProbability(double x0, double x1)
|
||||
throws MathException {
|
||||
return cummulativeProbability(x1) - cummulativeProbability(x0);
|
||||
return cumulativeProbability(x1) - cumulativeProbability(x0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,7 +95,7 @@ public abstract class AbstractContinuousDistribution
|
|||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCummulativeProbability(final double p)
|
||||
public double inverseCumulativeProbability(final double p)
|
||||
throws MathException {
|
||||
if (p < 0.0 || p > 1.0) {
|
||||
throw new IllegalArgumentException("p must be between 0.0 and 1.0, inclusive.");
|
||||
|
@ -107,7 +107,7 @@ public abstract class AbstractContinuousDistribution
|
|||
new UnivariateRealFunction() {
|
||||
|
||||
public double value(double x) throws MathException {
|
||||
return cummulativeProbability(x) - p;
|
||||
return cumulativeProbability(x) - p;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -132,7 +132,7 @@ public abstract class AbstractContinuousDistribution
|
|||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
|
@ -142,7 +142,7 @@ public abstract class AbstractContinuousDistribution
|
|||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
|
@ -153,7 +153,7 @@ public abstract class AbstractContinuousDistribution
|
|||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
|
|
|
@ -61,7 +61,7 @@ import org.apache.commons.math.MathException;
|
|||
* implementations for some of the methods that do not vary from distribution
|
||||
* to distribution.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public abstract class AbstractDiscreteDistribution
|
||||
implements DiscreteDistribution {
|
||||
|
@ -77,11 +77,11 @@ public abstract class AbstractDiscreteDistribution
|
|||
* For this disbution, X, this method returns P(x0 ≤ X ≤ x1).
|
||||
* @param x0 the inclusive, lower bound
|
||||
* @param x1 the inclusive, upper bound
|
||||
* @return the cummulative probability.
|
||||
* @return the cumulative probability.
|
||||
*/
|
||||
public double cummulativeProbability(int x0, int x1) throws MathException{
|
||||
return cummulativeProbability(x1) -
|
||||
cummulativeProbability(x0 - 1);
|
||||
public double cumulativeProbability(int x0, int x1) throws MathException{
|
||||
return cumulativeProbability(x1) -
|
||||
cumulativeProbability(x0 - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -91,7 +91,7 @@ public abstract class AbstractDiscreteDistribution
|
|||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public int inverseCummulativeProbability(final double p) throws MathException{
|
||||
public int inverseCumulativeProbability(final double p) throws MathException{
|
||||
if (p < 0.0 || p > 1.0) {
|
||||
throw new IllegalArgumentException(
|
||||
"p must be between 0.0 and 1.0, inclusive.");
|
||||
|
@ -104,7 +104,7 @@ public abstract class AbstractDiscreteDistribution
|
|||
double pm;
|
||||
while (x0 < x1) {
|
||||
int xm = x0 + (x1 - x0) / 2;
|
||||
pm = cummulativeProbability(xm);
|
||||
pm = cumulativeProbability(xm);
|
||||
if (pm > p) {
|
||||
// update x1
|
||||
if (xm == x1) {
|
||||
|
@ -129,10 +129,10 @@ public abstract class AbstractDiscreteDistribution
|
|||
}
|
||||
|
||||
// insure x0 is the correct critical point
|
||||
pm = cummulativeProbability(x0);
|
||||
pm = cumulativeProbability(x0);
|
||||
while (pm > p) {
|
||||
--x0;
|
||||
pm = cummulativeProbability(x0);
|
||||
pm = cumulativeProbability(x0);
|
||||
}
|
||||
|
||||
return x0;
|
||||
|
@ -141,7 +141,7 @@ public abstract class AbstractDiscreteDistribution
|
|||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a PDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
|
@ -152,7 +152,7 @@ public abstract class AbstractDiscreteDistribution
|
|||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a PDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
|
|
|
@ -62,7 +62,7 @@ import org.apache.commons.math.util.MathUtils;
|
|||
/**
|
||||
* The default implementation of {@link BinomialDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class BinomialDistributionImpl
|
||||
extends AbstractDiscreteDistribution
|
||||
|
@ -153,7 +153,7 @@ public class BinomialDistributionImpl
|
|||
* @param x the value at which the PDF is evaluated.
|
||||
* @return PDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(int x) throws MathException {
|
||||
public double cumulativeProbability(int x) throws MathException {
|
||||
double ret;
|
||||
if (x < 0) {
|
||||
ret = 0.0;
|
||||
|
|
|
@ -60,7 +60,7 @@ import org.apache.commons.math.MathException;
|
|||
/**
|
||||
* The default implementation of {@link ChiSquaredDistribution}
|
||||
*
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class ChiSquaredDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
|
@ -100,14 +100,14 @@ public class ChiSquaredDistributionImpl
|
|||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(double x) throws MathException {
|
||||
return getGamma().cummulativeProbability(x);
|
||||
public double cumulativeProbability(double x) throws MathException {
|
||||
return getGamma().cumulativeProbability(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
|
@ -120,7 +120,7 @@ public class ChiSquaredDistributionImpl
|
|||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
|
@ -146,7 +146,7 @@ public class ChiSquaredDistributionImpl
|
|||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
|
|
|
@ -58,7 +58,7 @@ import org.apache.commons.math.MathException;
|
|||
/**
|
||||
* Base interface for various continuous distributions.
|
||||
*
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public interface ContinuousDistribution {
|
||||
/**
|
||||
|
@ -66,20 +66,20 @@ public interface ContinuousDistribution {
|
|||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF for this distribution.
|
||||
*/
|
||||
double cummulativeProbability(double x) throws MathException;
|
||||
double cumulativeProbability(double x) throws MathException;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return the cumulative probability.
|
||||
*/
|
||||
double cummulativeProbability(double x0, double x1) throws MathException;
|
||||
double cumulativeProbability(double x0, double x1) throws MathException;
|
||||
|
||||
/**
|
||||
* For this disbution, X, this method returns x such that P(X < x) = p.
|
||||
* @param p the cummulative probability.
|
||||
* @param p the cumulative probability.
|
||||
* @return x.
|
||||
*/
|
||||
double inverseCummulativeProbability(double p) throws MathException;
|
||||
double inverseCumulativeProbability(double p) throws MathException;
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ import org.apache.commons.math.MathException;
|
|||
/**
|
||||
* Base interface for various discrete distributions.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.9 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public interface DiscreteDistribution {
|
||||
/**
|
||||
|
@ -73,20 +73,20 @@ public interface DiscreteDistribution {
|
|||
* @param x the value at which the PDF is evaluated.
|
||||
* @return PDF for this distribution.
|
||||
*/
|
||||
double cummulativeProbability(int x) throws MathException;
|
||||
double cumulativeProbability(int x) throws MathException;
|
||||
|
||||
/**
|
||||
* For this disbution, X, this method returns P(x0 ≤ X ≤ x1).
|
||||
* @param x0 the inclusive, lower bound
|
||||
* @param x1 the inclusive, upper bound
|
||||
* @return the cummulative probability.
|
||||
* @return the cumulative probability.
|
||||
*/
|
||||
double cummulativeProbability(int x0, int x1) throws MathException;
|
||||
double cumulativeProbability(int x0, int x1) throws MathException;
|
||||
|
||||
/**
|
||||
* For this disbution, X, this method returns x such that P(X ≤ x) <= p.
|
||||
* @param p the cummulative probability.
|
||||
* @param p the cumulative probability.
|
||||
* @return x.
|
||||
*/
|
||||
int inverseCummulativeProbability(double p) throws MathException;
|
||||
int inverseCumulativeProbability(double p) throws MathException;
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ import org.apache.commons.math.MathException;
|
|||
/**
|
||||
* The default implementation of {@link ExponentialDistribution}
|
||||
*
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.12 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class ExponentialDistributionImpl
|
||||
implements ExponentialDistribution, Serializable {
|
||||
|
@ -109,7 +109,7 @@ public class ExponentialDistributionImpl
|
|||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(double x) throws MathException{
|
||||
public double cumulativeProbability(double x) throws MathException{
|
||||
double ret;
|
||||
if (x <= 0.0) {
|
||||
ret = 0.0;
|
||||
|
@ -126,7 +126,7 @@ public class ExponentialDistributionImpl
|
|||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCummulativeProbability(double p) throws MathException{
|
||||
public double inverseCumulativeProbability(double p) throws MathException{
|
||||
double ret;
|
||||
|
||||
if (p < 0.0 || p > 1.0) {
|
||||
|
@ -144,9 +144,9 @@ public class ExponentialDistributionImpl
|
|||
* 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.
|
||||
* @return the cumulative probability.
|
||||
*/
|
||||
public double cummulativeProbability(double x0, double x1) throws MathException{
|
||||
return cummulativeProbability(x1) - cummulativeProbability(x0);
|
||||
public double cumulativeProbability(double x0, double x1) throws MathException{
|
||||
return cumulativeProbability(x1) - cumulativeProbability(x0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ import org.apache.commons.math.special.Beta;
|
|||
* Default implementation of
|
||||
* {@link org.apache.commons.math.distribution.FDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class FDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
|
@ -99,7 +99,7 @@ public class FDistributionImpl
|
|||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(double x) throws MathException {
|
||||
public double cumulativeProbability(double x) throws MathException {
|
||||
double ret;
|
||||
if (x <= 0.0) {
|
||||
ret = 0.0;
|
||||
|
@ -117,7 +117,7 @@ public class FDistributionImpl
|
|||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
|
@ -130,7 +130,7 @@ public class FDistributionImpl
|
|||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
|
@ -143,7 +143,7 @@ public class FDistributionImpl
|
|||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
|
|
|
@ -61,7 +61,7 @@ import org.apache.commons.math.special.Gamma;
|
|||
/**
|
||||
* The default implementation of {@link GammaDistribution}
|
||||
*
|
||||
* @version $Revision: 1.15 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.16 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class GammaDistributionImpl extends AbstractContinuousDistribution
|
||||
implements GammaDistribution, Serializable {
|
||||
|
@ -98,7 +98,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
|
|||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(double x) throws MathException{
|
||||
public double cumulativeProbability(double x) throws MathException{
|
||||
double ret;
|
||||
|
||||
if (x <= 0.0) {
|
||||
|
@ -151,7 +151,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
|
|||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
|
@ -165,7 +165,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
|
|||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
|
@ -192,7 +192,7 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
|
|||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
|
|
|
@ -62,7 +62,7 @@ import org.apache.commons.math.util.MathUtils;
|
|||
/**
|
||||
* The default implementation of {@link HypergeometricDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.9 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class HypergeometricDistributionImpl extends AbstractDiscreteDistribution
|
||||
implements HypergeometricDistribution, Serializable
|
||||
|
@ -98,7 +98,7 @@ public class HypergeometricDistributionImpl extends AbstractDiscreteDistribution
|
|||
* @param x the value at which the PDF is evaluated.
|
||||
* @return PDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(int x) throws MathException{
|
||||
public double cumulativeProbability(int x) throws MathException{
|
||||
double ret;
|
||||
|
||||
int n = getPopulationSize();
|
||||
|
|
|
@ -1,102 +1,102 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.distribution;
|
||||
|
||||
/**
|
||||
* Normal (Gauss) Distribution.
|
||||
* Instances of NormalDistribution objects should be created using
|
||||
* {@link DistributionFactory#createNormalDistribution(double, double)}.<p>
|
||||
*
|
||||
* References:<p>
|
||||
* <ul>
|
||||
* <li><a href="http://mathworld.wolfram.com/NormalDistribution.html">
|
||||
* Normal Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
*/
|
||||
public interface NormalDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
* Access the mean.
|
||||
* @return mean for this distribution
|
||||
*/
|
||||
double getMean();
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean for this distribution
|
||||
*/
|
||||
void setMean(double mean);
|
||||
/**
|
||||
* Access the standard deviation.
|
||||
* @return standard deviation for this distribution
|
||||
*/
|
||||
double getStandardDeviation();
|
||||
/**
|
||||
* Modify the standard deviation.
|
||||
* @param sd standard deviation for this distribution
|
||||
*/
|
||||
void setStandardDeviation(double sd);
|
||||
|
||||
/**
|
||||
* Access algorithm used to calculate cummulative probability
|
||||
* @return cdfAlgorithm the value of cummulative probability
|
||||
*/
|
||||
public NormalCDFAlgorithm getCdfAlgorithm();
|
||||
|
||||
/**
|
||||
* Modify the algorithm used to calculate cummulative probability
|
||||
* @param normalCDF the algorithm used to calculate cummulative probability
|
||||
*/
|
||||
public void setCdfAlgorithm(NormalCDFAlgorithm normalCDF);
|
||||
}
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.distribution;
|
||||
|
||||
/**
|
||||
* Normal (Gauss) Distribution.
|
||||
* Instances of NormalDistribution objects should be created using
|
||||
* {@link DistributionFactory#createNormalDistribution(double, double)}.<p>
|
||||
*
|
||||
* References:<p>
|
||||
* <ul>
|
||||
* <li><a href="http://mathworld.wolfram.com/NormalDistribution.html">
|
||||
* Normal Distribution</a></li>
|
||||
* </ul>
|
||||
*
|
||||
*/
|
||||
public interface NormalDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
* Access the mean.
|
||||
* @return mean for this distribution
|
||||
*/
|
||||
double getMean();
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean for this distribution
|
||||
*/
|
||||
void setMean(double mean);
|
||||
/**
|
||||
* Access the standard deviation.
|
||||
* @return standard deviation for this distribution
|
||||
*/
|
||||
double getStandardDeviation();
|
||||
/**
|
||||
* Modify the standard deviation.
|
||||
* @param sd standard deviation for this distribution
|
||||
*/
|
||||
void setStandardDeviation(double sd);
|
||||
|
||||
/**
|
||||
* Access algorithm used to calculate cumulative probability
|
||||
* @return cdfAlgorithm the value of cumulative probability
|
||||
*/
|
||||
public NormalCDFAlgorithm getCdfAlgorithm();
|
||||
|
||||
/**
|
||||
* Modify the algorithm used to calculate cumulative probability
|
||||
* @param normalCDF the algorithm used to calculate cumulative probability
|
||||
*/
|
||||
public void setCdfAlgorithm(NormalCDFAlgorithm normalCDF);
|
||||
}
|
||||
|
|
|
@ -1,290 +1,290 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.distribution;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Default implementation of
|
||||
* {@link org.apache.commons.math.distribution.NormalDistribution}.<p>
|
||||
* You can choose the algorithm used to calculate cummulative probability
|
||||
* using method {@link #setCdfAlgorithm}. The deafault is the Cody algorithm
|
||||
* {@link org.apache.commons.math.distribution.NormalCDFPreciseAlgorithm}
|
||||
*/
|
||||
public class NormalDistributionImpl extends AbstractContinuousDistribution
|
||||
implements NormalDistribution, Serializable {
|
||||
private double mean = 0;
|
||||
private double standardDeviation = 1;
|
||||
private NormalCDFAlgorithm cdfAlgorithm = new NormalCDFPreciseAlgorithm();
|
||||
|
||||
/**
|
||||
* Create a normal distribution using the given mean and standard deviation.
|
||||
* @param mean mean for this distribution
|
||||
* @param sd standard deviation for this distribution
|
||||
*/
|
||||
public NormalDistributionImpl(double mean, double sd){
|
||||
super();
|
||||
setMean(mean);
|
||||
setStandardDeviation(sd);
|
||||
}
|
||||
/**
|
||||
* Creates normal distribution with the mean equal to zero and standard
|
||||
* deviation equal to one.
|
||||
*/
|
||||
public NormalDistributionImpl(){
|
||||
super();
|
||||
setMean(0.0);
|
||||
setStandardDeviation(1.0);
|
||||
}
|
||||
/**
|
||||
* Access the mean.
|
||||
* @return mean for this distribution
|
||||
*/
|
||||
public double getMean() {
|
||||
return mean;
|
||||
}
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean for this distribution
|
||||
*/
|
||||
public void setMean(double mean) {
|
||||
this.mean = mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the standard deviation.
|
||||
* @return standard deviation for this distribution
|
||||
*/
|
||||
public double getStandardDeviation() {
|
||||
return standardDeviation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the standard deviation.
|
||||
* @param sd standard deviation for this distribution
|
||||
*/
|
||||
public void setStandardDeviation(double sd) {
|
||||
if (sd < 0.0) {
|
||||
throw new IllegalArgumentException("Standard deviation must be" +
"positive or zero.");
|
||||
}
|
||||
standardDeviation = sd;
|
||||
}
|
||||
|
||||
/**
|
||||
* For this disbution, X, this method returns P(X < <code>x</code>).
|
||||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF evaluted at <code>x</code>.
|
||||
*/
|
||||
public double cummulativeProbability(double x) {
|
||||
double z = x;
|
||||
if(standardDeviation > 0){
|
||||
z = (x - mean)/standardDeviation;
|
||||
}else{
|
||||
return 0.0;
|
||||
}
|
||||
return cdfAlgorithm.cdf(z);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For this distribution, X, this method returns the critical point x, such
|
||||
* that P(X < x) = <code>p</code>.<p>
|
||||
* Provided implementation is adopted from
|
||||
* <a href="http://www.r-project.org/">R statistical package</a> function
|
||||
* <code>qnorm(...)</code>.<p>
|
||||
* References:
|
||||
* <ul>
|
||||
* <li>
|
||||
* Beasley, J. D. and S. G. Springer (1977).
|
||||
* <a href="http://lib.stat.cmu.edu/apstat/111">
|
||||
* Algorithm AS 111: The percentage points of the normal distribution</a>,
|
||||
* Applied Statistics, 26, 118-121.
|
||||
* </li>
|
||||
* <li>
|
||||
* Wichura, M.J. (1988).
|
||||
* <a href="http://lib.stat.cmu.edu/apstat/241">
|
||||
* Algorithm AS 241: The Percentage Points of the Normal Distribution.</a>
|
||||
* Applied Statistics, 37, 477-484.
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCummulativeProbability(double p) {
|
||||
if (p < 0.0 || p > 1.0) {
|
||||
throw new IllegalArgumentException("p must be between 0.0 and 1.0, inclusive.");
|
||||
}
|
||||
|
||||
//TODO is this ok?
|
||||
if(standardDeviation == 0){
|
||||
return mean;
|
||||
}
|
||||
|
||||
double r, val;
|
||||
double q = p - 0.5;
|
||||
|
||||
if (Math.abs(q) <= .425) {/* 0.075 <= p <= 0.925 */
|
||||
r = 0.180625 - q*q;
|
||||
val =
|
||||
q * (((((((r * 2509.0809287301226727 +
|
||||
33430.575583588128105) * r + 67265.770927008700853) * r +
|
||||
45921.953931549871457) * r + 13731.693765509461125) * r +
|
||||
1971.5909503065514427) * r + 133.14166789178437745) * r +
|
||||
3.387132872796366608)
|
||||
/ (((((((r * 5226.495278852854561 +
|
||||
28729.085735721942674) * r + 39307.89580009271061) * r +
|
||||
21213.794301586595867) * r + 5394.1960214247511077) * r +
|
||||
687.1870074920579083) * r + 42.313330701600911252) * r + 1.);
|
||||
}else { //closer than 0.075 from {0,1} boundary
|
||||
if (q > 0)
|
||||
r = 1 - p;
|
||||
else
|
||||
r = p;
|
||||
r = Math.sqrt(- Math.log(r));
|
||||
if (r <= 5.0) {
|
||||
r += -1.6;
|
||||
val = (((((((r * 7.7454501427834140764e-4 +
|
||||
0.0227238449892691845833) * r + 0.24178072517745061177) *
|
||||
r + 1.27045825245236838258) * r +
|
||||
3.64784832476320460504) * r + 5.7694972214606914055) *
|
||||
r + 4.6303378461565452959) * r +
|
||||
1.42343711074968357734)
|
||||
/ (((((((r *
|
||||
1.05075007164441684324e-9 + 5.475938084995344946e-4) *
|
||||
r + 0.0151986665636164571966) * r +
|
||||
0.14810397642748007459) * r + 0.68976733498510000455) *
|
||||
r + 1.6763848301838038494) * r +
|
||||
2.05319162663775882187) * r + 1.0);
|
||||
}else { //very close to 0 or 1
|
||||
r += -5.;
|
||||
val = (((((((r * 2.01033439929228813265e-7 +
|
||||
2.71155556874348757815e-5) * r +
|
||||
0.0012426609473880784386) * r + 0.026532189526576123093) *
|
||||
r + 0.29656057182850489123) * r +
|
||||
1.7848265399172913358) * r + 5.4637849111641143699) *
|
||||
r + 6.6579046435011037772)
|
||||
/ (((((((r *
|
||||
2.04426310338993978564e-15 + 1.4215117583164458887e-7)*
|
||||
r + 1.8463183175100546818e-5) * r +
|
||||
7.868691311456132591e-4) * r + 0.0148753612908506148525)
|
||||
* r + 0.13692988092273580531) * r +
|
||||
0.59983220655588793769) * r + 1.0);
|
||||
}
|
||||
if(q < 0.0)
|
||||
val = -val;
|
||||
}
|
||||
return mean + standardDeviation*val;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Access algorithm used to calculate cummulative probability
|
||||
* @return cdfAlgorithm the value of cummulative probability
|
||||
*/
|
||||
public NormalCDFAlgorithm getCdfAlgorithm() {
|
||||
return cdfAlgorithm;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Modify the algorithm used to calculate cummulative probability
|
||||
* @param normalCDF the algorithm used to calculate cummulative probability
|
||||
*/
|
||||
public void setCdfAlgorithm(NormalCDFAlgorithm normalCDF) {
|
||||
cdfAlgorithm = normalCDF;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #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 double getDomainLowerBound(double p) {
|
||||
return -Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #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 double getDomainUpperBound(double p) {
|
||||
return Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
*/
|
||||
protected double getInitialDomain(double p) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.distribution;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Default implementation of
|
||||
* {@link org.apache.commons.math.distribution.NormalDistribution}.<p>
|
||||
* You can choose the algorithm used to calculate cumulative probability
|
||||
* using method {@link #setCdfAlgorithm}. The deafault is the Cody algorithm
|
||||
* {@link org.apache.commons.math.distribution.NormalCDFPreciseAlgorithm}
|
||||
*/
|
||||
public class NormalDistributionImpl extends AbstractContinuousDistribution
|
||||
implements NormalDistribution, Serializable {
|
||||
private double mean = 0;
|
||||
private double standardDeviation = 1;
|
||||
private NormalCDFAlgorithm cdfAlgorithm = new NormalCDFPreciseAlgorithm();
|
||||
|
||||
/**
|
||||
* Create a normal distribution using the given mean and standard deviation.
|
||||
* @param mean mean for this distribution
|
||||
* @param sd standard deviation for this distribution
|
||||
*/
|
||||
public NormalDistributionImpl(double mean, double sd){
|
||||
super();
|
||||
setMean(mean);
|
||||
setStandardDeviation(sd);
|
||||
}
|
||||
/**
|
||||
* Creates normal distribution with the mean equal to zero and standard
|
||||
* deviation equal to one.
|
||||
*/
|
||||
public NormalDistributionImpl(){
|
||||
super();
|
||||
setMean(0.0);
|
||||
setStandardDeviation(1.0);
|
||||
}
|
||||
/**
|
||||
* Access the mean.
|
||||
* @return mean for this distribution
|
||||
*/
|
||||
public double getMean() {
|
||||
return mean;
|
||||
}
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean for this distribution
|
||||
*/
|
||||
public void setMean(double mean) {
|
||||
this.mean = mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the standard deviation.
|
||||
* @return standard deviation for this distribution
|
||||
*/
|
||||
public double getStandardDeviation() {
|
||||
return standardDeviation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the standard deviation.
|
||||
* @param sd standard deviation for this distribution
|
||||
*/
|
||||
public void setStandardDeviation(double sd) {
|
||||
if (sd < 0.0) {
|
||||
throw new IllegalArgumentException("Standard deviation must be" +
"positive or zero.");
|
||||
}
|
||||
standardDeviation = sd;
|
||||
}
|
||||
|
||||
/**
|
||||
* For this disbution, X, this method returns P(X < <code>x</code>).
|
||||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF evaluted at <code>x</code>.
|
||||
*/
|
||||
public double cumulativeProbability(double x) {
|
||||
double z = x;
|
||||
if(standardDeviation > 0){
|
||||
z = (x - mean)/standardDeviation;
|
||||
}else{
|
||||
return 0.0;
|
||||
}
|
||||
return cdfAlgorithm.cdf(z);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For this distribution, X, this method returns the critical point x, such
|
||||
* that P(X < x) = <code>p</code>.<p>
|
||||
* Provided implementation is adopted from
|
||||
* <a href="http://www.r-project.org/">R statistical package</a> function
|
||||
* <code>qnorm(...)</code>.<p>
|
||||
* References:
|
||||
* <ul>
|
||||
* <li>
|
||||
* Beasley, J. D. and S. G. Springer (1977).
|
||||
* <a href="http://lib.stat.cmu.edu/apstat/111">
|
||||
* Algorithm AS 111: The percentage points of the normal distribution</a>,
|
||||
* Applied Statistics, 26, 118-121.
|
||||
* </li>
|
||||
* <li>
|
||||
* Wichura, M.J. (1988).
|
||||
* <a href="http://lib.stat.cmu.edu/apstat/241">
|
||||
* Algorithm AS 241: The Percentage Points of the Normal Distribution.</a>
|
||||
* Applied Statistics, 37, 477-484.
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCumulativeProbability(double p) {
|
||||
if (p < 0.0 || p > 1.0) {
|
||||
throw new IllegalArgumentException("p must be between 0.0 and 1.0, inclusive.");
|
||||
}
|
||||
|
||||
//TODO is this ok?
|
||||
if(standardDeviation == 0){
|
||||
return mean;
|
||||
}
|
||||
|
||||
double r, val;
|
||||
double q = p - 0.5;
|
||||
|
||||
if (Math.abs(q) <= .425) {/* 0.075 <= p <= 0.925 */
|
||||
r = 0.180625 - q*q;
|
||||
val =
|
||||
q * (((((((r * 2509.0809287301226727 +
|
||||
33430.575583588128105) * r + 67265.770927008700853) * r +
|
||||
45921.953931549871457) * r + 13731.693765509461125) * r +
|
||||
1971.5909503065514427) * r + 133.14166789178437745) * r +
|
||||
3.387132872796366608)
|
||||
/ (((((((r * 5226.495278852854561 +
|
||||
28729.085735721942674) * r + 39307.89580009271061) * r +
|
||||
21213.794301586595867) * r + 5394.1960214247511077) * r +
|
||||
687.1870074920579083) * r + 42.313330701600911252) * r + 1.);
|
||||
}else { //closer than 0.075 from {0,1} boundary
|
||||
if (q > 0)
|
||||
r = 1 - p;
|
||||
else
|
||||
r = p;
|
||||
r = Math.sqrt(- Math.log(r));
|
||||
if (r <= 5.0) {
|
||||
r += -1.6;
|
||||
val = (((((((r * 7.7454501427834140764e-4 +
|
||||
0.0227238449892691845833) * r + 0.24178072517745061177) *
|
||||
r + 1.27045825245236838258) * r +
|
||||
3.64784832476320460504) * r + 5.7694972214606914055) *
|
||||
r + 4.6303378461565452959) * r +
|
||||
1.42343711074968357734)
|
||||
/ (((((((r *
|
||||
1.05075007164441684324e-9 + 5.475938084995344946e-4) *
|
||||
r + 0.0151986665636164571966) * r +
|
||||
0.14810397642748007459) * r + 0.68976733498510000455) *
|
||||
r + 1.6763848301838038494) * r +
|
||||
2.05319162663775882187) * r + 1.0);
|
||||
}else { //very close to 0 or 1
|
||||
r += -5.;
|
||||
val = (((((((r * 2.01033439929228813265e-7 +
|
||||
2.71155556874348757815e-5) * r +
|
||||
0.0012426609473880784386) * r + 0.026532189526576123093) *
|
||||
r + 0.29656057182850489123) * r +
|
||||
1.7848265399172913358) * r + 5.4637849111641143699) *
|
||||
r + 6.6579046435011037772)
|
||||
/ (((((((r *
|
||||
2.04426310338993978564e-15 + 1.4215117583164458887e-7)*
|
||||
r + 1.8463183175100546818e-5) * r +
|
||||
7.868691311456132591e-4) * r + 0.0148753612908506148525)
|
||||
* r + 0.13692988092273580531) * r +
|
||||
0.59983220655588793769) * r + 1.0);
|
||||
}
|
||||
if(q < 0.0)
|
||||
val = -val;
|
||||
}
|
||||
return mean + standardDeviation*val;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Access algorithm used to calculate cumulative probability
|
||||
* @return cdfAlgorithm the value of cumulative probability
|
||||
*/
|
||||
public NormalCDFAlgorithm getCdfAlgorithm() {
|
||||
return cdfAlgorithm;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Modify the algorithm used to calculate cumulative probability
|
||||
* @param normalCDF the algorithm used to calculate cumulative probability
|
||||
*/
|
||||
public void setCdfAlgorithm(NormalCDFAlgorithm normalCDF) {
|
||||
cdfAlgorithm = normalCDF;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCumulativeProbability(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 double getDomainLowerBound(double p) {
|
||||
return -Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCumulativeProbability(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 double getDomainUpperBound(double p) {
|
||||
return Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
*/
|
||||
protected double getInitialDomain(double p) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ import org.apache.commons.math.special.Beta;
|
|||
* Default implementation of
|
||||
* {@link org.apache.commons.math.distribution.TDistribution}.
|
||||
*
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/29 00:48:58 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class TDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
|
@ -104,7 +104,7 @@ public class TDistributionImpl
|
|||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF evaluted at <code>x</code>.
|
||||
*/
|
||||
public double cummulativeProbability(double x) throws MathException{
|
||||
public double cumulativeProbability(double x) throws MathException{
|
||||
double ret;
|
||||
if (x == 0.0) {
|
||||
ret = 0.5;
|
||||
|
@ -127,7 +127,7 @@ public class TDistributionImpl
|
|||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
|
@ -140,7 +140,7 @@ public class TDistributionImpl
|
|||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
|
@ -153,7 +153,7 @@ public class TDistributionImpl
|
|||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
* {@link #inverseCumulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
|
|
|
@ -87,7 +87,7 @@ import org.apache.commons.math.distribution.TDistribution;
|
|||
* the necessary computations to return the requested statistic.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/29 00:49:01 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class BivariateRegression implements Serializable {
|
||||
|
||||
|
@ -461,7 +461,7 @@ public class BivariateRegression implements Serializable {
|
|||
throw new IllegalArgumentException();
|
||||
}
|
||||
return getSlopeStdErr()
|
||||
* getTDistribution().inverseCummulativeProbability(1d - alpha / 2d);
|
||||
* getTDistribution().inverseCumulativeProbability(1d - alpha / 2d);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -487,7 +487,7 @@ public class BivariateRegression implements Serializable {
|
|||
public double getSignificance() throws MathException {
|
||||
return (
|
||||
1d
|
||||
- getTDistribution().cummulativeProbability(
|
||||
- getTDistribution().cumulativeProbability(
|
||||
Math.abs(getSlope()) / getSlopeStdErr()));
|
||||
}
|
||||
|
||||
|
|
|
@ -69,7 +69,7 @@ import org.apache.commons.collections.TreeBag;
|
|||
* The values are ordered using the default (natural order), unless a <code>Comparator</code>
|
||||
* is supplied in the constructor.
|
||||
*
|
||||
* @version $Revision: 1.13 $ $Date: 2004/02/16 06:32:47 $
|
||||
* @version $Revision: 1.14 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class Frequency implements Serializable {
|
||||
|
||||
|
@ -276,7 +276,7 @@ public class Frequency implements Serializable {
|
|||
//-----------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the cummulative frequency of values less than or equal to v.
|
||||
* Returns the cumulative frequency of values less than or equal to v.
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
*
|
||||
|
@ -310,7 +310,7 @@ public class Frequency implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the cummulative frequency of values less than or equal to v.
|
||||
* Returns the cumulative frequency of values less than or equal to v.
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
*
|
||||
|
@ -322,7 +322,7 @@ public class Frequency implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the cummulative frequency of values less than or equal to v.
|
||||
* Returns the cumulative frequency of values less than or equal to v.
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
*
|
||||
|
@ -334,7 +334,7 @@ public class Frequency implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the cummulative frequency of values less than or equal to v.
|
||||
* Returns the cumulative frequency of values less than or equal to v.
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
*
|
||||
|
@ -348,7 +348,7 @@ public class Frequency implements Serializable {
|
|||
//----------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the cummulative percentatge of values less than or equal to v
|
||||
* Returns the cumulative percentatge of values less than or equal to v
|
||||
* (as a proportion between 0 and 1).
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
|
@ -361,7 +361,7 @@ public class Frequency implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the cummulative percentatge of values less than or equal to v
|
||||
* Returns the cumulative percentatge of values less than or equal to v
|
||||
* (as a proportion between 0 and 1).
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
|
@ -374,7 +374,7 @@ public class Frequency implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the cummulative percentatge of values less than or equal to v
|
||||
* Returns the cumulative percentatge of values less than or equal to v
|
||||
* (as a proportion between 0 and 1).
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
|
@ -387,7 +387,7 @@ public class Frequency implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the cummulative percentatge of values less than or equal to v
|
||||
* Returns the cumulative percentatge of values less than or equal to v
|
||||
* (as a proportion between 0 and 1).
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
|
|
|
@ -64,7 +64,7 @@ import org.apache.commons.math.distribution.ChiSquaredDistribution;
|
|||
/**
|
||||
* Implements test statistics defined in the TestStatistic interface.
|
||||
*
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/25 21:30:41 $
|
||||
* @version $Revision: 1.12 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
public class TestStatisticImpl implements TestStatistic, Serializable {
|
||||
|
||||
|
@ -115,7 +115,7 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
DistributionFactory.newInstance().createChiSquareDistribution(
|
||||
(double) expected.length - 1);
|
||||
return 1
|
||||
- chiSquaredDistribution.cummulativeProbability(
|
||||
- chiSquaredDistribution.cumulativeProbability(
|
||||
chiSquare(expected, observed));
|
||||
}
|
||||
|
||||
|
@ -450,7 +450,7 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
TDistribution tDistribution =
|
||||
DistributionFactory.newInstance().createTDistribution(
|
||||
df(v1, v2, n1, n2));
|
||||
return 1.0 - tDistribution.cummulativeProbability(-t, t);
|
||||
return 1.0 - tDistribution.cumulativeProbability(-t, t);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -467,6 +467,6 @@ public class TestStatisticImpl implements TestStatistic, Serializable {
|
|||
double t = Math.abs(t(m, mu, v, n));
|
||||
TDistribution tDistribution =
|
||||
DistributionFactory.newInstance().createTDistribution(n - 1);
|
||||
return 1.0 - tDistribution.cummulativeProbability(-t, t);
|
||||
return 1.0 - tDistribution.cumulativeProbability(-t, t);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ import org.apache.commons.math.MathException;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class BinomialDistributionTest extends TestCase {
|
||||
private BinomialDistribution b;
|
||||
|
@ -90,7 +90,7 @@ public class BinomialDistributionTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability001() {
|
||||
public void testInverseCumulativeProbability001() {
|
||||
testValue(1, .001);
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ public class BinomialDistributionTest extends TestCase {
|
|||
testValue(4, .100);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability999() {
|
||||
public void testInverseCumulativeProbability999() {
|
||||
testValue(9, .999);
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,7 @@ public class BinomialDistributionTest extends TestCase {
|
|||
testValue(8, .900);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability1() {
|
||||
public void testCumulativeProbability1() {
|
||||
testProbability(1, .00014);
|
||||
}
|
||||
|
||||
|
@ -150,13 +150,13 @@ public class BinomialDistributionTest extends TestCase {
|
|||
testProbability(9, .97175);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability8() {
|
||||
public void testcumulativeProbability8() {
|
||||
testProbability(8, .85069);
|
||||
}
|
||||
|
||||
private void testProbability(int x, double expected) {
|
||||
try {
|
||||
double actual = b.cummulativeProbability(x);
|
||||
double actual = b.cumulativeProbability(x);
|
||||
assertEquals(expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -167,10 +167,10 @@ public class BinomialDistributionTest extends TestCase {
|
|||
|
||||
private void testValue(int expected, double p) {
|
||||
try {
|
||||
int actual = b.inverseCummulativeProbability(p);
|
||||
int actual = b.inverseCumulativeProbability(p);
|
||||
assertEquals(expected, actual);
|
||||
assertTrue(b.cummulativeProbability(actual) <= p);
|
||||
assertTrue(b.cummulativeProbability(actual + 1) >= p);
|
||||
assertTrue(b.cumulativeProbability(actual) <= p);
|
||||
assertTrue(b.cumulativeProbability(actual + 1) >= p);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -59,7 +59,7 @@ import org.apache.commons.math.MathException;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.11 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.12 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class ChiSquareDistributionTest extends TestCase {
|
||||
private ChiSquaredDistribution chiSquare;
|
||||
|
@ -122,7 +122,7 @@ public class ChiSquareDistributionTest extends TestCase {
|
|||
|
||||
private void testProbability(double x, double expected){
|
||||
try {
|
||||
double actual = chiSquare.cummulativeProbability(x);
|
||||
double actual = chiSquare.cumulativeProbability(x);
|
||||
assertEquals("probability for " + x, expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -132,7 +132,7 @@ public class ChiSquareDistributionTest extends TestCase {
|
|||
|
||||
private void testValue(double p, double expected){
|
||||
try {
|
||||
double actual = chiSquare.inverseCummulativeProbability(p);
|
||||
double actual = chiSquare.inverseCumulativeProbability(p);
|
||||
assertEquals("value for " + p, expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -59,7 +59,7 @@ import org.apache.commons.math.TestUtils;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class ExponentialDistributionTest extends TestCase {
|
||||
private ExponentialDistribution exp;
|
||||
|
@ -90,113 +90,113 @@ public class ExponentialDistributionTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability001() {
|
||||
public void testInverseCumulativeProbability001() {
|
||||
testValue(.005003, .001);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability010() {
|
||||
public void testInverseCumulativeProbability010() {
|
||||
testValue(0.050252, .010);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability025() {
|
||||
public void testInverseCumulativeProbability025() {
|
||||
testValue(0.126589, .025);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability050() {
|
||||
public void testInverseCumulativeProbability050() {
|
||||
testValue(0.256566, .050);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability100() {
|
||||
public void testInverseCumulativeProbability100() {
|
||||
testValue(0.526803, .100);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability999() {
|
||||
public void testInverseCumulativeProbability999() {
|
||||
testValue(34.5388, .999);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability990() {
|
||||
public void testInverseCumulativeProbability990() {
|
||||
testValue(23.0259, .990);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability975() {
|
||||
public void testInverseCumulativeProbability975() {
|
||||
testValue(18.4444, .975);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability950() {
|
||||
public void testInverseCumulativeProbability950() {
|
||||
testValue(14.9787, .950);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability900() {
|
||||
public void testInverseCumulativeProbability900() {
|
||||
testValue(11.5129, .900);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability001() {
|
||||
public void testCumulativeProbability001() {
|
||||
testProbability(0.005003, .001);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability010() {
|
||||
public void testCumulativeProbability010() {
|
||||
testProbability(0.050252, .010);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability025() {
|
||||
public void testCumulativeProbability025() {
|
||||
testProbability(0.126589, .025);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability050() {
|
||||
public void testCumulativeProbability050() {
|
||||
testProbability(0.256566, .050);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability100() {
|
||||
public void testCumulativeProbability100() {
|
||||
testProbability(0.526803, .100);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability999() {
|
||||
public void testCumulativeProbability999() {
|
||||
testProbability(34.5388, .999);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability990() {
|
||||
public void testCumulativeProbability990() {
|
||||
testProbability(23.0259, .990);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability975() {
|
||||
public void testCumulativeProbability975() {
|
||||
testProbability(18.4444, .975);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability950() {
|
||||
public void testCumulativeProbability950() {
|
||||
testProbability(14.9787, .950);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability900() {
|
||||
public void testCumulativeProbability900() {
|
||||
testProbability(11.5129, .900);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilityNegative() {
|
||||
public void testCumulativeProbabilityNegative() {
|
||||
testProbability(-1.0, 0.0);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilityZero() {
|
||||
public void testCumulativeProbabilityZero() {
|
||||
testProbability(0.0, 0.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityNegative() {
|
||||
public void testInverseCumulativeProbabilityNegative() {
|
||||
testValue(Double.NaN, -1.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityZero() {
|
||||
public void testInverseCumulativeProbabilityZero() {
|
||||
testValue(0.0, 0.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityOne() {
|
||||
public void testInverseCumulativeProbabilityOne() {
|
||||
testValue(Double.POSITIVE_INFINITY, 1.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityPositive() {
|
||||
public void testInverseCumulativeProbabilityPositive() {
|
||||
testValue(Double.NaN, 2.0);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability2() {
|
||||
public void testCumulativeProbability2() {
|
||||
try {
|
||||
double actual = exp.cummulativeProbability(0.25, 0.75);
|
||||
double actual = exp.cumulativeProbability(0.25, 0.75);
|
||||
assertEquals(0.0905214, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -207,7 +207,7 @@ public class ExponentialDistributionTest extends TestCase {
|
|||
|
||||
private void testProbability(double x, double expected) {
|
||||
try {
|
||||
double actual = exp.cummulativeProbability(x);
|
||||
double actual = exp.cumulativeProbability(x);
|
||||
TestUtils.assertEquals(expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -217,7 +217,7 @@ public class ExponentialDistributionTest extends TestCase {
|
|||
|
||||
private void testValue(double expected, double p) {
|
||||
try {
|
||||
double actual = exp.inverseCummulativeProbability(p);
|
||||
double actual = exp.inverseCumulativeProbability(p);
|
||||
TestUtils.assertEquals(expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
|
|
@ -58,7 +58,7 @@ import org.apache.commons.math.MathException;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.9 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class FDistributionTest extends TestCase {
|
||||
private FDistribution f;
|
||||
|
@ -117,7 +117,7 @@ public class FDistributionTest extends TestCase {
|
|||
|
||||
private void testProbability(double x, double expected) {
|
||||
try {
|
||||
double actual = f.cummulativeProbability(x);
|
||||
double actual = f.cumulativeProbability(x);
|
||||
assertEquals("probability for " + x, expected, actual, 1e-3);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -127,7 +127,7 @@ public class FDistributionTest extends TestCase {
|
|||
|
||||
private void testValue(double expected, double p) {
|
||||
try {
|
||||
double actual = f.inverseCummulativeProbability(p);
|
||||
double actual = f.inverseCumulativeProbability(p);
|
||||
assertEquals("value for " + p, expected, actual, 1e-2);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
|
|
@ -59,7 +59,7 @@ import org.apache.commons.math.MathException;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.12 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class GammaDistributionTest extends TestCase {
|
||||
public void testProbabilities() {
|
||||
|
@ -87,7 +87,7 @@ public class GammaDistributionTest extends TestCase {
|
|||
DistributionFactory
|
||||
.newInstance()
|
||||
.createGammaDistribution(a, b)
|
||||
.cummulativeProbability(x);
|
||||
.cumulativeProbability(x);
|
||||
assertEquals("probability for " + x, expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -101,7 +101,7 @@ public class GammaDistributionTest extends TestCase {
|
|||
DistributionFactory
|
||||
.newInstance()
|
||||
.createGammaDistribution(a, b)
|
||||
.inverseCummulativeProbability(p);
|
||||
.inverseCumulativeProbability(p);
|
||||
assertEquals("critical value for " + p, expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
|
|
@ -59,7 +59,7 @@ import org.apache.commons.math.MathException;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.8 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.9 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class HypergeometricDistributionTest extends TestCase {
|
||||
private HypergeometricDistribution h;
|
||||
|
@ -92,7 +92,7 @@ public class HypergeometricDistributionTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability001() {
|
||||
public void testInverseCumulativeProbability001() {
|
||||
testValue(-1, .001);
|
||||
}
|
||||
|
||||
|
@ -112,7 +112,7 @@ public class HypergeometricDistributionTest extends TestCase {
|
|||
testValue(0, .100);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability999() {
|
||||
public void testInverseCumulativeProbability999() {
|
||||
testValue(4, .999);
|
||||
}
|
||||
|
||||
|
@ -132,11 +132,11 @@ public class HypergeometricDistributionTest extends TestCase {
|
|||
testValue(3, .900);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability0() {
|
||||
public void testCumulativeProbability0() {
|
||||
testProbability(0, .00400);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability1() {
|
||||
public void testCumulativeProbability1() {
|
||||
testProbability(1, .10318);
|
||||
}
|
||||
|
||||
|
@ -158,7 +158,7 @@ public class HypergeometricDistributionTest extends TestCase {
|
|||
|
||||
private void testProbability(int x, double expected) {
|
||||
try {
|
||||
double actual = h.cummulativeProbability(x);
|
||||
double actual = h.cumulativeProbability(x);
|
||||
assertEquals(expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -168,10 +168,10 @@ public class HypergeometricDistributionTest extends TestCase {
|
|||
|
||||
private void testValue(int expected, double p) {
|
||||
try {
|
||||
int actual = h.inverseCummulativeProbability(p);
|
||||
int actual = h.inverseCumulativeProbability(p);
|
||||
assertEquals(expected, actual);
|
||||
assertTrue(h.cummulativeProbability(actual) <= p);
|
||||
assertTrue(h.cummulativeProbability(actual + 1) >= p);
|
||||
assertTrue(h.cumulativeProbability(actual) <= p);
|
||||
assertTrue(h.cumulativeProbability(actual + 1) >= p);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -1,222 +1,222 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.distribution;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for NormalDistribution implementation
|
||||
*
|
||||
* "True" results are taken from R - the same as in Mathematica
|
||||
*
|
||||
*/
|
||||
public class NormalDistributionTest extends TestCase {
|
||||
|
||||
private NormalDistribution z;
|
||||
private static final double PRECISION = 10e-6;
|
||||
private static final double M = 2.1;
|
||||
private static final double SD = 1.4;
|
||||
|
||||
/**
|
||||
* Constructor for NormalDistributionTest.
|
||||
* @param arg0
|
||||
*/
|
||||
public NormalDistributionTest(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.swingui.TestRunner.run(NormalDistributionTest.class);
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
z = DistributionFactory.newInstance().createNormalDistribution(M, SD);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
z = null;
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_MINUS_2SD() throws MathException {
|
||||
testProbability(M - 2*SD, 0.02275013);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_MINUS_SD() throws MathException {
|
||||
testProbability(M - SD, 0.1586553);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM() throws MathException {
|
||||
testProbability(M, 0.5);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_PLUS_SD() throws MathException {
|
||||
testProbability(M + SD, 0.8413447);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_PLUS_2SD() throws MathException {
|
||||
testProbability(M + 2*SD, 0.9772499);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_PLUS_3SD() throws MathException {
|
||||
testProbability(M + 3*SD, 0.9986501);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_PLUS_4SD() throws MathException {
|
||||
testProbability(M + 4*SD, 0.9999683);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilitydoubleM_PLUS_5SD() throws MathException {
|
||||
testProbability(M + 5*SD, 0.9999997);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability0() throws MathException {
|
||||
assertEquals(Double.isNaN(z.inverseCummulativeProbability(0.0)), true);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability001() throws MathException {
|
||||
testValue(-2.226325, .001);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability010() throws MathException{
|
||||
testValue(-1.156887, .010);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability025() throws MathException{
|
||||
testValue(-0.6439496, .025);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability050() throws MathException{
|
||||
testValue(-0.2027951, .050);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability100() throws MathException{
|
||||
testValue(0.3058278, .100);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability900() throws MathException{
|
||||
testValue(3.894172, .900);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability950() throws MathException{
|
||||
testValue(4.402795, .950);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability975() throws MathException{
|
||||
testValue(4.84395, .975);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability990() throws MathException{
|
||||
testValue(5.356887, .990);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability999() throws MathException{
|
||||
testValue(6.426325, .999);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability1() throws MathException {
|
||||
assertEquals(Double.isNaN(z.inverseCummulativeProbability(1.0)), true);
|
||||
}
|
||||
|
||||
public void testGetMean() {
|
||||
assertEquals(M, z.getMean(), 0);
|
||||
}
|
||||
|
||||
public void testSetMean() throws MathException {
|
||||
double mu = Math.random();
|
||||
z.setMean(mu);
|
||||
assertEquals(mu, z.getMean(), 0);
|
||||
assertEquals(0.5d, z.cummulativeProbability(mu), PRECISION);
|
||||
}
|
||||
|
||||
public void testGetStandardDeviation() {
|
||||
assertEquals(SD, z.getStandardDeviation(), 0);
|
||||
}
|
||||
|
||||
public void testSetStandardDeviation() throws MathException{
|
||||
double sigma = 0.1d + Math.random();
|
||||
z.setStandardDeviation(sigma);
|
||||
assertEquals(sigma, z.getStandardDeviation(), 0);
|
||||
assertEquals(0.84134475, z.cummulativeProbability(z.getMean() + z.getStandardDeviation()), PRECISION );
|
||||
}
|
||||
|
||||
public void testGetCdfAlgorithm() {
|
||||
assertTrue(z.getCdfAlgorithm() != null);
|
||||
}
|
||||
|
||||
public void testSetCdfAlgorithm() {
|
||||
z.setCdfAlgorithm(new NormalCDFFastAlgorithm());
|
||||
assertTrue(z.getCdfAlgorithm() instanceof NormalCDFFastAlgorithm);
|
||||
}
|
||||
|
||||
private void testProbability(double x, double expected) throws MathException {
|
||||
double actual = Double.NaN;
|
||||
z.setCdfAlgorithm(new NormalCDFPreciseAlgorithm());
|
||||
actual = z.cummulativeProbability(x);
|
||||
assertEquals(expected, actual, PRECISION);
|
||||
z.setCdfAlgorithm(new NormalCDFFastAlgorithm());
|
||||
actual = z.cummulativeProbability(x);
|
||||
assertEquals(expected, actual, PRECISION);
|
||||
}
|
||||
|
||||
private void testValue(double expected, double p) throws MathException {
|
||||
double actual = z.inverseCummulativeProbability(p);
|
||||
assertEquals(expected, actual, PRECISION);
|
||||
}
|
||||
|
||||
}
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2004 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 acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements 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 name 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.distribution;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for NormalDistribution implementation
|
||||
*
|
||||
* "True" results are taken from R - the same as in Mathematica
|
||||
*
|
||||
*/
|
||||
public class NormalDistributionTest extends TestCase {
|
||||
|
||||
private NormalDistribution z;
|
||||
private static final double PRECISION = 10e-6;
|
||||
private static final double M = 2.1;
|
||||
private static final double SD = 1.4;
|
||||
|
||||
/**
|
||||
* Constructor for NormalDistributionTest.
|
||||
* @param arg0
|
||||
*/
|
||||
public NormalDistributionTest(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.swingui.TestRunner.run(NormalDistributionTest.class);
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
z = DistributionFactory.newInstance().createNormalDistribution(M, SD);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
z = null;
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_MINUS_2SD() throws MathException {
|
||||
testProbability(M - 2*SD, 0.02275013);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_MINUS_SD() throws MathException {
|
||||
testProbability(M - SD, 0.1586553);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM() throws MathException {
|
||||
testProbability(M, 0.5);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_PLUS_SD() throws MathException {
|
||||
testProbability(M + SD, 0.8413447);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_PLUS_2SD() throws MathException {
|
||||
testProbability(M + 2*SD, 0.9772499);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_PLUS_3SD() throws MathException {
|
||||
testProbability(M + 3*SD, 0.9986501);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_PLUS_4SD() throws MathException {
|
||||
testProbability(M + 4*SD, 0.9999683);
|
||||
}
|
||||
|
||||
public void testCumulativeProbabilitydoubleM_PLUS_5SD() throws MathException {
|
||||
testProbability(M + 5*SD, 0.9999997);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability0() throws MathException {
|
||||
assertEquals(Double.isNaN(z.inverseCumulativeProbability(0.0)), true);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability001() throws MathException {
|
||||
testValue(-2.226325, .001);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability010() throws MathException{
|
||||
testValue(-1.156887, .010);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability025() throws MathException{
|
||||
testValue(-0.6439496, .025);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability050() throws MathException{
|
||||
testValue(-0.2027951, .050);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability100() throws MathException{
|
||||
testValue(0.3058278, .100);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability900() throws MathException{
|
||||
testValue(3.894172, .900);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability950() throws MathException{
|
||||
testValue(4.402795, .950);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability975() throws MathException{
|
||||
testValue(4.84395, .975);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability990() throws MathException{
|
||||
testValue(5.356887, .990);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability999() throws MathException{
|
||||
testValue(6.426325, .999);
|
||||
}
|
||||
|
||||
public void testInverseCumulativeProbability1() throws MathException {
|
||||
assertEquals(Double.isNaN(z.inverseCumulativeProbability(1.0)), true);
|
||||
}
|
||||
|
||||
public void testGetMean() {
|
||||
assertEquals(M, z.getMean(), 0);
|
||||
}
|
||||
|
||||
public void testSetMean() throws MathException {
|
||||
double mu = Math.random();
|
||||
z.setMean(mu);
|
||||
assertEquals(mu, z.getMean(), 0);
|
||||
assertEquals(0.5d, z.cumulativeProbability(mu), PRECISION);
|
||||
}
|
||||
|
||||
public void testGetStandardDeviation() {
|
||||
assertEquals(SD, z.getStandardDeviation(), 0);
|
||||
}
|
||||
|
||||
public void testSetStandardDeviation() throws MathException{
|
||||
double sigma = 0.1d + Math.random();
|
||||
z.setStandardDeviation(sigma);
|
||||
assertEquals(sigma, z.getStandardDeviation(), 0);
|
||||
assertEquals(0.84134475, z.cumulativeProbability(z.getMean() + z.getStandardDeviation()), PRECISION );
|
||||
}
|
||||
|
||||
public void testGetCdfAlgorithm() {
|
||||
assertTrue(z.getCdfAlgorithm() != null);
|
||||
}
|
||||
|
||||
public void testSetCdfAlgorithm() {
|
||||
z.setCdfAlgorithm(new NormalCDFFastAlgorithm());
|
||||
assertTrue(z.getCdfAlgorithm() instanceof NormalCDFFastAlgorithm);
|
||||
}
|
||||
|
||||
private void testProbability(double x, double expected) throws MathException {
|
||||
double actual = Double.NaN;
|
||||
z.setCdfAlgorithm(new NormalCDFPreciseAlgorithm());
|
||||
actual = z.cumulativeProbability(x);
|
||||
assertEquals(expected, actual, PRECISION);
|
||||
z.setCdfAlgorithm(new NormalCDFFastAlgorithm());
|
||||
actual = z.cumulativeProbability(x);
|
||||
assertEquals(expected, actual, PRECISION);
|
||||
}
|
||||
|
||||
private void testValue(double expected, double p) throws MathException {
|
||||
double actual = z.inverseCumulativeProbability(p);
|
||||
assertEquals(expected, actual, PRECISION);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ import org.apache.commons.math.MathException;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.10 $ $Date: 2004/01/29 00:48:59 $
|
||||
* @version $Revision: 1.11 $ $Date: 2004/02/18 04:04:17 $
|
||||
*/
|
||||
public class TDistributionTest extends TestCase {
|
||||
private TDistribution t;
|
||||
|
@ -87,7 +87,7 @@ public class TDistributionTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability001() {
|
||||
public void testInverseCumulativeProbability001() {
|
||||
testValue(-5.893, .001);
|
||||
}
|
||||
|
||||
|
@ -107,7 +107,7 @@ public class TDistributionTest extends TestCase {
|
|||
testValue(-1.476, .100);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability999() {
|
||||
public void testInverseCumulativeProbability999() {
|
||||
testValue(5.893, .999);
|
||||
}
|
||||
|
||||
|
@ -127,7 +127,7 @@ public class TDistributionTest extends TestCase {
|
|||
testValue(1.476, .900);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability001() {
|
||||
public void testCumulativeProbability001() {
|
||||
testProbability(-5.893, .001);
|
||||
}
|
||||
|
||||
|
@ -147,7 +147,7 @@ public class TDistributionTest extends TestCase {
|
|||
testProbability(-1.476, .100);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability999() {
|
||||
public void testCumulativeProbability999() {
|
||||
testProbability(5.893, .999);
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@ public class TDistributionTest extends TestCase {
|
|||
|
||||
private void testProbability(double x, double expected) {
|
||||
try {
|
||||
double actual = t.cummulativeProbability(x);
|
||||
double actual = t.cumulativeProbability(x);
|
||||
assertEquals(expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
@ -179,7 +179,7 @@ public class TDistributionTest extends TestCase {
|
|||
|
||||
private void testValue(double expected, double p) {
|
||||
try {
|
||||
double actual = t.inverseCummulativeProbability(p);
|
||||
double actual = t.inverseCumulativeProbability(p);
|
||||
assertEquals(expected, actual, 10e-4);
|
||||
} catch (MathException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
|
|
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link Frequency} class.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2004/02/08 19:51:25 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/02/18 04:04:18 $
|
||||
*/
|
||||
|
||||
public final class FrequencyTest extends TestCase {
|
||||
|
@ -104,11 +104,11 @@ public final class FrequencyTest extends TestCase {
|
|||
assertEquals("two frequency count",1,f.getCount(2));
|
||||
assertEquals("three frequency count",0,f.getCount(3));
|
||||
assertEquals("total count",4,f.getSumFreq());
|
||||
assertEquals("zero cummulative frequency", 0, f.getCumFreq(0));
|
||||
assertEquals("one cummulative frequency", 3, f.getCumFreq(1));
|
||||
assertEquals("two cummulative frequency", 4, f.getCumFreq(2));
|
||||
assertEquals("two cummulative frequency", 4, f.getCumFreq(5));
|
||||
assertEquals("two cummulative frequency", 0, f.getCumFreq("foo"));
|
||||
assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));
|
||||
assertEquals("one cumulative frequency", 3, f.getCumFreq(1));
|
||||
assertEquals("two cumulative frequency", 4, f.getCumFreq(2));
|
||||
assertEquals("two cumulative frequency", 4, f.getCumFreq(5));
|
||||
assertEquals("two cumulative frequency", 0, f.getCumFreq("foo"));
|
||||
f.clear();
|
||||
assertEquals("total count",0,f.getSumFreq());
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0"?>
|
||||
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
|
||||
<!-- $Revision: 1.5 $ $Date: 2003/11/15 18:38:16 $ -->
|
||||
<!-- $Revision: 1.6 $ $Date: 2004/02/18 04:04:18 $ -->
|
||||
<document url="stat.html">
|
||||
<properties>
|
||||
<title>The Commons Math User Guide - Statistics</title>
|
||||
|
@ -62,18 +62,18 @@
|
|||
</p>
|
||||
<p>
|
||||
Using a distribution object, PDF and CDF probabilities are easily computed
|
||||
using the <code>cummulativeProbability</code> methods. For a distribution <code>X</code>,
|
||||
and a domain value, <code>x</code>, <code>cummulativeProbability</code> computes
|
||||
using the <code>cumulativeProbability</code> methods. For a distribution <code>X</code>,
|
||||
and a domain value, <code>x</code>, <code>cumulativeProbability</code> computes
|
||||
<code>P(X <= x)</code> (i.e. the lower tail probability of <code>X</code>).
|
||||
</p>
|
||||
<source>DistributionFactory factory = DistributionFactory.newInstance();
|
||||
TDistribution t = factory.createBinomialDistribution(29);
|
||||
double lowerTail = t.cummulativeProbability(-2.656); // P(T <= -2.656)
|
||||
double upperTail = 1.0 - t.cummulativeProbability(2.75); // P(T >= 2.75)</source>
|
||||
double lowerTail = t.cumulativeProbability(-2.656); // P(T <= -2.656)
|
||||
double upperTail = 1.0 - t.cumulativeProbability(2.75); // P(T >= 2.75)</source>
|
||||
<p>
|
||||
The inverse PDF and CDF values are just as easily computed using the
|
||||
<code>inverseCummulativeProbability</code>methods. For a distribution <code>X</code>,
|
||||
and a probability, <code>p</code>, <code>inverseCummulativeProbability</code>
|
||||
<code>inverseCumulativeProbability</code>methods. For a distribution <code>X</code>,
|
||||
and a probability, <code>p</code>, <code>inverseCumulativeProbability</code>
|
||||
computes the domain value <code>x</code>, such that:
|
||||
<ul>
|
||||
<li><code>P(X <= x) = p</code>, for continuous distributions</li>
|
||||
|
@ -82,7 +82,7 @@
|
|||
Notice the different cases for continuous and discrete distributions. This is the result
|
||||
of PDFs not being invertible functions. As such, for discrete distributions, an exact
|
||||
domain value can not be returned. Only the "best" domain value. For Commons-Math, the "best"
|
||||
domain value is determined by the largest domain value whose cummulative probability is
|
||||
domain value is determined by the largest domain value whose cumulative probability is
|
||||
less-than or equal to the given probability.
|
||||
</p>
|
||||
</subsection>
|
||||
|
|
Loading…
Reference in New Issue