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