Author: psteitz
Date: Sun May 16 23:48:07 2010
New Revision: 944939

URL: http://svn.apache.org/viewvc?rev=944939&view=rev
Log:
Fixed loss of significance error in PersonsCorrelation p-value computation
causing p-values smaller than the machine epsilon (~1E-16) to be reported as 0.
JIRA: MATH-371
Reported and patched by Kevin Childs

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/PearsonsCorrelationTest.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java?rev=944939&r1=944938&r2=944939&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java
 Sun May 16 23:48:07 2010
@@ -168,7 +168,7 @@ public class PearsonsCorrelation {
                 } else {
                     double r = correlationMatrix.getEntry(i, j);
                     double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
-                    out[i][j] = 2 * (1 - 
tDistribution.cumulativeProbability(t));
+                    out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
                 }
             }
         }

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=944939&r1=944938&r2=944939&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun May 16 23:48:07 2010
@@ -52,6 +52,10 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="2.2" date="TBD" description="TBD">
+      <action dev="psteitz" type="fix" issue="MATH-371" due-to="Kevin Childs">
+        Fixed loss of significance error in PersonsCorrelation p-value 
computation causing p-values
+        smaller than the machine epsilon (~1E-16) to be reported as 0.
+      </action>
       <action dev="luc" type="fix" issue="MATH-369" due-to="Sasun Pundev">
         Fix NullPointerException in BisectionSolver.solve(f, min, max, initial)
       </action>

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/PearsonsCorrelationTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/PearsonsCorrelationTest.java?rev=944939&r1=944938&r2=944939&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/PearsonsCorrelationTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/correlation/PearsonsCorrelationTest.java
 Sun May 16 23:48:07 2010
@@ -160,6 +160,27 @@ public class PearsonsCorrelationTest ext
          fillUpper(rPMatrix, 0d);
          TestUtils.assertEquals("correlation p values", rPMatrix, 
corrInstance.getCorrelationPValues(), 10E-15);
     }
+    
+    /**
+     * Test p-value near 0. JIRA: MATH-371
+     */
+    public void testPValueNearZero() throws Exception {
+        /*
+         * Create a dataset that has r -> 1, p -> 0 as dimension increases.
+         * Prior to the fix for MATH-371, p vanished for dimension >= 14.
+         * Post fix, p-values diminish smoothly, vanishing at dimension = 127.
+         * Tested value is ~1E-303.
+         */
+        int dimension = 120; 
+        double[][] data = new double[dimension][2];
+        for (int i = 0; i < dimension; i++) {
+            data[i][0] = i;
+            data[i][1] = i + 1/((double)i + 1);
+        }
+        PearsonsCorrelation corrInstance = new PearsonsCorrelation(data);
+        assertTrue(corrInstance.getCorrelationPValues().getEntry(0, 1) > 0);
+    }
+    
 
     /**
      * Constant column


Reply via email to