Beispiel #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()));
  }
Beispiel #2
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);
  }
Beispiel #3
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);
  }
Beispiel #4
0
 public Double getAlpha() {
   return reg.getSlope();
 }
Beispiel #5
0
  public static void calcCharts() throws Exception {

    // alle Dateien einlesen im obigen Ordner
    File[] files = new File(folderIN).listFiles();
    int i = 0;

    Messreihe[] RQs = new Messreihe[zINTER];
    Messreihe[] RISs = new Messreihe[zINTER];
    Vector<Messreihe>[] vRISs = new Vector[zINTER];
    Vector<Messreihe>[] vFITDATA = new Vector[zINTER];

    ReturnIntervallStatistik[] ris = new ReturnIntervallStatistik[zINTER];

    // 58 files and 9 sections ...
    double tTEST_DATA[][][] = new double[files.length][zINTER][5];
    // 0=RQs,  1=m  2=RR  3=FEHLENDE-m  4=FEHLENDE-RR

    for (int ii = 0; ii < zINTER; ii++) {

      ris[ii] = new ReturnIntervallStatistik(pScale, pBinning);
      //            ris[ii].scaleY_LN = sLN;
      //            ris[ii].scaleY_LOG = sLOG;

      RQs[ii] = new Messreihe("RQ_" + ii);

      RISs[ii] = new Messreihe("RIS_" + ii);
      vRISs[ii] = new Vector<Messreihe>();
      vFITDATA[ii] = new Vector<Messreihe>();
    }

    Vector<Messreihe> all = new Vector<Messreihe>();

    double linFitMIN;
    double linFitMAX;

    for (File f : files) {

      if (f.getName().endsWith(".txt")) {

        DecimalFormat nf = new DecimalFormat("0.0000000");

        Messreihe data =
            MessreihenLoader.getLoader().loadMessreihe(new File(folderIN + f.getName()));

        Messreihe[] parts = data.split(zL, zINTER);

        int j = 0;

        // r ist die Messreihe mit den normalen Daten
        for (Messreihe r : parts) {

          double vRQ = r.getAvarage();
          // System.out.print(  "file -> " + i + " part -> " + j + "\t" + nf.format( vRQ ) + "\t" );
          // System.out.print( r.toString() );
          tTEST_DATA[i][j][0] = vRQ;

          vRISs[j].add(r);

          RQs[j].addValue(vRQ);

          ReturnIntervallStatistik rr = new ReturnIntervallStatistik(pScale, pBinning);

          rr.doScale_by_Rq = scaleRQ;
          //                    rr.scaleY_LN = sLN;
          //                    rr.scaleY_LOG = sLOG;
          rr.setRQ(vRQ);

          rr.addDistData(r.getYValues());

          if (scaleRQ) {

            linFitMIN = linFit_MIN * vRQ;
            linFitMAX = linFit_MAX * vRQ;
            // System.out.println( "( " + linFit_MIN + ", " + linFit_MAX + ") ");

          } else {
            linFitMIN = linFit_MIN;
            linFitMAX = linFit_MAX;
            // System.out.println( "( " + linFitMIN + ", " + linFitMAX + ") ");
          }
          ;

          // hier sollten nun die Daten für jede Reihe da sein ...

          /** Problem: in den einzelnen Reihen kann man nicht sinnvoll den Anstieg bestimmen !!! */
          Messreihe toFit = rr.mrHaeufigkeit.shrinkX(linFit_MIN, linFit_MAX);

          SimpleRegression reg;
          try {
            reg = toFit.linFit(linFit_MIN, linFit_MAX);

            double slope = reg.getSlope();
            double RR = reg.getRSquare();

            // System.out.println("m=" + slope + " \tRR=" + RR);
            tTEST_DATA[i][j][1] = slope;
            tTEST_DATA[i][j][2] = RR;
            all.add(toFit);

          } catch (Exception ex) {
            // Logger.getLogger(RISAnalyse3.class.getName()).log(Level.SEVERE, null, ex);

          }

          vFITDATA[j].add(rr.mrHaeufigkeit);
          try {
            ris[j].add(rr);
          } catch (Exception ex) {
            Logger.getLogger(RISAnalyse3.class.getName()).log(Level.SEVERE, null, ex);
          }
          j++;
        }
        i++;
      }
    }

    if (showRAWData) {
      for (int i3 = 0; i3 < zINTER; i3++) {
        MultiChart.openAndStore(
            vRISs[i3],
            label + " Intervall: " + i3 + " - scale=" + pScale + ", binning=" + pBinning,
            "r",
            "P(r)",
            false,
            folderOUT,
            label + "_P(r)",
            getParameterSet());
      }
    }

    System.out.print(folderOUT);
    Messreihe rq = new Messreihe("Rq(i)");
    Messreihe rqSIGMA = new Messreihe("sigma(Rq(i))");
    Vector<Messreihe> chart2Data = new Vector<Messreihe>();
    chart2Data.add(rq);
    chart2Data.add(rqSIGMA);

    Vector<Messreihe> vMR = new Vector<Messreihe>();
    // für alle Intervalle des Files ...
    for (int d = 0; d < zINTER; d++) {
      try {
        ris[d].calcMessreihen();
      } catch (Exception ex) {
        Logger.getLogger(RISAnalyse3.class.getName()).log(Level.SEVERE, null, ex);
      }
      RISs[d] = ris[d].mrHaeufigkeit;

      // System.out.println( "(b)\n" + ris[d].toString() );
      ris[d].mrVerteilung.setLabel("Int_" + (d + 1));
      vMR.add(ris[d].mrVerteilung);

      RQs[d].calcAverage();

      rq.addValuePair((double) d, RQs[d].getAvarage());

      rqSIGMA.addValuePair((double) d, RQs[d].getStddev());
    }
    rq.normalize();
    rqSIGMA.normalize();

    // auf VMR nun den Fit ANWENDEN ...
    Messreihe fitData1 = new Messreihe();
    fitData1.setLabel("m");

    Messreihe fitData2 = new Messreihe();
    fitData2.setLabel("RR");

    Vector<Messreihe> vFITS = new Vector<Messreihe>();

    SimpleRegression reg2;
    int c = 0;
    for (Messreihe mr : vMR) {
      try {
        reg2 = mr.linFit(linFit_MIN, linFit_MAX);

        double slope = reg2.getSlope();
        double RR = reg2.getRSquare();

        fitData1.addValuePair(c, slope);
        fitData2.addValuePair(c, RR);

        System.out.println("\tm=" + slope + " \tRR=" + RR);
        c++;
      } catch (Exception ex) {

      }
    }
    ;
    fitData1.normalize();
    fitData2.normalize();

    vFITS.add(fitData1);
    vFITS.add(fitData2);

    if (showRAWRIS) {
      for (int i3 = 0; i3 < zINTER; i3++) {
        MultiChart.openAndStore(
            vFITDATA[i3],
            "vFIT " + label + " Intervall: " + i3 + " - scale=" + pScale + ", binning=" + pBinning,
            "r",
            "P(r)",
            false,
            folderOUT,
            label + "_P(r)",
            getParameterSet());
      }
    }

    String labelX = "r/Rq";
    String labelY = "P(r)*Rq";

    if (!scaleRQ) {
      labelX = "r";
      labelY = "P(r)";
    }
    if (sLOG) {
      labelY = "log(" + labelY + ")";
    }
    if (sLN) {
      labelY = "ln(" + labelY + ")";
    }

    if (showCharts) {
      MultiChart.openAndStore(
          vMR,
          label + " - scale=" + pScale + ", binning=" + pBinning,
          labelX,
          labelY,
          true,
          folderOUT,
          label + "_P(r)",
          getParameterSet());

      MultiChart.openAndStore(
          vFITS,
          "FIT " + label + " - scale=" + pScale + ", binning=" + pBinning,
          "i",
          "m(i) RR(i)",
          true,
          folderOUT,
          label + "_m(i)_RR(i)",
          getParameterSet());

      MultiChart.openAndStore(
          chart2Data,
          label + " - Rq(i)",
          "i",
          "<Rq>, <sigma(Rq)>",
          true,
          folderOUT,
          label + "_Rq(i)",
          getParameterSet());

      MultiChart.open(
          all,
          "ALL " + label + " - scale=" + pScale + ", binning=" + pBinning,
          "x",
          "y",
          false,
          getParameterSet());
    }

    MesswertTabelle tab1 = new MesswertTabelle();
    tab1.setHeader(getParameterSet());
    tab1.setLabel(label + "_P(r)");
    tab1.setMessReihen(vMR);
    tab1.writeToFile(new File(folderOUT + File.separator + tab1.getLabel() + ".dat"));

    MesswertTabelle tab6 = new MesswertTabelle();
    tab6.setHeader(getParameterSet());
    tab6.setLabel(label + "_m(i)_RR(i)");
    tab6.setMessReihen(vFITS);
    tab6.writeToFile(
        new File(folderOUT + File.separator + "out" + File.separator + tab6.getLabel() + ".dat"));

    MesswertTabelle tab2 = new MesswertTabelle();
    tab2.setHeader(getParameterSet());
    tab2.setLabel(label + "_Rq(i)");
    tab2.setMessReihen(chart2Data);
    tab2.writeToFile(new File(folderOUT + File.separator + tab2.getLabel() + ".dat"));

    String fn = folderOUT + "/tTests_" + label + ".xls";
    String fnTEMPL = folderOUT + "/tTests.xls";

    HSSFWorkbook wb = null;
    HSSFSheet[] sheets = new HSSFSheet[3];

    File f2 = new File(fn);
    File f1 = new File(fnTEMPL);

    // Lesen der bestehenden Tabelle
    FileInputStream fis = null;
    try {
      //
      // Create a FileInputStream that will be use to read the excel file.
      //
      fis = new FileInputStream(fn);

      //
      // Create an excel workbook from the file system.
      //
      wb = new HSSFWorkbook(fis);
      //
      // Get the first sheet on the workbook.
      //
    } catch (Exception ex) {
      ex.printStackTrace();

      System.err.println(">>> " + ex.getMessage());
      System.out.println("> Erzeuge neue XLS Datei ... ");

      wb = new HSSFWorkbook();
      sheets[0] = wb.createSheet("R_q");
      sheets[1] = wb.createSheet("m");
      sheets[2] = wb.createSheet("RR");
    }

    Messreihe[] mrRESULTS = new Messreihe[5];
    try {
      sheets[0] = wb.getSheet("R_q");
      sheets[1] = wb.getSheet("m");
      sheets[2] = wb.getSheet("RR");

      // zur Sammlung der Mittelwerte für die Resultate ...
      mrRESULTS[0] = new Messreihe("<R_q>");
      mrRESULTS[1] = new Messreihe("<m>");
      mrRESULTS[2] = new Messreihe("<RR>");
      mrRESULTS[3] = new Messreihe("{m}");
      mrRESULTS[4] = new Messreihe("{RR}");

      // SCHRANKE für auszulassende Werte
      double epsilon = 1e-6;

      // ANZAHL der Reihen
      for (int i2 = 1; i2 < files.length; i2++) {
        // ANZAHL der INTERVALLE
        for (int j2 = 0; j2 < zINTER; j2++) {
          // ANZAHL der MESSSGRÖSSEN
          for (int k2 = 0; k2 < 3; k2++) {

            HSSFRow row = sheets[k2].getRow(i2);
            if (row == null) row = sheets[k2].createRow(i2);

            row.createCell(1).setCellValue(i2);
            double value = tTEST_DATA[i2][j2][k2];
            if (!Double.isNaN(value)) {
              if (value > epsilon) {
                row.createCell(j2 + 1).setCellValue(value);
              }
            } else {
              row.createCell(j2 + 1).setCellValue("");
            }
            // System.out.println( i2 +"\t" + j2 + "\t" + k2 + "\t" + tTEST_DATA[i2][j2][k2]);

          }
        }
      }

      // MW für alle Spalten berechnen ... für drei Messwerte ...
      for (int x = 0; x < 3; x++) {

        double eps = 1e-3;

        double anz = 0.0;
        double summe = 0.0;

        int counterNAN_values = 0;
        // ANZAHL der INTERVALLE
        for (int j2 = 0; j2 < zINTER; j2++) {

          // ANZAHL der Reihen
          for (int i2 = 1; i2 < files.length; i2++) {
            double value = tTEST_DATA[i2][j2][x];

            double RR = tTEST_DATA[i2][j2][2];

            if (!Double.isNaN(value)) {
              //                            if ( value > eps ) {  // Variante 1
              //                            if ( RR > 0.75) {  // Variante 2
              summe = summe + value;
              anz++;
              //                            }
            } else {
              counterNAN_values++;
            }
          }
          double mw = summe / anz;
          mrRESULTS[x].addValuePair(j2, mw);
          if (x > 0) mrRESULTS[x + 2].addValuePair(j2, counterNAN_values);

          anz = 0.0;
          summe = 0.0;
          counterNAN_values = 0;
        }
      }

      mrRESULTS[3].normalize();
      mrRESULTS[4].normalize();

      Vector<Messreihe> v = new Vector<Messreihe>();
      v.add(mrRESULTS[0]);
      v.add(mrRESULTS[1]);
      v.add(mrRESULTS[2]);
      v.add(mrRESULTS[3]);
      v.add(mrRESULTS[4]);

      if (showCharts) {
        MultiChart.openAndStore(
            v,
            label + " - scale=" + pScale + ", binning=" + pBinning,
            "i",
            "f(i)",
            true,
            folderOUT,
            label + "_chart3",
            getParameterSet());
      }

      FileOutputStream fout2 = new FileOutputStream(f2.getAbsolutePath());
      wb.write(fout2);
      fout2.close();

      String[] titles = {"P(r)", "Rq(i)", "chart3", "m(i)_RR(i)"};
      report.createResultlinePNG(label, titles, getParameterSetHTML());

      System.out.println("[READY] " + label + "");

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }