1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HEAL.Fossil;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
30 | [StorableType("DF08D8C4-90F8-456E-855B-C8F487FF7B51")]
|
---|
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<double> PrognosedTestValues { get; }
|
---|
67 | public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
|
---|
68 |
|
---|
69 | #region Results
|
---|
70 | public double TrainingDirectionalSymmetry {
|
---|
71 | get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
|
---|
72 | private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
|
---|
73 | }
|
---|
74 | public double TestDirectionalSymmetry {
|
---|
75 | get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
|
---|
76 | private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
|
---|
77 | }
|
---|
78 | public double TrainingWeightedDirectionalSymmetry {
|
---|
79 | get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
|
---|
80 | private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
|
---|
81 | }
|
---|
82 | public double TestWeightedDirectionalSymmetry {
|
---|
83 | get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
|
---|
84 | private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
|
---|
85 | }
|
---|
86 | public double TrainingTheilsUStatisticAR1 {
|
---|
87 | get { return ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value; }
|
---|
88 | private set { ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value = value; }
|
---|
89 | }
|
---|
90 | public double TestTheilsUStatisticAR1 {
|
---|
91 | get { return ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value; }
|
---|
92 | private set { ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value = value; }
|
---|
93 | }
|
---|
94 | public double TrainingTheilsUStatisticMean {
|
---|
95 | get { return ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value; }
|
---|
96 | private set { ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value = value; }
|
---|
97 | }
|
---|
98 | public double TestTheilsUStatisticMean {
|
---|
99 | get { return ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value; }
|
---|
100 | private set { ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value = value; }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public TimeSeriesPrognosisResults TimeSeriesPrognosisResults {
|
---|
104 | get {
|
---|
105 | if (!ContainsKey(TimeSeriesPrognosisResultName)) return null;
|
---|
106 | return (TimeSeriesPrognosisResults)this[TimeSeriesPrognosisResultName];
|
---|
107 | }
|
---|
108 | set {
|
---|
109 | if (ContainsKey(TimeSeriesPrognosisResultName)) Remove(TimeSeriesPrognosisResultName);
|
---|
110 | Add(new Result(TimeSeriesPrognosisResultName, TimeSeriesPrognosisResultDescription, value));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 |
|
---|
116 | public override IEnumerable<double> EstimatedValues {
|
---|
117 | get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
118 | }
|
---|
119 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
120 | get { return GetEstimatedValues(ProblemData.TrainingIndices); }
|
---|
121 | }
|
---|
122 | public override IEnumerable<double> EstimatedTestValues {
|
---|
123 | get { return GetEstimatedValues(ProblemData.TestIndices); }
|
---|
124 | }
|
---|
125 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
126 | return Model.GetEstimatedValues(ProblemData.Dataset, rows);
|
---|
127 | }
|
---|
128 |
|
---|
129 | [StorableConstructor]
|
---|
130 | protected TimeSeriesPrognosisSolutionBase(StorableConstructorFlag _) : base(_) { }
|
---|
131 | protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) : base(original, cloner) { }
|
---|
132 | protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
|
---|
133 | : base(model, problemData) {
|
---|
134 | Add(new Result(TrainingDirectionalSymmetryResultName, TrainingDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
135 | Add(new Result(TestDirectionalSymmetryResultName, TestDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
136 | Add(new Result(TrainingWeightedDirectionalSymmetryResultName, TrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
137 | Add(new Result(TestWeightedDirectionalSymmetryResultName, TestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
|
---|
138 | Add(new Result(TrainingTheilsUStatisticAR1ResultName, TrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
|
---|
139 | Add(new Result(TestTheilsUStatisticLastResultName, TestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
|
---|
140 | Add(new Result(TrainingTheilsUStatisticMeanResultName, TrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
|
---|
141 | Add(new Result(TestTheilsUStatisticMeanResultName, TestTheilsUStatisticMeanResultDescription, new DoubleValue()));
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected override void RecalculateResults() {
|
---|
145 | base.RecalculateResults();
|
---|
146 | CalculateTimeSeriesResults();
|
---|
147 | CalculateTimeSeriesResults(ProblemData.TrainingHorizon, ProblemData.TestHorizon);
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected void CalculateTimeSeriesResults() {
|
---|
151 | OnlineCalculatorError errorState;
|
---|
152 | double trainingMean = ProblemData.TrainingIndices.Any() ? ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average() : double.NaN;
|
---|
153 | var meanModel = new ConstantModel(trainingMean, ProblemData.TargetVariable);
|
---|
154 |
|
---|
155 | double alpha, beta;
|
---|
156 | IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
|
---|
157 | OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
|
---|
158 | var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
|
---|
159 |
|
---|
160 |
|
---|
161 | #region Calculate training quality measures
|
---|
162 | if (ProblemData.TrainingIndices.Any()) {
|
---|
163 | IEnumerable<double> trainingTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
164 | IEnumerable<double> trainingEstimatedValues = EstimatedTrainingValues.ToList();
|
---|
165 | IEnumerable<double> trainingMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
|
---|
166 | IEnumerable<double> trainingAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
|
---|
167 |
|
---|
168 | TrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
|
---|
169 | TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
|
---|
170 | TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
|
---|
171 | TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
|
---|
172 | TrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
|
---|
173 | TrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticAR1 : double.PositiveInfinity;
|
---|
174 | TrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
|
---|
175 | TrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity;
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 |
|
---|
179 | #region Calculate test quality measures
|
---|
180 | if (ProblemData.TestIndices.Any()) {
|
---|
181 | IEnumerable<double> testTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToList();
|
---|
182 | IEnumerable<double> testEstimatedValues = EstimatedTestValues.ToList();
|
---|
183 | IEnumerable<double> testMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
|
---|
184 | IEnumerable<double> testAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
|
---|
185 |
|
---|
186 | TestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
|
---|
187 | TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
|
---|
188 | TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
|
---|
189 | TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
|
---|
190 | TestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
|
---|
191 | TestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticAR1 : double.PositiveInfinity;
|
---|
192 | TestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
|
---|
193 | TestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity;
|
---|
194 | }
|
---|
195 | #endregion
|
---|
196 | }
|
---|
197 |
|
---|
198 | protected void CalculateTimeSeriesResults(int trainingHorizon, int testHorizon) {
|
---|
199 | TimeSeriesPrognosisResults = new TimeSeriesPrognosisResults(trainingHorizon, testHorizon, this);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|