commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1296570 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/stat/regression/ test/java/org/apache/commons/math3/stat/regression/
Date Sat, 03 Mar 2012 03:35:21 GMT
Author: erans
Date: Sat Mar  3 03:35:20 2012
New Revision: 1296570

URL: http://svn.apache.org/viewvc?rev=1296570&view=rev
Log:
Variable visibility: "protected" -> "private". Added "protected"
getter methods.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegression.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegressionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressionAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.java?rev=1296570&r1=1296569&r2=1296570&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.java
Sat Mar  3 03:35:20 2012
@@ -39,15 +39,29 @@ public abstract class AbstractMultipleLi
         MultipleLinearRegression {
 
     /** X sample data. */
-    protected RealMatrix X;
+    private RealMatrix xMatrix;
 
     /** Y sample data. */
-    protected RealVector Y;
+    private RealVector yVector;
 
     /** Whether or not the regression model includes an intercept.  True means no intercept.
*/
     private boolean noIntercept = false;
 
     /**
+     * @return the X sample data.
+     */
+    protected RealMatrix getX() {
+        return xMatrix;
+    }
+
+    /**
+     * @return the Y sample data.
+     */
+    protected RealVector getY() {
+        return yVector;
+    }
+
+    /**
      * @return true if the model has no intercept term; false otherwise
      * @since 2.2
      */
@@ -121,8 +135,8 @@ public abstract class AbstractMultipleLi
                 x[i][j] = data[pointer++];
             }
         }
-        this.X = new Array2DRowRealMatrix(x);
-        this.Y = new ArrayRealVector(y);
+        this.xMatrix = new Array2DRowRealMatrix(x);
+        this.yVector = new ArrayRealVector(y);
     }
 
     /**
@@ -139,7 +153,7 @@ public abstract class AbstractMultipleLi
         if (y.length == 0) {
             throw new NoDataException();
         }
-        this.Y = new ArrayRealVector(y);
+        this.yVector = new ArrayRealVector(y);
     }
 
     /**
@@ -175,7 +189,7 @@ public abstract class AbstractMultipleLi
             throw new NoDataException();
         }
         if (noIntercept) {
-            this.X = new Array2DRowRealMatrix(x, true);
+            this.xMatrix = new Array2DRowRealMatrix(x, true);
         } else { // Augment design matrix with initial unitary column
             final int nVars = x[0].length;
             final double[][] xAug = new double[x.length][nVars + 1];
@@ -186,7 +200,7 @@ public abstract class AbstractMultipleLi
                 xAug[i][0] = 1.0d;
                 System.arraycopy(x[i], 0, xAug[i], 1, nVars);
             }
-            this.X = new Array2DRowRealMatrix(xAug, false);
+            this.xMatrix = new Array2DRowRealMatrix(xAug, false);
         }
     }
 
@@ -257,7 +271,7 @@ public abstract class AbstractMultipleLi
      */
     public double[] estimateResiduals() {
         RealVector b = calculateBeta();
-        RealVector e = Y.subtract(X.operate(b));
+        RealVector e = yVector.subtract(xMatrix.operate(b));
         return e.toArray();
     }
 
@@ -332,7 +346,7 @@ public abstract class AbstractMultipleLi
      * @return Y variance
      */
     protected double calculateYVariance() {
-        return new Variance().evaluate(Y.toArray());
+        return new Variance().evaluate(yVector.toArray());
     }
 
     /**
@@ -349,7 +363,7 @@ public abstract class AbstractMultipleLi
     protected double calculateErrorVariance() {
         RealVector residuals = calculateResiduals();
         return residuals.dotProduct(residuals) /
-               (X.getRowDimension() - X.getColumnDimension());
+               (xMatrix.getRowDimension() - xMatrix.getColumnDimension());
     }
 
     /**
@@ -364,7 +378,7 @@ public abstract class AbstractMultipleLi
      */
     protected RealVector calculateResiduals() {
         RealVector b = calculateBeta();
-        return Y.subtract(X.operate(b));
+        return yVector.subtract(xMatrix.operate(b));
     }
 
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegression.java?rev=1296570&r1=1296569&r2=1296570&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegression.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegression.java
Sat Mar  3 03:35:20 2012
@@ -93,10 +93,10 @@ public class GLSMultipleLinearRegression
     @Override
     protected RealVector calculateBeta() {
         RealMatrix OI = getOmegaInverse();
-        RealMatrix XT = X.transpose();
-        RealMatrix XTOIX = XT.multiply(OI).multiply(X);
+        RealMatrix XT = getX().transpose();
+        RealMatrix XTOIX = XT.multiply(OI).multiply(getX());
         RealMatrix inverse = new LUDecomposition(XTOIX).getSolver().getInverse();
-        return inverse.multiply(XT).multiply(OI).operate(Y);
+        return inverse.multiply(XT).multiply(OI).operate(getY());
     }
 
     /**
@@ -109,7 +109,7 @@ public class GLSMultipleLinearRegression
     @Override
     protected RealMatrix calculateBetaVariance() {
         RealMatrix OI = getOmegaInverse();
-        RealMatrix XTOIX = X.transpose().multiply(OI).multiply(X);
+        RealMatrix XTOIX = getX().transpose().multiply(OI).multiply(getX());
         return new LUDecomposition(XTOIX).getSolver().getInverse();
     }
 
@@ -129,7 +129,7 @@ public class GLSMultipleLinearRegression
     protected double calculateErrorVariance() {
         RealVector residuals = calculateResiduals();
         double t = residuals.dotProduct(getOmegaInverse().operate(residuals));
-        return t / (X.getRowDimension() - X.getColumnDimension());
+        return t / (getX().getRowDimension() - getX().getColumnDimension());
 
     }
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java?rev=1296570&r1=1296569&r2=1296570&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
Sat Mar  3 03:35:20 2012
@@ -78,7 +78,7 @@ public class OLSMultipleLinearRegression
     @Override
     public void newSampleData(double[] data, int nobs, int nvars) {
         super.newSampleData(data, nobs, nvars);
-        qr = new QRDecomposition(X);
+        qr = new QRDecomposition(getX());
     }
 
     /**
@@ -132,9 +132,9 @@ public class OLSMultipleLinearRegression
      */
     public double calculateTotalSumOfSquares() {
         if (isNoIntercept()) {
-            return StatUtils.sumSq(Y.toArray());
+            return StatUtils.sumSq(getY().toArray());
         } else {
-            return new SecondMoment().evaluate(Y.toArray());
+            return new SecondMoment().evaluate(getY().toArray());
         }
     }
 
@@ -180,12 +180,12 @@ public class OLSMultipleLinearRegression
      * @since 2.2
      */
     public double calculateAdjustedRSquared() {
-        final double n = X.getRowDimension();
+        final double n = getX().getRowDimension();
         if (isNoIntercept()) {
-            return 1 - (1 - calculateRSquared()) * (n / (n - X.getColumnDimension()));
+            return 1 - (1 - calculateRSquared()) * (n / (n - getX().getColumnDimension()));
         } else {
             return 1 - (calculateResidualSumOfSquares() * (n - 1)) /
-                (calculateTotalSumOfSquares() * (n - X.getColumnDimension()));
+                (calculateTotalSumOfSquares() * (n - getX().getColumnDimension()));
         }
     }
 
@@ -197,7 +197,7 @@ public class OLSMultipleLinearRegression
     @Override
     protected void newXSampleData(double[][] x) {
         super.newXSampleData(x);
-        qr = new QRDecomposition(X);
+        qr = new QRDecomposition(getX());
     }
 
     /**
@@ -207,7 +207,7 @@ public class OLSMultipleLinearRegression
      */
     @Override
     protected RealVector calculateBeta() {
-        return qr.getSolver().solve(Y);
+        return qr.getSolver().solve(getY());
     }
 
     /**
@@ -223,7 +223,7 @@ public class OLSMultipleLinearRegression
      */
     @Override
     protected RealMatrix calculateBetaVariance() {
-        int p = X.getColumnDimension();
+        int p = getX().getColumnDimension();
         RealMatrix Raug = qr.getR().getSubMatrix(0, p - 1 , 0, p - 1);
         RealMatrix Rinv = new LUDecomposition(Raug).getSolver().getInverse();
         return Rinv.multiply(Rinv.transpose());

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegressionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegressionTest.java?rev=1296570&r1=1296569&r2=1296570&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegressionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegressionTest.java
Sat Mar  3 03:35:20 2012
@@ -178,13 +178,13 @@ public class GLSMultipleLinearRegression
         double[][] covariance = MatrixUtils.createRealIdentityMatrix(4).scalarMultiply(2).getData();
         GLSMultipleLinearRegression regression = new GLSMultipleLinearRegression();
         regression.newSampleData(y, x, covariance);
-        RealMatrix combinedX = regression.X.copy();
-        RealVector combinedY = regression.Y.copy();
+        RealMatrix combinedX = regression.getX().copy();
+        RealVector combinedY = regression.getY().copy();
         RealMatrix combinedCovInv = regression.getOmegaInverse();
         regression.newXSampleData(x);
         regression.newYSampleData(y);
-        Assert.assertEquals(combinedX, regression.X);
-        Assert.assertEquals(combinedY, regression.Y);
+        Assert.assertEquals(combinedX, regression.getX());
+        Assert.assertEquals(combinedY, regression.getY());
         Assert.assertEquals(combinedCovInv, regression.getOmegaInverse());
     }
     
@@ -253,7 +253,7 @@ public class GLSMultipleLinearRegression
         OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
         ols.newSampleData(longley, nObs, 6);
         final RealVector b = ols.calculateBeta().copy();
-        final RealMatrix x = ols.X.copy();
+        final RealMatrix x = ols.getX().copy();
         
         // Create a GLS model to reuse
         GLSMultipleLinearRegression gls = new GLSMultipleLinearRegression();

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressionAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressionAbstractTest.java?rev=1296570&r1=1296569&r2=1296570&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressionAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressionAbstractTest.java
Sat Mar  3 03:35:20 2012
@@ -86,22 +86,22 @@ public abstract class MultipleLinearRegr
         };
         AbstractMultipleLinearRegression regression = createRegression();
         regression.newSampleData(design, 4, 3);
-        RealMatrix flatX = regression.X.copy();
-        RealVector flatY = regression.Y.copy();
+        RealMatrix flatX = regression.getX().copy();
+        RealVector flatY = regression.getY().copy();
         regression.newXSampleData(x);
         regression.newYSampleData(y);
-        Assert.assertEquals(flatX, regression.X);
-        Assert.assertEquals(flatY, regression.Y);
+        Assert.assertEquals(flatX, regression.getX());
+        Assert.assertEquals(flatY, regression.getY());
         
         // No intercept
         regression.setNoIntercept(true);
         regression.newSampleData(design, 4, 3);
-        flatX = regression.X.copy();
-        flatY = regression.Y.copy();
+        flatX = regression.getX().copy();
+        flatY = regression.getY().copy();
         regression.newXSampleData(x);
         regression.newYSampleData(y);
-        Assert.assertEquals(flatX, regression.X);
-        Assert.assertEquals(flatY, regression.Y);
+        Assert.assertEquals(flatX, regression.getX());
+        Assert.assertEquals(flatY, regression.getY());
     }
     
     @Test(expected=IllegalArgumentException.class)

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java?rev=1296570&r1=1296569&r2=1296570&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
Sat Mar  3 03:35:20 2012
@@ -434,7 +434,7 @@ public class OLSMultipleLinearRegression
          */
         double[] residuals = model.estimateResiduals();
         RealMatrix I = MatrixUtils.createRealIdentityMatrix(10);
-        double[] hatResiduals = I.subtract(hat).operate(model.Y).toArray();
+        double[] hatResiduals = I.subtract(hat).operate(model.getY()).toArray();
         TestUtils.assertEquals(residuals, hatResiduals, 10e-12);
     }
 
@@ -457,11 +457,11 @@ public class OLSMultipleLinearRegression
      */
     protected void checkVarianceConsistency(OLSMultipleLinearRegression model) throws Exception
{
         // Check Y variance consistency
-        TestUtils.assertEquals(StatUtils.variance(model.Y.toArray()), model.calculateYVariance(),
0);
+        TestUtils.assertEquals(StatUtils.variance(model.getY().toArray()), model.calculateYVariance(),
0);
         
         // Check residual variance consistency
         double[] residuals = model.calculateResiduals().toArray();
-        RealMatrix X = model.X;
+        RealMatrix X = model.getX();
         TestUtils.assertEquals(
                 StatUtils.variance(model.calculateResiduals().toArray()) * (residuals.length
- 1),
                 model.calculateErrorVariance() * (X.getRowDimension() - X.getColumnDimension()),
1E-20);
@@ -482,22 +482,22 @@ public class OLSMultipleLinearRegression
         };
         OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
         regression.newSampleData(y, x);
-        RealMatrix combinedX = regression.X.copy();
-        RealVector combinedY = regression.Y.copy();
+        RealMatrix combinedX = regression.getX().copy();
+        RealVector combinedY = regression.getY().copy();
         regression.newXSampleData(x);
         regression.newYSampleData(y);
-        Assert.assertEquals(combinedX, regression.X);
-        Assert.assertEquals(combinedY, regression.Y);
+        Assert.assertEquals(combinedX, regression.getX());
+        Assert.assertEquals(combinedY, regression.getY());
         
         // No intercept
         regression.setNoIntercept(true);
         regression.newSampleData(y, x);
-        combinedX = regression.X.copy();
-        combinedY = regression.Y.copy();
+        combinedX = regression.getX().copy();
+        combinedY = regression.getY().copy();
         regression.newXSampleData(x);
         regression.newYSampleData(y);
-        Assert.assertEquals(combinedX, regression.X);
-        Assert.assertEquals(combinedY, regression.Y);
+        Assert.assertEquals(combinedX, regression.getX());
+        Assert.assertEquals(combinedY, regression.getY());
     }
     
     @Test(expected=IllegalArgumentException.class)



Mime
View raw message