[14889] | 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;
|
---|
[15024] | 22 | using System.Collections.Generic;
|
---|
[14889] | 23 | using System.Drawing;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
| 30 | [View("Residual Analysis")]
|
---|
| 31 | [Content(typeof(IRegressionSolution))]
|
---|
[14943] | 32 | public sealed partial class RegressionSolutionResidualAnalysisView : DataAnalysisSolutionEvaluationView {
|
---|
[14889] | 33 |
|
---|
[14943] | 34 | // names should be relatively save to prevent collisions with variable names in the dataset
|
---|
[15088] | 35 | private const string TargetLabel = "> Target";
|
---|
[14943] | 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 |
|
---|
[14889] | 43 | public new IRegressionSolution Content {
|
---|
| 44 | get { return (IRegressionSolution)base.Content; }
|
---|
[14943] | 45 | set { base.Content = value; }
|
---|
[14889] | 46 | }
|
---|
| 47 |
|
---|
[14943] | 48 | public RegressionSolutionResidualAnalysisView() : base() {
|
---|
[14889] | 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 |
|
---|
[14943] | 65 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
[14889] | 66 | OnContentChanged();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[14943] | 69 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[14889] | 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();
|
---|
[15024] | 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();
|
---|
[15094] | 93 | var dateTimeVars = ds.DateTimeVariables.Where(vn => ds.GetDateTimeValues(vn).Distinct().Skip(1).Any()).ToArray();
|
---|
[15024] | 94 |
|
---|
[14889] | 95 | // produce training and test values separately as they might overlap (e.g. for ensembles)
|
---|
| 96 | var predictedValuesTrain = Content.EstimatedTrainingValues.ToArray();
|
---|
| 97 | int j = 0; // idx for predictedValues array
|
---|
| 98 | foreach (var i in problemData.TrainingIndices) {
|
---|
[15094] | 99 | var run = CreateRunForIdx(i, problemData, doubleVars, stringVars, dateTimeVars);
|
---|
[14889] | 100 | var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i);
|
---|
| 101 | AddErrors(run, predictedValuesTrain[j++], targetValue);
|
---|
[14943] | 102 | run.Results.Add(PartitionLabel, new StringValue("Training"));
|
---|
[14889] | 103 | run.Color = Color.Gold;
|
---|
| 104 | runs.Add(run);
|
---|
| 105 | }
|
---|
| 106 | var predictedValuesTest = Content.EstimatedTestValues.ToArray();
|
---|
| 107 | j = 0;
|
---|
| 108 | foreach (var i in problemData.TestIndices) {
|
---|
[15094] | 109 | var run = CreateRunForIdx(i, problemData, doubleVars, stringVars, dateTimeVars);
|
---|
[14889] | 110 | var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i);
|
---|
| 111 | AddErrors(run, predictedValuesTest[j++], targetValue);
|
---|
[14943] | 112 | run.Results.Add(PartitionLabel, new StringValue("Test"));
|
---|
[14889] | 113 | run.Color = Color.Red;
|
---|
| 114 | runs.Add(run);
|
---|
| 115 | }
|
---|
| 116 | if (string.IsNullOrEmpty(selectedXAxis))
|
---|
| 117 | selectedXAxis = "Index";
|
---|
| 118 | if (string.IsNullOrEmpty(selectedYAxis))
|
---|
| 119 | selectedYAxis = "Residual";
|
---|
| 120 |
|
---|
| 121 | bubbleChartView.Content = runs;
|
---|
| 122 | bubbleChartView.SelectedXAxis = selectedXAxis;
|
---|
| 123 | bubbleChartView.SelectedYAxis = selectedYAxis;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | private void AddErrors(IRun run, double pred, double target) {
|
---|
| 127 | var residual = target - pred;
|
---|
| 128 | var relError = residual / target;
|
---|
[15088] | 129 | run.Results.Add(TargetLabel, new DoubleValue(target));
|
---|
[14943] | 130 | run.Results.Add(PredictionLabel, new DoubleValue(pred));
|
---|
| 131 | run.Results.Add(ResidualLabel, new DoubleValue(residual));
|
---|
| 132 | run.Results.Add(AbsResidualLabel, new DoubleValue(Math.Abs(residual)));
|
---|
| 133 | run.Results.Add(RelativeErrorLabel, new DoubleValue(relError));
|
---|
| 134 | run.Results.Add(AbsRelativeErrorLabel, new DoubleValue(Math.Abs(relError)));
|
---|
[14889] | 135 | }
|
---|
| 136 |
|
---|
[15094] | 137 | private IRun CreateRunForIdx(int i, IRegressionProblemData problemData, IEnumerable<string> doubleVars, IEnumerable<string> stringVars, IEnumerable<string> dateTimeVars) {
|
---|
[14889] | 138 | var ds = problemData.Dataset;
|
---|
| 139 | var run = new Run();
|
---|
[15024] | 140 | foreach (var variableName in doubleVars) {
|
---|
[14889] | 141 | run.Results.Add(variableName, new DoubleValue(ds.GetDoubleValue(variableName, i)));
|
---|
| 142 | }
|
---|
[15024] | 143 | foreach (var variableName in stringVars) {
|
---|
[14889] | 144 | run.Results.Add(variableName, new StringValue(ds.GetStringValue(variableName, i)));
|
---|
| 145 | }
|
---|
[15094] | 146 | foreach (var variableName in dateTimeVars) {
|
---|
| 147 | run.Results.Add(variableName, new DateTimeValue(ds.GetDateTimeValue(variableName, i)));
|
---|
| 148 | }
|
---|
[14889] | 149 | return run;
|
---|
| 150 | }
|
---|
| 151 | #endregion
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 | }
|
---|