Exemplo n.º 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()));
  }
Exemplo n.º 2
0
 public void testClear() {
   SimpleRegression regression = new SimpleRegression();
   regression.addData(corrData);
   assertEquals("number of observations", 17, regression.getN());
   regression.clear();
   assertEquals("number of observations", 0, regression.getN());
   regression.addData(corrData);
   assertEquals("r-square", .896123, regression.getRSquare(), 10E-6);
   regression.addData(data);
   assertEquals("number of observations", 53, regression.getN());
 }
Exemplo n.º 3
0
  /**
   * Adds a sample to the model with the given frequency and length values.
   *
   * @param averageStepFrequency
   * @param averageStepLength
   */
  public void addSampleToModel(Double averageStepFrequency, Double averageStepLength) {
    // ...insert it into the regression model
    reg.addData(averageStepFrequency, averageStepLength);

    // ...and sample list
    sampleList.add(new AutoGaitSample(averageStepFrequency, averageStepLength));
  }
Exemplo n.º 4
0
  public AutoGaitModel(double[][] samples) {
    // Add samples to the regression model
    reg.addData(samples);

    // Add values to the sample list
    for (double[] line : samples) addSampleToModel(line[0], line[1]);
  }
Exemplo n.º 5
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);
 }
Exemplo n.º 6
0
 public void testCorr() {
   SimpleRegression regression = new SimpleRegression();
   regression.addData(corrData);
   assertEquals("number of observations", 17, regression.getN());
   assertEquals("r-square", .896123, regression.getRSquare(), 10E-6);
   assertEquals("r", -0.94663767742, regression.getR(), 1E-10);
 }
Exemplo n.º 7
0
 // Jira MATH-85 = Bugzilla 39432
 public void testSSENonNegative() {
   double[] y = {8915.102, 8919.302, 8923.502};
   double[] x = {1.107178495E2, 1.107264895E2, 1.107351295E2};
   SimpleRegression reg = new SimpleRegression();
   for (int i = 0; i < x.length; i++) {
     reg.addData(x[i], y[i]);
   }
   assertTrue(reg.getSumSquaredErrors() >= 0.0);
 }
Exemplo n.º 8
0
  public void testRandom() throws Exception {
    SimpleRegression regression = new SimpleRegression();
    Random random = new Random(1);
    int n = 100;
    for (int i = 0; i < n; i++) {
      regression.addData(((double) i) / (n - 1), random.nextDouble());
    }

    assertTrue(0.0 < regression.getSignificance() && regression.getSignificance() < 1.0);
  }
Exemplo n.º 9
0
  public void testPerfectNegative() throws Exception {
    SimpleRegression regression = new SimpleRegression();
    int n = 100;
    for (int i = 0; i < n; i++) {
      regression.addData(-((double) i) / (n - 1), i);
    }

    assertEquals(0.0, regression.getSignificance(), 1.0e-5);
    assertTrue(regression.getSlope() < 0.0);
  }
Exemplo n.º 10
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
    }
  }
 /**
  * Computes the Pearson's product-moment correlation coefficient between the two arrays. Throws
  * IllegalArgumentException if the arrays do not have the same length or their common length is
  * less than 2
  *
  * @param xArray first data array
  * @param yArray second data array
  * @return Returns Pearson's correlation coefficient for the two arrays
  * @throws IllegalArgumentException if the arrays lengths do not match or there is insufficient
  *     data
  */
 public double correlation(final double[] xArray, final double[] yArray)
     throws IllegalArgumentException {
   SimpleRegression regression = new SimpleRegression();
   if (xArray.length == yArray.length && xArray.length > 1) {
     for (int i = 0; i < xArray.length; i++) {
       regression.addData(xArray[i], yArray[i]);
     }
     return regression.getR();
   } else {
     throw MathRuntimeException.createIllegalArgumentException(
         "invalid array dimensions. xArray has size {0}; yArray has {1} elements",
         xArray.length, yArray.length);
   }
 }
Exemplo n.º 12
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);
  }
 /**
  * Adds the observations represented by the elements in <code>data</code>.
  *
  * <p><code>(data[0][0],data[0][1])</code> will be the first observation, then <code>
  * (data[1][0],data[1][1])</code>, etc.
  *
  * <p>This method does not replace data that has already been added. The observations represented
  * by <code>data</code> are added to the existing dataset.
  *
  * <p>To replace all data, use <code>clear()</code> before adding the new data.
  *
  * @param data array of observations to be added
  */
 public void addData(double[][] data) {
   for (int i = 0; i < data.length; i++) {
     addData(data[i][0], data[i][1]);
   }
 }
Exemplo n.º 14
0
 // Remove multiple observations past empty (i.e. size of array > n)
 public void testRemoveMultiplePastEmpty() {
   SimpleRegression regression = new SimpleRegression();
   regression.addData(removeX, removeY);
   regression.removeData(removeMultiple);
   assertEquals(regression.getN(), 0);
 }
Exemplo n.º 15
0
 // Remove single observation to empty
 public void testRemoveObsFromSingle() {
   SimpleRegression regression = new SimpleRegression();
   regression.addData(removeX, removeY);
   regression.removeData(removeX, removeY);
   assertEquals(regression.getN(), 0);
 }
Exemplo n.º 16
0
  public void SummuarySimulation() {
    calculatePower();
    PrintStream Pout1 = null;
    PrintStream Pout2 = null;
    PrintStream Pout3 = null;
    PrintStream Pout4 = null;
    PrintStream Pout5 = null;
    PrintStream Pout6 = null;
    ChiSquaredDistribution chi = new ChiSquaredDistributionImpl(weight.length);
    try {
      Pout1 = new PrintStream(new BufferedOutputStream(new FileOutputStream("LogAP.txt")));
      Pout2 = new PrintStream(new BufferedOutputStream(new FileOutputStream("LNP.txt")));
      Pout3 = new PrintStream(new BufferedOutputStream(new FileOutputStream("LN.txt")));
      Pout4 = new PrintStream(new BufferedOutputStream(new FileOutputStream("Wald.txt")));
      Pout5 = new PrintStream(new BufferedOutputStream(new FileOutputStream("F.txt")));
      Pout6 = new PrintStream(new BufferedOutputStream(new FileOutputStream("LogDP.txt")));
    } catch (Exception E) {
      E.printStackTrace(System.err);
    }
    for (int i = 0; i < SimulationResults.size(); i++) {
      ArrayList PointStatistics = (ArrayList) SimulationResults.get(i);
      double[] LRT = new double[PointStatistics.size()];
      double[] WALD = new double[PointStatistics.size()];
      DescriptiveStatistics dsLRT = new DescriptiveStatisticsImpl();
      DescriptiveStatistics dsWALD = new DescriptiveStatisticsImpl();
      SimpleRegression sr = new SimpleRegression();
      for (int j = 0; j < PointStatistics.size(); j++) {
        PointMappingStatistic pms = (PointMappingStatistic) PointStatistics.get(j);
        double lod = pms.get_LOD() > 0 ? pms.get_LOD() : 0;
        double ln = pms.get_LN();
        double p = 0;
        try {
          p = chi.cumulativeProbability(ln);
        } catch (Exception E) {
          E.printStackTrace(System.err);
        }
        double logLOD = -1 * Math.log10(1 - p);
        Pout1.print(pms.get_logP_additive() + " ");
        Pout2.print(logLOD + " ");
        Pout3.print(pms.get_LN() + " ");
        Pout4.print(pms.get_wald() + " ");
        Pout5.print(pms.get_logP_F() + " ");
        Pout6.print(pms.get_logP_dominance() + " ");

        dsLRT.addValue(pms.get_LN());
        dsWALD.addValue(pms.get_wald());
        LRT[j] = pms.get_LN();
        WALD[j] = pms.get_wald();
        sr.addData(pms.get_LN(), pms.get_wald());
      }
      System.out.println(
          dsLRT.getMean()
              + " +- "
              + dsLRT.getStandardDeviation()
              + " "
              + dsWALD.getMean()
              + " +- "
              + dsWALD.getStandardDeviation()
              + " cor "
              + sr.getR());
      dsLRT.clear();
      dsWALD.clear();
      sr.clear();
      Pout1.println();
      Pout2.println();
      Pout3.println();
      Pout4.println();
      Pout5.println();
      Pout6.println();
    }
    Pout1.close();
    Pout2.close();
    Pout3.close();
    Pout4.close();
    Pout5.close();
    Pout6.close();
  }