Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.DataAnalysis.Views/3.4/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView.cs @ 6878

Last change on this file since 6878 was 6878, checked in by abeham, 13 years ago

#1614

  • updated branch from trunk
File size: 9.0 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Error Characteristics Curve")]
31  [Content(typeof(ITimeSeriesPrognosisSolution))]
32  public partial class TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView : DataAnalysisSolutionEvaluationView {
33    protected const string TrainingSamples = "Training";
34    protected const string TestSamples = "Test";
35    protected const string AllSamples = "All Samples";
36
37    public TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView()
38      : base() {
39      InitializeComponent();
40
41      cmbSamples.Items.Add(TrainingSamples);
42      cmbSamples.Items.Add(TestSamples);
43      cmbSamples.Items.Add(AllSamples);
44
45      cmbSamples.SelectedIndex = 0;
46
47      chart.CustomizeAllChartAreas();
48      chart.ChartAreas[0].AxisX.Title = "Absolute Error";
49      chart.ChartAreas[0].AxisX.Minimum = 0.0;
50      chart.ChartAreas[0].AxisX.Maximum = 1.0;
51      chart.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
52      chart.ChartAreas[0].CursorX.Interval = 0.01;
53
54      chart.ChartAreas[0].AxisY.Title = "Number of Samples";
55      chart.ChartAreas[0].AxisY.Minimum = 0.0;
56      chart.ChartAreas[0].AxisY.Maximum = 1.0;
57      chart.ChartAreas[0].AxisY.MajorGrid.Interval = 0.2;
58      chart.ChartAreas[0].CursorY.Interval = 0.01;
59    }
60
61    public new ITimeSeriesPrognosisSolution Content {
62      get { return (ITimeSeriesPrognosisSolution)base.Content; }
63      set { base.Content = value; }
64    }
65    public ITimeSeriesPrognosisProblemData ProblemData {
66      get {
67        if (Content == null) return null;
68        return Content.ProblemData;
69      }
70    }
71
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.ModelChanged += new EventHandler(Content_ModelChanged);
75      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
76    }
77    protected override void DeregisterContentEvents() {
78      base.DeregisterContentEvents();
79      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
80      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
81    }
82
83    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
84      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ModelChanged, sender, e);
85      else UpdateChart();
86    }
87    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
88      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ProblemDataChanged, sender, e);
89      else {
90        UpdateChart();
91      }
92    }
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95      UpdateChart();
96    }
97
98    protected virtual void UpdateChart() {
99      chart.Series.Clear();
100      chart.Annotations.Clear();
101      if (Content == null) return;
102
103      var originalValues = GetOriginalValues();
104      var meanModelEstimatedValues = GetMeanModelEstimatedValues(originalValues);
105      var meanModelResiduals = GetResiduals(originalValues, meanModelEstimatedValues);
106
107      meanModelResiduals.Sort();
108      chart.ChartAreas[0].AxisX.Maximum = Math.Ceiling(meanModelResiduals.Last());
109      chart.ChartAreas[0].CursorX.Interval = meanModelResiduals.First() / 100;
110
111      Series meanModelSeries = new Series("Mean Model");
112      meanModelSeries.ChartType = SeriesChartType.FastLine;
113      UpdateSeries(meanModelResiduals, meanModelSeries);
114      meanModelSeries.ToolTip = "Area over Curve: " + CalculateAreaOverCurve(meanModelSeries);
115      chart.Series.Add(meanModelSeries);
116
117      AddTimeSeriesPrognosisSolution(Content);
118    }
119
120    protected void AddTimeSeriesPrognosisSolution(ITimeSeriesPrognosisSolution solution) {
121      if (chart.Series.Any(s => s.Name == solution.Name)) return;
122
123      Series solutionSeries = new Series(solution.Name);
124      solutionSeries.Tag = solution;
125      solutionSeries.ChartType = SeriesChartType.FastLine;
126      var estimatedValues = GetResiduals(GetOriginalValues(), GetPrognosedValues(solution));
127      UpdateSeries(estimatedValues, solutionSeries);
128      solutionSeries.ToolTip = "Area over Curve: " + CalculateAreaOverCurve(solutionSeries);
129      chart.Series.Add(solutionSeries);
130    }
131
132    protected void UpdateSeries(List<double> residuals, Series series) {
133      series.Points.Clear();
134      residuals.Sort();
135
136      series.Points.AddXY(0, 0);
137      for (int i = 0; i < residuals.Count; i++) {
138        var point = new DataPoint();
139        if (residuals[i] > chart.ChartAreas[0].AxisX.Maximum) {
140          point.XValue = chart.ChartAreas[0].AxisX.Maximum;
141          point.YValues[0] = ((double)i) / residuals.Count;
142          point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
143          series.Points.Add(point);
144          break;
145        }
146
147        point.XValue = residuals[i];
148        point.YValues[0] = ((double)i + 1) / residuals.Count;
149        point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
150        series.Points.Add(point);
151      }
152
153      if (series.Points.Last().XValue < chart.ChartAreas[0].AxisX.Maximum) {
154        var point = new DataPoint();
155        point.XValue = chart.ChartAreas[0].AxisX.Maximum;
156        point.YValues[0] = 1;
157        point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
158        series.Points.Add(point);
159      }
160    }
161
162    protected IEnumerable<double> GetOriginalValues() {
163      IEnumerable<double> originalValues;
164      switch (cmbSamples.SelectedItem.ToString()) {
165        case TrainingSamples:
166          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
167          break;
168        case TestSamples:
169          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
170          break;
171        case AllSamples:
172          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable);
173          break;
174        default:
175          throw new NotSupportedException();
176      }
177      return originalValues;
178    }
179
180    protected IEnumerable<double> GetPrognosedValues(ITimeSeriesPrognosisSolution solution) {
181      IEnumerable<double> prognosedValues;
182      switch (cmbSamples.SelectedItem.ToString()) {
183        case TrainingSamples:
184          prognosedValues = solution.PrognosedTrainingValues;
185          break;
186        case TestSamples:
187          prognosedValues = solution.PrognosedTestValues;
188          break;
189        case AllSamples:
190          prognosedValues = solution.PrognosedValues;
191          break;
192        default:
193          throw new NotSupportedException();
194      }
195      return prognosedValues;
196    }
197
198    protected IEnumerable<double> GetMeanModelEstimatedValues(IEnumerable<double> originalValues) {
199      double averageTrainingTarget = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).Average();
200      return Enumerable.Repeat(averageTrainingTarget, originalValues.Count());
201    }
202
203    protected virtual List<double> GetResiduals(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues) {
204      return originalValues.Zip(estimatedValues, (x, y) => Math.Abs(x - y)).ToList();
205    }
206
207    private double CalculateAreaOverCurve(Series series) {
208      if (series.Points.Count < 1) throw new ArgumentException("Could not calculate area under curve if less than 1 data points were given.");
209
210      double auc = 0.0;
211      for (int i = 1; i < series.Points.Count; i++) {
212        double width = series.Points[i].XValue - series.Points[i - 1].XValue;
213        double y1 = 1 - series.Points[i - 1].YValues[0];
214        double y2 = 1 - series.Points[i].YValues[0];
215
216        auc += (y1 + y2) * width / 2;
217      }
218
219      return auc;
220    }
221
222    protected void cmbSamples_SelectedIndexChanged(object sender, EventArgs e) {
223      if (InvokeRequired) Invoke((Action<object, EventArgs>)cmbSamples_SelectedIndexChanged, sender, e);
224      else UpdateChart();
225    }
226  }
227}
Note: See TracBrowser for help on using the repository browser.