Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs @ 14186

Last change on this file since 14186 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 13.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 TimeSeriesPrognosisResultName = "Prognosis Results";
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";
53    protected const string TimeSeriesPrognosisResultDescription = "The calculated results of predictions in the future.";
54    #endregion
55
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
66    public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
67
68    #region Results
69    public double TrainingDirectionalSymmetry {
70      get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
71      private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
72    }
73    public double TestDirectionalSymmetry {
74      get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
75      private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
76    }
77    public double TrainingWeightedDirectionalSymmetry {
78      get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
79      private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
80    }
81    public double TestWeightedDirectionalSymmetry {
82      get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
83      private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
84    }
85    public double TrainingTheilsUStatisticAR1 {
86      get { return ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value; }
87      private set { ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value = value; }
88    }
89    public double TestTheilsUStatisticAR1 {
90      get { return ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value; }
91      private set { ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value = value; }
92    }
93    public double TrainingTheilsUStatisticMean {
94      get { return ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value; }
95      private set { ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value = value; }
96    }
97    public double TestTheilsUStatisticMean {
98      get { return ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value; }
99      private set { ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value = value; }
100    }
101
102    public TimeSeriesPrognosisResults TimeSeriesPrognosisResults {
103      get {
104        if (!ContainsKey(TimeSeriesPrognosisResultName)) return null;
105        return (TimeSeriesPrognosisResults)this[TimeSeriesPrognosisResultName];
106      }
107      set {
108        if (ContainsKey(TimeSeriesPrognosisResultName)) Remove(TimeSeriesPrognosisResultName);
109        Add(new Result(TimeSeriesPrognosisResultName, TimeSeriesPrognosisResultDescription, value));
110      }
111    }
112    #endregion
113
114
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
128    [StorableConstructor]
129    protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
130    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) : base(original, cloner) { }
131    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
132      : base(model, problemData) {
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()));
141    }
142
143    protected override void RecalculateResults() {
144      base.RecalculateResults();
145      CalculateTimeSeriesResults();
146      CalculateTimeSeriesResults(ProblemData.TrainingHorizon, ProblemData.TestHorizon);
147    }
148
149    protected void CalculateTimeSeriesResults() {
150      OnlineCalculatorError errorState;
151      double trainingMean = ProblemData.TrainingIndices.Any() ? ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average() : double.NaN;
152      var meanModel = new ConstantModel(trainingMean,ProblemData.TargetVariable);
153
154      double alpha, beta;
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);
157      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
158
159
160      #region Calculate training quality measures
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();
166
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      }
176      #endregion
177
178      #region Calculate test quality measures
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();
184
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      }
194      #endregion
195    }
196
197    protected void CalculateTimeSeriesResults(int trainingHorizon, int testHorizon) {
198      TimeSeriesPrognosisResults = new TimeSeriesPrognosisResults(trainingHorizon, testHorizon, this);
199    }
200  }
201}
Note: See TracBrowser for help on using the repository browser.