Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2157 changed label for y-axis in error characteristic curve 'Number of Samples' -> 'Ratio of Residuals'

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