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 2009/04/12 20:51:10 UTC

svn commit: r764313 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/stat/correlation/Covariance.java test/org/apache/commons/math/stat/correlation/CovarianceTest.java

Author: psteitz
Date: Sun Apr 12 18:51:10 2009
New Revision: 764313

URL: http://svn.apache.org/viewvc?rev=764313&view=rev
Log:
Made method names consistent, added methods to default bias-correction.

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/correlation/Covariance.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/stat/correlation/CovarianceTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/correlation/Covariance.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/correlation/Covariance.java?rev=764313&r1=764312&r2=764313&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/correlation/Covariance.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/correlation/Covariance.java Sun Apr 12 18:51:10 2009
@@ -113,7 +113,7 @@
     public Covariance(RealMatrix matrix, boolean biasCorrected) {
        checkSufficientData(matrix);
        n = matrix.getRowDimension();
-       covarianceMatrix = computeCovariance(matrix, biasCorrected);
+       covarianceMatrix = computeCovarianceMatrix(matrix, biasCorrected);
     }
     
     /**
@@ -150,13 +150,13 @@
     }
     
     /**
-     * Create a covariance matrix from a matrix whose columns represent
+     * Compute a covariance matrix from a matrix whose columns represent
      * covariates.
      * @param matrix input matrix (must have at least two columns and two rows)
      * @param biasCorrected determines whether or not covariance estimates are bias-corrected
      * @return covariance matrix
      */
-    protected RealMatrix computeCovariance(RealMatrix matrix, boolean biasCorrected) {
+    protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) {
         int dimension = matrix.getColumnDimension();
         Variance variance = new Variance(biasCorrected);
         RealMatrix outMatrix = new DenseRealMatrix(dimension, dimension);
@@ -172,6 +172,39 @@
     }
     
     /**
+     * Create a covariance matrix from a matrix whose columns represent
+     * covariates. Covariances are computed using the bias-corrected formula.
+     * @param matrix input matrix (must have at least two columns and two rows)
+     * @return covariance matrix
+     * @see #Covariance
+     */
+    protected RealMatrix computeCovarianceMatrix(RealMatrix matrix) {
+        return computeCovarianceMatrix(matrix, true);
+    }
+    
+    /**
+     * Compute a covariance matrix from a rectangular array whose columns represent
+     * covariates.
+     * @param data input array (must have at least two columns and two rows)
+     * @param biasCorrected determines whether or not covariance estimates are bias-corrected
+     * @return covariance matrix
+     */
+    protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected) {
+        return computeCovarianceMatrix(new DenseRealMatrix(data), biasCorrected);
+    }
+    
+    /**
+     * Create a covariance matrix from a rectangual array whose columns represent
+     * covariates. Covariances are computed using the bias-corrected formula.
+     * @param data input array (must have at least two columns and two rows)
+     * @return covariance matrix
+     * @see #Covariance
+     */
+    protected RealMatrix computeCovarianceMatrix(double[][] data) {
+        return computeCovarianceMatrix(data, true);
+    }
+    
+    /**
      * Computes the covariance between the two arrays.
      * 
      * <p>Array lengths must match and the common length must be at least 2.</p>
@@ -207,6 +240,23 @@
     }
     
     /**
+     * Computes the covariance between the two arrays, using the bias-corrected
+     * formula.
+     * 
+     * <p>Array lengths must match and the common length must be at least 2.</p>
+     *
+     * @param xArray first data array
+     * @param yArray second data array
+     * @return returns the covariance for the two arrays 
+     * @throws  IllegalArgumentException if the arrays lengths do not match or
+     * there is insufficient data
+     */
+    public double covariance(final double[] xArray, final double[] yArray) 
+        throws IllegalArgumentException {
+        return covariance(xArray, yArray, true);
+    }
+    
+    /**
      * Throws IllegalArgumentException of the matrix does not have at least
      * two columns and two rows
      * @param matrix matrix to check

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/stat/correlation/CovarianceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/correlation/CovarianceTest.java?rev=764313&r1=764312&r2=764313&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/stat/correlation/CovarianceTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/stat/correlation/CovarianceTest.java Sun Apr 12 18:51:10 2009
@@ -184,8 +184,8 @@
      * column-by-column covariances
      */
     public void testConsistency() {
-        RealMatrix matrix = createRealMatrix(swissData, 47, 5);
-        RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
+        final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
+        final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
         
         // Variances on the diagonal
         Variance variance = new Variance();
@@ -203,14 +203,25 @@
         for (int i = 0; i < 3; i++) {
             repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
         }
-        covarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
+        RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
         double columnVariance = variance.evaluate(matrix.getColumn(0));
         for (int i = 0; i < 3; i++) {
             for (int j = 0; j < 3; j++) {
-                assertEquals(columnVariance, covarianceMatrix.getEntry(i, j), 10E-14);
+                assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
             }
         }
         
+        // Check bias-correction defaults
+        double[][] data = matrix.getData();
+        TestUtils.assertEquals("Covariances", 
+                covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
+        TestUtils.assertEquals("Covariances", 
+                covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);
+        
+        double[] x = data[0];
+        double[] y = data[1];
+        assertEquals(new Covariance().covariance(x, y), 
+                new Covariance().covariance(x, y, true), Double.MIN_VALUE); 
     }
     
     protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {