Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12448 was 12365, checked in by mkommend, 10 years ago

#2345: Fixed x-axis maximum in error characteristics curve.

File size: 9.7 KB
RevLine 
[4417]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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;
[12365]50      chart.ChartAreas[0].AxisX.Maximum = 0.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();
[11093]101
[6642]102      if (Content == null) return;
[11093]103      if (cmbSamples.SelectedItem.ToString() == TrainingSamples && !ProblemData.TrainingIndices.Any()) return;
104      if (cmbSamples.SelectedItem.ToString() == TestSamples && !ProblemData.TestIndices.Any()) return;
[4417]105
[11093]106      if (Content.ProblemData.TrainingIndices.Any()) {
[11367]107        AddRegressionSolution(CreateConstantSolution());
[11093]108      }
[4417]109
[6642]110      AddRegressionSolution(Content);
111    }
[4417]112
[6642]113    protected void AddRegressionSolution(IRegressionSolution solution) {
114      if (chart.Series.Any(s => s.Name == solution.Name)) return;
[4417]115
[6642]116      Series solutionSeries = new Series(solution.Name);
117      solutionSeries.Tag = solution;
118      solutionSeries.ChartType = SeriesChartType.FastLine;
[11093]119      var residuals = GetResiduals(GetOriginalValues(), GetEstimatedValues(solution));
[12365]120
121
122      var maxValue = residuals.Max();
123      if (maxValue >= chart.ChartAreas[0].AxisX.Maximum) {
124        double scale = Math.Pow(10, Math.Floor(Math.Log10(maxValue)));
125        var maximum = scale * (1 + (int)(maxValue / scale));
126        chart.ChartAreas[0].AxisX.Maximum = maximum;
127      }
[11093]128      chart.ChartAreas[0].CursorX.Interval = residuals.Min() / 100;
129
130      UpdateSeries(residuals, solutionSeries);
131
[6642]132      solutionSeries.ToolTip = "Area over Curve: " + CalculateAreaOverCurve(solutionSeries);
[8105]133      solutionSeries.LegendToolTip = "Double-click to open model";
[6642]134      chart.Series.Add(solutionSeries);
135    }
[5417]136
[6642]137    protected void UpdateSeries(List<double> residuals, Series series) {
138      series.Points.Clear();
139      residuals.Sort();
[6982]140      if (!residuals.Any() || residuals.All(double.IsNaN)) return;
[4417]141
[6642]142      series.Points.AddXY(0, 0);
143      for (int i = 0; i < residuals.Count; i++) {
144        var point = new DataPoint();
145        if (residuals[i] > chart.ChartAreas[0].AxisX.Maximum) {
146          point.XValue = chart.ChartAreas[0].AxisX.Maximum;
[6750]147          point.YValues[0] = ((double)i) / residuals.Count;
[6642]148          point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
149          series.Points.Add(point);
150          break;
151        }
[4417]152
[6642]153        point.XValue = residuals[i];
[6982]154        point.YValues[0] = ((double)i + 1) / residuals.Count;
[6642]155        point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
156        series.Points.Add(point);
157      }
[4417]158
[6642]159      if (series.Points.Last().XValue < chart.ChartAreas[0].AxisX.Maximum) {
160        var point = new DataPoint();
161        point.XValue = chart.ChartAreas[0].AxisX.Maximum;
162        point.YValues[0] = 1;
163        point.ToolTip = "Error: " + point.XValue + "\n" + "Samples: " + point.YValues[0];
164        series.Points.Add(point);
165      }
166    }
[4417]167
[6642]168    protected IEnumerable<double> GetOriginalValues() {
169      IEnumerable<double> originalValues;
170      switch (cmbSamples.SelectedItem.ToString()) {
171        case TrainingSamples:
[8139]172          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
[6642]173          break;
174        case TestSamples:
[8139]175          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices);
[6642]176          break;
177        case AllSamples:
[6740]178          originalValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable);
[6642]179          break;
180        default:
181          throw new NotSupportedException();
182      }
183      return originalValues;
184    }
[4417]185
[6642]186    protected IEnumerable<double> GetEstimatedValues(IRegressionSolution solution) {
187      IEnumerable<double> estimatedValues;
188      switch (cmbSamples.SelectedItem.ToString()) {
189        case TrainingSamples:
190          estimatedValues = solution.EstimatedTrainingValues;
191          break;
192        case TestSamples:
193          estimatedValues = solution.EstimatedTestValues;
194          break;
195        case AllSamples:
196          estimatedValues = solution.EstimatedValues;
197          break;
198        default:
199          throw new NotSupportedException();
[4417]200      }
[6642]201      return estimatedValues;
[4417]202    }
203
[6642]204    protected virtual List<double> GetResiduals(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues) {
205      return originalValues.Zip(estimatedValues, (x, y) => Math.Abs(x - y)).ToList();
[4417]206    }
207
[6642]208    private double CalculateAreaOverCurve(Series series) {
[6982]209      if (series.Points.Count < 1) return 0;
[4417]210
211      double auc = 0.0;
212      for (int i = 1; i < series.Points.Count; i++) {
213        double width = series.Points[i].XValue - series.Points[i - 1].XValue;
[6642]214        double y1 = 1 - series.Points[i - 1].YValues[0];
215        double y2 = 1 - series.Points[i].YValues[0];
[4417]216
217        auc += (y1 + y2) * width / 2;
218      }
219
220      return auc;
221    }
222
[6642]223    protected void cmbSamples_SelectedIndexChanged(object sender, EventArgs e) {
224      if (InvokeRequired) Invoke((Action<object, EventArgs>)cmbSamples_SelectedIndexChanged, sender, e);
225      else UpdateChart();
[4417]226    }
[7043]227
[7701]228    #region Baseline
[7700]229    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
[7043]230      HitTestResult result = chart.HitTest(e.X, e.Y);
231      if (result.ChartElementType != ChartElementType.LegendItem) return;
232
233      MainFormManager.MainForm.ShowContent((IRegressionSolution)result.Series.Tag);
234    }
235
[11367]236    private ConstantRegressionSolution CreateConstantSolution() {
[8139]237      double averageTrainingTarget = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average();
[8963]238      var model = new ConstantRegressionModel(averageTrainingTarget);
[11093]239      var solution = new ConstantRegressionSolution(model, (IRegressionProblemData)ProblemData.Clone());
[7700]240      solution.Name = "Baseline";
[7043]241      return solution;
242    }
[7701]243    #endregion
[7700]244
[7701]245    private void chart_MouseMove(object sender, MouseEventArgs e) {
246      HitTestResult result = chart.HitTest(e.X, e.Y);
[8102]247      if (result.ChartElementType == ChartElementType.LegendItem) {
[7701]248        Cursor = Cursors.Hand;
[8102]249      } else {
[7701]250        Cursor = Cursors.Default;
[8102]251      }
[7700]252    }
[4417]253  }
254}
Note: See TracBrowser for help on using the repository browser.