Changed inverseCumulativeProbability to correctly handle p=0,1 as discussed on commons-dev.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141410 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-07-24 20:43:29 +00:00
parent a33aa39627
commit 2bc3611d28
2 changed files with 41 additions and 3 deletions

View File

@ -22,7 +22,7 @@ import org.apache.commons.math.MathException;
/**
* The default implementation of {@link ChiSquaredDistribution}
*
* @version $Revision: 1.18 $ $Date: 2004/06/23 16:26:15 $
* @version $Revision: 1.19 $ $Date: 2004/07/24 20:43:29 $
*/
public class ChiSquaredDistributionImpl
extends AbstractContinuousDistribution
@ -70,6 +70,30 @@ public class ChiSquaredDistributionImpl
public double cumulativeProbability(double x) throws MathException {
return getGamma().cumulativeProbability(x);
}
/**
* For this distribution, X, this method returns the critical point x, such
* that P(X &lt; x) = <code>p</code>.
* <p>
* Returns 0 for p=0 and <code>Double.POSITIVE_INFINITY</code> for p=1.
*
* @param p the desired probability
* @return x, such that P(X &lt; x) = <code>p</code>
* @throws MathException if the inverse cumulative probability can not be
* computed due to convergence or other numerical errors.
* @throws IllegalArgumentException if <code>p</code> is not a valid
* probability.
*/
public double inverseCumulativeProbability(final double p)
throws MathException {
if (p == 0) {
return 0d;
}
if (p == 1) {
return Double.POSITIVE_INFINITY;
}
return super.inverseCumulativeProbability(p);
}
/**
* Access the domain value lower bound, based on <code>p</code>, used to

View File

@ -21,7 +21,7 @@ package org.apache.commons.math.distribution;
* Extends ContinuousDistributionAbstractTest. See class javadoc for
* ContinuousDistributionAbstractTest for details.
*
* @version $Revision: 1.15 $ $Date: 2004/05/29 22:52:44 $
* @version $Revision: 1.16 $ $Date: 2004/07/24 20:43:29 $
*/
public class ChiSquareDistributionTest extends ContinuousDistributionAbstractTest {
@ -51,7 +51,20 @@ public class ChiSquareDistributionTest extends ContinuousDistributionAbstractTes
public double[] makeCumulativeTestValues() {
return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
0.990d, 0.975d, 0.950d, 0.900d};
}
}
/** Creates the default inverse cumulative probability test input values */
public double[] makeInverseCumulativeTestPoints() {
return new double[] {0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
0.990d, 0.975d, 0.950d, 0.900d, 1};
}
/** Creates the default inverse cumulative probability density test expected values */
public double[] makeInverseCumulativeTestValues() {
return new double[] {0, 0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d,
20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d,
Double.POSITIVE_INFINITY};
}
// --------------------- Override tolerance --------------
protected void setup() throws Exception {
@ -69,6 +82,7 @@ public class ChiSquareDistributionTest extends ContinuousDistributionAbstractTes
1.144775E-26, 1.168926E-20, 5.472917, 2.175255, 1.13438,
0.5318646, 0.1526342});
setInverseCumulativeTestValues(getCumulativeTestPoints());
setInverseCumulativeTestPoints(getCumulativeTestValues());
verifyCumulativeProbabilities();
verifyInverseCumulativeProbabilities();
}