Changeset 7194


Ignore:
Timestamp:
12/16/11 14:48:26 (9 years ago)
Author:
gkronber
Message:

#1081 small changes in calculation of time series solution results

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs

    r7183 r7194  
    5050    private const string TrainingTheilsUStatisticMeanResultName = "Average Theil's U (mean) (training)";
    5151    private const string TestTheilsUStatisticMeanResultName = "Average Theil's U (mean) (test)";
    52     private const string TrainingTheilsUStatisticMAResultName = "Average Theil's U (moving average) (training)";
    53     private const string TestTheilsUStatisticMAResultName = "Average Theil's U (moving average) (test)";
     52    private const string TrainingTheilsUStatisticMaResultName = "Average Theil's U (moving average) (training)";
     53    private const string TestTheilsUStatisticMaResultName = "Average Theil's U (moving average) (test)";
    5454
    5555    public new ITimeSeriesPrognosisModel Model {
     
    153153    }
    154154    public double[] TrainingTheilsUStatisticMovingAverage {
    155       get { return ((DoubleArray)this[TrainingTheilsUStatisticMAResultName].Value).ToArray(); }
    156       private set { this[TrainingTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
     155      get { return ((DoubleArray)this[TrainingTheilsUStatisticMaResultName].Value).ToArray(); }
     156      private set { this[TrainingTheilsUStatisticMaResultName].Value = new DoubleArray(value); }
    157157    }
    158158    public double[] TestTheilsUStatisticMovingAverage {
    159       get { return ((DoubleArray)this[TestTheilsUStatisticMAResultName].Value).ToArray(); }
    160       private set { this[TestTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
     159      get { return ((DoubleArray)this[TestTheilsUStatisticMaResultName].Value).ToArray(); }
     160      private set { this[TestTheilsUStatisticMaResultName].Value = new DoubleArray(value); }
    161161    }
    162162    #endregion
     
    188188      Add(new Result(TrainingTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the training partition", new DoubleArray()));
    189189      Add(new Result(TestTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition", new DoubleArray()));
    190       Add(new Result(TrainingTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
    191       Add(new Result(TestTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
     190      Add(new Result(TrainingTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
     191      Add(new Result(TestTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
    192192      horizon = 1;
    193193    }
     
    213213        anyNewResult = true;
    214214      }
    215       if (!ContainsKey(TrainingTheilsUStatisticMAResultName)) {
    216         Add(new Result(TrainingTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
    217         anyNewResult = true;
    218       }
    219       if (!ContainsKey(TestTheilsUStatisticMAResultName)) {
    220         Add(new Result(TestTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
     215      if (!ContainsKey(TrainingTheilsUStatisticMaResultName)) {
     216        Add(new Result(TrainingTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
     217        anyNewResult = true;
     218      }
     219      if (!ContainsKey(TestTheilsUStatisticMaResultName)) {
     220        Add(new Result(TestTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
    221221        anyNewResult = true;
    222222      }
     
    226226
    227227    protected void CalculateResults() {
    228       OnlineCalculatorError errorState;
    229228      string[] targetVariables = ProblemData.TargetVariables.ToArray();
    230       /*
    231       double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values
    232       double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
    233       double[] estimatedTestValues = PrognosedTestValues.ToArray(); // cache values
    234       double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
    235 
    236       double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
    237       TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN;
    238       double testMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
    239       TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMse : double.NaN;
    240 
    241       double trainingMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
    242       TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMae : double.NaN;
    243       double testMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
    244       TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMae : double.NaN;
    245 
    246       double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
    247       TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
    248       double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
    249       TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
    250 
    251       double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
    252       TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
    253       double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
    254       TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
    255 
    256       double trainingNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
    257       TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNmse : double.NaN;
    258       double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
    259       TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN;
    260               */
    261 
    262       //double[] trainingDs = new double[targetVariables.Length];
    263       //double[] testDs = new double[targetVariables.Length];
    264 
    265       //double[] trainingWds = new double[targetVariables.Length];
    266       //double[] testWds = new double[targetVariables.Length];
    267 
    268       //double[] trainingTheilsU = new double[targetVariables.Length];
    269       //double[] testTheilsU = new double[targetVariables.Length];
    270229
    271230      var trainingMseCalculators = new OnlineMeanSquaredErrorCalculator[targetVariables.Length];
     
    315274
    316275      var allPrognosedTrainingValues = GetPrognosedValues(ProblemData.TrainingIndizes, horizon).GetEnumerator();
     276      double[] mean = new double[targetVariables.Length];
     277      for (int t = 0; t < targetVariables.Length; t++) {
     278        double variance;
     279        OnlineCalculatorError meanErrorState, varErrorState;
     280        OnlineMeanAndVarianceCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes), out mean[t], out variance, out meanErrorState, out varErrorState);
     281        if (meanErrorState != OnlineCalculatorError.None) mean[t] = 0.0;
     282      }
    317283      foreach (var row in ProblemData.TrainingIndizes) {
    318284        if (row + horizon < ProblemData.Dataset.Rows) {
    319285          allPrognosedTrainingValues.MoveNext();
    320           var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x).ToArray();
     286          var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x.ToArray()).ToArray();
    321287          for (int t = 0; t < targetVariables.Length; t++) {
    322288            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
     
    328294                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
    329295                                                   ).Average();
    330             var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
    331296            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
    332297            var prognosedContinuation = prognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length);
     
    334299            trainingWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
    335300            trainingTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
    336             trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
     301            trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean[t]), actualContinuation, prognosedContinuation);
    337302            trainingTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
    338303
     
    352317        }
    353318      }
    354       var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).GetEnumerator();
     319      var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).ToArray().AsEnumerable().GetEnumerator();
    355320      foreach (var row in ProblemData.TestIndizes) {
    356321        if (row + horizon < ProblemData.Dataset.Rows) {
    357322          allPrognosedTestValues.MoveNext();
    358           var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x).ToArray();
     323          var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x);
    359324          for (int t = 0; t < targetVariables.Length; t++) {
    360325            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
     
    366331                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
    367332                                                   ).Average();
    368             // mean must be calculated on the training set!
    369             var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
    370333            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
    371             var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length);
     334            var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length).ToArray();
    372335            testDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
    373336            testWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
    374337            testTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
    375             testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
     338            testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean[t]), actualContinuation, prognosedContinuation);
    376339            testTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
    377340
    378341            var actualContinuationEnumerator = actualContinuation.GetEnumerator();
    379             var prognosedContinuationEnumerator = prognosedContinuation.GetEnumerator();
     342            var prognosedContinuationEnumerator = prognosedContinuation.AsEnumerable().GetEnumerator();
    380343            while (actualContinuationEnumerator.MoveNext() & prognosedContinuationEnumerator.MoveNext()) {
    381344              testMseCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
Note: See TracChangeset for help on using the changeset viewer.