You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2012/09/05 08:53:45 UTC

svn commit: r1381029 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java

Author: psteitz
Date: Wed Sep  5 06:53:45 2012
New Revision: 1381029

URL: http://svn.apache.org/viewvc?rev=1381029&view=rev
Log:
Added density - cdf consistency test.

Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java?rev=1381029&r1=1381028&r2=1381029&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java Wed Sep  5 06:53:45 2012
@@ -17,8 +17,14 @@
 package org.apache.commons.math3.distribution;
 
 
+import java.util.ArrayList;
+import java.util.Collections;
+
 import org.apache.commons.math3.TestUtils;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator;
+import org.apache.commons.math3.analysis.integration.IterativeLegendreGaussIntegrator;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
 import org.apache.commons.math3.exception.NumberIsTooLargeException;
 import org.junit.After;
@@ -293,6 +299,45 @@ public abstract class RealDistributionAb
         }
         TestUtils.assertChiSquareAccept(expected, counts, 0.001);
     }
+    
+    /**
+     * Verify that density integrals match the distribution.
+     * The (filtered, sorted) cumulativeTestPoints array is used to source
+     * integration limits. The integral of the density (estimated using a
+     * Legendre-Gauss integrator) is compared with the cdf over the same
+     * interval. Test points outside of the domain of the density function
+     * are discarded.
+     */
+    @Test
+    public void testDensityIntegrals() {
+        final double tol = 1.0e-9;
+        final BaseAbstractUnivariateIntegrator integrator =
+            new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10);
+        final UnivariateFunction d = new UnivariateFunction() {
+            public double value(double x) {
+                return distribution.density(x);
+            }
+        };
+        final ArrayList<Double> integrationTestPoints = new ArrayList<Double>();
+        for (int i = 0; i < cumulativeTestPoints.length; i++) {
+            if (Double.isNaN(cumulativeTestValues[i]) ||
+                    cumulativeTestValues[i] < 1.0e-5 ||
+                    cumulativeTestValues[i] > 1 - 1.0e-5) {
+                continue; // exclude integrals outside domain.
+            }
+            integrationTestPoints.add(cumulativeTestPoints[i]);
+        }
+        Collections.sort(integrationTestPoints);
+        for (int i = 1; i < integrationTestPoints.size(); i++) {
+            Assert.assertEquals(
+                    distribution.cumulativeProbability(  // FIXME @4.0 when rename happens
+                            integrationTestPoints.get(0), integrationTestPoints.get(i)),
+                            integrator.integrate(
+                                    1000000, // Triangle integrals are very slow to converge
+                                    d, integrationTestPoints.get(0),
+                                    integrationTestPoints.get(i)), tol);
+        }
+    }
 
     //------------------ Getters / Setters for test instance data -----------
     /**