commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject [math] Improved or corrected javadoc; added some tests.
Date Sun, 11 Jan 2015 03:02:46 GMT
Repository: commons-math
Updated Branches:
  refs/heads/master e42ab0ec6 -> 3a7519fcb


Improved or corrected javadoc; added some tests.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/3a7519fc
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/3a7519fc
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/3a7519fc

Branch: refs/heads/master
Commit: 3a7519fcb190a096f4f00b9da357f79acd517bc7
Parents: e42ab0e
Author: Phil Steitz <phil.steitz@gmail.com>
Authored: Sat Jan 10 20:02:30 2015 -0700
Committer: Phil Steitz <phil.steitz@gmail.com>
Committed: Sat Jan 10 20:02:30 2015 -0700

----------------------------------------------------------------------
 .../regression/OLSMultipleLinearRegression.java | 27 ++++++++++-----
 .../OLSMultipleLinearRegressionTest.java        | 35 ++++++++++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/3a7519fc/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
b/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
index 8c5dc4c..7fff940 100644
--- a/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
+++ b/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.java
@@ -156,12 +156,11 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
      * the {@link #calculateRSquared() R-squared} computation.</p>
      *
      * @return SSTO - the total sum of squares
-     * @throws MathIllegalArgumentException if the sample has not been set or does
-     * not contain at least 3 observations
+     * @throws NullPointerException if the sample has not been set
      * @see #isNoIntercept()
      * @since 2.2
      */
-    public double calculateTotalSumOfSquares() throws MathIllegalArgumentException {
+    public double calculateTotalSumOfSquares() {
         if (isNoIntercept()) {
             return StatUtils.sumSq(getY().toArray());
         } else {
@@ -174,6 +173,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
      *
      * @return residual sum of squares
      * @since 2.2
+     * @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix
is singular
+     * @throws NullPointerException if the data for the model have not been loaded
      */
     public double calculateResidualSumOfSquares() {
         final RealVector residuals = calculateResiduals();
@@ -188,12 +189,14 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
      * where SSR is the {@link #calculateResidualSumOfSquares() sum of squared residuals}
      * and SSTO is the {@link #calculateTotalSumOfSquares() total sum of squares}
      *
+     * <p>If there is no variance in y, i.e., SSTO = 0, NaN is returned.</p>
+     *
      * @return R-square statistic
-     * @throws MathIllegalArgumentException if the sample has not been set or does
-     * not contain at least 3 observations
+     * @throws NullPointerException if the sample has not been set
+     * @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix
is singular
      * @since 2.2
      */
-    public double calculateRSquared() throws MathIllegalArgumentException {
+    public double calculateRSquared() {
         return 1 - calculateResidualSumOfSquares() / calculateTotalSumOfSquares();
     }
 
@@ -209,13 +212,15 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
      * <code> 1 - (1 - {@link #calculateRSquared()}) * (n / (n - p)) </code>
      * </pre></p>
      *
+     * <p>If there is no variance in y, i.e., SSTO = 0, NaN is returned.</p>
+     *
      * @return adjusted R-Squared statistic
-     * @throws MathIllegalArgumentException if the sample has not been set or does
-     * not contain at least 3 observations
+     * @throws NullPointerException if the sample has not been set
+     * @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix
is singular
      * @see #isNoIntercept()
      * @since 2.2
      */
-    public double calculateAdjustedRSquared() throws MathIllegalArgumentException {
+    public double calculateAdjustedRSquared() {
         final double n = getX().getRowDimension();
         if (isNoIntercept()) {
             return 1 - (1 - calculateRSquared()) * (n / (n - getX().getColumnDimension()));
@@ -244,6 +249,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
      * a {@code NullPointerException} will be thrown.</p>
      *
      * @return beta
+     * @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix
is singular
+     * @throws NullPointerException if the data for the model have not been loaded
      */
     @Override
     protected RealVector calculateBeta() {
@@ -264,6 +271,8 @@ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegressio
      * a {@code NullPointerException} will be thrown.</p>
      *
      * @return The beta variance-covariance matrix
+     * @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix
is singular
+     * @throws NullPointerException if the data for the model have not been loaded
      */
     @Override
     protected RealMatrix calculateBetaVariance() {

http://git-wip-us.apache.org/repos/asf/commons-math/blob/3a7519fc/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
b/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
index 6ae5d2c..d719c8b 100644
--- a/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
+++ b/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.java
@@ -782,4 +782,39 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
         TestUtils.assertEquals(835542680000.000, model.calculateResidualSumOfSquares(), 1.0e-3);
         return;
     }
+    
+    /**
+     * Anything requiring beta calculation should advertise SME.
+     */
+    @Test(expected=org.apache.commons.math3.linear.SingularMatrixException.class)
+    public void testSingularCalculateBeta() {
+        OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
+        model.newSampleData(new double[] {1,  2,  3, 1, 2, 3, 1, 2, 3}, 3, 2);
+        model.calculateBeta();
+    }
+    
+    @Test
+    public void testNoSSTOCalculateRsquare() {
+        OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
+        model.newSampleData(new double[] {1,  2,  3, 1, 7, 8, 1, 10, 12}, 3, 2);
+        Assert.assertTrue(Double.isNaN(model.calculateRSquared()));
+    }
+    
+    @Test(expected=NullPointerException.class)
+    public void testNoDataNPECalculateBeta() {
+        OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
+        model.calculateBeta();
+    }
+    
+    @Test(expected=NullPointerException.class)
+    public void testNoDataNPECalculateHat() {
+        OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
+        model.calculateHat();
+    }
+    
+    @Test(expected=NullPointerException.class)
+    public void testNoDataNPESSTO() {
+        OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
+        model.calculateTotalSumOfSquares();
+    }
 }


Mime
View raw message