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 2013/05/31 19:47:54 UTC

svn commit: r1488337 - in /commons/proper/math/trunk: ./ src/changes/ src/main/java/org/apache/commons/math3/stat/correlation/ src/test/java/org/apache/commons/math3/stat/correlation/

Author: psteitz
Date: Fri May 31 17:47:53 2013
New Revision: 1488337

URL: http://svn.apache.org/r1488337
Log:
Added append method to StorelessCovariance, making this class map/reducible.
JIRA: MATH-978
Contributed by Ajo Fod

Modified:
    commons/proper/math/trunk/pom.xml
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCovariance.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTest.java

Modified: commons/proper/math/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/pom.xml?rev=1488337&r1=1488336&r2=1488337&view=diff
==============================================================================
--- commons/proper/math/trunk/pom.xml (original)
+++ commons/proper/math/trunk/pom.xml Fri May 31 17:47:53 2013
@@ -184,6 +184,9 @@
       <name>Ted Dunning</name>
     </contributor>
     <contributor>
+      <name>Ajo Fod</name>
+    </contributor>
+    <contributor>
       <name>John Gant</name>
     </contributor>
     <contributor>

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1488337&r1=1488336&r2=1488337&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Fri May 31 17:47:53 2013
@@ -51,6 +51,9 @@ If the output is not quite correct, chec
   </properties>
   <body>
     <release version="x.y" date="TBD" description="TBD">
+      <action dev="psteitz" type="update" issue="MATH-978" due-to="Ajo Fod">
+        Added append method to StorelessCovariance, making this class map/reducible.
+      </action>
       <action dev="tn" type="add" issue="MATH-851" due-to="Clemens Novak">
         Added method "MathArrays#convolve(double[], double[])" to compute the
         discrete, linear convolution of two sequences.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCovariance.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCovariance.java?rev=1488337&r1=1488336&r2=1488337&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCovariance.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCovariance.java Fri May 31 17:47:53 2013
@@ -91,6 +91,24 @@ class StorelessBivariateCovariance {
     }
 
     /**
+     * Appends another bivariate covariance calculation to this.
+     * After this operation, statistics returned should be close to what would
+     * have been obtained by by performing all of the {@link #increment(double, double)}
+     * operations in {@code cov} directly on this.
+     *
+     * @param cov StorelessBivariateCovariance instance to append.
+     */
+    public void append(StorelessBivariateCovariance cov) {
+        double oldN = n;
+        n += cov.n;
+        final double deltaX = cov.meanX - meanX;
+        final double deltaY = cov.meanY - meanY;
+        meanX += deltaX * cov.n / n;
+        meanY += deltaY * cov.n / n;
+        covarianceNumerator += cov.covarianceNumerator + oldN * cov.n / n * deltaX * deltaY;
+    }
+
+    /**
      * Returns the number of observations.
      *
      * @return number of observations

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.java?rev=1488337&r1=1488336&r2=1488337&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.java Fri May 31 17:47:53 2013
@@ -163,6 +163,29 @@ public class StorelessCovariance extends
     }
 
     /**
+     * Appends {@code sc} to this, effectively aggregating the computations in {@code sc}
+     * with this.  After invoking this method, covariances returned should be close
+     * to what would have been obtained by performing all of the {@link #increment(double[])}
+     * operations in {@code sc} directly on this.
+     *
+     * @param sc externally computed StorelessCovariance to add to this
+     * @throws DimensionMismatchException if the dimension of sc does not match this
+     */
+    public void append(StorelessCovariance sc) throws DimensionMismatchException {
+        if (sc.dimension != dimension) {
+            throw new DimensionMismatchException(sc.dimension, dimension);
+        }
+
+        // only update the upper triangular part of the covariance matrix
+        // as only these parts are actually stored
+        for (int i = 0; i < dimension; i++) {
+            for (int j = i; j < dimension; j++) {
+                getElement(i, j).append(sc.getElement(i, j));
+            }
+        }
+    }
+
+    /**
      * {@inheritDoc}
      * @throws NumberIsTooSmallException if the number of observations
      * in a cell is &lt; 2

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTest.java?rev=1488337&r1=1488336&r2=1488337&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTest.java Fri May 31 17:47:53 2013
@@ -19,6 +19,7 @@ package org.apache.commons.math3.stat.co
 import org.apache.commons.math3.TestUtils;
 import org.apache.commons.math3.linear.Array2DRowRealMatrix;
 import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.random.ISAACRandom;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -221,6 +222,32 @@ public class StorelessCovarianceTest {
         }
     }
     
+    /**
+     * Test equality of covariance. chk: covariance of two
+     * samples separately and adds them together. cov: computes
+     * covariance of the combined sample showing both are equal.
+     */
+    @Test
+    public void testEquivalence() {
+        int num_sets = 2;
+        StorelessBivariateCovariance cov = new StorelessBivariateCovariance();// covariance of the superset
+        StorelessBivariateCovariance chk = new StorelessBivariateCovariance();// check covariance made by appending covariance of subsets
+        
+        ISAACRandom rand = new ISAACRandom(10L);// Seed can be changed
+        for (int s = 0; s < num_sets; s++) {// loop through sets of samlpes
+            StorelessBivariateCovariance covs = new StorelessBivariateCovariance();
+            for (int i = 0; i < 5; i++) { // loop through individual samlpes.
+                double x = rand.nextDouble();
+                double y = rand.nextDouble();
+                covs.increment(x, y);// add sample to the subset 
+                cov.increment(x, y);// add sample to the superset
+            }
+           chk.append(covs);
+        }
+        
+        TestUtils.assertEquals("covariance subset test", chk.getResult(), cov.getResult(), 10E-7);
+    }
+  
     protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
         double[][] matrixData = new double[nRows][nCols];
         int ptr = 0;