Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs @ 13332

Last change on this file since 13332 was 13100, checked in by gkronber, 9 years ago

#1998: merged changes from the branch to trunk (btw. this branch was difficult to merge back to trunk because of it's specific structure)

File size: 13.7 KB
RevLine 
[6802]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6802]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  [StorableClass]
[8458]31  public abstract class TimeSeriesPrognosisSolutionBase : RegressionSolutionBase, ITimeSeriesPrognosisSolution {
[8468]32    #region result names
33    protected const string TrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
34    protected const string TestDirectionalSymmetryResultName = "Average directional symmetry (test)";
35    protected const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
36    protected const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
37    protected const string TrainingTheilsUStatisticAR1ResultName = "Theil's U2 (AR1) (training)";
38    protected const string TestTheilsUStatisticLastResultName = "Theil's U2 (AR1) (test)";
39    protected const string TrainingTheilsUStatisticMeanResultName = "Theil's U2 (mean) (training)";
40    protected const string TestTheilsUStatisticMeanResultName = "Theil's U2 (mean) (test)";
[8750]41    protected const string TimeSeriesPrognosisResultName = "Prognosis Results";
[8468]42    #endregion
43
44    #region result descriptions
45    protected const string TrainingDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the training partition";
46    protected const string TestDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the test partition";
47    protected const string TrainingWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the training partition";
48    protected const string TestWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the test partition";
49    protected const string TrainingTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the training partition";
50    protected const string TestTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the test partition";
51    protected const string TrainingTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean model) of the forecasts of the model on the training partition";
52    protected const string TestTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition";
[8750]53    protected const string TimeSeriesPrognosisResultDescription = "The calculated results of predictions in the future.";
[8468]54    #endregion
55
[6802]56    public new ITimeSeriesPrognosisModel Model {
57      get { return (ITimeSeriesPrognosisModel)base.Model; }
58      protected set { base.Model = value; }
59    }
60
61    public new ITimeSeriesPrognosisProblemData ProblemData {
62      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
63      set { base.ProblemData = value; }
64    }
65
[8010]66    public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
[6802]67
68    #region Results
[7989]69    public double TrainingDirectionalSymmetry {
70      get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
71      private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
[6802]72    }
[7989]73    public double TestDirectionalSymmetry {
74      get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
75      private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
[6802]76    }
[7989]77    public double TrainingWeightedDirectionalSymmetry {
78      get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
79      private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
[6802]80    }
[7989]81    public double TestWeightedDirectionalSymmetry {
82      get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
83      private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
[6802]84    }
[8468]85    public double TrainingTheilsUStatisticAR1 {
86      get { return ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value; }
87      private set { ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value = value; }
[6802]88    }
[8468]89    public double TestTheilsUStatisticAR1 {
[7989]90      get { return ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value; }
91      private set { ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value = value; }
[6802]92    }
[7989]93    public double TrainingTheilsUStatisticMean {
94      get { return ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value; }
95      private set { ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value = value; }
[7160]96    }
[7989]97    public double TestTheilsUStatisticMean {
98      get { return ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value; }
99      private set { ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value = value; }
[7160]100    }
[8468]101
[8750]102    public TimeSeriesPrognosisResults TimeSeriesPrognosisResults {
[8468]103      get {
[8750]104        if (!ContainsKey(TimeSeriesPrognosisResultName)) return null;
105        return (TimeSeriesPrognosisResults)this[TimeSeriesPrognosisResultName];
[8468]106      }
[8750]107      set {
108        if (ContainsKey(TimeSeriesPrognosisResultName)) Remove(TimeSeriesPrognosisResultName);
109        Add(new Result(TimeSeriesPrognosisResultName, TimeSeriesPrognosisResultDescription, value));
[8468]110      }
111    }
[8750]112    #endregion
[8468]113
114
[8458]115    public override IEnumerable<double> EstimatedValues {
116      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
117    }
118    public override IEnumerable<double> EstimatedTrainingValues {
119      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
120    }
121    public override IEnumerable<double> EstimatedTestValues {
122      get { return GetEstimatedValues(ProblemData.TestIndices); }
123    }
124    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
125      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
126    }
127
[6802]128    [StorableConstructor]
129    protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
[8468]130    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) : base(original, cloner) { }
[6802]131    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
132      : base(model, problemData) {
[8468]133      Add(new Result(TrainingDirectionalSymmetryResultName, TrainingDirectionalSymmetryResultDescription, new DoubleValue()));
134      Add(new Result(TestDirectionalSymmetryResultName, TestDirectionalSymmetryResultDescription, new DoubleValue()));
135      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, TrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
136      Add(new Result(TestWeightedDirectionalSymmetryResultName, TestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
137      Add(new Result(TrainingTheilsUStatisticAR1ResultName, TrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
138      Add(new Result(TestTheilsUStatisticLastResultName, TestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
139      Add(new Result(TrainingTheilsUStatisticMeanResultName, TrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
140      Add(new Result(TestTheilsUStatisticMeanResultName, TestTheilsUStatisticMeanResultDescription, new DoubleValue()));
[6802]141    }
142
[8458]143    protected override void RecalculateResults() {
144      base.RecalculateResults();
145      CalculateTimeSeriesResults();
[8468]146      CalculateTimeSeriesResults(ProblemData.TrainingHorizon, ProblemData.TestHorizon);
[8458]147    }
[6802]148
[8742]149    protected void CalculateTimeSeriesResults() {
[8010]150      OnlineCalculatorError errorState;
[11031]151      double trainingMean = ProblemData.TrainingIndices.Any() ? ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average() : double.NaN;
[13100]152      var meanModel = new ConstantModel(trainingMean);
[6802]153
[8010]154      double alpha, beta;
[8430]155      IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
156      OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
[8468]157      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
[7183]158
159
[8010]160      #region Calculate training quality measures
[11031]161      if (ProblemData.TrainingIndices.Any()) {
162        IEnumerable<double> trainingTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
163        IEnumerable<double> trainingEstimatedValues = EstimatedTrainingValues.ToList();
164        IEnumerable<double> trainingMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
165        IEnumerable<double> trainingAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
[7989]166
[11031]167        TrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
168        TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
169        TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
170        TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
171        TrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
172        TrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticAR1 : double.PositiveInfinity;
173        TrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
174        TrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity;
175      }
[8010]176      #endregion
[7989]177
[8468]178      #region Calculate test quality measures
[11031]179      if (ProblemData.TestIndices.Any()) {
180        IEnumerable<double> testTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToList();
181        IEnumerable<double> testEstimatedValues = EstimatedTestValues.ToList();
182        IEnumerable<double> testMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
183        IEnumerable<double> testAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
[7989]184
[11031]185        TestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
186        TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
187        TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
188        TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
189        TestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
190        TestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticAR1 : double.PositiveInfinity;
191        TestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
192        TestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity;
193      }
[8430]194      #endregion
[6802]195    }
[8468]196
[8742]197    protected void CalculateTimeSeriesResults(int trainingHorizon, int testHorizon) {
[8750]198      TimeSeriesPrognosisResults = new TimeSeriesPrognosisResults(trainingHorizon, testHorizon, this);
[8468]199    }
[6802]200  }
201}
Note: See TracBrowser for help on using the repository browser.