commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r619924 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/optimization/ java/org/apache/commons/math/stat/descriptive/moment/ site/xdoc/ test/org/apache/commons/math/random/ test/org/apache/commons/math/stat/descriptive/moment/
Date Fri, 08 Feb 2008 16:07:36 GMT
Author: luc
Date: Fri Feb  8 08:07:33 2008
New Revision: 619924

URL: http://svn.apache.org/viewvc?rev=619924&view=rev
Log:
added a way to compute either sample or population vectorial covariance

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/moment/VectorialCovariance.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/random/CorrelatedRandomVectorGeneratorTest.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/random/UncorrelatedRandomVectorGeneratorTest.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/stat/descriptive/moment/VectorialCovarianceTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java?rev=619924&r1=619923&r2=619924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java
Fri Feb  8 08:07:33 2008
@@ -245,7 +245,7 @@
 
             // compute the statistical properties of the simplex points
             VectorialMean meanStat = new VectorialMean(vertices[0].length);
-            VectorialCovariance covStat = new VectorialCovariance(vertices[0].length);
+            VectorialCovariance covStat = new VectorialCovariance(vertices[0].length, true);
             for (int i = 0; i < vertices.length; ++i) {
                 meanStat.increment(vertices[i]);
                 covStat.increment(vertices[i]);

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/moment/VectorialCovariance.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/moment/VectorialCovariance.java?rev=619924&r1=619923&r2=619924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/moment/VectorialCovariance.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/descriptive/moment/VectorialCovariance.java
Fri Feb  8 08:07:33 2008
@@ -17,6 +17,7 @@
 package org.apache.commons.math.stat.descriptive.moment;
 
 import java.io.Serializable;
+import java.util.Arrays;
 
 import org.apache.commons.math.DimensionMismatchException;
 import org.apache.commons.math.linear.RealMatrix;
@@ -37,16 +38,22 @@
     /** Sums of products for each component. */
     private double[] productsSums;
 
+    /** Indicator for bias correction. */
+    private boolean isBiasCorrected;
+
     /** Number of vectors in the sample. */
     private long n;
 
     /** Constructs a VectorialMean.
      * @param dimension vectors dimension
+     * @param isBiasCorrected if true, computed the unbiased sample covariance,
+     * otherwise computes the biased population covariance
      */
-    public VectorialCovariance(int dimension) {
+    public VectorialCovariance(int dimension, boolean isBiasCorrected) {
         sums         = new double[dimension];
         productsSums = new double[dimension * (dimension + 1) / 2];
         n            = 0;
+        this.isBiasCorrected = isBiasCorrected;
     }
 
     /**
@@ -79,7 +86,7 @@
 
         if (n > 1) {
             double[][] resultData = result.getDataRef();
-            double c = 1.0 / (n * (n - 1));
+            double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
             int k = 0;
             for (int i = 0; i < dimension; ++i) {
                 for (int j = 0; j <= i; ++j) {
@@ -100,6 +107,15 @@
      */
     public long getN() {
         return n;
+    }
+
+    /**
+     * Clears the internal state of the Statistic
+     */
+    public void clear() {
+        n = 0;
+        Arrays.fill(sums, 0.0);
+        Arrays.fill(productsSums, 0.0);
     }
 
 }

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=619924&r1=619923&r2=619924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Feb  8 08:07:33 2008
@@ -150,6 +150,10 @@
         Added an angle normalization method in MathUtils to force angles in some
         user-defined interval
       </action> 
+      <action dev="luc" type="update" >
+        Added vectorial covariance computation (either sample or population
+        covariance)
+      </action>
     </release>
     <release version="1.1" date="2005-12-17"  
  description="This is a maintenance release containing bug fixes and enhancements.

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/random/CorrelatedRandomVectorGeneratorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/random/CorrelatedRandomVectorGeneratorTest.java?rev=619924&r1=619923&r2=619924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/random/CorrelatedRandomVectorGeneratorTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/random/CorrelatedRandomVectorGeneratorTest.java
Fri Feb  8 08:07:33 2008
@@ -52,7 +52,7 @@
     public void testMeanAndCovariance() throws DimensionMismatchException {
 
         VectorialMean meanStat = new VectorialMean(mean.length);
-        VectorialCovariance covStat = new VectorialCovariance(mean.length);
+        VectorialCovariance covStat = new VectorialCovariance(mean.length, true);
         for (int i = 0; i < 5000; ++i) {
             double[] v = generator.nextVector();
             meanStat.increment(v);

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/random/UncorrelatedRandomVectorGeneratorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/random/UncorrelatedRandomVectorGeneratorTest.java?rev=619924&r1=619923&r2=619924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/random/UncorrelatedRandomVectorGeneratorTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/random/UncorrelatedRandomVectorGeneratorTest.java
Fri Feb  8 08:07:33 2008
@@ -37,7 +37,7 @@
     public void testMeanAndCorrelation() throws DimensionMismatchException {
 
         VectorialMean meanStat = new VectorialMean(mean.length);
-        VectorialCovariance covStat = new VectorialCovariance(mean.length);
+        VectorialCovariance covStat = new VectorialCovariance(mean.length, true);
         for (int i = 0; i < 10000; ++i) {
             double[] v = generator.nextVector();
             meanStat.increment(v);

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/stat/descriptive/moment/VectorialCovarianceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/descriptive/moment/VectorialCovarianceTest.java?rev=619924&r1=619923&r2=619924&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/stat/descriptive/moment/VectorialCovarianceTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/stat/descriptive/moment/VectorialCovarianceTest.java
Fri Feb  8 08:07:33 2008
@@ -34,7 +34,7 @@
 
     public void testMismatch() {
         try {
-            new VectorialCovariance(8).increment(new double[5]);
+            new VectorialCovariance(8, true).increment(new double[5]);
             fail("an exception should have been thrown");
         } catch (DimensionMismatchException dme) {
             assertEquals(5, dme.getDimension1());
@@ -45,7 +45,7 @@
     }
 
     public void testSimplistic() throws DimensionMismatchException {
-        VectorialCovariance stat = new VectorialCovariance(2);
+        VectorialCovariance stat = new VectorialCovariance(2, true);
         stat.increment(new double[] {-1.0,  1.0});
         stat.increment(new double[] { 1.0, -1.0});
         RealMatrix c = stat.getResult();
@@ -56,7 +56,7 @@
 
     public void testBasicStats() throws DimensionMismatchException {
 
-        VectorialCovariance stat = new VectorialCovariance(points[0].length);
+        VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
         for (int i = 0; i < points.length; ++i) {
             stat.increment(points[i]);
         }



Mime
View raw message