commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r731356 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/stat/regression/ test/org/apache/commons/math/stat/regression/
Date Sun, 04 Jan 2009 20:39:45 GMT
Author: psteitz
Date: Sun Jan  4 12:39:45 2009
New Revision: 731356

URL: http://svn.apache.org/viewvc?rev=731356&view=rev
Log:
Added standard errors.

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/AbstractMultipleLinearRegression.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/MultipleLinearRegression.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/stat/regression/OLSMultipleLinearRegressionTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/AbstractMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/AbstractMultipleLinearRegression.java?rev=731356&r1=731355&r2=731356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/AbstractMultipleLinearRegression.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/AbstractMultipleLinearRegression.java
Sun Jan  4 12:39:45 2009
@@ -139,6 +139,20 @@
     public double[][] estimateRegressionParametersVariance() {
         return calculateBetaVariance().getData();
     }
+    
+    /**
+     * {@inheritDoc}
+     */
+    public double[] estimateRegressionParametersStandardErrors() {
+        double[][] betaVariance = estimateRegressionParametersVariance();
+        double sigma = calculateYVariance();
+        int length = betaVariance[0].length;
+        double[] result = new double[length];
+        for (int i = 0; i < length; i++) {
+            result[i] = Math.sqrt(sigma * betaVariance[i][i]);
+        }
+        return result;
+    }
 
     /**
      * {@inheritDoc}

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/MultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/MultipleLinearRegression.java?rev=731356&r1=731355&r2=731356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/MultipleLinearRegression.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/MultipleLinearRegression.java
Sun Jan  4 12:39:45 2009
@@ -59,5 +59,12 @@
      * @return The double representing the variance of y
      */
     double estimateRegressandVariance();
+    
+    /**
+     * Returns the standard errors of the regression parameters.
+     * 
+     * @return standard errors of estimated regression parameters
+     */
+     double[] estimateRegressionParametersStandardErrors();
 
 }

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java?rev=731356&r1=731355&r2=731356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
Sun Jan  4 12:39:45 2009
@@ -143,6 +143,7 @@
      * @return The beta variance
      */
     protected RealMatrix calculateBetaVariance() {
+        //TODO:  find a way to use QR decomp to avoid inverting XX' here
         RealMatrix XTX = X.transpose().multiply(X);
         return new LUDecompositionImpl(XTX).getSolver().getInverse();
     }

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/stat/regression/OLSMultipleLinearRegressionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/regression/OLSMultipleLinearRegressionTest.java?rev=731356&r1=731355&r2=731356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/stat/regression/OLSMultipleLinearRegressionTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/stat/regression/OLSMultipleLinearRegressionTest.java
Sun Jan  4 12:39:45 2009
@@ -149,8 +149,17 @@
                       1E-8);
         
         // Check standard errors from NIST
-        double[][] errors = model.estimateRegressionParametersVariance();
-        //TODO:  translate this into std error vector and check  
+        double[] errors = model.estimateRegressionParametersStandardErrors();
+        TestUtils.assertEquals(new double[] {890420.383607373,
+                       84.9149257747669,
+                       0.334910077722432E-01,
+                       0.488399681651699,
+                       0.214274163161675,
+                       0.226073200069370,
+                       455.478499142212}, errors, 1E-2); // Ugh..
+        // Bad accuracy is in intercept std error estimate.  Could be due to
+        // Current impl inverting XX' to get standard errors.
+        
     }
     
     /**
@@ -245,7 +254,15 @@
                 5.4326230830188482,-7.2375578629692230,2.1671550814448222,
                 15.0147574652763112,4.8625103516321015,-7.1597256413907706,
                 -0.4515205619767598,-10.2916870903837587,-15.7812984571900063},
-                1E-12);  
+                1E-12); 
+        
+        // Check standard errors from R
+        double[] errors = model.estimateRegressionParametersStandardErrors();
+        TestUtils.assertEquals(new double[] {6.94881329475087,
+                0.07360008972340,
+                0.27410957467466,
+                0.19454551679325,
+                0.03726654773803}, errors, 1E-10); 
     }
     
     /**



Mime
View raw message