Пример #1
0
  public void testNaNs() {
    SimpleRegression regression = new SimpleRegression();
    assertTrue("intercept not NaN", Double.isNaN(regression.getIntercept()));
    assertTrue("slope not NaN", Double.isNaN(regression.getSlope()));
    assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr()));
    assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr()));
    assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError()));
    assertTrue("e not NaN", Double.isNaN(regression.getR()));
    assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare()));
    assertTrue("RSS not NaN", Double.isNaN(regression.getRegressionSumSquares()));
    assertTrue("SSE not NaN", Double.isNaN(regression.getSumSquaredErrors()));
    assertTrue("SSTO not NaN", Double.isNaN(regression.getTotalSumSquares()));
    assertTrue("predict not NaN", Double.isNaN(regression.predict(0)));

    regression.addData(1, 2);
    regression.addData(1, 3);

    // No x variation, so these should still blow...
    assertTrue("intercept not NaN", Double.isNaN(regression.getIntercept()));
    assertTrue("slope not NaN", Double.isNaN(regression.getSlope()));
    assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr()));
    assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr()));
    assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError()));
    assertTrue("e not NaN", Double.isNaN(regression.getR()));
    assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare()));
    assertTrue("RSS not NaN", Double.isNaN(regression.getRegressionSumSquares()));
    assertTrue("SSE not NaN", Double.isNaN(regression.getSumSquaredErrors()));
    assertTrue("predict not NaN", Double.isNaN(regression.predict(0)));

    // but SSTO should be OK
    assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares()));

    regression = new SimpleRegression();

    regression.addData(1, 2);
    regression.addData(3, 3);

    // All should be OK except MSE, s(b0), s(b1) which need one more df
    assertTrue("interceptNaN", !Double.isNaN(regression.getIntercept()));
    assertTrue("slope NaN", !Double.isNaN(regression.getSlope()));
    assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr()));
    assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr()));
    assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError()));
    assertTrue("r NaN", !Double.isNaN(regression.getR()));
    assertTrue("r-square NaN", !Double.isNaN(regression.getRSquare()));
    assertTrue("RSS NaN", !Double.isNaN(regression.getRegressionSumSquares()));
    assertTrue("SSE NaN", !Double.isNaN(regression.getSumSquaredErrors()));
    assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares()));
    assertTrue("predict NaN", !Double.isNaN(regression.predict(0)));

    regression.addData(1, 4);

    // MSE, MSE, s(b0), s(b1) should all be OK now
    assertTrue("MSE NaN", !Double.isNaN(regression.getMeanSquareError()));
    assertTrue("slope std err NaN", !Double.isNaN(regression.getSlopeStdErr()));
    assertTrue("intercept std err NaN", !Double.isNaN(regression.getInterceptStdErr()));
  }
Пример #2
0
  public void testInference() throws Exception {
    // ----------  verified against R, version 1.8.1 -----
    // infData
    SimpleRegression regression = new SimpleRegression();
    regression.addData(infData);
    assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10);
    assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(), 1E-8);
    assertEquals("significance", 4.596e-07, regression.getSignificance(), 1E-8);
    assertEquals(
        "slope conf interval half-width",
        0.0270713794287,
        regression.getSlopeConfidenceInterval(),
        1E-8);
    // infData2
    regression = new SimpleRegression();
    regression.addData(infData2);
    assertEquals("slope std err", 1.07260253, regression.getSlopeStdErr(), 1E-8);
    assertEquals("std err intercept", 4.17718672, regression.getInterceptStdErr(), 1E-8);
    assertEquals("significance", 0.261829133982, regression.getSignificance(), 1E-11);
    assertEquals(
        "slope conf interval half-width",
        2.97802204827,
        regression.getSlopeConfidenceInterval(),
        1E-8);
    // ------------- End R-verified tests -------------------------------

    // FIXME: get a real example to test against with alpha = .01
    assertTrue(
        "tighter means wider",
        regression.getSlopeConfidenceInterval() < regression.getSlopeConfidenceInterval(0.01));

    try {
      regression.getSlopeConfidenceInterval(1);
      fail("expecting IllegalArgumentException for alpha = 1");
    } catch (IllegalArgumentException ex) {
      // ignored
    }
  }
Пример #3
0
 // Test remove multiple observations
 public void testRemoveMultiple() throws Exception {
   // Create regression with inference data then remove to test
   SimpleRegression regression = new SimpleRegression();
   regression.addData(infData);
   regression.removeData(removeMultiple);
   regression.addData(removeMultiple);
   // Use the inference assertions to make sure that everything worked
   assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10);
   assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(), 1E-8);
   assertEquals("significance", 4.596e-07, regression.getSignificance(), 1E-8);
   assertEquals(
       "slope conf interval half-width",
       0.0270713794287,
       regression.getSlopeConfidenceInterval(),
       1E-8);
 }
Пример #4
0
  public void testNorris() {
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < data.length; i++) {
      regression.addData(data[i][1], data[i][0]);
    }
    // Tests against certified values from
    // http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat
    assertEquals("slope", 1.00211681802045, regression.getSlope(), 10E-12);
    assertEquals("slope std err", 0.429796848199937E-03, regression.getSlopeStdErr(), 10E-12);
    assertEquals("number of observations", 36, regression.getN());
    assertEquals("intercept", -0.262323073774029, regression.getIntercept(), 10E-12);
    assertEquals("std err intercept", 0.232818234301152, regression.getInterceptStdErr(), 10E-12);
    assertEquals("r-square", 0.999993745883712, regression.getRSquare(), 10E-12);
    assertEquals("SSR", 4255954.13232369, regression.getRegressionSumSquares(), 10E-9);
    assertEquals("MSE", 0.782864662630069, regression.getMeanSquareError(), 10E-10);
    assertEquals("SSE", 26.6173985294224, regression.getSumSquaredErrors(), 10E-9);
    // ------------  End certified data tests

    assertEquals("predict(0)", -0.262323073774029, regression.predict(0), 10E-12);
    assertEquals("predict(1)", 1.00211681802045 - 0.262323073774029, regression.predict(1), 10E-12);
  }