Submitted by: brent@worden.org git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140906 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d20ee8ab0e
commit
09c8b57924
|
@ -6,7 +6,6 @@ package org.apache.commons.math.stat.distribution;
|
|||
* @author Brent Worden
|
||||
*/
|
||||
public class ExponentialDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
implements ExponentialDistribution {
|
||||
|
||||
/** The mean of this distribution. */
|
||||
|
@ -21,45 +20,6 @@ public class ExponentialDistributionImpl
|
|||
setMean(mean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 getMean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean the new mean.
|
||||
|
@ -98,7 +58,7 @@ public class ExponentialDistributionImpl
|
|||
*/
|
||||
public double cummulativeProbability(double x) {
|
||||
double ret;
|
||||
if(x < 0.0){
|
||||
if(x <= 0.0){
|
||||
ret = 0.0;
|
||||
} else {
|
||||
ret = 1.0 - Math.exp(-x / getMean());
|
||||
|
@ -114,10 +74,26 @@ public class ExponentialDistributionImpl
|
|||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCummulativeProbability(double p){
|
||||
double ret;
|
||||
|
||||
if(p < 0.0 || p > 1.0){
|
||||
throw new IllegalArgumentException(
|
||||
"p must be between 0.0 and 1.0, inclusive.");
|
||||
ret = Double.NaN;
|
||||
} else if(p == 1.0){
|
||||
ret = Double.POSITIVE_INFINITY;
|
||||
} else {
|
||||
ret = -getMean() * Math.log(1.0 - p);
|
||||
}
|
||||
return -getMean() * Math.log(1.0 - p);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public double cummulativeProbability(double x0, double x1) {
|
||||
return cummulativeProbability(x1) - cummulativeProbability(x0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package org.apache.commons.math;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
/**
|
||||
* @author Brent Worden
|
||||
*/
|
||||
public class TestUtils {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private TestUtils() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static void assertEquals(double expected, double actual, double delta) {
|
||||
// check for NaN
|
||||
if(Double.isNaN(expected)){
|
||||
Assert.assertTrue(Double.isNaN(actual));
|
||||
} else {
|
||||
Assert.assertEquals(expected, actual, delta);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -53,6 +53,8 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
|
||||
import org.apache.commons.math.TestUtils;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
|
@ -85,45 +87,122 @@ public class ExponentialDistributionTest extends TestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testLowerTailProbability(){
|
||||
public void testInverseCummulativeProbability001() {
|
||||
testValue(.005003, .001);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability010() {
|
||||
testValue(0.050252, .010);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability025() {
|
||||
testValue(0.126589, .025);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability050() {
|
||||
testValue(0.256566, .050);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability100() {
|
||||
testValue(0.526803, .100);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability999() {
|
||||
testValue(34.5388, .999);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability990() {
|
||||
testValue(23.0259, .990);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability975() {
|
||||
testValue(18.4444, .975);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability950() {
|
||||
testValue(14.9787, .950);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbability900() {
|
||||
testValue(11.5129, .900);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability001() {
|
||||
testProbability(0.005003, .001);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability010() {
|
||||
testProbability(0.050252, .010);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability025() {
|
||||
testProbability(0.126589, .025);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability050() {
|
||||
testProbability(0.256566, .050);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability100() {
|
||||
testProbability(0.526803, .100);
|
||||
}
|
||||
|
||||
public void testUpperTailProbability(){
|
||||
public void testCummulativeProbability999() {
|
||||
testProbability(34.5388, .999);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability990() {
|
||||
testProbability(23.0259, .990);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability975() {
|
||||
testProbability(18.4444, .975);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability950() {
|
||||
testProbability(14.9787, .950);
|
||||
}
|
||||
|
||||
public void testCummulativeProbability900() {
|
||||
testProbability(11.5129, .900);
|
||||
}
|
||||
|
||||
public void testLowerTailValues(){
|
||||
testValue(0.005003, .001);
|
||||
testValue(0.050252, .010);
|
||||
testValue(0.126589, .025);
|
||||
testValue(0.256566, .050);
|
||||
testValue(0.526803, .100);
|
||||
|
||||
public void testCummulativeProbabilityNegative() {
|
||||
testProbability(-1.0, 0.0);
|
||||
}
|
||||
|
||||
public void testCummulativeProbabilityZero() {
|
||||
testProbability(0.0, 0.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityNegative() {
|
||||
testValue(Double.NaN, -1.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityZero() {
|
||||
testValue(0.0, 0.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityOne() {
|
||||
testValue(Double.POSITIVE_INFINITY, 1.0);
|
||||
}
|
||||
|
||||
public void testInverseCummulativeProbabilityPositive() {
|
||||
testValue(Double.NaN, 2.0);
|
||||
}
|
||||
|
||||
public void testUpperTailValues(){
|
||||
testValue(34.5388, .999);
|
||||
testValue(23.0259, .990);
|
||||
testValue(18.4444, .975);
|
||||
testValue(14.9787, .950);
|
||||
testValue(11.5129, .900);
|
||||
public void testCummulativeProbability2() {
|
||||
double actual = exp.cummulativeProbability(0.25, 0.75);
|
||||
assertEquals(0.0905214, actual, 10e-4);
|
||||
}
|
||||
|
||||
private void testProbability(double x, double expected){
|
||||
double actual = exp.cummulativeProbability(x);
|
||||
assertEquals("probability for " + x, expected, actual, 10e-4);
|
||||
TestUtils.assertEquals(expected, actual, 10e-4);
|
||||
}
|
||||
|
||||
private void testValue(double expected, double p){
|
||||
double actual = exp.inverseCummulativeProbability(p);
|
||||
assertEquals("value for " + p, expected, actual, 10e-4);
|
||||
TestUtils.assertEquals(expected, actual, 10e-4);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue