source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs @ 8750

Last change on this file since 8750 was 8750, checked in by mkommend, 10 years ago

#1081: Extracted prognosis results into separate class and added a view for them.

File size: 16.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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]
31  public abstract class TimeSeriesPrognosisSolutionBase : RegressionSolutionBase, ITimeSeriesPrognosisSolution {
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)";
41    protected const string TrainingTheilsUStatisticMovingAverageResultName = "Theil's U2 (moving average) (training)";
42    protected const string TestTheilsUStatisticMovingAverageResultName = "Theil's U2 (moving average) (test)";
43    protected const string TimeSeriesPrognosisResultName = "Prognosis Results";
44    #endregion
45
46    #region result descriptions
47    protected const string TrainingDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the training partition";
48    protected const string TestDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the test partition";
49    protected const string TrainingWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the training partition";
50    protected const string TestWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the test partition";
51    protected const string TrainingTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the training partition";
52    protected const string TestTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the test partition";
53    protected const string TrainingTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean model) of the forecasts of the model on the training partition";
54    protected const string TestTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition";
55    protected const string TrainingTheilsUStatisticMovingAverageResultDescription = "The Theil's U statistic (reference: moving average model) of the forecasts of the model on the training partition";
56    protected const string TestTheilsUStatisticMovingAverageResultDescription = "The Theil's U statistic (reference: moving average model) of the forecasts of the model on the test partition";
57    protected const string TimeSeriesPrognosisResultDescription = "The calculated results of predictions in the future.";
58    #endregion
59
60    public new ITimeSeriesPrognosisModel Model {
61      get { return (ITimeSeriesPrognosisModel)base.Model; }
62      protected set { base.Model = value; }
63    }
64
65    public new ITimeSeriesPrognosisProblemData ProblemData {
66      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
67      set { base.ProblemData = value; }
68    }
69
70    public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
71
72    #region Results
73    public double TrainingDirectionalSymmetry {
74      get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
75      private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
76    }
77    public double TestDirectionalSymmetry {
78      get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
79      private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
80    }
81    public double TrainingWeightedDirectionalSymmetry {
82      get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
83      private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
84    }
85    public double TestWeightedDirectionalSymmetry {
86      get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
87      private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
88    }
89    public double TrainingTheilsUStatisticAR1 {
90      get { return ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value; }
91      private set { ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value = value; }
92    }
93    public double TestTheilsUStatisticAR1 {
94      get { return ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value; }
95      private set { ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value = value; }
96    }
97    public double TrainingTheilsUStatisticMean {
98      get { return ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value; }
99      private set { ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value = value; }
100    }
101    public double TestTheilsUStatisticMean {
102      get { return ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value; }
103      private set { ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value = value; }
104    }
105    public double TrainingTheilsUStatisticMovingAverage {
106      get { return ((DoubleValue)this[TrainingTheilsUStatisticMovingAverageResultName].Value).Value; }
107      private set { ((DoubleValue)this[TrainingTheilsUStatisticMovingAverageResultName].Value).Value = value; }
108    }
109    public double TestTheilsUStatisticMovingAverage {
110      get { return ((DoubleValue)this[TestTheilsUStatisticMovingAverageResultName].Value).Value; }
111      private set { ((DoubleValue)this[TestTheilsUStatisticMovingAverageResultName].Value).Value = value; }
112    }
113
114    public TimeSeriesPrognosisResults TimeSeriesPrognosisResults {
115      get {
116        if (!ContainsKey(TimeSeriesPrognosisResultName)) return null;
117        return (TimeSeriesPrognosisResults)this[TimeSeriesPrognosisResultName];
118      }
119      set {
120        if (ContainsKey(TimeSeriesPrognosisResultName)) Remove(TimeSeriesPrognosisResultName);
121        Add(new Result(TimeSeriesPrognosisResultName, TimeSeriesPrognosisResultDescription, value));
122      }
123    }
124    #endregion
125
126
127    public override IEnumerable<double> EstimatedValues {
128      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
129    }
130    public override IEnumerable<double> EstimatedTrainingValues {
131      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
132    }
133    public override IEnumerable<double> EstimatedTestValues {
134      get { return GetEstimatedValues(ProblemData.TestIndices); }
135    }
136    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
137      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
138    }
139
140    [StorableConstructor]
141    protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
142    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) : base(original, cloner) { }
143    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
144      : base(model, problemData) {
145      Add(new Result(TrainingDirectionalSymmetryResultName, TrainingDirectionalSymmetryResultDescription, new DoubleValue()));
146      Add(new Result(TestDirectionalSymmetryResultName, TestDirectionalSymmetryResultDescription, new DoubleValue()));
147      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, TrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
148      Add(new Result(TestWeightedDirectionalSymmetryResultName, TestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
149      Add(new Result(TrainingTheilsUStatisticAR1ResultName, TrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
150      Add(new Result(TestTheilsUStatisticLastResultName, TestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
151      Add(new Result(TrainingTheilsUStatisticMeanResultName, TrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
152      Add(new Result(TestTheilsUStatisticMeanResultName, TestTheilsUStatisticMeanResultDescription, new DoubleValue()));
153      Add(new Result(TrainingTheilsUStatisticMovingAverageResultName, TrainingTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
154      Add(new Result(TestTheilsUStatisticMovingAverageResultName, TestTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
155    }
156
157    protected override void RecalculateResults() {
158      base.RecalculateResults();
159      CalculateTimeSeriesResults();
160      CalculateTimeSeriesResults(ProblemData.TrainingHorizon, ProblemData.TestHorizon);
161    }
162
163    protected void CalculateTimeSeriesResults() {
164      OnlineCalculatorError errorState;
165      double trainingMean = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average();
166      var meanModel = new ConstantTimeSeriesPrognosisModel(trainingMean);
167
168      double alpha, beta;
169      IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
170      OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
171      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
172
173      //MA model
174      const int movingAverageWindowSize = 10;
175      var movingAverageModel = new TimeSeriesPrognosisMovingAverageModel(movingAverageWindowSize, ProblemData.TargetVariable);
176
177      #region Calculate training quality measures
178      IEnumerable<double> trainingTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
179      IEnumerable<double> trainingEstimatedValues = EstimatedTrainingValues.ToList();
180      IEnumerable<double> trainingMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
181      IEnumerable<double> trainingAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
182      IEnumerable<double> trainingMovingAverageModelPredictions = movingAverageModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
183
184      TrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
185      TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
186      TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
187      TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
188      TrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
189      TrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticAR1 : double.PositiveInfinity;
190      TrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
191      TrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity;
192      TrainingTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMovingAverageModelPredictions, trainingEstimatedValues, out errorState);
193      TrainingTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMovingAverage : double.PositiveInfinity;
194      #endregion
195
196      #region Calculate test quality measures
197      IEnumerable<double> testTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToList();
198      IEnumerable<double> testEstimatedValues = EstimatedTestValues.ToList();
199      IEnumerable<double> testMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
200      IEnumerable<double> testAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
201      IEnumerable<double> testMovingAverageModelPredictions = movingAverageModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
202
203      TestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
204      TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
205      TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
206      TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
207      TestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
208      TestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticAR1 : double.PositiveInfinity;
209      TestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
210      TestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity;
211      TestTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMovingAverageModelPredictions, testEstimatedValues, out errorState);
212      TestTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMovingAverage : double.PositiveInfinity;
213      #endregion
214    }
215
216    protected void CalculateTimeSeriesResults(int trainingHorizon, int testHorizon) {
217      TimeSeriesPrognosisResults = new TimeSeriesPrognosisResults(trainingHorizon, testHorizon, this);
218    }
219  }
220}
Note: See TracBrowser for help on using the repository browser.