commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r705211 - in /commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math: estimation/GaussNewtonEstimator.java stat/regression/GLSMultipleLinearRegression.java stat/regression/OLSMultipleLinearRegression.java
Date Thu, 16 Oct 2008 11:34:41 GMT
Author: luc
Date: Thu Oct 16 04:34:38 2008
New Revision: 705211

URL: http://svn.apache.org/viewvc?rev=705211&view=rev
Log:
replaced calls to deprecated methods from linear algebra package

Modified:
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java?rev=705211&r1=705210&r2=705211&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/estimation/GaussNewtonEstimator.java
Thu Oct 16 04:34:38 2008
@@ -20,8 +20,11 @@
 import java.io.Serializable;
 
 import org.apache.commons.math.linear.InvalidMatrixException;
+import org.apache.commons.math.linear.LUDecompositionImpl;
 import org.apache.commons.math.linear.RealMatrix;
 import org.apache.commons.math.linear.RealMatrixImpl;
+import org.apache.commons.math.linear.RealVector;
+import org.apache.commons.math.linear.RealVectorImpl;
 
 /** 
  * This class implements a solver for estimation problems.
@@ -106,8 +109,8 @@
 
         // work matrices
         double[] grad             = new double[parameters.length];
-        RealMatrixImpl bDecrement = new RealMatrixImpl(parameters.length, 1);
-        double[][] bDecrementData = bDecrement.getDataRef();
+        RealVectorImpl bDecrement = new RealVectorImpl(parameters.length);
+        double[] bDecrementData   = bDecrement.getDataRef();
         RealMatrixImpl wGradGradT = new RealMatrixImpl(parameters.length, parameters.length);
         double[][] wggData        = wGradGradT.getDataRef();
 
@@ -117,7 +120,7 @@
 
             // build the linear problem
             incrementJacobianEvaluationsCounter();
-            RealMatrix b = new RealMatrixImpl(parameters.length, 1);
+            RealVector b = new RealVectorImpl(parameters.length);
             RealMatrix a = new RealMatrixImpl(parameters.length, parameters.length);
             for (int i = 0; i < measurements.length; ++i) {
                 if (! measurements [i].isIgnored()) {
@@ -128,7 +131,7 @@
                     // compute the normal equation
                     for (int j = 0; j < parameters.length; ++j) {
                         grad[j] = measurements[i].getPartial(parameters[j]);
-                        bDecrementData[j][0] = weight * residual * grad[j];
+                        bDecrementData[j] = weight * residual * grad[j];
                     }
 
                     // build the contribution matrix for measurement i
@@ -150,11 +153,11 @@
             try {
 
                 // solve the linearized least squares problem
-                RealMatrix dX = a.solve(b);
+                RealVector dX = new LUDecompositionImpl(a).solve(b);
 
                 // update the estimated parameters
                 for (int i = 0; i < parameters.length; ++i) {
-                    parameters[i].setEstimate(parameters[i].getEstimate() + dX.getEntry(i,
0));
+                    parameters[i].setEstimate(parameters[i].getEstimate() + dX.getEntry(i));
                 }
 
             } catch(InvalidMatrixException e) {

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java?rev=705211&r1=705210&r2=705211&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java
Thu Oct 16 04:34:38 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math.stat.regression;
 
+import org.apache.commons.math.linear.LUDecompositionImpl;
 import org.apache.commons.math.linear.RealMatrix;
 import org.apache.commons.math.linear.RealMatrixImpl;
 
@@ -44,6 +45,9 @@
     /** Covariance matrix. */
     private RealMatrix Omega;
 
+    /** Inverse of covariance matrix. */
+    private RealMatrix OmegaInverse;
+
     public void newSampleData(double[] y, double[][] x, double[][] covariance) {
         validateSampleData(x, y);
         newYSampleData(y);
@@ -59,6 +63,19 @@
      */
     protected void newCovarianceData(double[][] omega){
         this.Omega = new RealMatrixImpl(omega);
+        this.OmegaInverse = null;
+    }
+
+    /**
+     * Get the inverse of the covariance.
+     * <p>The inverse of the covariance matrix is lazily evaluated and cached.</p>
+     * @return inverse of the covariance
+     */
+    protected RealMatrix getOmegaInverse() {
+        if (OmegaInverse == null) {
+            OmegaInverse = new LUDecompositionImpl(Omega).getInverse();
+        }
+        return OmegaInverse;
     }
     
     /**
@@ -69,10 +86,10 @@
      * @return beta
      */
     protected RealMatrix calculateBeta() {
-        RealMatrix OI = Omega.inverse();
+        RealMatrix OI = getOmegaInverse();
         RealMatrix XT = X.transpose();
         RealMatrix XTOIX = XT.multiply(OI).multiply(X);
-        return XTOIX.inverse().multiply(XT).multiply(OI).multiply(Y);
+        return new LUDecompositionImpl(XTOIX).getInverse().multiply(XT).multiply(OI).multiply(Y);
     }
 
     /**
@@ -83,8 +100,9 @@
      * @return The beta variance matrix
      */
     protected RealMatrix calculateBetaVariance() {
-        RealMatrix XTOIX = X.transpose().multiply(Omega.inverse()).multiply(X);
-        return XTOIX.inverse();
+        RealMatrix OI = getOmegaInverse();
+        RealMatrix XTOIX = X.transpose().multiply(OI).multiply(X);
+        return new LUDecompositionImpl(XTOIX).getInverse();
     }
 
     /**
@@ -96,7 +114,7 @@
      */
     protected double calculateYVariance() {
         RealMatrix u = calculateResiduals();
-        RealMatrix sse =  u.transpose().multiply(Omega.inverse()).multiply(u);
+        RealMatrix sse =  u.transpose().multiply(getOmegaInverse()).multiply(u);
         return sse.getTrace()/(X.getRowDimension()-X.getColumnDimension());
     }
     

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java?rev=705211&r1=705210&r2=705211&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
Thu Oct 16 04:34:38 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math.stat.regression;
 
+import org.apache.commons.math.linear.LUDecompositionImpl;
 import org.apache.commons.math.linear.QRDecomposition;
 import org.apache.commons.math.linear.QRDecompositionImpl;
 import org.apache.commons.math.linear.RealMatrix;
@@ -107,7 +108,7 @@
      */
     protected RealMatrix calculateBetaVariance() {
         RealMatrix XTX = X.transpose().multiply(X);
-        return XTX.inverse();
+        return new LUDecompositionImpl(XTX).getInverse();
     }
     
 



Mime
View raw message