commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r693598 - in /commons/proper/math/branches/MATH_2_0/src: java/org/apache/commons/math/distribution/ site/xdoc/ test/org/apache/commons/math/distribution/
Date Tue, 09 Sep 2008 20:37:09 GMT
Author: luc
Date: Tue Sep  9 13:37:08 2008
New Revision: 693598

URL: http://svn.apache.org/viewvc?rev=693598&view=rev
Log:
Added probability density functions computation for distributions for which it is feasible
JIRA: MATH-222

Added:
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java
  (with props)
Modified:
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistribution.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistribution.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistributionImpl.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistribution.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java
    commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ChiSquareDistributionTest.java
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ExponentialDistributionTest.java
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/GammaDistributionTest.java
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/NormalDistributionTest.java

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistribution.java
Tue Sep  9 13:37:08 2008
@@ -29,7 +29,7 @@
  *
  * @version $Revision$ $Date$
  */
-public interface ChiSquaredDistribution extends ContinuousDistribution {
+public interface ChiSquaredDistribution extends ContinuousDistribution, HasDensity<Double>
{
     /**
      * Modify the degrees of freedom.
      * @param degreesOfFreedom the new degrees of freedom.
@@ -41,4 +41,11 @@
      * @return the degrees of freedom.
      */
     double getDegreesOfFreedom();
+
+    /**
+     * Return the probability density for a particular point.
+     * @param x  The point at which the density should be computed.
+     * @return  The pdf at point x.
+     */
+    double density(Double x);
 }
\ No newline at end of file

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java
Tue Sep  9 13:37:08 2008
@@ -70,7 +70,17 @@
     public double getDegreesOfFreedom() {
         return getGamma().getAlpha() * 2.0;
     }
-        
+
+    /**
+     * Return the probability density for a particular point.
+     *
+     * @param x The point at which the density should be computed.
+     * @return The pdf at point x.
+     */
+    public double density(Double x) {
+        return gamma.density(x);
+    }
+
     /**
      * For this distribution, X, this method returns P(X &lt; x).
      * @param x the value at which the CDF is evaluated.

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistribution.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistribution.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistribution.java
Tue Sep  9 13:37:08 2008
@@ -29,7 +29,7 @@
  *
  * @version $Revision$ $Date$
  */
-public interface ExponentialDistribution extends ContinuousDistribution {
+public interface ExponentialDistribution extends ContinuousDistribution, HasDensity<Double>
{
     /**
      * Modify the mean.
      * @param mean the new mean.
@@ -41,4 +41,11 @@
      * @return the mean.
      */
     double getMean();
+
+    /**
+     * Return the probability density for a particular point.
+     * @param x  The point at which the density should be computed.
+     * @return  The pdf at point x.
+     */
+    double density(Double x);
 }
\ No newline at end of file

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/ExponentialDistributionImpl.java
Tue Sep  9 13:37:08 2008
@@ -64,6 +64,19 @@
     }
 
     /**
+     * Return the probability density for a particular point.
+     *
+     * @param x The point at which the density should be computed.
+     * @return The pdf at point x.
+     */
+    public double density(Double x) {
+        if (x < 0) {
+            return 0;
+        }
+        return Math.exp(-x / getMean()) / getMean();
+    }
+
+    /**
      * For this distribution, X, this method returns P(X &lt; x).
      * 
      * The implementation of this method is based on:
@@ -157,6 +170,8 @@
      */
     protected double getInitialDomain(double p) {
         // TODO: try to improve on this estimate
+        // TODO: what should really happen here is not derive from AbstractContinuousDistribution
+        // TODO: because the inverse cumulative distribution is simple.
         // Exponential is skewed to the left, therefore, P(X < &mu;) > .5
         if (p < .5) {
             // use 1/2 mean

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistribution.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistribution.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistribution.java
Tue Sep  9 13:37:08 2008
@@ -29,7 +29,7 @@
  *
  * @version $Revision$ $Date$
  */
-public interface GammaDistribution extends ContinuousDistribution {
+public interface GammaDistribution extends ContinuousDistribution, HasDensity<Double>
{
     /**
      * Modify the shape parameter, alpha.
      * @param alpha the new shape parameter.
@@ -53,4 +53,11 @@
      * @return beta.
      */
     double getBeta();
+
+    /**
+     * Return the probability density for a particular point.
+     * @param x  The point at which the density should be computed.
+     * @return  The pdf at point x.
+     */
+    double density(Double x);
 }

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistributionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistributionImpl.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistributionImpl.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/GammaDistributionImpl.java
Tue Sep  9 13:37:08 2008
@@ -141,7 +141,18 @@
     public double getBeta() {
         return beta;
     }
-    
+
+    /**
+     * Return the probability density for a particular point.
+     *
+     * @param x The point at which the density should be computed.
+     * @return The pdf at point x.
+     */
+    public double density(Double x) {
+        if (x < 0) return 0;
+        return Math.pow(x / getBeta(), getAlpha() - 1) / getBeta() * Math.exp(-x / getBeta())
/ Math.exp(Gamma.logGamma(getAlpha()));
+    }
+
     /**
      * Access the domain value lower bound, based on <code>p</code>, used to
      * bracket a CDF root.  This method is used by

Added: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java?rev=693598&view=auto
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java
(added)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java
Tue Sep  9 13:37:08 2008
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.math.MathException;
+
+/**
+ * Interface that signals that a distribution can compute the probability density function
+ * for a particular point.
+ * @version $Revision$ $Date$
+ */
+public interface HasDensity<P> {
+
+    /**
+     * Compute the probability density function.
+     * @param x point for which the probability density is requested
+     * @return probability density at point x
+     * @throws MathException if probability density cannot be computed at specifed point
+     */
+    double density(P x) throws MathException;
+
+}

Propchange: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/HasDensity.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistribution.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistribution.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistribution.java
Tue Sep  9 13:37:08 2008
@@ -30,7 +30,7 @@
  *
  * @version $Revision$ $Date$
  */
-public interface NormalDistribution extends ContinuousDistribution {
+public interface NormalDistribution extends ContinuousDistribution, HasDensity<Double>
{
     /**
      * Access the mean.
      * @return mean for this distribution
@@ -51,4 +51,11 @@
      * @param sd standard deviation for this distribution
      */
     void setStandardDeviation(double sd);
+
+    /**
+     * Return the probability density for a particular point.
+     * @param x  The point at which the density should be computed.
+     * @return  The pdf at point x.
+     */
+    double density(Double x);
 }

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/distribution/NormalDistributionImpl.java
Tue Sep  9 13:37:08 2008
@@ -40,7 +40,8 @@
     
     /** The standard deviation of this distribution. */
     private double standardDeviation = 1;
-    
+    private static final double SQRT2PI = Math.sqrt(2 * Math.PI);
+
     /**
      * Create a normal distribution using the given mean and standard deviation.
      * @param mean mean for this distribution
@@ -98,6 +99,17 @@
     }
 
     /**
+     * Return the probability density for a particular point.
+     *
+     * @param x The point at which the density should be computed.
+     * @return The pdf at point x.
+     */
+    public double density(Double x) {
+        double x0 = x - getMean();
+        return Math.exp(-x0 * x0 / (2 * getStandardDeviation() * getStandardDeviation()))
/ (getStandardDeviation() * SQRT2PI);
+    }
+
+    /**
      * For this distribution, 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>. 

Modified: commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml Tue Sep  9 13:37:08 2008
@@ -39,6 +39,10 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" issue="MATH-222" due-to="Ted Dunning">
+        Added probability density functions computation for distributions for which
+        it is feasible.
+      </action>
       <action dev="luc" type="fix" issue="MATH-221" due-to="Dieter Roth">
         Changed the Complex.equals() method so that it considers +0 and -0 are equal,
         as required by IEEE-754 standard.

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ChiSquareDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ChiSquareDistributionTest.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ChiSquareDistributionTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ChiSquareDistributionTest.java
Tue Sep  9 13:37:08 2008
@@ -99,6 +99,25 @@
         } catch (IllegalArgumentException ex) {
             // expected
         }
-    } 
-    
+    }
+
+    public void testDensity() {
+        double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5};
+        //R 2.5: print(dchisq(x, df=1), digits=10)
+        checkDensity(1, x, new double[]{0.00000000000, 398.94208093034, 0.43939128947, 0.24197072452,
0.10377687436, 0.01464498256});
+        //R 2.5: print(dchisq(x, df=0.1), digits=10)
+        checkDensity(0.1, x, new double[]{0.000000000e+00, 2.486453997e+04, 7.464238732e-02,
3.009077718e-02, 9.447299159e-03, 8.827199396e-04});
+        //R 2.5: print(dchisq(x, df=2), digits=10)
+        checkDensity(2, x, new double[]{0.00000000000, 0.49999975000, 0.38940039154, 0.30326532986,
0.18393972059, 0.04104249931});
+        //R 2.5: print(dchisq(x, df=10), digits=10)
+        checkDensity(10, x, new double[]{0.000000000e+00, 1.302082682e-27, 6.337896998e-05,
7.897534632e-04, 7.664155024e-03, 6.680094289e-02});
+    }
+
+    private void checkDensity(double df, double[] x, double[] expected) {
+        ChiSquaredDistribution d = new ChiSquaredDistributionImpl(df);
+        for (int i = 0; i < x.length; i++) {
+            assertEquals(expected[i], d.density(x[i]), 1e-5);
+        }
+    }
+
 }

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ExponentialDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ExponentialDistributionTest.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ExponentialDistributionTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/ExponentialDistributionTest.java
Tue Sep  9 13:37:08 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.math.distribution;
 
+import org.apache.commons.math.MathException;
+
 /**
  * Test cases for ExponentialDistribution.
  * Extends ContinuousDistributionAbstractTest.  See class javadoc for
@@ -71,6 +73,23 @@
         double actual = getDistribution().cumulativeProbability(0.25, 0.75);
         assertEquals(0.0905214, actual, 10e-4);
     }
+
+    public void testDensity() throws MathException {
+        ExponentialDistribution d1 = new ExponentialDistributionImpl(1);
+        assertEquals(0.0, d1.density(-1e-9));
+        assertEquals(1.0, d1.density(0.0));
+        assertEquals(0.0, d1.density(1000.0));
+        assertEquals(Math.exp(-1), d1.density(1.0));
+        assertEquals(Math.exp(-2), d1.density(2.0));
+
+        ExponentialDistribution d2 = new ExponentialDistributionImpl(3);
+        assertEquals(1/3.0, d2.density(0.0));
+        // computed using  print(dexp(1, rate=1/3), digits=10) in R 2.5
+        assertEquals(0.2388437702, d2.density(1.0), 1e-8);
+
+        // computed using  print(dexp(2, rate=1/3), digits=10) in R 2.5
+        assertEquals(0.1711390397, d2.density(2.0), 1e-8);
+    }
     
     public void testMeanAccessors() {
         ExponentialDistribution distribution = (ExponentialDistribution) getDistribution();

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/GammaDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/GammaDistributionTest.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/GammaDistributionTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/GammaDistributionTest.java
Tue Sep  9 13:37:08 2008
@@ -33,14 +33,14 @@
     public GammaDistributionTest(String name) {
         super(name);
     }
-    
+
     //-------------- Implementations for abstract methods -----------------------
-    
+
     /** Creates the default continuous distribution instance to use in tests. */
     public ContinuousDistribution makeDistribution() {
         return new GammaDistributionImpl(4d, 2d);
-    }   
-    
+    }
+
     /** Creates the default cumulative probability distribution test input values */
     public double[] makeCumulativeTestPoints() {
         // quantiles computed using R version 1.8.1 (linux version)
@@ -48,13 +48,13 @@
             3.489539, 26.12448, 20.09024, 17.53455,
             15.50731, 13.36157};
     }
-    
+
     /** 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.999d,
-                0.990d, 0.975d, 0.950d, 0.900d}; 
+                0.990d, 0.975d, 0.950d, 0.900d};
     }
-    
+
     // --------------------- Override tolerance  --------------
     protected void setUp() throws Exception {
         super.setUp();
@@ -82,8 +82,8 @@
         } catch (IllegalArgumentException ex) {
             // expected
         }
-    } 
-    
+    }
+
     public void testProbabilities() throws Exception {
         testProbability(-1.000, 4.0, 2.0, .0000);
         testProbability(15.501, 4.0, 2.0, .9499);
@@ -110,7 +110,34 @@
         double actual = distribution.inverseCumulativeProbability(p);
         assertEquals("critical value for " + p, expected, actual, 10e-4);
     }
-    
+
+    public void testDensity() {
+        double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5};
+        // R2.5: print(dgamma(x, shape=1, rate=1), digits=10)
+        checkDensity(1, 1, x, new double[]{0.000000000000, 0.999999000001, 0.606530659713,
0.367879441171, 0.135335283237, 0.006737946999});
+        // R2.5: print(dgamma(x, shape=2, rate=1), digits=10)
+        checkDensity(2, 1, x, new double[]{0.000000000000, 0.000000999999, 0.303265329856,
0.367879441171, 0.270670566473, 0.033689734995});
+        // R2.5: print(dgamma(x, shape=4, rate=1), digits=10)
+        checkDensity(4, 1, x, new double[]{0.000000000e+00, 1.666665000e-19, 1.263605541e-02,
6.131324020e-02, 1.804470443e-01, 1.403738958e-01});
+        // R2.5: print(dgamma(x, shape=4, rate=10), digits=10)
+        checkDensity(4, 10, x, new double[]{0.000000000e+00, 1.666650000e-15, 1.403738958e+00,
7.566654960e-02, 2.748204830e-05, 4.018228850e-17});
+        // R2.5: print(dgamma(x, shape=.1, rate=10), digits=10)
+        checkDensity(0.1, 10, x, new double[]{0.000000000e+00, 3.323953832e+04, 1.663849010e-03,
6.007786726e-06, 1.461647647e-10, 5.996008322e-24});
+        // R2.5: print(dgamma(x, shape=.1, rate=20), digits=10)
+        checkDensity(0.1, 20, x, new double[]{0.000000000e+00, 3.562489883e+04, 1.201557345e-05,
2.923295295e-10, 3.228910843e-19, 1.239484589e-45});
+        // R2.5: print(dgamma(x, shape=.1, rate=4), digits=10)
+        checkDensity(0.1, 4, x, new double[]{0.000000000e+00, 3.032938388e+04, 3.049322494e-02,
2.211502311e-03, 2.170613371e-05, 5.846590589e-11});
+        // R2.5: print(dgamma(x, shape=.1, rate=1), digits=10)
+        checkDensity(0.1, 1, x, new double[]{0.000000000e+00, 2.640334143e+04, 1.189704437e-01,
3.866916944e-02, 7.623306235e-03, 1.663849010e-04});
+    }
+
+    private void checkDensity(double alpha, double rate, double[] x, double[] expected) {
+        GammaDistribution d = new GammaDistributionImpl(alpha, 1 / rate);
+        for (int i = 0; i < x.length; i++) {
+            assertEquals(expected[i], d.density(x[i]), 1e-5);
+        }
+    }
+
     public void testInverseCumulativeProbabilityExtremes() throws Exception {
         setInverseCumulativeTestPoints(new double[] {0, 1});
         setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/NormalDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/NormalDistributionTest.java?rev=693598&r1=693597&r2=693598&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/NormalDistributionTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/distribution/NormalDistributionTest.java
Tue Sep  9 13:37:08 2008
@@ -120,7 +120,22 @@
             // Expected
         }
     }
-    
+
+    public void testDensity() {
+        double [] x = new double[]{-2, -1, 0, 1, 2};
+        // R 2.5: print(dnorm(c(-2,-1,0,1,2)), digits=10)
+        checkDensity(0, 1, x, new double[]{0.05399096651, 0.24197072452, 0.39894228040, 0.24197072452,
0.05399096651});
+        // R 2.5: print(dnorm(c(-2,-1,0,1,2), mean=1.1), digits=10) 
+        checkDensity(1.1, 1, x, new double[]{0.003266819056,0.043983595980,0.217852177033,0.396952547477,0.266085249899});
+    }
+
+    private void checkDensity(double mean, double sd, double[] x, double[] expected) {
+        NormalDistribution d = new NormalDistributionImpl(mean, sd);
+        for (int i = 0; i < x.length; i++) {
+            assertEquals(expected[i], d.density(x[i]), 1e-9);
+        }
+    }
+
     /**
      * Check to make sure top-coding of extreme values works correctly.
      * Verifies fix for JIRA MATH-167



Mime
View raw message