Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/09/11 12:56:21 (12 years ago)
Author:
gkronber
Message:

#1081 worked on multi-variate time series prognosis

File:
1 edited

Legend:

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

    r7154 r7160  
    4545    private const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
    4646    private const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
    47     private const string TrainingTheilsUStatisticResultName = "Average Theil's U (training)";
    48     private const string TestTheilsUStatisticResultName = "Average Theil's U (test)";
     47    private const string TrainingTheilsUStatisticLastResultName = "Average Theil's U (last) (training)";
     48    private const string TestTheilsUStatisticLastResultName = "Average Theil's U (last) (test)";
     49    private const string TrainingTheilsUStatisticMeanResultName = "Average Theil's U (mean) (training)";
     50    private const string TestTheilsUStatisticMeanResultName = "Average Theil's U (mean) (test)";
     51    private const string TrainingTheilsUStatisticMAResultName = "Average Theil's U (moving average) (training)";
     52    private const string TestTheilsUStatisticMAResultName = "Average Theil's U (moving average) (test)";
    4953
    5054    public new ITimeSeriesPrognosisModel Model {
     
    5660      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
    5761      set { base.ProblemData = value; }
     62    }
     63
     64    [Storable]
     65    private int horizon;
     66    public int Horizon {
     67      get { return horizon; }
     68      set {
     69        if (horizon != value) {
     70          horizon = value;
     71          RecalculateResults();
     72        }
     73      }
    5874    }
    5975
     
    119135      private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
    120136    }
    121     public double[] TrainingTheilsUStatistic {
    122       get { return ((DoubleArray)this[TrainingTheilsUStatisticResultName].Value).ToArray(); }
    123       private set { this[TrainingTheilsUStatisticResultName].Value = new DoubleArray(value); }
    124     }
    125     public double[] TestTheilsUStatistic {
    126       get { return ((DoubleArray)this[TestTheilsUStatisticResultName].Value).ToArray(); }
    127       private set { this[TestTheilsUStatisticResultName].Value = new DoubleArray(value); }
     137    public double[] TrainingTheilsUStatisticLast {
     138      get { return ((DoubleArray)this[TrainingTheilsUStatisticLastResultName].Value).ToArray(); }
     139      private set { this[TrainingTheilsUStatisticLastResultName].Value = new DoubleArray(value); }
     140    }
     141    public double[] TestTheilsUStatisticLast {
     142      get { return ((DoubleArray)this[TestTheilsUStatisticLastResultName].Value).ToArray(); }
     143      private set { this[TestTheilsUStatisticLastResultName].Value = new DoubleArray(value); }
     144    }
     145    public double[] TrainingTheilsUStatisticMean {
     146      get { return ((DoubleArray)this[TrainingTheilsUStatisticMeanResultName].Value).ToArray(); }
     147      private set { this[TrainingTheilsUStatisticMeanResultName].Value = new DoubleArray(value); }
     148    }
     149    public double[] TestTheilsUStatisticMean {
     150      get { return ((DoubleArray)this[TestTheilsUStatisticMeanResultName].Value).ToArray(); }
     151      private set { this[TestTheilsUStatisticMeanResultName].Value = new DoubleArray(value); }
     152    }
     153    public double[] TrainingTheilsUStatisticMovingAverage {
     154      get { return ((DoubleArray)this[TrainingTheilsUStatisticMAResultName].Value).ToArray(); }
     155      private set { this[TrainingTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
     156    }
     157    public double[] TestTheilsUStatisticMovingAverage {
     158      get { return ((DoubleArray)this[TestTheilsUStatisticMAResultName].Value).ToArray(); }
     159      private set { this[TestTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
    128160    }
    129161    #endregion
     
    133165    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner)
    134166      : base(original, cloner) {
     167      this.horizon = original.horizon;
    135168    }
    136169    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
     
    150183      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
    151184      Add(new Result(TestWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the test partition", new DoubleArray()));
    152       Add(new Result(TrainingTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the training partition", new DoubleArray()));
    153       Add(new Result(TestTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the test partition", new DoubleArray()));
     185      Add(new Result(TrainingTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the training partition", new DoubleArray()));
     186      Add(new Result(TestTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the test partition", new DoubleArray()));
     187      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()));
     188      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()));
     189      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()));
     190      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()));
     191      horizon = 1;
    154192    }
    155193
    156194    [StorableHook(HookType.AfterDeserialization)]
    157195    private void AfterDeserialization() {
    158 
     196      if (horizon == 0) horizon = 1;
    159197    }
    160198
     
    207245      var trainingWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
    208246      var testWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
    209       var trainingTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
    210       var testTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     247      var trainingTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     248      var testTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     249      var trainingTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     250      var testTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     251      var trainingTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
     252      var testTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
    211253      for (int i = 0; i < targetVariables.Length; i++) {
    212254        trainingDsCalculators[i] = new OnlineDirectionalSymmetryCalculator();
     
    214256        trainingWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
    215257        testWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
    216         trainingTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator();
    217         testTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator();
     258        trainingTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator();
     259        testTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator();
     260        trainingTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator();
     261        testTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator();
     262        trainingTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator();
     263        testTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator();
    218264      }
    219265
    220       var allPrognosedTrainingValues = PrognosedTrainingValues.SelectMany(x => x).ToArray();
    221       var allPrognosedTestValues = PrognosedTestValues.SelectMany(x => x).ToArray();
    222       for (int t = 0; t < targetVariables.Length; t++) {
    223         var actualTrainingValues = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes);
    224         double startTrainingValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], ProblemData.TrainingIndizes.First() - 1);
    225         var prognosedTrainingValues = allPrognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length);
    226         trainingDsCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues);
    227         trainingWdsCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues);
    228         trainingTheilsUCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues);
    229 
    230         var actualTestValues = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TestIndizes);
    231         double startTestValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], ProblemData.TestIndizes.First() - 1);
    232         var prognosedTestValues = allPrognosedTestValues.Skip(t).TakeEvery(targetVariables.Length);
    233         testDsCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues);
    234         testWdsCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues);
    235         testTheilsUCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues);
     266      var allPrognosedTrainingValues = GetPrognosedValues(ProblemData.TrainingIndizes, horizon).GetEnumerator();
     267      foreach (var row in ProblemData.TrainingIndizes) {
     268        if (row + horizon < ProblemData.Dataset.Rows) {
     269          allPrognosedTrainingValues.MoveNext();
     270          var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x).ToArray();
     271          for (int t = 0; t < targetVariables.Length; t++) {
     272            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
     273                                                                         Enumerable.Range(row, horizon));
     274            int maWindow = 10 * horizon;
     275            var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
     276                                            select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
     277                                                    where r > 0
     278                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
     279                                                   ).Average();
     280            var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
     281            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
     282            var prognosedContinuation = prognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length);
     283            trainingDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     284            trainingWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     285            trainingTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     286            trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
     287            trainingTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
     288          }
     289        }
    236290      }
     291      var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).GetEnumerator();
     292      foreach (var row in ProblemData.TestIndizes) {
     293        if (row + horizon < ProblemData.Dataset.Rows) {
     294          allPrognosedTestValues.MoveNext();
     295          var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x).ToArray();
     296          for (int t = 0; t < targetVariables.Length; t++) {
     297            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
     298                                                                         Enumerable.Range(row, horizon));
     299            int maWindow = 10 * horizon;
     300            var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
     301                                            select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
     302                                                    where r > 0
     303                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
     304                                                   ).Average();
     305            // mean must be calculated on the training set!
     306            var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
     307            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
     308            var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length);
     309            testDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     310            testWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     311            testTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
     312            testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
     313            testTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
     314          }
     315        }
     316      }
    237317
    238318      TrainingDirectionalSymmetry = trainingDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
     
    244324      TestWeightedDirectionalSymmetry = testWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
    245325        .ToArray();
    246       TrainingTheilsUStatistic = trainingDsCalculators
    247         .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
    248         .ToArray();
    249       TestTheilsUStatistic = testTheilsUCalculators
     326      TrainingTheilsUStatisticLast = trainingTheilsULastCalculators
     327        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     328        .ToArray();
     329      TestTheilsUStatisticLast = testTheilsULastCalculators
     330        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     331        .ToArray();
     332      TrainingTheilsUStatisticMean = trainingTheilsUMeanCalculators
     333        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     334        .ToArray();
     335      TestTheilsUStatisticMean = testTheilsUMeanCalculators
     336        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     337        .ToArray();
     338      TrainingTheilsUStatisticMovingAverage = trainingTheilsUMovingAverageCalculators
     339        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
     340        .ToArray();
     341      TestTheilsUStatisticMovingAverage = testTheilsUMovingAverageCalculators
    250342        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
    251343        .ToArray();
Note: See TracChangeset for help on using the changeset viewer.