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

Last change on this file since 3975 was 3975, checked in by mkommend, 10 years ago

added p-values for variable impacts (ticket #1011)

File size: 6.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;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Core;
33using HeuristicLab.Common;
34using HeuristicLab.Data;
35using alglib;
36
37namespace HeuristicLab.Optimization.Views {
38  [Content(typeof(RunCollection), false)]
39  [View("RunCollection Variable Impact View")]
40  public partial class RunCollectionVariableImpactView : AsynchronousContentView {
41    private const string variableImpactResultName = "Integrated variable frequencies";
42    public RunCollectionVariableImpactView() {
43      InitializeComponent();
44    }
45
46    public new RunCollection Content {
47      get { return (RunCollection)base.Content; }
48      set { base.Content = value; }
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      this.Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
54      this.Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
55      this.Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
56    }
57    protected override void DeregisterContentEvents() {
58      base.RegisterContentEvents();
59      this.Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
60      this.Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
61      this.Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      this.UpdateData();
67    }
68    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
69      this.UpdateData();
70    }
71    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
72      this.UpdateData();
73    }
74    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
75      this.UpdateData();
76    }
77
78    private void UpdateData() {
79      matrixView.Content = CalculateVariableImpactMatrix();
80    }
81
82    private DoubleMatrix CalculateVariableImpactMatrix() {
83      DoubleMatrix matrix = null;
84      if (Content != null) {
85        IEnumerable<IRun> runsWithVariables = Content.Where(r => r.Results.ContainsKey(variableImpactResultName));
86        IEnumerable<DoubleMatrix> variableImpacts = (from run in runsWithVariables
87                                                     select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
88        List<string> variableNames = (from varImpact in variableImpacts
89                                      from variableName in varImpact.RowNames
90                                      select variableName).Distinct().ToList();
91        List<string> statictics = new List<string> { "Mean", "Median", "StdDev", "Significance Mean>0", "Significance Median>0" };
92        List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
93        columnNames.AddRange(statictics);
94        int runs = runsWithVariables.Count();
95
96        matrix = new DoubleMatrix(variableNames.Count, runs + statictics.Count);
97        matrix.SortableView = true;
98        matrix.RowNames = variableNames;
99        matrix.ColumnNames = columnNames;
100
101        foreach (IRun run in runsWithVariables) {
102          DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
103          for (int i = 0; i < runVariableImpacts.Rows; i++) {
104            int rowIndex = variableNames.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(i));
105            int columnIndex = columnNames.FindIndex(s => s == run.Name);
106            matrix[rowIndex, columnIndex] = runVariableImpacts[i, 0];
107          }
108        }
109
110        for (int variableIndex = 0; variableIndex < matrix.Rows; variableIndex++) {
111          List<double> impacts = new List<double>();
112          for (int runIndex = 0; runIndex < runs; runIndex++)
113            impacts.Add(matrix[variableIndex, runIndex]);
114          matrix[variableIndex, runs] = impacts.Average();
115          matrix[variableIndex, runs + 1] = impacts.Median();
116          matrix[variableIndex, runs + 2] = impacts.StandardDeviation();
117          double leftTail = 0; double rightTail = 0; double bothTails = 0;
118          double[] impactsArray = impacts.ToArray();
119          studentttests.studentttest1(ref impactsArray, impactsArray.Length, 0, ref bothTails, ref leftTail, ref rightTail);
120          matrix[variableIndex, runs + 3] = leftTail;
121          wsr.wilcoxonsignedranktest(impacts.ToArray(), impactsArray.Length, 0, ref bothTails, ref leftTail, ref rightTail);
122          matrix[variableIndex, runs + 4] = leftTail;
123        }
124      }
125      return matrix;
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.