Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Views/3.3/ResultsView.cs @ 4695

Last change on this file since 4695 was 4556, checked in by gkronber, 14 years ago

Added classes and views for analysis of symbolic time series prognosis results. #1142

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
21using System;
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Drawing;
25using System.Data;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31using HeuristicLab.Data.Views;
32using HeuristicLab.Data;
33using HeuristicLab.Problems.DataAnalysis.Evaluators;
34using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic;
35
36namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Views {
37  [Content(typeof(SymbolicTimeSeriesPrognosisSolution), true)]
38  [View("Time Series Prognosis Results View")]
39  public partial class ResultsView : AsynchronousContentView {
40    private List<string> rowNames = new List<string>() {
41      "Mean squared error",
42      "Pearson's R²",
43      "Mean relative error",
44      "Directional symmetry",
45      "Weighted directional symmetry",
46      "Theil's U"
47    };
48
49    public ResultsView() {
50      InitializeComponent();
51    }
52
53    public new SymbolicTimeSeriesPrognosisSolution Content {
54      get { return (SymbolicTimeSeriesPrognosisSolution)base.Content; }
55      set { base.Content = value; }
56    }
57
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.ModelChanged += new EventHandler(Content_ModelChanged);
61      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
62      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
63    }
64    protected override void DeregisterContentEvents() {
65      base.DeregisterContentEvents();
66      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
67      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
68      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
69    }
70
71    private void Content_ModelChanged(object sender, EventArgs e) {
72      UpdateView();
73    }
74    private void Content_ProblemDataChanged(object sender, EventArgs e) {
75      UpdateView();
76    }
77    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
78      UpdateView();
79    }
80
81    protected override void OnContentChanged() {
82      base.OnContentChanged();
83      UpdateView();
84    }
85    private void UpdateView() {
86      if (Content != null) {
87        List<string> targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
88        DoubleMatrix matrix = new DoubleMatrix(rowNames.Count, targetVariables.Count() * 2);
89        matrix.RowNames = rowNames;
90        matrix.ColumnNames = targetVariables.SelectMany(x => new List<string>() { x + " (training)", x + " (test)" });
91        matrix.SortableView = false;
92
93        int trainingStart = Content.ProblemData.TrainingSamplesStart.Value;
94        int trainingEnd = Content.ProblemData.TrainingSamplesEnd.Value;
95        int testStart = Content.ProblemData.TestSamplesStart.Value;
96        int testEnd = Content.ProblemData.TestSamplesEnd.Value;
97        // create a list of time series evaluators for each target variable
98        Dictionary<string, List<IOnlineEvaluator>> trainingEvaluators =
99          new Dictionary<string, List<IOnlineEvaluator>>();
100        Dictionary<string, List<IOnlineEvaluator>> testEvaluators =
101          new Dictionary<string, List<IOnlineEvaluator>>();
102        foreach (string targetVariable in targetVariables) {
103          trainingEvaluators.Add(targetVariable, new List<IOnlineEvaluator>());
104          trainingEvaluators[targetVariable].Add(new OnlineMeanSquaredErrorEvaluator());
105          trainingEvaluators[targetVariable].Add(new OnlinePearsonsRSquaredEvaluator());
106          trainingEvaluators[targetVariable].Add(new OnlineMeanAbsolutePercentageErrorEvaluator());
107          trainingEvaluators[targetVariable].Add(new OnlineDirectionalSymmetryEvaluator());
108          trainingEvaluators[targetVariable].Add(new OnlineWeightedDirectionalSymmetryEvaluator());
109          trainingEvaluators[targetVariable].Add(new OnlineTheilsUStatisticEvaluator());
110
111          testEvaluators.Add(targetVariable, new List<IOnlineEvaluator>());
112          testEvaluators[targetVariable].Add(new OnlineMeanSquaredErrorEvaluator());
113          testEvaluators[targetVariable].Add(new OnlinePearsonsRSquaredEvaluator());
114          testEvaluators[targetVariable].Add(new OnlineMeanAbsolutePercentageErrorEvaluator());
115          testEvaluators[targetVariable].Add(new OnlineDirectionalSymmetryEvaluator());
116          testEvaluators[targetVariable].Add(new OnlineWeightedDirectionalSymmetryEvaluator());
117          testEvaluators[targetVariable].Add(new OnlineTheilsUStatisticEvaluator());
118        }
119
120        Evaluate(trainingStart, trainingEnd, trainingEvaluators);
121        Evaluate(testStart, testEnd, testEvaluators);
122
123        int columnIndex = 0;
124        foreach (string targetVariable in targetVariables) {
125          int rowIndex = 0;
126          // training
127          foreach (var evaluator in trainingEvaluators[targetVariable]) {
128            matrix[rowIndex++, columnIndex] = evaluator.Value;
129          }
130          columnIndex++;
131          // test
132          rowIndex = 0;
133          foreach (var evaluator in testEvaluators[targetVariable]) {
134            matrix[rowIndex++, columnIndex] = evaluator.Value;
135          }
136          columnIndex++;
137        }
138
139        matrixView.Content = matrix;
140      } else
141        matrixView.Content = null;
142    }
143
144    private void Evaluate(int start, int end, Dictionary<string, List<IOnlineEvaluator>> evaluators) {
145
146      for (int row = start; row < end; row++) {
147        if (string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) || Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row] != 0) {
148          // prepare evaluators for each target variable for a new prediction window
149          foreach (var entry in evaluators) {
150            double referenceOriginalValue = Content.ProblemData.Dataset[entry.Key, row - 1];
151            foreach (IOnlineTimeSeriesPrognosisEvaluator evaluator in entry.Value.OfType<IOnlineTimeSeriesPrognosisEvaluator>()) {
152              evaluator.StartNewPredictionWindow(referenceOriginalValue);
153            }
154          }
155          List<string> targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
156
157          if (string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) ||
158            Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row] > 0) {
159            int timestep = 0;
160            foreach (double[] x in Content.GetPrognosis(row)) {
161              int targetIndex = 0;
162              if (row + timestep < Content.ProblemData.Dataset.Rows) {
163                foreach (var targetVariable in targetVariables) {
164                  double originalValue = Content.ProblemData.Dataset[targetVariable, row + timestep];
165                  double estimatedValue = x[targetIndex];
166                  if (IsValidValue(originalValue) && IsValidValue(estimatedValue)) {
167                    foreach (IOnlineEvaluator evaluator in evaluators[targetVariable]) {
168                      evaluator.Add(originalValue, estimatedValue);
169                    }
170                  }
171                  targetIndex++;
172                }
173              }
174              timestep++;
175            }
176          }
177        }
178      }
179    }
180
181    private bool IsValidValue(double d) {
182      return !(double.IsNaN(d) || double.IsInfinity(d));
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.