commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdigg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution ExponentialDistribution.java ExponentialDistributionImpl.java DistributionFactory.java DistributionFactoryImpl.java
Date Fri, 13 Jun 2003 13:22:41 GMT
mdiggory    2003/06/13 06:22:41

  Modified:    math/src/test/org/apache/commons/math/stat/distribution
                        DistributionFactoryImplTest.java
               math/src/java/org/apache/commons/math/stat/distribution
                        DistributionFactory.java
                        DistributionFactoryImpl.java
  Added:       math/src/test/org/apache/commons/math/stat/distribution
                        ExponentialDistributionTest.java
               math/src/java/org/apache/commons/math/stat/distribution
                        ExponentialDistribution.java
                        ExponentialDistributionImpl.java
  Log:
  PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20633
  Submitted by:	brent@worden.org
  
  Revision  Changes    Path
  1.6       +26 -0     jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/DistributionFactoryImplTest.java
  
  Index: DistributionFactoryImplTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/DistributionFactoryImplTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DistributionFactoryImplTest.java	11 Jun 2003 01:19:18 -0000	1.5
  +++ DistributionFactoryImplTest.java	13 Jun 2003 13:22:40 -0000	1.6
  @@ -102,6 +102,32 @@
           }
       }
       
  +    public void testCreateExponentialDistributionNegative(){
  +        try {
  +            factory.createExponentialDistribution(-1.0);
  +            fail("negative mean.  IllegalArgumentException expected");
  +        } catch (IllegalArgumentException ex) {
  +            ;
  +        }
  +    }
  +    
  +    public void testCreateExponentialDistributionZero(){
  +        try {
  +            factory.createExponentialDistribution(0.0);
  +            fail("zero mean.  IllegalArgumentException expected");
  +        } catch (IllegalArgumentException ex) {
  +            ;
  +        }
  +    }
  +    
  +    public void testCreateExponentialDistributionPositive(){
  +        try {
  +            factory.createExponentialDistribution(1.0);
  +        } catch (IllegalArgumentException ex) {
  +            fail("positive mean.  IllegalArgumentException is not expected");
  +        }
  +    }
  +    
       public void testCreateGammaDistributionNegativePositive(){
           try {
               factory.createGammaDistribution(-1.0, 1.0);
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/distribution/ExponentialDistributionTest.java
  
  Index: ExponentialDistributionTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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 names 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.stat.distribution;
  
  import junit.framework.TestCase;
  
  /**
   * @author Brent Worden
   */
  public class ExponentialDistributionTest extends TestCase {
      private ExponentialDistribution exp;
      
      /**
       * Constructor for ChiSquareDistributionTest.
       * @param name
       */
      public ExponentialDistributionTest(String name) {
          super(name);
      }
  
      /*
       * @see TestCase#setUp()
       */
      protected void setUp() throws Exception {
          super.setUp();
          exp = DistributionFactory.newInstance().createExponentialDistribution(5.0);
      }
  
      /*
       * @see TestCase#tearDown()
       */
      protected void tearDown() throws Exception {
          exp = null;
          super.tearDown();
      }
  
      public void testLowerTailProbability(){
          testProbability(0.005003, .001);
          testProbability(0.050252, .010);
          testProbability(0.126589, .025);
          testProbability(0.256566, .050);
          testProbability(0.526803, .100);
      }
  
      public void testUpperTailProbability(){
          testProbability(34.5388, .999);
          testProbability(23.0259, .990);
          testProbability(18.4444, .975);
          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 testUpperTailValues(){
          testValue(34.5388, .999);
          testValue(23.0259, .990);
          testValue(18.4444, .975);
          testValue(14.9787, .950);
          testValue(11.5129, .900);
      }
      
      private void testProbability(double x, double expected){
          double actual = exp.cummulativeProbability(x);
          assertEquals("probability for " + x, expected, actual, 10e-4);
      }
      
      private void testValue(double expected, double p){
          double actual = exp.inverseCummulativeProbability(p);
          assertEquals("value for " + p, expected, actual, 10e-4);
      }
  }
  
  
  
  1.6       +8 -0      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactory.java
  
  Index: DistributionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DistributionFactory.java	11 Jun 2003 01:19:19 -0000	1.5
  +++ DistributionFactory.java	13 Jun 2003 13:22:41 -0000	1.6
  @@ -104,6 +104,14 @@
           double degreesOfFreedom);
       
       /**
  +     * Create a new exponential distribution with the given degrees of freedom.
  +     * @param mean mean.
  +     * @return a new exponential distribution.  
  +     */
  +    public abstract ExponentialDistribution createExponentialDistribution(
  +        double mean);
  +    
  +    /**
        * Create a new F-distribution with the given degrees of freedom.
        * @param numeratorDegreesOfFreedom numerator degrees of freedom.
        * @param denominatorDegreesOfFreedom denominator degrees of freedom.
  
  
  
  1.6       +15 -6     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactoryImpl.java
  
  Index: DistributionFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/DistributionFactoryImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DistributionFactoryImpl.java	11 Jun 2003 01:19:19 -0000	1.5
  +++ DistributionFactoryImpl.java	13 Jun 2003 13:22:41 -0000	1.6
  @@ -99,18 +99,27 @@
       public TDistribution createTDistribution(double degreesOfFreedom) {
           return new TDistributionImpl(degreesOfFreedom);
       }
  -    
  +
       /**
        * Create a new F-distribution with the given degrees of freedom.
        * @param numeratorDegreesOfFreedom numerator degrees of freedom.
        * @param denominatorDegreesOfFreedom denominator degrees of freedom.
        * @return a new F-distribution.  
        */
  -	public FDistribution createFDistribution(
  -		double numeratorDegreesOfFreedom,
  -		double denominatorDegreesOfFreedom) {
  -		return new FDistributionImpl(numeratorDegreesOfFreedom,
  +    public FDistribution createFDistribution(
  +        double numeratorDegreesOfFreedom,
  +        double denominatorDegreesOfFreedom) {
  +        return new FDistributionImpl(numeratorDegreesOfFreedom,
               denominatorDegreesOfFreedom);
  -	}
  +    }
  +
  +    /**
  +     * Create a new exponential distribution with the given degrees of freedom.
  +     * @param mean mean.
  +     * @return a new exponential distribution.  
  +     */
  +    public ExponentialDistribution createExponentialDistribution(double mean) {
  +        return new ExponentialDistributionImpl(mean);
  +    }    
   
   }
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ExponentialDistribution.java
  
  Index: ExponentialDistribution.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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 names 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.stat.distribution;
  
  /**
   * <p>
   * The Exponential Distribution
   * </p>
   * 
   * <p>
   * Instances of ExponentialDistribution objects should be created using
   * {@link DistributionFactory#createExponentialDistribution(double)}
   * </p>
   * 
   * <p>
   * References:
   * <ul>
   * <li><a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
   * Exponential Distribution</a></li>
   * </p>
   * 
   * @author Brent Worden
   */
  public interface ExponentialDistribution extends ContinuousDistribution {
      /**
       * Modify the mean.
       * @param mean the new mean.
       */
      void setMean(double mean);
      
      /**
       * Access the mean.
       * @return the mean.
       */
      double getMean();
  }
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/distribution/ExponentialDistributionImpl.java
  
  Index: ExponentialDistributionImpl.java
  ===================================================================
  package org.apache.commons.math.stat.distribution;
  
  /**
   * The default implementation of {@link ExponentialDistribution}
   * 
   * @author Brent Worden
   */
  public class ExponentialDistributionImpl
  	extends AbstractContinuousDistribution
  	implements ExponentialDistribution {
  
      /** The mean of this distribution. */
      private double mean;
      
      /**
       * Create a exponential distribution with the given mean.
       * @param degreesOfFreedom degrees of freedom.
       */
  	public ExponentialDistributionImpl(double mean) {
  		super();
          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.
       */
  	public void setMean(double mean) {
          if(mean <= 0.0){
              throw new IllegalArgumentException("mean must be positive.");
          }
          this.mean = mean;
  	}
  
      /**
       * Access the mean.
       * @return the mean.
       */
  	public double getMean() {
  		return mean;
  	}
  
      /**
       * <p>
       * For this disbution, X, this method returns P(X &lt; x).
       * </p>
       * 
       * <p>
       * The implementation of this method is based on:
       * <ul>
       * <li>
       * <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
       * Exponential Distribution</a>, equation (1).</li>
       * </ul>
       * </p>
       * 
       * @param x the value at which the CDF is evaluated.
       * @return CDF for this distribution.
       */
  	public double cummulativeProbability(double x) {
          double ret;
  		if(x < 0.0){
              ret = 0.0;
  		} else {
              ret = 1.0 - Math.exp(-x / getMean());
  		}
          return ret;
  	}
      
      /**
       * For this distribution, X, this method returns the critical point x, such
       * that P(X &lt; x) = <code>p</code>.
       *
       * @param p the desired probability
       * @return x, such that P(X &lt; 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.");
          }
          return -getMean() * Math.log(1.0 - p);
      }
  }
  
  
  

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


Mime
View raw message