Ejemplo n.º 1
0
  public TrainAndTestReportInterval computeAvgIntTS(
      List<TrainAndTestReportInterval> reportsIntTS, Model model) {
    if (!allTheSamePercentTrain(reportsIntTS)) { // throw an error, we cannot compute it like this
      return null;
    } else {
      MyRengine rengine = MyRengine.getRengine();

      if (reportsIntTS.size() == 1) { // does not make sense to compute average over one series
        return reportsIntTS.get(0);
      } else {
        StringBuilder avgAllLowersTrain = new StringBuilder("(");
        StringBuilder avgAllLowersTest = new StringBuilder("(");
        StringBuilder avgAllLowersFuture = new StringBuilder("(");
        StringBuilder avgAllUppersTrain = new StringBuilder("(");
        StringBuilder avgAllUppersTest = new StringBuilder("(");
        StringBuilder avgAllUppersFuture = new StringBuilder("(");
        StringBuilder sumWeightsTrain = new StringBuilder("(");
        StringBuilder sumWeightsTest = new StringBuilder("(");
        StringBuilder sumWeightsFuture = new StringBuilder("(");
        boolean next = false;
        for (TrainAndTestReportInterval r : reportsIntTS) {
          if (next) {
            avgAllLowersTrain.append(" + ");
            avgAllLowersTest.append(" + ");
            avgAllLowersFuture.append(" + ");
            avgAllUppersTrain.append(" + ");
            avgAllUppersTest.append(" + ");
            avgAllUppersFuture.append(" + ");
            sumWeightsTrain.append(" + ");
            sumWeightsTest.append(" + ");
            sumWeightsFuture.append(" + ");
          } else {
            next = true;
          }

          double weightTrain = getWeightForModelTrain(r);
          double weightTest = getWeightForModelTest(r);
          double weightFuture = getWeightForModelFuture(r);
          weightsInterval.put(r.toString(), weightFuture);

          sumWeightsTrain.append(weightTrain);
          sumWeightsTest.append(weightTest);

          avgAllLowersTrain
              .append(weightTrain)
              .append("*")
              .append(Utils.arrayToRVectorString(r.getFittedValuesLowers()));
          avgAllLowersTest
              .append(weightTest)
              .append("*")
              .append(Utils.arrayToRVectorString(r.getForecastValuesTestLowers()));

          avgAllUppersTrain
              .append(weightTrain)
              .append("*")
              .append(Utils.arrayToRVectorString(r.getFittedValuesUppers()));
          avgAllUppersTest
              .append(weightTest)
              .append("*")
              .append(Utils.arrayToRVectorString(r.getForecastValuesTestUppers()));

          avgAllLowersFuture.append(weightFuture).append("*");
          avgAllUppersFuture.append(weightFuture).append("*");
          if (r.getForecastValuesFuture().size() > 0) {
            avgAllLowersFuture.append(
                Utils.arrayToRVectorString(r.getForecastValuesFutureLowers()));
            avgAllUppersFuture.append(
                Utils.arrayToRVectorString(r.getForecastValuesFutureUppers()));

            sumWeightsFuture.append(weightFuture);
          } else {
            avgAllLowersFuture.append("0");
            avgAllUppersFuture.append("0");

            sumWeightsFuture.append("0");
          }
        }
        sumWeightsTrain.append(")");
        sumWeightsTest.append(")");
        sumWeightsFuture.append(")");

        avgAllLowersTrain.append(")/").append(sumWeightsTrain);
        avgAllLowersTest.append(")/").append(sumWeightsTest);
        avgAllLowersFuture.append(")/").append(sumWeightsFuture);
        avgAllUppersTrain.append(")/").append(sumWeightsTrain);
        avgAllUppersTest.append(")/").append(sumWeightsTest);
        avgAllUppersFuture.append(")/").append(sumWeightsFuture);

        rengine.eval("lowerTrain <- " + avgAllLowersTrain.toString());
        rengine.eval("lowerTest <- " + avgAllLowersTest.toString());
        rengine.eval("lowerFuture <- " + avgAllLowersFuture.toString());
        rengine.eval("upperTrain <- " + avgAllUppersTrain.toString());
        rengine.eval("upperTest <- " + avgAllUppersTest.toString());
        rengine.eval("upperFuture <- " + avgAllUppersFuture.toString());

        // add report:
        List<Double> allLowersTrainList = rengine.evalAndReturnList("lowerTrain");
        List<Double> allLowersTestList = rengine.evalAndReturnList("lowerTest");
        List<Double> allUppersTrainList = rengine.evalAndReturnList("upperTrain");
        List<Double> allUppersTestList = rengine.evalAndReturnList("upperTest");
        List<Interval> allIntervalsTrain =
            Utils.zipLowerUpperToIntervals(allLowersTrainList, allUppersTrainList);
        List<Interval> allIntervalsTest =
            Utils.zipLowerUpperToIntervals(allLowersTestList, allUppersTestList);

        List<Double> realValuesLowers = reportsIntTS.get(0).getRealValuesLowers();
        List<Double> realValuesUppers = reportsIntTS.get(0).getRealValuesUppers();
        List<Double> realValuesLowersTrain =
            realValuesLowers.subList(0, reportsIntTS.get(0).getNumTrainingEntries());
        List<Double> realValuesUppersTrain =
            realValuesUppers.subList(0, reportsIntTS.get(0).getNumTrainingEntries());
        List<Double> realValuesLowersTest =
            realValuesLowers.subList(
                reportsIntTS.get(0).getNumTrainingEntries(), realValuesLowers.size());
        List<Double> realValuesUppersTest =
            realValuesUppers.subList(
                reportsIntTS.get(0).getNumTrainingEntries(), realValuesUppers.size());

        List<Interval> realValuesTrain =
            Utils.zipLowerUpperToIntervals(realValuesLowersTrain, realValuesUppersTrain);
        List<Interval> realValuesTest =
            Utils.zipLowerUpperToIntervals(realValuesLowersTest, realValuesUppersTest);

        ErrorMeasuresInterval errorMeasures =
            ErrorMeasuresUtils.computeAllErrorMeasuresInterval(
                realValuesTrain,
                realValuesTest,
                allIntervalsTrain,
                allIntervalsTest,
                new WeightedEuclideanDistance(0.5),
                0);
        // TODO chg; for now takes WeightedEuclid, but allow any distance

        TrainAndTestReportInterval reportAvgAllITS =
            new TrainAndTestReportInterval(model, "_int(" + getName() + ")", true);
        reportAvgAllITS.setErrorMeasures(errorMeasures);
        reportAvgAllITS.setFittedValues(allIntervalsTrain);
        reportAvgAllITS.setForecastValuesTest(allIntervalsTest);

        List<Double> allLowersFutureList = rengine.evalAndReturnList("lowerFuture");
        List<Double> allUppersFutureList = rengine.evalAndReturnList("upperFuture");
        List<Interval> allIntervalsFuture =
            Utils.zipLowerUpperToIntervals(allLowersFutureList, allUppersFutureList);
        reportAvgAllITS.setForecastValuesFuture(allIntervalsFuture);
        reportAvgAllITS.setNumTrainingEntries(reportsIntTS.get(0).getNumTrainingEntries());
        realValuesTrain.addAll(realValuesTest);
        reportAvgAllITS.setRealValues(realValuesTrain);

        rengine.rm(
            "lowerTrain", "lowerTest", "lowerFuture", "upperTrain", "upperTest", "upperFuture");

        return reportAvgAllITS;
      }
    }
  }
Ejemplo n.º 2
0
  public TrainAndTestReportCrisp computeAvgCTS(
      List<TrainAndTestReportCrisp> reportsCTS, Model model) {
    if (reportsCTS.size() == 1) { // does not make sense to compute average over one series
      return reportsCTS.get(
          0); // TODO do not return anything (but take care of it on the receiving end), because
              // otherwise it draws twice. but then problems with "drawOnlyAVG"
    } else {
      if (!allTheSamePercentTrain(reportsCTS)) {
        return null;
      } else {
        StringBuilder fittedValsAvgAll = new StringBuilder("(");
        StringBuilder forecastValsTestAvgAll = new StringBuilder("(");
        StringBuilder forecastValsFutureAvgAll = new StringBuilder("(");
        StringBuilder sumWeightsTrain = new StringBuilder("(");
        StringBuilder sumWeightsTest = new StringBuilder("(");
        StringBuilder sumWeightsFuture = new StringBuilder("(");
        boolean next = false;
        for (TrainAndTestReportCrisp r : reportsCTS) {
          if (next) {
            fittedValsAvgAll.append(" + ");
            forecastValsTestAvgAll.append(" + ");
            forecastValsFutureAvgAll.append(" + ");
            sumWeightsTrain.append(" + ");
            sumWeightsTest.append(" + ");
            sumWeightsFuture.append(" + ");
          } else {
            next = true;
          }

          double weightTrain = getWeightForModelTrain(r);
          double weightTest = getWeightForModelTest(r);
          double weightFuture = getWeightForModelFuture(r);
          weightsCrisp.put(r.toString(), weightFuture);

          sumWeightsTrain.append(weightTrain);
          sumWeightsTest.append(weightTest);

          fittedValsAvgAll
              .append(weightTrain)
              .append("*")
              .append(Utils.arrayToRVectorString(r.getFittedValues()));
          forecastValsTestAvgAll
              .append(weightTest)
              .append("*")
              .append(Utils.arrayToRVectorString(r.getForecastValuesTest()));

          forecastValsFutureAvgAll.append(weightFuture).append("*");
          if (r.getForecastValuesFuture().length > 0) {
            forecastValsFutureAvgAll.append(
                Utils.arrayToRVectorString(r.getForecastValuesFuture()));

            sumWeightsFuture.append(weightFuture);
          } else {
            forecastValsFutureAvgAll.append("0");

            sumWeightsFuture.append("0");
          }
        }
        sumWeightsTrain.append(")");
        sumWeightsTest.append(")");
        sumWeightsFuture.append(")");

        fittedValsAvgAll.append(")/").append(sumWeightsTrain);
        forecastValsTestAvgAll.append(")/").append(sumWeightsTest);
        forecastValsFutureAvgAll.append(")/").append(sumWeightsFuture);

        String avgAll =
            "c("
                + fittedValsAvgAll
                + ","
                + forecastValsTestAvgAll
                + ","
                + forecastValsFutureAvgAll
                + ")";

        MyRengine rengine = MyRengine.getRengine();
        // and create a new report for this avg and add it to reportsCTS:
        TrainAndTestReportCrisp thisAvgReport =
            new TrainAndTestReportCrisp(model, "(" + getName() + ")", true);
        double[] fittedValsAvg = rengine.evalAndReturnArray(fittedValsAvgAll.toString());
        double[] forecastValsTestAvg =
            rengine.evalAndReturnArray(forecastValsTestAvgAll.toString());

        ErrorMeasuresCrisp errorMeasures =
            ErrorMeasuresUtils.computeAllErrorMeasuresCrisp(
                Utils.arrayToList(reportsCTS.get(0).getRealOutputsTrain()),
                Utils.arrayToList(reportsCTS.get(0).getRealOutputsTest()),
                Utils.arrayToList(fittedValsAvg),
                Utils.arrayToList(forecastValsTestAvg),
                0);

        thisAvgReport.setErrorMeasures(errorMeasures);
        double[] forecastValsFutureAvg =
            rengine.evalAndReturnArray(forecastValsFutureAvgAll.toString());
        thisAvgReport.setForecastValuesFuture(forecastValsFutureAvg);
        thisAvgReport.setPlotCode("plot.ts(" + avgAll + ", lty=2)");
        thisAvgReport.setFittedValues(fittedValsAvg);
        thisAvgReport.setForecastValuesTest(forecastValsTestAvg);
        thisAvgReport.setNumTrainingEntries(fittedValsAvg.length);
        thisAvgReport.setRealOutputsTrain(reportsCTS.get(0).getRealOutputsTrain());
        thisAvgReport.setRealOutputsTest(reportsCTS.get(0).getRealOutputsTest());

        return thisAvgReport;
      }
    }
  }