Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualAnalysisView.cs @ 15024

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

#2779: hide 'constant variables' (only one distinct value)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
21using System;
22using System.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Residual Analysis")]
32  [Content(typeof(IRegressionSolution))]
33  public sealed partial class RegressionSolutionResidualAnalysisView : DataAnalysisSolutionEvaluationView {
34
35    // names should be relatively save to prevent collisions with variable names in the dataset
36    private const string PredictionLabel = "> Prediction";
37    private const string ResidualLabel = "> Residual";
38    private const string AbsResidualLabel = "> Residual (abs.)";
39    private const string RelativeErrorLabel = "> Relative Error";
40    private const string AbsRelativeErrorLabel = "> Relative Error (abs.)";
41    private const string PartitionLabel = "> Partition";
42
43    public new IRegressionSolution Content {
44      get { return (IRegressionSolution)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public RegressionSolutionResidualAnalysisView() : base() {
49      InitializeComponent();
50    }
51
52    #region events
53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      Content.ModelChanged += new EventHandler(Content_ModelChanged);
56      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
57    }
58
59    protected override void DeregisterContentEvents() {
60      base.DeregisterContentEvents();
61      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
62      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
63    }
64
65    private void Content_ProblemDataChanged(object sender, EventArgs e) {
66      OnContentChanged();
67    }
68
69    private void Content_ModelChanged(object sender, EventArgs e) {
70      OnContentChanged();
71    }
72
73    protected override void OnContentChanged() {
74      base.OnContentChanged();
75      if (Content == null) {
76        bubbleChartView.Content = null;
77      } else {
78        UpdateBubbleChart();
79      }
80    }
81
82    private void UpdateBubbleChart() {
83      if (Content == null) return;
84      var selectedXAxis = bubbleChartView.SelectedXAxis;
85      var selectedYAxis = bubbleChartView.SelectedYAxis;
86
87      var problemData = Content.ProblemData;
88      var ds = problemData.Dataset;
89      var runs = new RunCollection();
90      // determine relevant variables (at least two different values)
91      var doubleVars = ds.DoubleVariables.Where(vn => ds.GetDoubleValues(vn).Max() > ds.GetDoubleValues(vn).Min()).ToArray();
92      var stringVars = ds.StringVariables.Where(vn => ds.GetStringValues(vn).Distinct().Skip(1).Any()).ToArray();
93
94      // produce training and test values separately as they might overlap (e.g. for ensembles)
95      var predictedValuesTrain = Content.EstimatedTrainingValues.ToArray();
96      int j = 0; // idx for predictedValues array
97      foreach (var i in problemData.TrainingIndices) {
98        var run = CreateRunForIdx(i, problemData, doubleVars, stringVars);
99        var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i);
100        AddErrors(run, predictedValuesTrain[j++], targetValue);
101        run.Results.Add(PartitionLabel, new StringValue("Training"));
102        run.Color = Color.Gold;
103        runs.Add(run);
104      }
105      var predictedValuesTest = Content.EstimatedTestValues.ToArray();
106      j = 0;
107      foreach (var i in problemData.TestIndices) {
108        var run = CreateRunForIdx(i, problemData, doubleVars, stringVars);
109        var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i);
110        AddErrors(run, predictedValuesTest[j++], targetValue);
111        run.Results.Add(PartitionLabel, new StringValue("Test"));
112        run.Color = Color.Red;
113        runs.Add(run);
114      }
115      if (string.IsNullOrEmpty(selectedXAxis))
116        selectedXAxis = "Index";
117      if (string.IsNullOrEmpty(selectedYAxis))
118        selectedYAxis = "Residual";
119
120      bubbleChartView.Content = runs;
121      bubbleChartView.SelectedXAxis = selectedXAxis;
122      bubbleChartView.SelectedYAxis = selectedYAxis;
123    }
124
125    private void AddErrors(IRun run, double pred, double target) {
126      var residual = target - pred;
127      var relError = residual / target;
128      run.Results.Add(PredictionLabel, new DoubleValue(pred));
129      run.Results.Add(ResidualLabel, new DoubleValue(residual));
130      run.Results.Add(AbsResidualLabel, new DoubleValue(Math.Abs(residual)));
131      run.Results.Add(RelativeErrorLabel, new DoubleValue(relError));
132      run.Results.Add(AbsRelativeErrorLabel, new DoubleValue(Math.Abs(relError)));
133    }
134
135    private IRun CreateRunForIdx(int i, IRegressionProblemData problemData, IEnumerable<string> doubleVars, IEnumerable<string> stringVars) {
136      var ds = problemData.Dataset;
137      var run = new Run();
138      foreach (var variableName in doubleVars) {
139        run.Results.Add(variableName, new DoubleValue(ds.GetDoubleValue(variableName, i)));
140      }
141      foreach (var variableName in stringVars) {
142        run.Results.Add(variableName, new StringValue(ds.GetStringValue(variableName, i)));
143      }
144      return run;
145    }
146    #endregion
147
148  }
149}
Note: See TracBrowser for help on using the repository browser.