Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs @ 7005

Last change on this file since 7005 was 6802, checked in by gkronber, 13 years ago

#1081 added classes (problem, evaluators, analyzers, solution, model, online-calculators, and views) for time series prognosis problems and added an algorithm implementation to generation linear AR (auto-regressive) time series prognosis solution.

File size: 14.8 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 : DataAnalysisSolution, ITimeSeriesPrognosisSolution {
32    private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
33    private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
34    private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
35    private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
36    private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
37    private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
38    private const string TrainingRelativeErrorResultName = "Average relative error (training)";
39    private const string TestRelativeErrorResultName = "Average relative error (test)";
40    private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
41    private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
42    private const string TrainingDirectionalSymmetryResultName = "Directional symmetry (training)";
43    private const string TestDirectionalSymmetryResultName = "Directional symmetry (test)";
44    private const string TrainingWeightedDirectionalSymmetryResultName = "Weighted directional symmetry (training)";
45    private const string TestWeightedDirectionalSymmetryResultName = "Weighted directional symmetry (test)";
46    private const string TrainingTheilsUStatisticResultName = "Theil's U (training)";
47    private const string TestTheilsUStatisticResultName = "Theil's U (test)";
48
49    public new ITimeSeriesPrognosisModel Model {
50      get { return (ITimeSeriesPrognosisModel)base.Model; }
51      protected set { base.Model = value; }
52    }
53
54    public new ITimeSeriesPrognosisProblemData ProblemData {
55      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
56      set { base.ProblemData = value; }
57    }
58
59    public abstract IEnumerable<double> PrognosedValues { get; }
60    public abstract IEnumerable<double> PrognosedTrainingValues { get; }
61    public abstract IEnumerable<double> PrognosedTestValues { get; }
62    public abstract IEnumerable<double> GetPrognosedValues(IEnumerable<int> rows);
63
64    #region Results
65    public double TrainingMeanSquaredError {
66      get { return ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value; }
67      private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }
68    }
69    public double TestMeanSquaredError {
70      get { return ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value; }
71      private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }
72    }
73    public double TrainingMeanAbsoluteError {
74      get { return ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }
75      private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }
76    }
77    public double TestMeanAbsoluteError {
78      get { return ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value; }
79      private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }
80    }
81    public double TrainingRSquared {
82      get { return ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value; }
83      private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }
84    }
85    public double TestRSquared {
86      get { return ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value; }
87      private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }
88    }
89    public double TrainingRelativeError {
90      get { return ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value; }
91      private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }
92    }
93    public double TestRelativeError {
94      get { return ((DoubleValue)this[TestRelativeErrorResultName].Value).Value; }
95      private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }
96    }
97    public double TrainingNormalizedMeanSquaredError {
98      get { return ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }
99      private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }
100    }
101    public double TestNormalizedMeanSquaredError {
102      get { return ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }
103      private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }
104    }
105    public double TrainingDirectionalSymmetry {
106      get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
107      private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
108    }
109    public double TestDirectionalSymmetry {
110      get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
111      private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
112    }
113    public double TrainingWeightedDirectionalSymmetry {
114      get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
115      private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
116    }
117    public double TestWeightedDirectionalSymmetry {
118      get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
119      private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
120    }
121    public double TrainingTheilsUStatistic {
122      get { return ((DoubleValue)this[TrainingTheilsUStatisticResultName].Value).Value; }
123      private set { ((DoubleValue)this[TrainingTheilsUStatisticResultName].Value).Value = value; }
124    }
125    public double TestTheilsUStatistic {
126      get { return ((DoubleValue)this[TestTheilsUStatisticResultName].Value).Value; }
127      private set { ((DoubleValue)this[TestTheilsUStatisticResultName].Value).Value = 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 DoubleValue()));
139      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleValue()));
140      Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleValue()));
141      Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleValue()));
142      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleValue()));
143      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleValue()));
144      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));
145      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));
146      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleValue()));
147      Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleValue()));
148      Add(new Result(TrainingDirectionalSymmetryResultName, "The directional symmetry of the output of the model on the training partition", new DoubleValue()));
149      Add(new Result(TestDirectionalSymmetryResultName, "The directional symmetry of the output of the model on the test partition", new DoubleValue()));
150      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The weighted directional symmetry of the output of the model on the training partition", new DoubleValue()));
151      Add(new Result(TestWeightedDirectionalSymmetryResultName, "The weighted directional symmetry of the output of the model on the test partition", new DoubleValue()));
152      Add(new Result(TrainingTheilsUStatisticResultName, "The Theil's U statistic of the output of the model on the training partition", new DoubleValue()));
153      Add(new Result(TestTheilsUStatisticResultName, "The Theil's U statistic of the output of the model on the test partition", new DoubleValue()));
154    }
155
156    [StorableHook(HookType.AfterDeserialization)]
157    private void AfterDeserialization() {
158
159    }
160
161    protected void CalculateResults() {
162      double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values
163      double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
164      double[] estimatedTestValues = PrognosedTestValues.ToArray(); // cache values
165      double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
166
167      OnlineCalculatorError errorState;
168      double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
169      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN;
170      double testMse = OnlineMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
171      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMse : double.NaN;
172
173      double trainingMae = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
174      TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMae : double.NaN;
175      double testMae = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
176      TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMae : double.NaN;
177
178      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
179      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
180      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
181      TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
182
183      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
184      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
185      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
186      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
187
188      double trainingNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
189      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNmse : double.NaN;
190      double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
191      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN;
192
193      double trainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
194      TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? trainingDirectionalSymmetry : double.NaN;
195      double testDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
196      TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? testDirectionalSymmetry : double.NaN;
197
198      double trainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
199      TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? trainingWeightedDirectionalSymmetry : double.NaN;
200      double testWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
201      TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? testWeightedDirectionalSymmetry : double.NaN;
202
203      double trainingTheilsU = OnlineTheilsUStatisticCalculator.Calculate(estimatedTrainingValues, originalTrainingValues, out errorState);
204      TrainingTheilsUStatistic = errorState == OnlineCalculatorError.None ? trainingTheilsU : double.NaN;
205      double testTheilsU = OnlineTheilsUStatisticCalculator.Calculate(estimatedTestValues, originalTestValues, out errorState);
206      TestTheilsUStatistic = errorState == OnlineCalculatorError.None ? testTheilsU : double.NaN;
207
208
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.