Author: brentworden
Date: Wed Mar 16 19:20:12 2005
New Revision: 157874

URL: http://svn.apache.org/viewcvs?view=rev&rev=157874
Log:
added weibull distribution

Added:
    
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistribution.java
    
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java
    
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/WeibullDistributionTest.java
Modified:
    
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java
    
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/DistributionFactory.java
    
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/DistributionFactoryImplTest.java
    jakarta/commons/proper/math/trunk/xdocs/changes.xml
    jakarta/commons/proper/math/trunk/xdocs/userguide/distribution.xml

Modified: 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java?view=diff&r1=157873&r2=157874
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java
 (original)
+++ 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java
 Wed Mar 16 19:20:12 2005
@@ -22,7 +22,7 @@
  * Default implementation of
  * [EMAIL PROTECTED] org.apache.commons.math.distribution.CauchyDistribution}.
  *
- * @version $Revision$ $Date$
+ * @version $Revision: 1.13 $ $Date$
  */
 public class CauchyDistributionImpl extends AbstractContinuousDistribution 
                implements CauchyDistribution, Serializable {
@@ -37,8 +37,8 @@
        private double scale = 1;
     
        /**
-        * Creates normal distribution with the mean equal to zero and standard
-        * deviation equal to one. 
+        * Creates cauchy distribution with the medain equal to zero and scale
+        * equal to one. 
         */
        public CauchyDistributionImpl(){
                this(0.0, 1.0);

Modified: 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/DistributionFactory.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/DistributionFactory.java?view=diff&r1=157873&r2=157874
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/DistributionFactory.java
 (original)
+++ 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/DistributionFactory.java
 Wed Mar 16 19:20:12 2005
@@ -32,6 +32,7 @@
  * <li>Poisson</li>
  * <li>Normal</li>
  * <li>Student's t</li>
+ * <li>Weibull</li>
  * </ul>
  *
  * Common usage:<pre>
@@ -175,8 +176,22 @@
      * Create a new Poisson distribution with poisson parameter lambda.
      * 
      * @param lambda poisson parameter
-     * @return a new normal distribution.  
+     * @return a new poisson distribution.  
      */               
     public abstract PoissonDistribution 
         createPoissonDistribution(double lambda);
+    
+    /**
+     * Create a new Weibull distribution with the given shape and scale
+     * parameters.
+     * 
+     * @param alpha the shape parameter.
+     * @param beta the scale parameter.
+     * @return a new Weibull distribution.  
+     */               
+    public WeibullDistribution createWeibullDistribution(
+        double alpha, double beta)
+    {
+        return new WeibullDistributionImpl(alpha, beta);
+    }
 }

Added: 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistribution.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistribution.java?view=auto&rev=157874
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistribution.java
 (added)
+++ 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistribution.java
 Wed Mar 16 19:20:12 2005
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.distribution;
+
+/**
+ * Weibull Distribution.  This interface defines the two parameter form of the
+ * distribution as defined by
+ * <a href="http://mathworld.wolfram.com/WeibullDistribution.html";>
+ * Weibull Distribution</a>, equations (1) and (2).
+ *
+ * Instances of WeibullDistribution objects should be created using
+ * [EMAIL PROTECTED] DistributionFactory#createWeibullDistribution(double, 
double)}
+ *
+ * <p>
+ * References:
+ * <ul>
+ * <li><a href="http://mathworld.wolfram.com/WeibullDistribution.html";>
+ * Weibull Distribution</a></li>
+ * </ul>
+ * </p>
+ *
+ * @version $Revision: 1.12 $ $Date: 2004-06-23 11:26:18 -0500 (Wed, 23 Jun 
2004) $
+ */
+public interface WeibullDistribution extends ContinuousDistribution {
+
+    /**
+     * Access the shape parameter.
+     * @return the shape parameter.
+     */
+    double getShape();
+    
+    /**
+     * Access the scale parameter.
+     * @return the scale parameter.
+     */
+    double getScale();
+    
+    /**
+     * Modify the shape parameter.
+     * @param alpha The new shape parameter value.
+     */
+    void setShape(double alpha);
+    
+    /**
+     * Modify the scale parameter.
+     * @param beta The new scale parameter value.
+     */
+    void setScale(double beta);
+}

Added: 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java?view=auto&rev=157874
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java
 (added)
+++ 
jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/WeibullDistributionImpl.java
 Wed Mar 16 19:20:12 2005
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.distribution;
+
+import java.io.Serializable;
+
+/**
+ * Default implementation of
+ * [EMAIL PROTECTED] org.apache.commons.math.distribution.WeibullDistribution}.
+ *
+ * @version $Revision: 1.13 $ $Date: 2004-07-24 16:41:37 -0500 (Sat, 24 Jul 
2004) $
+ */
+public class WeibullDistributionImpl extends AbstractContinuousDistribution
+        implements WeibullDistribution, Serializable {
+    
+    /** Serializable version identifier */
+    static final long serialVersionUID = 8589540077390120676L;
+    
+    /** The shape parameter. */
+    private double alpha;
+    
+    /** The scale parameter. */
+    private double beta;
+    
+    /**
+     * Creates weibull distribution with the given shape and scale and a
+     * location equal to zero.
+     * @param alpha the shape parameter.
+     * @param beta the scale parameter.
+     */
+    public WeibullDistributionImpl(double alpha, double beta){
+        super();
+        setShape(alpha);
+        setScale(beta);
+    }
+
+    /**
+     * For this disbution, X, this method returns P(X &lt; <code>x</code>).
+     * @param x the value at which the CDF is evaluated.
+     * @return CDF evaluted at <code>x</code>. 
+     */
+    public double cumulativeProbability(double x) {
+        double ret;
+        if (x <= 0.0) {
+            ret = 0.0;
+        } else {
+            ret = 1.0 - Math.exp(-Math.pow(x / getScale(), getShape()));
+        }
+        return ret;
+    }
+
+    /**
+     * Access alpha.
+     * @return the alpha.
+     */
+    public double getShape() {
+        return alpha;
+    }
+    
+    /**
+     * Access beta.
+     * @return the beta.
+     */
+    public double getScale() {
+        return beta;
+    }
+    
+    /**
+     * 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 IllegalArgumentException if <code>p</code> is not a valid
+     *         probability.
+     */
+    public double inverseCumulativeProbability(double p) {
+        double ret;
+        if (p < 0.0 || p > 1.0) {
+            throw new IllegalArgumentException
+                ("probability argument must be between 0 and 1 (inclusive)");
+        } else if (p == 0) {
+            ret = 0.0;
+        } else  if (p == 1) {
+            ret = Double.POSITIVE_INFINITY;
+        } else {
+            ret = getScale() * Math.pow(-Math.log(1.0 - p), 1.0 / getShape());
+        }
+        return ret;
+    }
+    
+    /**
+     * Modify alpha.
+     * @param alpha The new alpha value.
+     */
+    public void setShape(double alpha) {
+        if (alpha <= 0.0) {
+            throw new IllegalArgumentException(
+                "Shape must be positive.");
+        }       
+        this.alpha = alpha;
+    }
+    
+    /**
+     * Modify beta.
+     * @param beta The new beta value.
+     */
+    public void setScale(double beta) {
+        if (beta <= 0.0) {
+            throw new IllegalArgumentException(
+                "Scale must be positive.");
+        }       
+        this.beta = beta;
+    }
+
+    /**
+     * Access the domain value lower bound, based on <code>p</code>, used to
+     * bracket a CDF root.  This method is used by
+     * [EMAIL PROTECTED] #inverseCumulativeProbability(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
+     * [EMAIL PROTECTED] #inverseCumulativeProbability(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
+     * [EMAIL PROTECTED] #inverseCumulativeProbability(double)} to find 
critical values.
+     * 
+     * @param p the desired probability for the critical value
+     * @return initial domain value
+     */
+    protected double getInitialDomain(double p) {
+        // use median
+        return Math.pow(getScale() * Math.log(2.0), 1.0 / getShape());
+    }
+}

Modified: 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/DistributionFactoryImplTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/DistributionFactoryImplTest.java?view=diff&r1=157873&r2=157874
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/DistributionFactoryImplTest.java
 (original)
+++ 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/DistributionFactoryImplTest.java
 Wed Mar 16 19:20:12 2005
@@ -325,4 +325,52 @@
         } catch(IllegalArgumentException ex) {
         }
     }
+    
+    public void testCauchyDistributionNegative() {
+        try {
+            factory.createCauchyDistribution(0.0, -1.0);
+            fail("invalid scale. IllegalArgumentException expected");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+    
+    public void testCauchyDistributionZero() {
+        try {
+            factory.createCauchyDistribution(0.0, 0.0);
+            fail("invalid scale. IllegalArgumentException expected");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+    
+    public void testWeibullDistributionNegativePositive() {
+        try {
+            factory.createWeibullDistribution(-1.0, 1.0);
+            fail("invalid shape. IllegalArgumentException expected");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+    
+    public void testWeibullDistributionZeroPositive() {
+        try {
+            factory.createWeibullDistribution(0.0, 1.0);
+            fail("invalid shape. IllegalArgumentException expected");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+    
+    public void testWeibullDistributionPositiveNegative() {
+        try {
+            factory.createWeibullDistribution(1.0, -1.0);
+            fail("invalid scale. IllegalArgumentException expected");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
+    
+    public void testWeibullDistributionPositiveZero() {
+        try {
+            factory.createWeibullDistribution(1.0, 0.0);
+            fail("invalid scale. IllegalArgumentException expected");
+        } catch(IllegalArgumentException ex) {
+        }
+    }
 }

Added: 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/WeibullDistributionTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/WeibullDistributionTest.java?view=auto&rev=157874
==============================================================================
--- 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/WeibullDistributionTest.java
 (added)
+++ 
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/WeibullDistributionTest.java
 Wed Mar 16 19:20:12 2005
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.distribution;
+
+/**
+ * Test cases for WeibullDistribution.
+ * Extends ContinuousDistributionAbstractTest.  See class javadoc for
+ * ContinuousDistributionAbstractTest for details.
+ * 
+ * @version $Revision: 1.8 $ $Date: 2004-07-24 16:41:37 -0500 (Sat, 24 Jul 
2004) $
+ */
+public class WeibullDistributionTest extends 
ContinuousDistributionAbstractTest  {
+    
+    /**
+     * Constructor for CauchyDistributionTest.
+     * @param arg0
+     */
+    public WeibullDistributionTest(String arg0) {
+        super(arg0);
+    }
+    
+    //-------------- Implementations for abstract methods 
-----------------------
+    
+    /** Creates the default continuous distribution instance to use in tests. 
*/
+    public ContinuousDistribution makeDistribution() {
+        return 
DistributionFactory.newInstance().createWeibullDistribution(1.2, 2.1);
+    }   
+    
+    /** Creates the default cumulative probability distribution test input 
values */
+    public double[] makeCumulativeTestPoints() {
+        // quantiles computed using Mathematica 
+        return new double[] {0.00664355181d, 0.04543282833d, 0.09811627374d,
+                0.1767135246d, 0.3219468654d, 4.207902826d, 5.23968437d,
+                6.232056007d, 7.497630467d, 10.51154969d};
+    }
+    
+    /** Creates the default cumulative probability density test expected 
values */
+    public double[] makeCumulativeTestValues() {
+        return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.900d, 
0.950d,
+                0.975d, 0.990d, 0.999d};
+    }
+    
+    //---------------------------- Additional test cases 
-------------------------
+    
+    public void testInverseCumulativeProbabilityExtremes() throws Exception {
+        setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
+        setInverseCumulativeTestValues(
+                new double[] {0.0, Double.POSITIVE_INFINITY});
+        verifyInverseCumulativeProbabilities();
+    }
+    
+    public void testAlpha() {
+        WeibullDistribution distribution = (WeibullDistribution) 
getDistribution();
+        double expected = Math.random();
+        distribution.setShape(expected);
+        assertEquals(expected, distribution.getShape(), 0.0);
+    }
+    
+    public void testBeta() {
+        WeibullDistribution distribution = (WeibullDistribution) 
getDistribution();
+        double expected = Math.random();
+        distribution.setScale(expected);
+        assertEquals(expected, distribution.getScale(), 0.0);
+    }
+    
+    public void testSetAlpha() {
+        WeibullDistribution distribution = (WeibullDistribution) 
getDistribution();
+        try {
+            distribution.setShape(0.0);
+            fail("Can not have 0.0 alpha.");
+        } catch (IllegalArgumentException ex) {
+            // success
+        }
+        
+        try {
+            distribution.setShape(-1.0);
+            fail("Can not have negative alpha.");
+        } catch (IllegalArgumentException ex) {
+            // success
+        }
+    }
+    
+    public void testSetBeta() {
+        WeibullDistribution distribution = (WeibullDistribution) 
getDistribution();
+        try {
+            distribution.setScale(0.0);
+            fail("Can not have 0.0 beta.");
+        } catch (IllegalArgumentException ex) {
+            // success
+        }
+        
+        try {
+            distribution.setScale(-1.0);
+            fail("Can not have negative beta.");
+        } catch (IllegalArgumentException ex) {
+            // success
+        }
+    }
+}

Modified: jakarta/commons/proper/math/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/xdocs/changes.xml?view=diff&r1=157873&r2=157874
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/trunk/xdocs/changes.xml Wed Mar 16 19:20:12 2005
@@ -40,6 +40,9 @@
     <release version="1.1" date="In Development"  
        description="Jakarta Commons Math 1.1 - Development">
       <action dev="brentworden" type="add">
+        Added Weibull distribution implementation.
+      </action>
+      <action dev="brentworden" type="add">
         Added Cauchy distribution implementation.
       </action>
       <action dev="brentworden" type="add">

Modified: jakarta/commons/proper/math/trunk/xdocs/userguide/distribution.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/xdocs/userguide/distribution.xml?view=diff&r1=157873&r2=157874
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/userguide/distribution.xml 
(original)
+++ jakarta/commons/proper/math/trunk/xdocs/userguide/distribution.xml Wed Mar 
16 19:20:12 2005
@@ -63,6 +63,7 @@
             <tr><td>Normal 
(Gaussian)</td><td>createNormalDistribution</td><td><div>Mean</div><div>Standard
 Deviation</div></td></tr>
             
<tr><td>Poisson</td><td>createPoissonDistribution</td><td><div>Mean</div></td></tr>
             <tr><td>t</td><td>createTDistribution</td><td><div>Degrees of 
freedom</div></td></tr>
+            
<tr><td>Weibull</td><td>createWeibullDistribution</td><td><div>Shape</div><div>Scale</div><div>Location</div></td></tr>
           </table>
         </p>
         <p>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to