You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/10/16 17:11:48 UTC

svn commit: r825925 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math: MessagesResources_fr.java stat/regression/OLSMultipleLinearRegression.java

Author: luc
Date: Fri Oct 16 15:11:47 2009
New Revision: 825925

URL: http://svn.apache.org/viewvc?rev=825925&view=rev
Log:
replaced custom linear solve computation by use of the linear package features

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/MessagesResources_fr.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/MessagesResources_fr.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/MessagesResources_fr.java?rev=825925&r1=825924&r2=825925&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/MessagesResources_fr.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/MessagesResources_fr.java Fri Oct 16 15:11:47 2009
@@ -724,10 +724,6 @@
    { "out of bounds significance level {0}, must be between {1} and {2}",
      "niveau de signification {0} hors domaine, doit \u00eatre entre {1} et {2}" },
 
-   // org.apache.commons.math.stat.regression.OLSMultipleLinearRegression
-   { "matrix is not upper-triangular, entry ({0}, {1}) = {2} is too large",
-     "matrice non triangulaire sup\u00e9rieure, l''\u00e9l\u00e9ment ({0}, {1}) = {2} est trop grand" },
-
    // org.apache.commons.math.stat.regression.AbstractMultipleLinearRegression
    { "not enough data ({0} rows) for this many predictors ({1} predictors)",
      "pas assez de donn\u00e9es ({0} lignes) pour {1} pr\u00e9dicteurs" },

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java?rev=825925&r1=825924&r2=825925&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java Fri Oct 16 15:11:47 2009
@@ -16,14 +16,12 @@
  */
 package org.apache.commons.math.stat.regression;
 
-import org.apache.commons.math.MathRuntimeException;
+import org.apache.commons.math.linear.Array2DRowRealMatrix;
 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;
-import org.apache.commons.math.linear.Array2DRowRealMatrix;
 import org.apache.commons.math.linear.RealVector;
-import org.apache.commons.math.linear.ArrayRealVector;
 
 /**
  * <p>Implements ordinary least squares (OLS) to estimate the parameters of a
@@ -141,7 +139,7 @@
      */
     @Override
     protected RealVector calculateBeta() {
-        return solveUpperTriangular(qr.getR(), qr.getQ().transpose().operate(Y));
+        return qr.getSolver().solve(Y);
     }
 
     /**
@@ -178,65 +176,4 @@
                (X.getRowDimension() - X.getColumnDimension());
     }
 
-    /** TODO:  Find a home for the following methods in the linear package */
-
-    /**
-     * <p>Uses back substitution to solve the system</p>
-     *
-     * <p>coefficients X = constants</p>
-     *
-     * <p>coefficients must upper-triangular and constants must be a column
-     * matrix.  The solution is returned as a column matrix.</p>
-     *
-     * <p>The number of columns in coefficients determines the length
-     * of the returned solution vector (column matrix).  If constants
-     * has more rows than coefficients has columns, excess rows are ignored.
-     * Similarly, extra (zero) rows in coefficients are ignored</p>
-     *
-     * @param coefficients upper-triangular coefficients matrix
-     * @param constants column RHS constants vector
-     * @return solution matrix as a column vector
-     *
-     */
-    private static RealVector solveUpperTriangular(RealMatrix coefficients,
-                                                   RealVector constants) {
-        checkUpperTriangular(coefficients, 1E-12);
-        int length = coefficients.getColumnDimension();
-        double x[] = new double[length];
-        for (int i = 0; i < length; i++) {
-            int index = length - 1 - i;
-            double sum = 0;
-            for (int j = index + 1; j < length; j++) {
-                sum += coefficients.getEntry(index, j) * x[j];
-            }
-            x[index] = (constants.getEntry(index) - sum) / coefficients.getEntry(index, index);
-        }
-        return new ArrayRealVector(x);
-    }
-
-    /**
-     * <p>Check if a matrix is upper-triangular.</p>
-     *
-     * <p>Makes sure all below-diagonal elements are within epsilon of 0.</p>
-     *
-     * @param m matrix to check
-     * @param epsilon maximum allowable absolute value for elements below
-     * the main diagonal
-     *
-     * @throws IllegalArgumentException if m is not upper-triangular
-     */
-    private static void checkUpperTriangular(RealMatrix m, double epsilon) {
-        int nCols = m.getColumnDimension();
-        int nRows = m.getRowDimension();
-        for (int r = 0; r < nRows; r++) {
-            int bound = Math.min(r, nCols);
-            for (int c = 0; c < bound; c++) {
-                if (Math.abs(m.getEntry(r, c)) > epsilon) {
-                    throw MathRuntimeException.createIllegalArgumentException(
-                          "matrix is not upper-triangular, entry ({0}, {1}) = {2} is too large",
-                          r, c, m.getEntry(r, c));
-                }
-            }
-        }
-    }
 }