Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2937_SymReg_AnalyticalQuotient/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualHistogram.cs @ 16179

Last change on this file since 16179 was 15810, checked in by gkronber, 7 years ago

#2383: made some changes while reviewing

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Residual Histogram")]
33  [Content(typeof(IRegressionSolution))]
34  public partial class RegressionSolutionResidualHistogram : DataAnalysisSolutionEvaluationView {
35
36    #region variables
37    protected const string ALL_SAMPLES = "All samples";
38    protected const string TRAINING_SAMPLES = "Training samples";
39    protected const string TEST_SAMPLES = "Test samples";
40    /// <summary>
41    /// approximate amount of bins
42    /// </summary>
43    protected const double bins = 25;
44    #endregion
45
46    public new IRegressionSolution Content {
47      get { return (IRegressionSolution)base.Content; }
48      set { base.Content = value; }
49    }
50
51    public RegressionSolutionResidualHistogram()
52      : base() {
53      InitializeComponent();
54      foreach (string series in new List<String>() { ALL_SAMPLES, TRAINING_SAMPLES, TEST_SAMPLES }) {
55        chart.Series.Add(series);
56        chart.Series[series].LegendText = series;
57        chart.Series[series].ChartType = SeriesChartType.Column;
58        chart.Series[series]["PointWidth"] = "0.9";
59        chart.Series[series].BorderWidth = 1;
60        chart.Series[series].BorderDashStyle = ChartDashStyle.Solid;
61        chart.Series[series].BorderColor = Color.Black;
62        chart.Series[series].ToolTip = series + " Y = #VALY from #CUSTOMPROPERTY(from) to #CUSTOMPROPERTY(to)";
63      }
64      //configure axis
65      chart.CustomizeAllChartAreas();
66      chart.ChartAreas[0].AxisX.Title = "Residuals";
67      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
68      chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
69      chart.ChartAreas[0].CursorX.Interval = 1;
70      chart.ChartAreas[0].CursorY.Interval = 1;
71      chart.ChartAreas[0].AxisY.Title = "Relative Frequency";
72      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
73      chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
74      chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
75    }
76
77    private void RedrawChart() {
78      foreach (Series series in chart.Series) {
79        series.Points.Clear();
80      }
81      if (Content != null) {
82        List<double> residuals = CalculateResiduals(Content);
83
84        double max = 0.0;
85        foreach (Series series in chart.Series) {
86          CalculateFrequencies(residuals, series);
87          double seriesMax = series.Points.Select(p => p.YValues.First()).Max();
88          max = max < seriesMax ? seriesMax : max;
89        }
90
91        // ALL_SAMPLES has to be calculated to know its highest frequency, but it is not shown in the beginning
92        chart.Series.First(s => s.Name.Equals(ALL_SAMPLES)).Points.Clear();
93
94        double roundedMax, intervalWidth;
95        CalculateResidualParameters(residuals, out roundedMax, out intervalWidth);
96
97        ChartArea chartArea = chart.ChartAreas[0];
98        chartArea.AxisX.Title = string.Format("Residuals ({0})", Content.ProblemData.TargetVariable);
99        chartArea.AxisX.Minimum = -roundedMax - intervalWidth;
100        chartArea.AxisX.Maximum = roundedMax + intervalWidth;
101        // get the highest frequency of a residual of any series
102        chartArea.AxisY.Maximum = max;
103        if (chartArea.AxisY.Maximum < 0.1) {
104          chartArea.AxisY.Interval = 0.01;
105          chartArea.AxisY.Maximum = Math.Ceiling(chartArea.AxisY.Maximum * 100) / 100;
106        } else {
107          chartArea.AxisY.Interval = 0.1;
108          chartArea.AxisY.Maximum = Math.Ceiling(chartArea.AxisY.Maximum * 10) / 10;
109        }
110        chartArea.AxisX.Interval = intervalWidth;
111        int curBins = (int)Math.Round((roundedMax * 2) / intervalWidth);
112        //shifts the x axis label so that zero is in the middle
113        if (curBins % 2 == 0)
114          chartArea.AxisX.IntervalOffset = intervalWidth;
115        else
116          chartArea.AxisX.IntervalOffset = intervalWidth / 2;
117      }
118    }
119
120    private List<double> CalculateResiduals(IRegressionSolution solution) {
121      List<double> residuals = new List<double>();
122
123      IRegressionProblemData problemdata = solution.ProblemData;
124      List<double> targetValues = problemdata.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToList();
125      List<double> estimatedValues = solution.EstimatedValues.ToList();
126
127      for (int i = 0; i < solution.ProblemData.Dataset.Rows; i++) {
128        if (double.IsNaN(estimatedValues[i]) || double.IsInfinity(estimatedValues[i])) continue;
129        if (double.IsNaN(targetValues[i]) || double.IsInfinity(targetValues[i])) continue;
130        double residual = estimatedValues[i] - targetValues[i];
131        residuals.Add(residual);
132      }
133      return residuals;
134    }
135
136    private void CalculateFrequencies(List<double> residualValues, Series series) {
137      double roundedMax, intervalWidth;
138      CalculateResidualParameters(residualValues, out roundedMax, out intervalWidth);
139
140      IEnumerable<double> relevantResiduals = residualValues;
141      IRegressionProblemData problemdata = Content.ProblemData;
142      if (series.Name.Equals(TRAINING_SAMPLES)) {
143        relevantResiduals = residualValues.Skip(problemdata.TrainingPartition.Start).Take(problemdata.TrainingPartition.Size);
144      } else if (series.Name.Equals(TEST_SAMPLES)) {
145        relevantResiduals = residualValues.Skip(problemdata.TestPartition.Start).Take(problemdata.TestPartition.Size);
146      }
147
148      double intervalCenter = intervalWidth / 2.0;
149      double sampleCount = relevantResiduals.Count();
150      double current = -roundedMax;
151      DataPointCollection seriesPoints = series.Points;
152
153      for (int i = 0; i <= bins; i++) {
154        IEnumerable<double> help = relevantResiduals.Where(x => x >= (current - intervalCenter) && x < (current + intervalCenter));
155        seriesPoints.AddXY(current, help.Count() / sampleCount);
156        seriesPoints[seriesPoints.Count - 1]["from"] = (current - intervalCenter).ToString();
157        seriesPoints[seriesPoints.Count - 1]["to"] = (current + intervalCenter).ToString();
158        current += intervalWidth;
159      }
160    }
161
162    private void ToggleSeriesData(Series series) {
163      if (series.Points.Count > 0) {  //checks if series is shown
164        if (chart.Series.Any(s => s != series && s.Points.Count > 0)) {
165          series.Points.Clear();
166        }
167      } else if (Content != null) {
168        List<double> residuals = CalculateResiduals(Content);
169        CalculateFrequencies(residuals, series);
170        chart.Legends[series.Legend].ForeColor = Color.Black;
171        chart.Refresh();
172      }
173    }
174
175    private static void CalculateResidualParameters(List<double> residuals, out double roundedMax, out double intervalWidth) {
176      double realMax = Math.Max(Math.Abs(residuals.Min()), Math.Abs(residuals.Max()));
177      roundedMax = HumanRoundMax(realMax);
178      intervalWidth = (roundedMax * 2.0) / bins;
179      intervalWidth = HumanRoundMax(intervalWidth);
180      // sets roundedMax to a value, so that zero will be in the middle of the x axis
181      double help = realMax / intervalWidth;
182      help = help % 1 < 0.5 ? (int)help : (int)help + 1;
183      roundedMax = help * intervalWidth;
184    }
185
186    private static double HumanRoundMax(double max) {
187      double base10;
188      if (max > 0) base10 = Math.Pow(10.0, Math.Floor(Math.Log10(max)));
189      else base10 = Math.Pow(10.0, Math.Ceiling(Math.Log10(-max)));
190      double rounding = (max > 0) ? base10 : -base10;
191      while (rounding < max) rounding += base10;
192      return rounding;
193    }
194
195    #region events
196    protected override void RegisterContentEvents() {
197      base.RegisterContentEvents();
198      Content.ModelChanged += new EventHandler(Content_ModelChanged);
199      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
200    }
201    protected override void DeregisterContentEvents() {
202      base.DeregisterContentEvents();
203      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
204      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
205    }
206
207    protected override void OnContentChanged() {
208      base.OnContentChanged();
209      RedrawChart();
210    }
211    private void Content_ProblemDataChanged(object sender, EventArgs e) {
212      RedrawChart();
213    }
214    private void Content_ModelChanged(object sender, EventArgs e) {
215      RedrawChart();
216    }
217    private void chart_MouseDown(object sender, MouseEventArgs e) {
218      HitTestResult result = chart.HitTest(e.X, e.Y);
219      if (result.ChartElementType == ChartElementType.LegendItem) {
220        ToggleSeriesData(result.Series);
221      }
222    }
223    private void chart_MouseMove(object sender, MouseEventArgs e) {
224      HitTestResult result = chart.HitTest(e.X, e.Y);
225      if (result.ChartElementType == ChartElementType.LegendItem)
226        Cursor = Cursors.Hand;
227      else
228        Cursor = Cursors.Default;
229    }
230    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
231      if (chart.Series.Count != 3) return;
232      e.LegendItems[0].Cells[1].ForeColor = chart.Series[ALL_SAMPLES].Points.Count == 0 ? Color.Gray : Color.Black;
233      e.LegendItems[1].Cells[1].ForeColor = chart.Series[TRAINING_SAMPLES].Points.Count == 0 ? Color.Gray : Color.Black;
234      e.LegendItems[2].Cells[1].ForeColor = chart.Series[TEST_SAMPLES].Points.Count == 0 ? Color.Gray : Color.Black;
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.