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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis.Views.Regression {
|
---|
29 | [View("Residual Histogram")]
|
---|
30 | [Content(typeof(IRegressionSolution))]
|
---|
31 | public partial class RegressionSolutionResidualHistogram : DataAnalysisSolutionEvaluationView {
|
---|
32 | private const string ALL_SERIES = "All samples";
|
---|
33 | private const string TRAINING_SERIES = "Training samples";
|
---|
34 | private const string TEST_SERIES = "Test samples";
|
---|
35 |
|
---|
36 | // for test purposes
|
---|
37 | private const int bins = 9;
|
---|
38 |
|
---|
39 | public new IRegressionSolution Content {
|
---|
40 | get { return (IRegressionSolution)base.Content; }
|
---|
41 | set { base.Content = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public RegressionSolutionResidualHistogram() {
|
---|
45 | InitializeComponent();
|
---|
46 |
|
---|
47 | this.chart.Series.Add(ALL_SERIES);
|
---|
48 | this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
|
---|
49 | this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.Column;
|
---|
50 |
|
---|
51 | //this.chart.Series.Add(TRAINING_SERIES);
|
---|
52 | //this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
|
---|
53 | //this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
54 | //this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
|
---|
55 |
|
---|
56 | //this.chart.Series.Add(TEST_SERIES);
|
---|
57 | //this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
|
---|
58 | //this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
59 |
|
---|
60 | //configure axis
|
---|
61 | this.chart.CustomizeAllChartAreas();
|
---|
62 | this.chart.ChartAreas[0].AxisX.Title = "Residuals";
|
---|
63 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
64 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
65 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
66 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
67 |
|
---|
68 | this.chart.ChartAreas[0].AxisY.Title = "Relative Frequency";
|
---|
69 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
70 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
71 | this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void RedrawChart() {
|
---|
75 | this.chart.Series[ALL_SERIES].Points.Clear();
|
---|
76 | if (Content != null) {
|
---|
77 | List<double> residuals = new List<double>();
|
---|
78 | IEnumerable<double> targetValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable);
|
---|
79 | IEnumerable<double> estimatedValues = Content.EstimatedValues;
|
---|
80 | for (int i = 0; i < Content.ProblemData.Dataset.Rows; i++) {
|
---|
81 | residuals.Add(estimatedValues.ElementAt(i) - targetValues.ElementAt(i));
|
---|
82 | }
|
---|
83 |
|
---|
84 | //no check if both are negative
|
---|
85 | //assumption that all values are around zero
|
---|
86 | double max = Math.Ceiling(Math.Max(Math.Abs(residuals.Min()), Math.Abs(residuals.Max())));
|
---|
87 | double intervalWidth = (max * 2) / bins;
|
---|
88 | double intervalCenter = intervalWidth / 2.0;
|
---|
89 | double current = -max;
|
---|
90 |
|
---|
91 | Series histogramSeries = this.chart.Series[ALL_SERIES];
|
---|
92 |
|
---|
93 | Dictionary<double, int> frequencies = new Dictionary<double, int>();
|
---|
94 |
|
---|
95 | for (int i = 0; i <= bins; i++) {
|
---|
96 | IEnumerable<double> help = residuals.Where(x => x >= (current - intervalCenter) && x < (current + intervalCenter));
|
---|
97 | frequencies.Add(current, help.Count());
|
---|
98 | current += intervalWidth;
|
---|
99 | }
|
---|
100 |
|
---|
101 | foreach (var item in frequencies) {
|
---|
102 | histogramSeries.Points.AddXY(item.Key, item.Value);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | #region events
|
---|
108 | protected override void RegisterContentEvents() {
|
---|
109 | base.RegisterContentEvents();
|
---|
110 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
111 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
112 | }
|
---|
113 | protected override void DeregisterContentEvents() {
|
---|
114 | base.DeregisterContentEvents();
|
---|
115 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
116 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void OnContentChanged() {
|
---|
120 | base.OnContentChanged();
|
---|
121 | RedrawChart();
|
---|
122 | }
|
---|
123 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
124 | RedrawChart();
|
---|
125 | }
|
---|
126 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
127 | RedrawChart();
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 | }
|
---|
131 | }
|
---|