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 |
|
---|
22 | using System.Collections.Concurrent;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class TimeSeriesPrognosisSolutionBase : DataAnalysisSolution, ITimeSeriesPrognosisSolution {
|
---|
33 | private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
|
---|
34 | private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
|
---|
35 | private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
|
---|
36 | private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
|
---|
37 | private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
|
---|
38 | private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
|
---|
39 | private const string TrainingRelativeErrorResultName = "Average relative error (training)";
|
---|
40 | private const string TestRelativeErrorResultName = "Average relative error (test)";
|
---|
41 | private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
|
---|
42 | private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
|
---|
43 | private const string TrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
|
---|
44 | private const string TestDirectionalSymmetryResultName = "Average directional symmetry (test)";
|
---|
45 | private const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
|
---|
46 | 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)";
|
---|
49 |
|
---|
50 | public new ITimeSeriesPrognosisModel Model {
|
---|
51 | get { return (ITimeSeriesPrognosisModel)base.Model; }
|
---|
52 | protected set { base.Model = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public new ITimeSeriesPrognosisProblemData ProblemData {
|
---|
56 | get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
|
---|
57 | set { base.ProblemData = value; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public abstract IEnumerable<IEnumerable<double>> PrognosedTrainingValues { get; }
|
---|
61 | public abstract IEnumerable<IEnumerable<double>> PrognosedTestValues { get; }
|
---|
62 | public abstract IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(IEnumerable<int> rows, int horizon);
|
---|
63 |
|
---|
64 | #region Results
|
---|
65 | public double[] TrainingMeanSquaredError {
|
---|
66 | get { return ((DoubleArray)this[TrainingMeanSquaredErrorResultName].Value).ToArray(); }
|
---|
67 | private set { this[TrainingMeanSquaredErrorResultName].Value = new DoubleArray(value); }
|
---|
68 | }
|
---|
69 | public double[] TestMeanSquaredError {
|
---|
70 | get { return ((DoubleArray)this[TestMeanSquaredErrorResultName].Value).ToArray(); }
|
---|
71 | private set { this[TestMeanSquaredErrorResultName].Value = new DoubleArray(value); }
|
---|
72 | }
|
---|
73 | public double[] TrainingMeanAbsoluteError {
|
---|
74 | get { return ((DoubleArray)this[TrainingMeanAbsoluteErrorResultName].Value).ToArray(); }
|
---|
75 | private set { this[TrainingMeanAbsoluteErrorResultName].Value = new DoubleArray(value); }
|
---|
76 | }
|
---|
77 | public double[] TestMeanAbsoluteError {
|
---|
78 | get { return ((DoubleArray)this[TestMeanAbsoluteErrorResultName].Value).ToArray(); }
|
---|
79 | private set { this[TestMeanAbsoluteErrorResultName].Value = new DoubleArray(value); }
|
---|
80 | }
|
---|
81 | public double[] TrainingRSquared {
|
---|
82 | get { return ((DoubleArray)this[TrainingSquaredCorrelationResultName].Value).ToArray(); }
|
---|
83 | private set { this[TrainingSquaredCorrelationResultName].Value = new DoubleArray(value); }
|
---|
84 | }
|
---|
85 | public double[] TestRSquared {
|
---|
86 | get { return ((DoubleArray)this[TestSquaredCorrelationResultName].Value).ToArray(); }
|
---|
87 | private set { this[TestSquaredCorrelationResultName].Value = new DoubleArray(value); }
|
---|
88 | }
|
---|
89 | public double[] TrainingRelativeError {
|
---|
90 | get { return ((DoubleArray)this[TrainingRelativeErrorResultName].Value).ToArray(); }
|
---|
91 | private set { this[TrainingRelativeErrorResultName].Value = new DoubleArray(value); }
|
---|
92 | }
|
---|
93 | public double[] TestRelativeError {
|
---|
94 | get { return ((DoubleArray)this[TestRelativeErrorResultName].Value).ToArray(); }
|
---|
95 | private set { this[TestRelativeErrorResultName].Value = new DoubleArray(value); }
|
---|
96 | }
|
---|
97 | public double[] TrainingNormalizedMeanSquaredError {
|
---|
98 | get { return ((DoubleArray)this[TrainingNormalizedMeanSquaredErrorResultName].Value).ToArray(); }
|
---|
99 | private set { this[TrainingNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); }
|
---|
100 | }
|
---|
101 | public double[] TestNormalizedMeanSquaredError {
|
---|
102 | get { return ((DoubleArray)this[TestNormalizedMeanSquaredErrorResultName].Value).ToArray(); }
|
---|
103 | private set { this[TestNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); }
|
---|
104 | }
|
---|
105 | public double[] TrainingDirectionalSymmetry {
|
---|
106 | get { return ((DoubleArray)this[TrainingDirectionalSymmetryResultName].Value).ToArray(); }
|
---|
107 | private set { this[TrainingDirectionalSymmetryResultName].Value = new DoubleArray(value); }
|
---|
108 | }
|
---|
109 | public double[] TestDirectionalSymmetry {
|
---|
110 | get { return ((DoubleArray)this[TestDirectionalSymmetryResultName].Value).ToArray(); }
|
---|
111 | private set { this[TestDirectionalSymmetryResultName].Value = new DoubleArray(value); }
|
---|
112 | }
|
---|
113 | public double[] TrainingWeightedDirectionalSymmetry {
|
---|
114 | get { return ((DoubleArray)this[TrainingWeightedDirectionalSymmetryResultName].Value).ToArray(); }
|
---|
115 | private set { this[TrainingWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
|
---|
116 | }
|
---|
117 | public double[] TestWeightedDirectionalSymmetry {
|
---|
118 | get { return ((DoubleArray)this[TestWeightedDirectionalSymmetryResultName].Value).ToArray(); }
|
---|
119 | private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
|
---|
120 | }
|
---|
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); }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | [StorableConstructor]
|
---|
132 | protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
|
---|
133 | protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner)
|
---|
134 | : base(original, cloner) {
|
---|
135 | }
|
---|
136 | protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
|
---|
137 | : base(model, problemData) {
|
---|
138 | Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleArray()));
|
---|
139 | Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleArray()));
|
---|
140 | Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleArray()));
|
---|
141 | Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleArray()));
|
---|
142 | Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleArray()));
|
---|
143 | Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleArray()));
|
---|
144 | Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new DoubleArray()));
|
---|
145 | Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new DoubleArray()));
|
---|
146 | Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleArray()));
|
---|
147 | Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleArray()));
|
---|
148 | Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
|
---|
149 | Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new DoubleArray()));
|
---|
150 | Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
|
---|
151 | 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()));
|
---|
154 | }
|
---|
155 |
|
---|
156 | [StorableHook(HookType.AfterDeserialization)]
|
---|
157 | private void AfterDeserialization() {
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | protected void CalculateResults() {
|
---|
162 | OnlineCalculatorError errorState;
|
---|
163 | string[] targetVariables = ProblemData.TargetVariables.ToArray();
|
---|
164 | /*
|
---|
165 | double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values
|
---|
166 | double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
|
---|
167 | double[] estimatedTestValues = PrognosedTestValues.ToArray(); // cache values
|
---|
168 | double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
|
---|
169 |
|
---|
170 | double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
171 | TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN;
|
---|
172 | double testMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
173 | TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMse : double.NaN;
|
---|
174 |
|
---|
175 | double trainingMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
176 | TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMae : double.NaN;
|
---|
177 | double testMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
178 | TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMae : double.NaN;
|
---|
179 |
|
---|
180 | double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
181 | TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
|
---|
182 | double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
183 | TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
|
---|
184 |
|
---|
185 | double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
186 | TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
|
---|
187 | double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
188 | TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
|
---|
189 |
|
---|
190 | double trainingNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
|
---|
191 | TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNmse : double.NaN;
|
---|
192 | double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
|
---|
193 | TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN;
|
---|
194 | */
|
---|
195 |
|
---|
196 | //double[] trainingDs = new double[targetVariables.Length];
|
---|
197 | //double[] testDs = new double[targetVariables.Length];
|
---|
198 |
|
---|
199 | //double[] trainingWds = new double[targetVariables.Length];
|
---|
200 | //double[] testWds = new double[targetVariables.Length];
|
---|
201 |
|
---|
202 | //double[] trainingTheilsU = new double[targetVariables.Length];
|
---|
203 | //double[] testTheilsU = new double[targetVariables.Length];
|
---|
204 |
|
---|
205 | var trainingDsCalculators = new OnlineDirectionalSymmetryCalculator[targetVariables.Length];
|
---|
206 | var testDsCalculators = new OnlineDirectionalSymmetryCalculator[targetVariables.Length];
|
---|
207 | var trainingWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
|
---|
208 | var testWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
|
---|
209 | var trainingTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
|
---|
210 | var testTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
|
---|
211 | for (int i = 0; i < targetVariables.Length; i++) {
|
---|
212 | trainingDsCalculators[i] = new OnlineDirectionalSymmetryCalculator();
|
---|
213 | testDsCalculators[i] = new OnlineDirectionalSymmetryCalculator();
|
---|
214 | trainingWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
|
---|
215 | testWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
|
---|
216 | trainingTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator();
|
---|
217 | testTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator();
|
---|
218 | }
|
---|
219 |
|
---|
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);
|
---|
236 | }
|
---|
237 |
|
---|
238 | TrainingDirectionalSymmetry = trainingDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
|
---|
239 | .ToArray();
|
---|
240 | TestDirectionalSymmetry = testDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
|
---|
241 | .ToArray();
|
---|
242 | TrainingWeightedDirectionalSymmetry = trainingWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
|
---|
243 | .ToArray();
|
---|
244 | TestWeightedDirectionalSymmetry = testWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
|
---|
245 | .ToArray();
|
---|
246 | TrainingTheilsUStatistic = trainingDsCalculators
|
---|
247 | .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
|
---|
248 | .ToArray();
|
---|
249 | TestTheilsUStatistic = testTheilsUCalculators
|
---|
250 | .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
|
---|
251 | .ToArray();
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|