Changed inverseCumulativeProbability to return correct values for p=0,1 as discussed on commons-dev.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141412 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-07-24 21:41:37 +00:00
parent 86b71e2963
commit f078965871
9 changed files with 132 additions and 13 deletions

View File

@ -22,7 +22,7 @@ import org.apache.commons.math.MathException;
/**
* The default implementation of {@link ExponentialDistribution}
*
* @version $Revision: 1.18 $ $Date: 2004/06/23 16:26:15 $
* @version $Revision: 1.19 $ $Date: 2004/07/24 21:41:36 $
*/
public class ExponentialDistributionImpl extends AbstractContinuousDistribution
implements ExponentialDistribution, Serializable {
@ -90,7 +90,9 @@ public class ExponentialDistributionImpl extends AbstractContinuousDistribution
/**
* 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

View File

@ -24,7 +24,7 @@ import org.apache.commons.math.special.Beta;
* Default implementation of
* {@link org.apache.commons.math.distribution.FDistribution}.
*
* @version $Revision: 1.18 $ $Date: 2004/06/23 16:26:15 $
* @version $Revision: 1.19 $ $Date: 2004/07/24 21:41:36 $
*/
public class FDistributionImpl
extends AbstractContinuousDistribution
@ -80,6 +80,30 @@ public class FDistributionImpl
}
return ret;
}
/**
* 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

@ -23,7 +23,7 @@ import org.apache.commons.math.special.Gamma;
/**
* The default implementation of {@link GammaDistribution}
*
* @version $Revision: 1.21 $ $Date: 2004/06/23 16:26:15 $
* @version $Revision: 1.22 $ $Date: 2004/07/24 21:41:36 $
*/
public class GammaDistributionImpl extends AbstractContinuousDistribution
implements GammaDistribution, Serializable {
@ -77,6 +77,30 @@ public class GammaDistributionImpl extends AbstractContinuousDistribution
return ret;
}
/**
* 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);
}
/**
* Modify the shape parameter, alpha.
* @param alpha the new shape parameter.

View File

@ -25,7 +25,7 @@ import org.apache.commons.math.special.Erf;
* Default implementation of
* {@link org.apache.commons.math.distribution.NormalDistribution}.
*
* @version $Revision: 1.12 $ $Date: 2004/06/23 16:26:15 $
* @version $Revision: 1.13 $ $Date: 2004/07/24 21:41:36 $
*/
public class NormalDistributionImpl extends AbstractContinuousDistribution
implements NormalDistribution, Serializable {
@ -105,6 +105,31 @@ public class NormalDistributionImpl extends AbstractContinuousDistribution
return 0.5 * (1.0 + Erf.erf((x - mean) /
(standardDeviation * Math.sqrt(2.0))));
}
/**
* For this distribution, X, this method returns the critical point x, such
* that P(X &lt; x) = <code>p</code>.
* <p>
* Returns <code>Double.NEGATIVE_INFINITY</code> 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 Double.NEGATIVE_INFINITY;
}
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

@ -24,7 +24,7 @@ import org.apache.commons.math.special.Beta;
* Default implementation of
* {@link org.apache.commons.math.distribution.TDistribution}.
*
* @version $Revision: 1.18 $ $Date: 2004/06/23 16:26:15 $
* @version $Revision: 1.19 $ $Date: 2004/07/24 21:41:36 $
*/
public class TDistributionImpl
extends AbstractContinuousDistribution
@ -90,6 +90,31 @@ public class TDistributionImpl
return ret;
}
/**
* For this distribution, X, this method returns the critical point x, such
* that P(X &lt; x) = <code>p</code>.
* <p>
* Returns <code>Double.NEGATIVE_INFINITY</code> 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 Double.NEGATIVE_INFINITY;
}
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/06/10 18:27:47 $
* @version $Revision: 1.16 $ $Date: 2004/07/24 21:41:37 $
*/
public class FDistributionTest extends ContinuousDistributionAbstractTest {
@ -69,9 +69,8 @@ public class FDistributionTest extends ContinuousDistributionAbstractTest {
}
public void testInverseCumulativeProbabilityExtremes() throws Exception {
//TODO: decide what to do about p = 1. This currently blows up the solver.
setInverseCumulativeTestPoints(new double[] {0});
setInverseCumulativeTestValues(new double[] {0});
setInverseCumulativeTestPoints(new double[] {0, 1});
setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
verifyInverseCumulativeProbabilities();
}

View File

@ -21,7 +21,7 @@ package org.apache.commons.math.distribution;
* Extends ContinuousDistributionAbstractTest. See class javadoc for
* ContinuousDistributionAbstractTest for details.
*
* @version $Revision: 1.17 $ $Date: 2004/05/31 00:55:22 $
* @version $Revision: 1.18 $ $Date: 2004/07/24 21:41:37 $
*/
public class GammaDistributionTest extends ContinuousDistributionAbstractTest {
@ -111,4 +111,10 @@ public class GammaDistributionTest extends ContinuousDistributionAbstractTest {
double actual = distribution.inverseCumulativeProbability(p);
assertEquals("critical value for " + p, expected, actual, 10e-4);
}
public void testInverseCumulativeProbabilityExtremes() throws Exception {
setInverseCumulativeTestPoints(new double[] {0, 1});
setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
verifyInverseCumulativeProbabilities();
}
}

View File

@ -21,7 +21,7 @@ package org.apache.commons.math.distribution;
* Extends ContinuousDistributionAbstractTest. See class javadoc for
* ContinuousDistributionAbstractTest for details.
*
* @version $Revision: 1.7 $ $Date: 2004/05/30 05:54:43 $
* @version $Revision: 1.8 $ $Date: 2004/07/24 21:41:37 $
*/
public class NormalDistributionTest extends ContinuousDistributionAbstractTest {
@ -86,6 +86,13 @@ public class NormalDistributionTest extends ContinuousDistributionAbstractTest
verifyQuantiles();
}
public void testInverseCumulativeProbabilityExtremes() throws Exception {
setInverseCumulativeTestPoints(new double[] {0, 1});
setInverseCumulativeTestValues(
new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
verifyInverseCumulativeProbabilities();
}
public void testGetMean() {
NormalDistribution distribution = (NormalDistribution) getDistribution();
assertEquals(2.1, distribution.getMean(), 0);

View File

@ -20,7 +20,7 @@ package org.apache.commons.math.distribution;
* Extends ContinuousDistributionAbstractTest. See class javadoc for
* ContinuousDistributionAbstractTest for details.
*
* @version $Revision: 1.14 $ $Date: 2004/05/30 22:13:35 $
* @version $Revision: 1.15 $ $Date: 2004/07/24 21:41:37 $
*/
public class TDistributionTest extends ContinuousDistributionAbstractTest {
@ -83,6 +83,13 @@ public class TDistributionTest extends ContinuousDistributionAbstractTest {
verifyInverseCumulativeProbabilities();
}
public void testInverseCumulativeProbabilityExtremes() throws Exception {
setInverseCumulativeTestPoints(new double[] {0, 1});
setInverseCumulativeTestValues(
new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
verifyInverseCumulativeProbabilities();
}
public void testDfAccessors() {
TDistribution distribution = (TDistribution) getDistribution();
assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);