Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionErrorCharacteristicsCurveView.cs @ 12547

Last change on this file since 12547 was 12493, checked in by gkronber, 10 years ago

#2369: added support for squared errors and relative errors to error-characteristic-curve view

File size: 10.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Common;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Error Characteristics Curve")]
32  [Content(typeof(IRegressionSolution))]
33  public partial class RegressionSolutionErrorCharacteristicsCurveView : DataAnalysisSolutionEvaluationView {
34    protected const string TrainingSamples = "Training";
35    protected const string TestSamples = "Test";
36    protected const string AllSamples = "All Samples";
37
38    public RegressionSolutionErrorCharacteristicsCurveView()
39      : base() {
40      InitializeComponent();
41
42      cmbSamples.Items.Add(TrainingSamples);
43      cmbSamples.Items.Add(TestSamples);
44      cmbSamples.Items.Add(AllSamples);
45
46      cmbSamples.SelectedIndex = 0;
47
48      residualComboBox.SelectedIndex = 0;
49
50      chart.CustomizeAllChartAreas();
51      chart.ChartAreas[0].AxisX.Title = residualComboBox.SelectedItem.ToString();
52      chart.ChartAreas[0].AxisX.Minimum = 0.0;
53      chart.ChartAreas[0].AxisX.Maximum = 0.0;
54      chart.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
55      chart.ChartAreas[0].CursorX.Interval = 0.01;
56
57      chart.ChartAreas[0].AxisY.Title = "Ratio of Residuals";
58      chart.ChartAreas[0].AxisY.Minimum = 0.0;
59      chart.ChartAreas[0].AxisY.Maximum = 1.0;
60      chart.ChartAreas[0].AxisY.MajorGrid.Interval = 0.2;
61      chart.ChartAreas[0].CursorY.Interval = 0.01;
62    }
63
64    public new IRegressionSolution Content {
65      get { return (IRegressionSolution)base.Content; }
66      set { base.Content = value; }
67    }
68    public IRegressionProblemData ProblemData {
69      get {
70        if (Content == null) return null;
71        return Content.ProblemData;
72      }
73    }
74
75    protected override void RegisterContentEvents() {
76      base.RegisterContentEvents();
77      Content.ModelChanged += new EventHandler(Content_ModelChanged);
78      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
79    }
80    protected override void DeregisterContentEvents() {
81      base.DeregisterContentEvents();
82      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
83      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
84    }
85
86    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
87      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ModelChanged, sender, e);
88      else UpdateChart();
89    }
90    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
91      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ProblemDataChanged, sender, e);
92      else {
93        UpdateChart();
94      }
95    }
96    protected override void OnContentChanged() {
97      base.OnContentChanged();
98      UpdateChart();
99    }
100
101    protected virtual void UpdateChart() {
102      chart.Series.Clear();
103      chart.Annotations.Clear();
104
105      if (Content == null) return;
106      if (cmbSamples.SelectedItem.ToString() == TrainingSamples && !ProblemData.TrainingIndices.Any()) return;
107      if (cmbSamples.SelectedItem.ToString() == TestSamples && !ProblemData.TestIndices.Any()) return;
108
109      if (Content.ProblemData.TrainingIndices.Any()) {
110        AddRegressionSolution(CreateConstantSolution());
111      }
112
113      AddRegressionSolution(Content);
114
115      chart.ChartAreas[0].AxisX.Title = residualComboBox.SelectedItem.ToString();
116    }
117
118    protected void AddRegressionSolution(IRegressionSolution solution) {
119      if (chart.Series.Any(s => s.Name == solution.Name)) return;
120
121      Series solutionSeries = new Series(solution.Name);
122      solutionSeries.Tag = solution;
123      solutionSeries.ChartType = SeriesChartType.FastLine;
124      var residuals = GetResiduals(GetOriginalValues(), GetEstimatedValues(solution));
125
126      var maxValue = residuals.Max();
127      double scale = Math.Pow(10, Math.Floor(Math.Log10(maxValue)));
128      var maximum = scale * (1 + (int)(maxValue / scale));
129      chart.ChartAreas[0].AxisX.Maximum = maximum;
130      chart.ChartAreas[0].CursorX.Interval = residuals.Min() / 100;
131
132      UpdateSeries(residuals, solutionSeries);
133
134      solutionSeries.ToolTip = "Area over Curve: " + CalculateAreaOverCurve(solutionSeries);
135      solutionSeries.LegendToolTip = "Double-click to open model";
136      chart.Series.Add(solutionSeries);
137    }
138
139    protected void UpdateSeries(List<double> residuals, Series series) {
140      series.Points.Clear();
141      residuals.Sort();
142      if (!residuals.Any() || residuals.All(double.IsNaN)) return;
143
144      series.Points.AddXY(0, 0);
145      for (int i = 0; i < residuals.Count; i++) {
146        var point = new DataPoint();
147        if (residuals[i] > chart.ChartAreas[0].AxisX.Maximum) {
148          point.XValue = chart.ChartAreas[0].AxisX.Maximum;
149          point.YValues[0] = ((double)i) / residuals.Count;
150          point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
151          series.Points.Add(point);
152          break;
153        }
154
155        point.XValue = residuals[i];
156        point.YValues[0] = ((double)i + 1) / residuals.Count;
157        point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
158        series.Points.Add(point);
159      }
160
161      if (series.Points.Last().XValue < chart.ChartAreas[0].AxisX.Maximum) {
162        var point = new DataPoint();
163        point.XValue = chart.ChartAreas[0].AxisX.Maximum;
164        point.YValues[0] = 1;
165        point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
166        series.Points.Add(point);
167      }
168    }
169
170    protected IEnumerable<double> GetOriginalValues() {
171      IEnumerable<double> originalValues;
172      switch (cmbSamples.SelectedItem.ToString()) {
173        case TrainingSamples:
174          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
175          break;
176        case TestSamples:
177          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
178          break;
179        case AllSamples:
180          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable);
181          break;
182        default:
183          throw new NotSupportedException();
184      }
185      return originalValues;
186    }
187
188    protected IEnumerable<double> GetEstimatedValues(IRegressionSolution solution) {
189      IEnumerable<double> estimatedValues;
190      switch (cmbSamples.SelectedItem.ToString()) {
191        case TrainingSamples:
192          estimatedValues = solution.EstimatedTrainingValues;
193          break;
194        case TestSamples:
195          estimatedValues = solution.EstimatedTestValues;
196          break;
197        case AllSamples:
198          estimatedValues = solution.EstimatedValues;
199          break;
200        default:
201          throw new NotSupportedException();
202      }
203      return estimatedValues;
204    }
205
206    protected virtual List<double> GetResiduals(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues) {
207      switch (residualComboBox.SelectedItem.ToString()) {
208        case "Absolute error": return originalValues.Zip(estimatedValues, (x, y) => Math.Abs(x - y)).ToList();
209        case "Squared error": return originalValues.Zip(estimatedValues, (x, y) => (x - y) * (x - y)).ToList();
210        case "Relative error": return originalValues.Zip(estimatedValues, (x, y) => x.IsAlmost(0.0) ? -1 : Math.Abs((x - y) / x))
211          .Where(x => x > 0) // remove entries where the original value is 0
212          .ToList();
213      }
214      // should never happen
215      return new List<double>();
216    }
217
218    private double CalculateAreaOverCurve(Series series) {
219      if (series.Points.Count < 1) return 0;
220
221      double auc = 0.0;
222      for (int i = 1; i < series.Points.Count; i++) {
223        double width = series.Points[i].XValue - series.Points[i - 1].XValue;
224        double y1 = 1 - series.Points[i - 1].YValues[0];
225        double y2 = 1 - series.Points[i].YValues[0];
226
227        auc += (y1 + y2) * width / 2;
228      }
229
230      return auc;
231    }
232
233    protected void cmbSamples_SelectedIndexChanged(object sender, EventArgs e) {
234      if (InvokeRequired) Invoke((Action<object, EventArgs>)cmbSamples_SelectedIndexChanged, sender, e);
235      else UpdateChart();
236    }
237
238    #region Baseline
239    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
240      HitTestResult result = chart.HitTest(e.X, e.Y);
241      if (result.ChartElementType != ChartElementType.LegendItem) return;
242
243      MainFormManager.MainForm.ShowContent((IRegressionSolution)result.Series.Tag);
244    }
245
246    private ConstantRegressionSolution CreateConstantSolution() {
247      double averageTrainingTarget = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average();
248      var model = new ConstantRegressionModel(averageTrainingTarget);
249      var solution = new ConstantRegressionSolution(model, (IRegressionProblemData)ProblemData.Clone());
250      solution.Name = "Baseline";
251      return solution;
252    }
253    #endregion
254
255    private void chart_MouseMove(object sender, MouseEventArgs e) {
256      HitTestResult result = chart.HitTest(e.X, e.Y);
257      if (result.ChartElementType == ChartElementType.LegendItem) {
258        Cursor = Cursors.Hand;
259      } else {
260        Cursor = Cursors.Default;
261      }
262    }
263
264    private void residualComboBox_SelectedIndexChanged(object sender, EventArgs e) {
265      UpdateChart();
266    }
267  }
268}
Note: See TracBrowser for help on using the repository browser.