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