Ejemplo n.º 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));
    }
  }
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;
      }
    }
  }