Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionVariableImpactView.cs @ 4124

Last change on this file since 4124 was 4124, checked in by gkronber, 14 years ago

Improved variable impact view. #1011

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System.Collections.Generic;
23using System.Linq;
24using System.Windows.Forms;
25using alglib;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Optimization;
31using System;
32
33namespace HeuristicLab.Problems.DataAnalysis.Views {
34  [Content(typeof(RunCollection), false)]
35  [View("RunCollection Variable Impact View")]
36  public partial class RunCollectionVariableImpactView : AsynchronousContentView {
37    private const string variableImpactResultName = "Integrated variable frequencies";
38    public RunCollectionVariableImpactView() {
39      InitializeComponent();
40    }
41
42    public new RunCollection Content {
43      get { return (RunCollection)base.Content; }
44      set { base.Content = value; }
45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
49      this.Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
50      this.Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
51      this.Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
52    }
53    protected override void DeregisterContentEvents() {
54      base.RegisterContentEvents();
55      this.Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
56      this.Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
57      this.Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
58    }
59
60    protected override void OnContentChanged() {
61      base.OnContentChanged();
62      this.UpdateData();
63    }
64    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
65      this.UpdateData();
66    }
67    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
68      this.UpdateData();
69    }
70    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
71      this.UpdateData();
72    }
73
74    private void UpdateData() {
75      matrixView.Content = CalculateVariableImpactMatrix();
76    }
77
78    private DoubleMatrix CalculateVariableImpactMatrix() {
79      DoubleMatrix matrix = null;
80      if (Content != null) {
81        List<IRun> runsWithVariables = Content.Where(r => r.Results.ContainsKey(variableImpactResultName)).ToList();
82        IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runsWithVariables
83                                                        select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
84        IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
85                                             from variableName in variableImpact.RowNames
86                                             select variableName)
87                                            .Distinct();
88        // filter variableNames: only include names that have at least one non-zero value in a run
89        List<string> variableNamesList = (from variableName in variableNames
90                                          where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
91                                          select variableName)
92                                         .ToList();
93
94        List<string> statictics = new List<string> { "Median Rank", "pValue", "Mean", "StdDev", };
95        List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
96        columnNames.AddRange(statictics);
97        int runs = runsWithVariables.Count();
98
99        matrix = new DoubleMatrix(variableNamesList.Count, runs + statictics.Count);
100        matrix.SortableView = true;
101        matrix.RowNames = variableNamesList;
102        matrix.ColumnNames = columnNames;
103
104        for (int i = 0; i < runsWithVariables.Count; i++) {
105          IRun run = runsWithVariables[i];
106          DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
107          for (int j = 0; j < runVariableImpacts.Rows; j++) {
108            int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
109            if (rowIndex > -1) {
110              matrix[rowIndex, i] = runVariableImpacts[j, 0];
111            }
112          }
113        }
114
115        List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
116                                                      select GetVariableImpacts(variableName, allVariableImpacts).ToList())
117                                                     .ToList();
118        List<List<double>> variableRanks = (from variableName in variableNamesList
119                                            select GetVariableImpactRanks(variableName, allVariableImpacts).ToList())
120                                        .ToList();
121        if (variableImpactsOverRuns.Count() > 0) {
122          // reference median is the worst median rank
123          double referenceMedian = (from impacts in variableRanks
124                                    let med = impacts.Median()
125                                    orderby med
126                                    select med)
127                                           .Last();
128          // for all variables
129          for (int row = 0; row < variableImpactsOverRuns.Count; row++) {
130            matrix[row, runs] = variableRanks[row].Median();
131
132            // check if the median of the ranks is significantly different to the reference median rank
133            double leftTail = 0; double rightTail = 0; double bothTails = 0;
134            double[] ranksArray = variableRanks[row].ToArray();
135
136            // wilcoxon signed rank test is used because the ranks of two variables in a single run are not independent
137            alglib.wsr.wilcoxonsignedranktest(ranksArray, ranksArray.Length, referenceMedian, ref bothTails, ref leftTail, ref rightTail);
138            matrix[row, runs + 1] = bothTails;
139
140            // also show mean and std.dev. of relative variable impacts to indicate the relative difference in impacts of variables
141            matrix[row, runs + 2] = variableImpactsOverRuns[row].Average();
142            matrix[row, runs + 3] = variableImpactsOverRuns[row].StandardDeviation();
143
144          }
145        }
146      }
147      return matrix;
148    }
149
150    private IEnumerable<double> GetVariableImpactRanks(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts) {
151      foreach (DoubleMatrix runVariableImpacts in allVariableImpacts) {
152        // certainly not yet very efficient because ranks are computed multiple times for the same run
153        string[] variableNames = runVariableImpacts.RowNames.ToArray();
154        double[] values = (from row in Enumerable.Range(0, runVariableImpacts.Rows)
155                           select runVariableImpacts[row, 0] * -1)
156                          .ToArray();
157        Array.Sort(values, variableNames);
158        // calculate ranks
159        double[] ranks = new double[values.Length];
160        // check for tied ranks
161        int i = 0;
162        while (i < values.Length) {
163          ranks[i] = i + 1;
164          int j = i + 1;
165          while (j < values.Length && values[i].IsAlmost(values[j])) {
166            ranks[j] = ranks[i];
167            j++;
168          }
169          i = j;
170        }
171        int rankIndex = 0;
172        foreach (string rowVariableName in variableNames) {
173          if (rowVariableName == variableName)
174            yield return ranks[rankIndex];
175          rankIndex++;
176        }
177      }
178    }
179
180    private IEnumerable<double> GetVariableImpacts(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts) {
181      foreach (DoubleMatrix runVariableImpacts in allVariableImpacts) {
182        int row = 0;
183        foreach (string rowName in runVariableImpacts.RowNames) {
184          if (rowName == variableName)
185            yield return runVariableImpacts[row, 0];
186          row++;
187        }
188      }
189    }
190
191  }
192}
Note: See TracBrowser for help on using the repository browser.