You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/06/14 06:24:43 UTC

cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution ExponentialDistributionImpl.java

mdiggory    2003/06/13 21:24:43

  Modified:    math/src/test/org/apache/commons/math/stat/distribution
                        ExponentialDistributionTest.java
               math/src/java/org/apache/commons/math/stat/distribution
                        ExponentialDistributionImpl.java
  Added:       math/src/test/org/apache/commons/math TestUtils.java
  Log:
  PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20773
  Submitted by:	brent@worden.org
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/math/src/test/org/apache/commons/math/TestUtils.java
  
  Index: TestUtils.java
  ===================================================================
  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);
          }
      }
  }
  
  
  
  1.2       +96 -17    jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/ExponentialDistributionTest.java
  
  Index: ExponentialDistributionTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/ExponentialDistributionTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExponentialDistributionTest.java	13 Jun 2003 13:22:40 -0000	1.1
  +++ ExponentialDistributionTest.java	14 Jun 2003 04:24:43 -0000	1.2
  @@ -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 @@
           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);
  -        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 testCummulativeProbability900() {
  +        testProbability(11.5129, .900);
  +    }
  +
  +    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);
       }
   }
  
  
  
  1.2       +20 -44    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ExponentialDistributionImpl.java
  
  Index: ExponentialDistributionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ExponentialDistributionImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExponentialDistributionImpl.java	13 Jun 2003 13:22:41 -0000	1.1
  +++ ExponentialDistributionImpl.java	14 Jun 2003 04:24:43 -0000	1.2
  @@ -6,7 +6,6 @@
    * @author Brent Worden
    */
   public class ExponentialDistributionImpl
  -	extends AbstractContinuousDistribution
   	implements ExponentialDistribution {
   
       /** The mean of this distribution. */
  @@ -21,45 +20,6 @@
           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 &lt; <i>lower bound</i>) &lt; <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 &lt; <i>upper bound</i>) &gt; <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 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 @@
        * @return x, such that P(X &lt; 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 &lt; X &lt; 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);
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org