Repository: commons-math Updated Branches: refs/heads/master 2f461bdb0 -> afcfbf57b
http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java b/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java index a2f5f62..06fbc26 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java @@ -20,6 +20,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.apache.commons.math4.TestUtils; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.linear.MatrixUtils; import org.apache.commons.math4.linear.RealMatrix; @@ -38,7 +39,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs private double[] y; private double[][] x; private double[][] omega; - private double[] longley = new double[] { + private final double[] longley = new double[] { 60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, @@ -88,7 +89,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs createRegression().newSampleData(null, new double[][]{}, null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MathIllegalArgumentException.class) public void cannotAddSampleDataWithSizeMismatch() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[1][]; @@ -96,12 +97,12 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs createRegression().newSampleData(y, x, null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MathIllegalArgumentException.class) public void cannotAddNullCovarianceData() { createRegression().newSampleData(new double[]{}, new double[][]{}, null); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MathIllegalArgumentException.class) public void notEnoughData() { double[] reducedY = new double[y.length - 1]; double[][] reducedX = new double[x.length - 1][]; @@ -112,7 +113,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs createRegression().newSampleData(reducedY, reducedX, reducedO); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MathIllegalArgumentException.class) public void cannotAddCovarianceDataWithSampleSizeMismatch() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[2][]; @@ -123,7 +124,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs createRegression().newSampleData(y, x, omega); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MathIllegalArgumentException.class) public void cannotAddCovarianceDataThatIsNotSquare() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[2][]; @@ -165,18 +166,18 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs model.newSampleData(y, x, omega); TestUtils.assertEquals(model.calculateYVariance(), 3.5, 0); } - + /** * Verifies that setting X, Y and covariance separately has the same effect as newSample(X,Y,cov). */ @Test public void testNewSample2() { - double[] y = new double[] {1, 2, 3, 4}; + double[] y = new double[] {1, 2, 3, 4}; double[][] x = new double[][] { {19, 22, 33}, {20, 30, 40}, {25, 35, 45}, - {27, 37, 47} + {27, 37, 47} }; double[][] covariance = MatrixUtils.createRealIdentityMatrix(4).scalarMultiply(2).getData(); GLSMultipleLinearRegression regression = new GLSMultipleLinearRegression(); @@ -190,13 +191,13 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs Assert.assertEquals(combinedY, regression.getY()); Assert.assertEquals(combinedCovInv, regression.getOmegaInverse()); } - + /** * Verifies that GLS with identity covariance matrix gives the same results * as OLS. */ @Test - public void testGLSOLSConsistency() { + public void testGLSOLSConsistency() { RealMatrix identityCov = MatrixUtils.createRealIdentityMatrix(16); GLSMultipleLinearRegression glsModel = new GLSMultipleLinearRegression(); OLSMultipleLinearRegression olsModel = new OLSMultipleLinearRegression(); @@ -211,7 +212,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs TestUtils.assertRelativelyEquals(olsBeta[i], glsBeta[i], 10E-7); } } - + /** * Generate an error covariance matrix and sample data representing models * with this error structure. Then verify that GLS estimated coefficients, @@ -221,7 +222,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs public void testGLSEfficiency() { RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(200); // Seed has been selected to generate non-trivial covariance - + // Assume model has 16 observations (will use Longley data). Start by generating // non-constant variances for the 16 error terms. final int nObs = 16; @@ -229,7 +230,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs for (int i = 0; i < nObs; i++) { sigma[i] = 10 * rg.nextDouble(); } - + // Now generate 1000 error vectors to use to estimate the covariance matrix // Columns are draws on N(0, sigma[col]) final int numSeeds = 1000; @@ -239,16 +240,16 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs errorSeeds.setEntry(i, j, rg.nextGaussian() * sigma[j]); } } - + // Get covariance matrix for columns RealMatrix cov = (new Covariance(errorSeeds)).getCovarianceMatrix(); - + // Create a CorrelatedRandomVectorGenerator to use to generate correlated errors GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg); double[] errorMeans = new double[nObs]; // Counting on init to 0 here CorrelatedRandomVectorGenerator gen = new CorrelatedRandomVectorGenerator(errorMeans, cov, 1.0e-12 * cov.getNorm(), rawGenerator); - + // Now start generating models. Use Longley X matrix on LHS // and Longley OLS beta vector as "true" beta. Generate // Y values by XB + u where u is a CorrelatedRandomVector generated @@ -257,44 +258,44 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs ols.newSampleData(longley, nObs, 6); final RealVector b = ols.calculateBeta().copy(); final RealMatrix x = ols.getX().copy(); - + // Create a GLS model to reuse GLSMultipleLinearRegression gls = new GLSMultipleLinearRegression(); gls.newSampleData(longley, nObs, 6); gls.newCovarianceData(cov.getData()); - + // Create aggregators for stats measuring model performance DescriptiveStatistics olsBetaStats = new DescriptiveStatistics(); DescriptiveStatistics glsBetaStats = new DescriptiveStatistics(); - + // Generate Y vectors for 10000 models, estimate GLS and OLS and // Verify that OLS estimates are better final int nModels = 10000; for (int i = 0; i < nModels; i++) { - + // Generate y = xb + u with u cov RealVector u = MatrixUtils.createRealVector(gen.nextVector()); double[] y = u.add(x.operate(b)).toArray(); - + // Estimate OLS parameters ols.newYSampleData(y); RealVector olsBeta = ols.calculateBeta(); - + // Estimate GLS parameters gls.newYSampleData(y); RealVector glsBeta = gls.calculateBeta(); - + // Record deviations from "true" beta double dist = olsBeta.getDistance(b); olsBetaStats.addValue(dist * dist); dist = glsBeta.getDistance(b); glsBetaStats.addValue(dist * dist); - + } - + // Verify that GLS is on average more efficient, lower variance assert(olsBetaStats.getMean() > 1.5 * glsBetaStats.getMean()); - assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation()); + assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation()); } - + } http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/stat/regression/MillerUpdatingRegressionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/regression/MillerUpdatingRegressionTest.java b/src/test/java/org/apache/commons/math4/stat/regression/MillerUpdatingRegressionTest.java index 0034d75..30c5bff 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/MillerUpdatingRegressionTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/MillerUpdatingRegressionTest.java @@ -17,6 +17,7 @@ package org.apache.commons.math4.stat.regression; import org.apache.commons.math4.TestUtils; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.linear.RealMatrix; import org.apache.commons.math4.stat.correlation.PearsonsCorrelation; import org.apache.commons.math4.stat.regression.MillerUpdatingRegression; @@ -33,9 +34,9 @@ public class MillerUpdatingRegressionTest { public MillerUpdatingRegressionTest() { } - /* This is the Greene Airline Cost data. + /* This is the Greene Airline Cost data. * The data can be downloaded from http://www.indiana.edu/~statmath/stat/all/panel/airline.csv - */ + */ private final static double[][] airdata = { /*"I",*/new double[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, /*"T",*/ new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, @@ -94,44 +95,44 @@ public class MillerUpdatingRegressionTest { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); try { instance.addObservation(new double[]{1.0}, 0.0); - Assert.fail("Should throw IllegalArgumentException"); - } catch (IllegalArgumentException iae) { + Assert.fail("Should throw MathIllegalArgumentException"); + } catch (MathIllegalArgumentException iae) { } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException"); + Assert.fail("Should throw MathIllegalArgumentException"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0); - Assert.fail("Should throw IllegalArgumentException"); - } catch (IllegalArgumentException iae) { + Assert.fail("Should throw MathIllegalArgumentException"); + } catch (MathIllegalArgumentException iae) { } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException"); + Assert.fail("Should throw MathIllegalArgumentException"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0}, 0.0); } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException"); + Assert.fail("Should throw MathIllegalArgumentException"); } //now we try it without an intercept instance = new MillerUpdatingRegression(3, false); try { instance.addObservation(new double[]{1.0}, 0.0); - Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); - } catch (IllegalArgumentException iae) { + Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]"); + } catch (MathIllegalArgumentException iae) { } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); + Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0); - Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); - } catch (IllegalArgumentException iae) { + Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]"); + } catch (MathIllegalArgumentException iae) { } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); + Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0}, 0.0); } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); + Assert.fail("Should throw MathIllegalArgumentException [NOINTERCEPT]"); } } @@ -143,10 +144,10 @@ public class MillerUpdatingRegressionTest { double[] y = {1.0}; instance.addObservations(tst, y); - Assert.fail("Should throw IllegalArgumentException"); - } catch (IllegalArgumentException iae) { + Assert.fail("Should throw MathIllegalArgumentException"); + } catch (MathIllegalArgumentException iae) { } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException"); + Assert.fail("Should throw MathIllegalArgumentException"); } try { @@ -154,10 +155,10 @@ public class MillerUpdatingRegressionTest { double[] y = {1.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; instance.addObservations(tst, y); - Assert.fail("Should throw IllegalArgumentException"); - } catch (IllegalArgumentException iae) { + Assert.fail("Should throw MathIllegalArgumentException"); + } catch (MathIllegalArgumentException iae) { } catch (Exception e) { - Assert.fail("Should throw IllegalArgumentException"); + Assert.fail("Should throw MathIllegalArgumentException"); } } @@ -330,7 +331,7 @@ public class MillerUpdatingRegressionTest { // tmp[6] = tmp[2] * tmp[3]; //^7 // tmp[7] = tmp[3] * tmp[3]; //^8 // tmp[8] = tmp[4] * tmp[3]; //^9 -// tmp[9] = tmp[4] * tmp[4]; //^10 +// tmp[9] = tmp[4] * tmp[4]; //^10 tmp[1] = tmp[0] * tmp[0]; tmp[2] = tmp[0] * tmp[1]; tmp[3] = tmp[0] * tmp[2]; @@ -681,12 +682,12 @@ public class MillerUpdatingRegressionTest { 0.214274163161675, 0.226073200069370, 455.478499142212}, errors, 1E-6); -// +// // Check R-Square statistics against R TestUtils.assertEquals(0.995479004577296, result.getRSquared(), 1E-12); TestUtils.assertEquals(0.992465007628826, result.getAdjustedRSquared(), 1E-12); -// -// +// +// // // Estimate model without intercept model = new MillerUpdatingRegression(6, false); off = 0; @@ -702,13 +703,13 @@ public class MillerUpdatingRegressionTest { new double[]{-52.99357013868291, 0.07107319907358, -0.42346585566399, -0.57256866841929, -0.41420358884978, 48.41786562001326}, 1E-11); -// +// // Check standard errors from R errors = result.getStdErrorOfEstimates(); TestUtils.assertEquals(new double[]{129.54486693117232, 0.03016640003786, 0.41773654056612, 0.27899087467676, 0.32128496193363, 17.68948737819961}, errors, 1E-11); -// +// // // Check R-Square statistics against R TestUtils.assertEquals(0.9999670130706, result.getRSquared(), 1E-12); @@ -1048,11 +1049,11 @@ public class MillerUpdatingRegressionTest { } return; } - - + + @Test public void testSubsetRegression() { - + MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); MillerUpdatingRegression redRegression = new MillerUpdatingRegression(2, true); double[][] x = new double[airdata[0].length][]; @@ -1063,23 +1064,23 @@ public class MillerUpdatingRegressionTest { x[i][0] = FastMath.log(airdata[3][i]); x[i][1] = FastMath.log(airdata[4][i]); x[i][2] = airdata[5][i]; - + xReduced[i] = new double[2]; xReduced[i][0] = FastMath.log(airdata[3][i]); xReduced[i][1] = FastMath.log(airdata[4][i]); - + y[i] = FastMath.log(airdata[2][i]); } instance.addObservations(x, y); redRegression.addObservations(xReduced, y); - + RegressionResults resultsInstance = instance.regress( new int[]{0,1,2} ); RegressionResults resultsReduced = redRegression.regress(); - + TestUtils.assertEquals(resultsInstance.getParameterEstimates(), resultsReduced.getParameterEstimates(), 1.0e-12); TestUtils.assertEquals(resultsInstance.getStdErrorOfEstimates(), resultsReduced.getStdErrorOfEstimates(), 1.0e-12); } - - + + } http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java b/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java index 1fc839b..b7ef50a 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math4.stat.regression; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.linear.RealMatrix; import org.apache.commons.math4.linear.RealVector; @@ -65,7 +66,7 @@ public abstract class MultipleLinearRegressionAbstractTest { Assert.assertTrue(variance > 0.0); } } - + /** * Verifies that newSampleData methods consistently insert unitary columns * in design matrix. Confirms the fix for MATH-411. @@ -78,12 +79,12 @@ public abstract class MultipleLinearRegressionAbstractTest { 3, 25, 35, 45, 4, 27, 37, 47 }; - double[] y = new double[] {1, 2, 3, 4}; + double[] y = new double[] {1, 2, 3, 4}; double[][] x = new double[][] { {19, 22, 33}, {20, 30, 40}, {25, 35, 45}, - {27, 37, 47} + {27, 37, 47} }; AbstractMultipleLinearRegression regression = createRegression(); regression.newSampleData(design, 4, 3); @@ -93,7 +94,7 @@ public abstract class MultipleLinearRegressionAbstractTest { regression.newYSampleData(y); Assert.assertEquals(flatX, regression.getX()); Assert.assertEquals(flatY, regression.getY()); - + // No intercept regression.setNoIntercept(true); regression.newSampleData(design, 4, 3); @@ -104,30 +105,30 @@ public abstract class MultipleLinearRegressionAbstractTest { Assert.assertEquals(flatX, regression.getX()); Assert.assertEquals(flatY, regression.getY()); } - + @Test(expected=NullArgumentException.class) public void testNewSampleNullData() { double[] data = null; - createRegression().newSampleData(data, 2, 3); + createRegression().newSampleData(data, 2, 3); } - - @Test(expected=IllegalArgumentException.class) + + @Test(expected=MathIllegalArgumentException.class) public void testNewSampleInvalidData() { double[] data = new double[] {1, 2, 3, 4}; createRegression().newSampleData(data, 2, 3); } - - @Test(expected=IllegalArgumentException.class) + + @Test(expected=MathIllegalArgumentException.class) public void testNewSampleInsufficientData() { double[] data = new double[] {1, 2, 3, 4}; createRegression().newSampleData(data, 1, 3); } - + @Test(expected=NullArgumentException.class) public void testXSampleDataNull() { createRegression().newXSampleData(null); } - + @Test(expected=NullArgumentException.class) public void testYSampleDataNull() { createRegression().newYSampleData(null); http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java b/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java index d383d0f..16001d1 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java @@ -18,6 +18,7 @@ package org.apache.commons.math4.stat.regression; import org.apache.commons.math4.TestUtils; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.linear.Array2DRowRealMatrix; import org.apache.commons.math4.linear.DefaultRealMatrixChangingVisitor; @@ -65,8 +66,8 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs protected int getSampleSize() { return y.length; } - - @Test(expected=IllegalArgumentException.class) + + @Test(expected=MathIllegalArgumentException.class) public void cannotAddSampleDataWithSizeMismatch() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[1][]; @@ -172,33 +173,33 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 0.214274163161675, 0.226073200069370, 455.478499142212}, errors, 1E-6); - + // Check regression standard error against R Assert.assertEquals(304.8540735619638, model.estimateRegressionStandardError(), 1E-10); - + // Check R-Square statistics against R Assert.assertEquals(0.995479004577296, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.992465007628826, model.calculateAdjustedRSquared(), 1E-12); - + checkVarianceConsistency(model); - + // Estimate model without intercept model.setNoIntercept(true); model.newSampleData(design, nobs, nvars); - + // Check expected beta values from R betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{-52.99357013868291, 0.07107319907358, -0.42346585566399,-0.57256866841929, - -0.41420358884978, 48.41786562001326}, 1E-11); - + -0.41420358884978, 48.41786562001326}, 1E-11); + // Check standard errors from R errors = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(new double[] {129.54486693117232, 0.03016640003786, 0.41773654056612, 0.27899087467676, 0.32128496193363, 17.68948737819961}, errors, 1E-11); - + // Check expected residuals from R residuals = model.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{ @@ -207,14 +208,14 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 73.09368242049943, 913.21694494481869, 424.82484953610174, -8.56475876776709, -361.32974610842876, 27.34560497213464, 151.28955976355002, -492.49937355336846}, 1E-10); - + // Check regression standard error against R Assert.assertEquals(475.1655079819517, model.estimateRegressionStandardError(), 1E-10); - + // Check R-Square statistics against R Assert.assertEquals(0.9999670130706, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.999947220913, model.calculateAdjustedRSquared(), 1E-12); - + } /** @@ -272,7 +273,7 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33 }; - + final int nobs = 47; final int nvars = 4; @@ -317,16 +318,16 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 0.27410957467466, 0.19454551679325, 0.03726654773803}, errors, 1E-10); - + // Check regression standard error against R Assert.assertEquals(7.73642194433223, model.estimateRegressionStandardError(), 1E-12); - + // Check R-Square statistics against R Assert.assertEquals(0.649789742860228, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.6164363850373927, model.calculateAdjustedRSquared(), 1E-12); - + checkVarianceConsistency(model); - + // Estimate the model with no intercept model = new OLSMultipleLinearRegression(); model.setNoIntercept(true); @@ -337,15 +338,15 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs TestUtils.assertEquals(betaHat, new double[]{0.52191832900513, 2.36588087917963, - -0.94770353802795, + -0.94770353802795, 0.30851985863609}, 1E-12); // Check expected residuals from R residuals = model.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{ - 44.138759883538249, 27.720705122356215, 35.873200836126799, + 44.138759883538249, 27.720705122356215, 35.873200836126799, 34.574619581211977, 26.600168342080213, 15.074636243026923, -12.704904871199814, - 1.497443824078134, 2.691972687079431, 5.582798774291231, -4.422986561283165, + 1.497443824078134, 2.691972687079431, 5.582798774291231, -4.422986561283165, -9.198581600334345, 4.481765170730647, 2.273520207553216, -22.649827853221336, -17.747900013943308, 20.298314638496436, 6.861405135329779, -8.684712790954924, -10.298639278062371, -9.896618896845819, 4.568568616351242, -15.313570491727944, @@ -361,10 +362,10 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs errors = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(new double[] {0.10470063765677, 0.41684100584290, 0.43370143099691, 0.07694953606522}, errors, 1E-10); - + // Check regression standard error against R Assert.assertEquals(17.24710630547, model.estimateRegressionStandardError(), 1E-10); - + // Check R-Square statistics against R Assert.assertEquals(0.946350722085, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.9413600915813, model.calculateAdjustedRSquared(), 1E-12); @@ -451,7 +452,7 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs model.newSampleData(y, x); TestUtils.assertEquals(model.calculateYVariance(), 3.5, 0); } - + /** * Verifies that calculateYVariance and calculateResidualVariance return consistent * values with direct variance computation from Y, residuals, respectively. @@ -459,27 +460,27 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs protected void checkVarianceConsistency(OLSMultipleLinearRegression model) { // Check Y variance consistency TestUtils.assertEquals(StatUtils.variance(model.getY().toArray()), model.calculateYVariance(), 0); - + // Check residual variance consistency double[] residuals = model.calculateResiduals().toArray(); RealMatrix X = model.getX(); TestUtils.assertEquals( StatUtils.variance(model.calculateResiduals().toArray()) * (residuals.length - 1), model.calculateErrorVariance() * (X.getRowDimension() - X.getColumnDimension()), 1E-20); - + } - + /** * Verifies that setting X and Y separately has the same effect as newSample(X,Y). */ @Test public void testNewSample2() { - double[] y = new double[] {1, 2, 3, 4}; + double[] y = new double[] {1, 2, 3, 4}; double[][] x = new double[][] { {19, 22, 33}, {20, 30, 40}, {25, 35, 45}, - {27, 37, 47} + {27, 37, 47} }; OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.newSampleData(y, x); @@ -489,7 +490,7 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs regression.newYSampleData(y); Assert.assertEquals(combinedX, regression.getX()); Assert.assertEquals(combinedY, regression.getY()); - + // No intercept regression.setNoIntercept(true); regression.newSampleData(y, x); @@ -500,17 +501,17 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs Assert.assertEquals(combinedX, regression.getX()); Assert.assertEquals(combinedY, regression.getY()); } - + @Test(expected=NullArgumentException.class) public void testNewSampleDataYNull() { createRegression().newSampleData(null, new double[][] {}); } - + @Test(expected=NullArgumentException.class) public void testNewSampleDataXNull() { createRegression().newSampleData(new double[] {}, null); } - + /* * This is a test based on the Wampler1 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler1.shtml @@ -570,7 +571,7 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs new double[]{0.0, 0.0, 0.0, 0.0, 0.0, - 0.0}, 1E-8); + 0.0}, 1E-8); TestUtils.assertEquals(1.0, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(0, model.estimateErrorVariance(), 1.0e-7); @@ -578,7 +579,7 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs return; } - + /* * This is a test based on the Wampler2 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler2.shtml @@ -640,13 +641,13 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs new double[]{0.0, 0.0, 0.0, 0.0, 0.0, - 0.0}, 1E-8); + 0.0}, 1E-8); TestUtils.assertEquals(1.0, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(0, model.estimateErrorVariance(), 1.0e-7); TestUtils.assertEquals(0.00, model.calculateResidualSumOfSquares(), 1.0e-6); return; } - + /* * This is a test based on the Wampler3 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler3.shtml @@ -701,7 +702,7 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 1.0, 1.0, 1.0, - 1.0}, 1E-8); + 1.0}, 1E-8); double[] se = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(se, @@ -770,21 +771,21 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 1.0, 1.0, 1.0, - 1.0}, 1E-6); + 1.0}, 1E-6); double[] se = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(se, new double[]{215232.624678170, 236355.173469681, 77934.3524331583, 10147.5507550350, 564.566512170752, - 11.2324854679312}, 1E-8); + 11.2324854679312}, 1E-8); TestUtils.assertEquals(.957478440825662, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(55702845333.3333, model.estimateErrorVariance(), 1.0e-4); TestUtils.assertEquals(835542680000.000, model.calculateResidualSumOfSquares(), 1.0e-3); return; } - + /** * Anything requiring beta calculation should advertise SME. */ @@ -794,26 +795,26 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs 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(); http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/transform/FastCosineTransformerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/transform/FastCosineTransformerTest.java b/src/test/java/org/apache/commons/math4/transform/FastCosineTransformerTest.java index 44a351a..0769706 100644 --- a/src/test/java/org/apache/commons/math4/transform/FastCosineTransformerTest.java +++ b/src/test/java/org/apache/commons/math4/transform/FastCosineTransformerTest.java @@ -22,6 +22,7 @@ import java.util.Collection; import org.apache.commons.math4.analysis.UnivariateFunction; import org.apache.commons.math4.analysis.function.Sin; import org.apache.commons.math4.analysis.function.Sinc; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.MathIllegalStateException; import org.apache.commons.math4.transform.DctNormalization; import org.apache.commons.math4.transform.FastCosineTransformer; @@ -46,7 +47,7 @@ import org.junit.runners.Parameterized.Parameters; public final class FastCosineTransformerTest extends RealTransformerAbstractTest { - private DctNormalization normalization; + private final DctNormalization normalization; private final int[] invalidDataSize; @@ -229,24 +230,22 @@ public final class FastCosineTransformerTest try { // bad interval transformer.transform(f, 1, -1, 65, TransformType.FORWARD); - Assert.fail("Expecting IllegalArgumentException - bad interval"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException - bad interval"); + } catch (MathIllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 1, TransformType.FORWARD); - Assert - .fail("Expecting IllegalArgumentException - bad samples number"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException - bad samples number"); + } catch (MathIllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 64, TransformType.FORWARD); - Assert - .fail("Expecting IllegalArgumentException - bad samples number"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException - bad samples number"); + } catch (MathIllegalArgumentException ex) { // expected } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/transform/FastHadamardTransformerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/transform/FastHadamardTransformerTest.java b/src/test/java/org/apache/commons/math4/transform/FastHadamardTransformerTest.java index 89eeed4..8fe0173 100644 --- a/src/test/java/org/apache/commons/math4/transform/FastHadamardTransformerTest.java +++ b/src/test/java/org/apache/commons/math4/transform/FastHadamardTransformerTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math4.transform; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.transform.FastHadamardTransformer; import org.apache.commons.math4.transform.TransformType; import org.apache.commons.math4.util.Precision; @@ -68,7 +69,7 @@ public final class FastHadamardTransformerTest { try { new FastHadamardTransformer().transform(new double[3], TransformType.FORWARD); Assert.fail("an exception should have been thrown"); - } catch (IllegalArgumentException iae) { + } catch (MathIllegalArgumentException iae) { // expected } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/transform/FastSineTransformerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/transform/FastSineTransformerTest.java b/src/test/java/org/apache/commons/math4/transform/FastSineTransformerTest.java index 64b8e8d..9a1dfcd 100644 --- a/src/test/java/org/apache/commons/math4/transform/FastSineTransformerTest.java +++ b/src/test/java/org/apache/commons/math4/transform/FastSineTransformerTest.java @@ -283,22 +283,22 @@ public final class FastSineTransformerTest extends RealTransformerAbstractTest { try { // bad interval transformer.transform(f, 1, -1, 64, TransformType.FORWARD); - Assert.fail("Expecting IllegalArgumentException - bad interval"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException - bad interval"); + } catch (MathIllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 0, TransformType.FORWARD); - Assert.fail("Expecting IllegalArgumentException - bad samples number"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException - bad samples number"); + } catch (MathIllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 100, TransformType.FORWARD); - Assert.fail("Expecting IllegalArgumentException - bad samples number"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException - bad samples number"); + } catch (MathIllegalArgumentException ex) { // expected } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/afcfbf57/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java b/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java index 1f31c0c..c7be6ea 100644 --- a/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java +++ b/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java @@ -18,6 +18,7 @@ package org.apache.commons.math4.util; import org.apache.commons.math4.distribution.IntegerDistribution; import org.apache.commons.math4.distribution.UniformIntegerDistribution; +import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.util.ResizableDoubleArray.ExpansionMode; import org.junit.After; @@ -57,15 +58,15 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { Assert.assertEquals(defaultMode, testDa.getExpansionMode()); try { da = new ResizableDoubleArray(-1); - Assert.fail("Expecting IllegalArgumentException"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { // expected } testDa = new ResizableDoubleArray((double[]) null); Assert.assertEquals(0, testDa.getNumElements()); - double[] initialArray = new double[] { 0, 1, 2 }; + double[] initialArray = new double[] { 0, 1, 2 }; testDa = new ResizableDoubleArray(initialArray); Assert.assertEquals(3, testDa.getNumElements()); @@ -78,8 +79,8 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { try { da = new ResizableDoubleArray(2, 0.5); - Assert.fail("Expecting IllegalArgumentException"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { // expected } @@ -96,8 +97,8 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { try { da = new ResizableDoubleArray(2, 2.0, 1.5); - Assert.fail("Expecting IllegalArgumentException"); - } catch (IllegalArgumentException ex) { + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { // expected } @@ -194,24 +195,24 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { "16 and an expansion factor of 2.0", 1024, ((ResizableDoubleArray) da).getCapacity()); } - + @Test public void testAddElements() { ResizableDoubleArray testDa = new ResizableDoubleArray(); - + // MULTIPLICATIVE_MODE testDa.addElements(new double[] {4, 5, 6}); Assert.assertEquals(3, testDa.getNumElements(), 0); Assert.assertEquals(4, testDa.getElement(0), 0); Assert.assertEquals(5, testDa.getElement(1), 0); Assert.assertEquals(6, testDa.getElement(2), 0); - + testDa.addElements(new double[] {4, 5, 6}); Assert.assertEquals(6, testDa.getNumElements()); // ADDITIVE_MODE (x's are occupied storage locations, 0's are open) testDa = new ResizableDoubleArray(2, 2.0, 2.5, - ResizableDoubleArray.ExpansionMode.ADDITIVE); + ResizableDoubleArray.ExpansionMode.ADDITIVE); Assert.assertEquals(2, testDa.getCapacity()); testDa.addElements(new double[] { 1d }); // x,0 testDa.addElements(new double[] { 2d }); // x,x @@ -298,7 +299,7 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { try { ((ResizableDoubleArray) da).setNumElements( -3 ); Assert.fail( "Setting number of elements to negative should've thrown an exception"); - } catch( IllegalArgumentException iae ) { + } catch(MathIllegalArgumentException iae) { } ((ResizableDoubleArray) da).setNumElements(1024);