Esempio 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()));
  }
Esempio n. 2
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);
 }
 /**
  * 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);
   }
 }
Esempio n. 4
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();
  }