Пример #1
0
  private static void recomputeTrainTestSets(TrainAndTestReport r) { // TODO a nicer way? :)
    if (r instanceof TrainAndTestReportCrisp) {
      TrainAndTestReportCrisp rep = (TrainAndTestReportCrisp) r;
      double[] newFittedValues =
          Arrays.copyOfRange(rep.getFittedValues(), 0, rep.getNumTrainingEntries());
      double[] newForecastValsTest =
          Arrays.copyOfRange(
              rep.getFittedValues(), rep.getNumTrainingEntries(), rep.getFittedValues().length);
      rep.setFittedValues(newFittedValues);
      rep.setForecastValuesTest(newForecastValsTest);
      double[] newRealTrain =
          Arrays.copyOfRange(rep.getRealOutputsTrain(), 0, rep.getNumTrainingEntries());
      double[] newRealTest =
          Arrays.copyOfRange(
              rep.getRealOutputsTrain(),
              rep.getNumTrainingEntries(),
              rep.getRealOutputsTrain().length);
      rep.setRealOutputsTrain(newRealTrain);
      rep.setRealOutputsTest(newRealTest);
      rep.setErrorMeasures(
          ErrorMeasuresUtils.computeAllErrorMeasuresCrisp(
              Utils.arrayToList(newRealTrain),
              Utils.arrayToList(newRealTest),
              Utils.arrayToList(newFittedValues),
              Utils.arrayToList(newForecastValsTest),
              0)); // TODO I hope the 0 is not a problem
    } else if (r instanceof TrainAndTestReportInterval) {
      TrainAndTestReportInterval rep = (TrainAndTestReportInterval) r;
      List<Interval> newFittedValues =
          new ArrayList<>(rep.getFittedValues().subList(0, rep.getNumTrainingEntries()));
      List<Interval> newForecastValsTest =
          new ArrayList<>(
              rep.getFittedValues()
                  .subList(rep.getNumTrainingEntries(), rep.getFittedValues().size()));
      rep.setFittedValues(newFittedValues);
      rep.setForecastValuesTest(newForecastValsTest);

      List<Interval> realVals =
          Utils.zipLowerUpperToIntervals(rep.getRealValuesLowers(), rep.getRealValuesUppers());
      List<Interval> realValsTrain =
          new ArrayList<>(realVals.subList(0, rep.getNumTrainingEntries()));
      List<Interval> realValsTest =
          new ArrayList<>(realVals.subList(rep.getNumTrainingEntries(), realVals.size()));

      // TODO somehow add the actual distance and seasonality from params
      rep.setErrorMeasures(
          ErrorMeasuresUtils.computeAllErrorMeasuresInterval(
              realValsTrain,
              realValsTest,
              newFittedValues,
              newForecastValsTest,
              new WeightedEuclideanDistance(0.5),
              0));
    }
  }
Пример #2
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;
      }
    }
  }