Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9714 was 5305, checked in by gkronber, 14 years ago

worked on data analysis feature exploration branch. #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        matrixView.Content = CalculateMatrix();
88      } else
89        matrixView.Content = null;
90    }
91
92    public DoubleMatrix CalculateMatrix() {
93      List<string> targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
94      DoubleMatrix matrix = new DoubleMatrix(rowNames.Count, targetVariables.Count() * 2);
95      matrix.RowNames = rowNames;
96      matrix.ColumnNames = targetVariables.SelectMany(x => new List<string>() { x + " (training)", x + " (test)" });
97      matrix.SortableView = false;
98
99      int trainingStart = Content.ProblemData.TrainingSamplesStart.Value;
100      int trainingEnd = Content.ProblemData.TrainingSamplesEnd.Value;
101      int testStart = Content.ProblemData.TestSamplesStart.Value;
102      int testEnd = Content.ProblemData.TestSamplesEnd.Value;
103      // create a list of time series evaluators for each target variable
104      Dictionary<string, List<IOnlineEvaluator>> trainingEvaluators =
105        new Dictionary<string, List<IOnlineEvaluator>>();
106      Dictionary<string, List<IOnlineEvaluator>> testEvaluators =
107        new Dictionary<string, List<IOnlineEvaluator>>();
108      foreach (string targetVariable in targetVariables) {
109        trainingEvaluators.Add(targetVariable, new List<IOnlineEvaluator>());
110        trainingEvaluators[targetVariable].Add(new OnlineMeanSquaredErrorEvaluator());
111        trainingEvaluators[targetVariable].Add(new OnlinePearsonsRSquaredEvaluator());
112        trainingEvaluators[targetVariable].Add(new OnlineMeanAbsolutePercentageErrorEvaluator());
113        trainingEvaluators[targetVariable].Add(new OnlineDirectionalSymmetryEvaluator());
114        trainingEvaluators[targetVariable].Add(new OnlineWeightedDirectionalSymmetryEvaluator());
115        trainingEvaluators[targetVariable].Add(new OnlineTheilsUStatisticEvaluator());
116
117        testEvaluators.Add(targetVariable, new List<IOnlineEvaluator>());
118        testEvaluators[targetVariable].Add(new OnlineMeanSquaredErrorEvaluator());
119        testEvaluators[targetVariable].Add(new OnlinePearsonsRSquaredEvaluator());
120        testEvaluators[targetVariable].Add(new OnlineMeanAbsolutePercentageErrorEvaluator());
121        testEvaluators[targetVariable].Add(new OnlineDirectionalSymmetryEvaluator());
122        testEvaluators[targetVariable].Add(new OnlineWeightedDirectionalSymmetryEvaluator());
123        testEvaluators[targetVariable].Add(new OnlineTheilsUStatisticEvaluator());
124      }
125
126      Evaluate(trainingStart, trainingEnd, trainingEvaluators);
127      Evaluate(testStart, testEnd, testEvaluators);
128
129      int columnIndex = 0;
130      foreach (string targetVariable in targetVariables) {
131        int rowIndex = 0;
132        // training
133        foreach (var evaluator in trainingEvaluators[targetVariable]) {
134          matrix[rowIndex++, columnIndex] = Math.Round(evaluator.Value, 3);
135        }
136        columnIndex++;
137        // test
138        rowIndex = 0;
139        foreach (var evaluator in testEvaluators[targetVariable]) {
140          matrix[rowIndex++, columnIndex] = Math.Round(evaluator.Value, 3);
141        }
142        columnIndex++;
143      }
144      return matrix;
145    }
146
147    private void Evaluate(int start, int end, Dictionary<string, List<IOnlineEvaluator>> evaluators) {
148
149      for (int row = start; row < end; row++) {
150        if (string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) || Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row] != 0) {
151          // prepare evaluators for each target variable for a new prediction window
152          foreach (var entry in evaluators) {
153            double referenceOriginalValue = Content.ProblemData.Dataset[entry.Key, row - 1];
154            foreach (IOnlineTimeSeriesPrognosisEvaluator evaluator in entry.Value.OfType<IOnlineTimeSeriesPrognosisEvaluator>()) {
155              evaluator.StartNewPredictionWindow(referenceOriginalValue);
156            }
157          }
158          List<string> targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
159
160          if (string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) ||
161            Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row] > 0) {
162            int timestep = 0;
163            foreach (double[] x in Content.GetPrognosis(row)) {
164              int targetIndex = 0;
165              if (row + timestep < Content.ProblemData.Dataset.Rows) {
166                foreach (var targetVariable in targetVariables) {
167                  double originalValue = Content.ProblemData.Dataset[targetVariable, row + timestep];
168                  double estimatedValue = x[targetIndex];
169                  if (IsValidValue(originalValue) && IsValidValue(estimatedValue)) {
170                    foreach (IOnlineEvaluator evaluator in evaluators[targetVariable]) {
171                      evaluator.Add(originalValue, estimatedValue);
172                    }
173                  }
174                  targetIndex++;
175                }
176              }
177              timestep++;
178            }
179          }
180        }
181      }
182    }
183
184    private bool IsValidValue(double d) {
185      return !(double.IsNaN(d) || double.IsInfinity(d));
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.