Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/StatisticalTestingView.cs @ 9813

Last change on this file since 9813 was 9813, checked in by ascheibe, 11 years ago

#2031 fixed a bug when calculating the percentage of equal groups

File size: 11.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Views;
31
32namespace HeuristicLab.Analysis.Statistics {
33  [View("RunCollection Statistical Testing")]
34  [Content(typeof(RunCollection), false)]
35  public sealed partial class StatisticalTestingView : ItemView {
36    private double[][] data;
37
38    public StatisticalTestingView() {
39      InitializeComponent();
40    }
41
42    public new RunCollection Content {
43      get { return (RunCollection)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public override bool ReadOnly {
48      get { return true; }
49      set { /*not needed because results are always readonly */}
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54
55      if (Content != null) {
56        UpdateResultComboBox();
57        UpdateGroupsComboBox();
58        FillCompComboBox();
59        RebuildDataTable();
60      }
61    }
62
63    #region events
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66    }
67
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70    }
71    #endregion
72
73    private void UpdateGroupsComboBox() {
74      groupComboBox.Items.Clear();
75
76      var parameters = (from run in Content
77                        where run.Visible
78                        from param in run.Parameters
79                        select param.Key).Distinct().ToArray();
80
81      foreach (var p in parameters) {
82        var variations = (from run in Content
83                          where run.Visible && run.Parameters.ContainsKey(p) &&
84                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
85                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
86                          select ((dynamic)run.Parameters[p]).Value).Distinct();
87
88        if (variations.Count() > 1) {
89          groupComboBox.Items.Add(p);
90        }
91      }
92
93      if (groupComboBox.Items.Count > 0) {
94        //try to select something different than "Seed" or "Algorithm Name" as this makes no sense
95        //and takes a long time to group
96        List<int> possibleIndizes = new List<int>();
97        for (int i = 0; i < groupComboBox.Items.Count; i++) {
98          if (groupComboBox.Items[i].ToString() != "Seed"
99            && groupComboBox.Items[i].ToString() != "Algorithm Name") {
100            possibleIndizes.Add(i);
101          }
102        }
103
104        if (possibleIndizes.Count > 0) {
105          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
106        } else {
107          groupComboBox.SelectedItem = groupComboBox.Items[0];
108        }
109      }
110    }
111
112    private string[] GetColumnNames(IEnumerable<IRun> runs) {
113      string parameterName = (string)groupComboBox.SelectedItem;
114      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
115      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
116    }
117
118    private void UpdateResultComboBox() {
119      resultComboBox.Items.Clear();
120      var results = (from run in Content
121                     where run.Visible
122                     from result in run.Results
123                     where result.Value is IntValue || result.Value is DoubleValue
124                     select result.Key).Distinct().ToArray();
125
126      resultComboBox.Items.AddRange(results);
127      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
128    }
129
130    private void FillCompComboBox() {
131      string parameterName = (string)groupComboBox.SelectedItem;
132      if (parameterName != null) {
133        string resultName = (string)resultComboBox.SelectedItem;
134        if (resultName != null) {
135          var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
136          var columnNames = GetColumnNames(runs).ToList();
137          groupCompComboBox.Items.Clear();
138          columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
139          if (groupCompComboBox.Items.Count > 0) groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
140        }
141      }
142    }
143
144    private void RebuildDataTable() {
145      string parameterName = (string)groupComboBox.SelectedItem;
146      if (parameterName != null) {
147        string resultName = (string)resultComboBox.SelectedItem;
148
149        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
150        var columnNames = GetColumnNames(runs);
151        var groups = GetGroups(columnNames, runs);
152        data = new double[columnNames.Count()][];
153
154        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
155        dt.ColumnNames = columnNames;
156
157        int i = 0;
158        int j = 0;
159        foreach (string columnName in columnNames) {
160          j = 0;
161          data[i] = new double[groups[i].Count()];
162          foreach (IRun run in groups[i]) {
163            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
164            data[i][j] = dt[j, i];
165            j++;
166          }
167          i++;
168        }
169
170        stringConvertibleMatrixView.Content = dt;
171      }
172    }
173
174    private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
175      List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
176      string parameterName = (string)groupComboBox.SelectedItem;
177
178      foreach (string cn in columnNames) {
179        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
180        runCols.Add(tmpRuns);
181      }
182
183      return runCols;
184    }
185
186    private void ResetUI() {
187      normalityLabel.Image = null;
188      groupCompLabel.Image = null;
189      pairwiseLabel.Image = null;
190      pValTextBox.Text = string.Empty;
191      equalDistsTextBox.Text = string.Empty;
192    }
193
194    private void testButton_Click(object sender, EventArgs e) {
195      double pval = KruskalWallis.Test(data);
196      pValTextBox.Text = pval.ToString();
197      if (pval < 0.05) {
198        groupCompLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
199      } else {
200        groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
201      }
202    }
203
204    private void normalDistButton_Click(object sender, EventArgs e) {
205      double val;
206      List<double> res = new List<double>();
207
208      for (int i = 0; i < data.Length; i++) {
209        alglib.jarqueberatest(data[i], data[i].Length, out val);
210        res.Add(val);
211      }
212
213      for (int i = 0; i < res.Count(); i++) {
214        if (res[i] < 0.1) {
215          normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
216        } else {
217          normalityLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
218        }
219      }
220    }
221
222    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
223      RebuildDataTable();
224      ResetUI();
225    }
226
227    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
228      FillCompComboBox();
229      RebuildDataTable();
230      ResetUI();
231    }
232
233    private void normalityDetails_Click(object sender, EventArgs e) {
234      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
235      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
236      pValsMatrix.RowNames = new string[] { "p-Value" };
237
238      double val;
239      for (int i = 0; i < data.Length; i++) {
240        alglib.jarqueberatest(data[i], data[i].Length, out val);
241        pValsMatrix[0, i] = val;
242      }
243
244      MainFormManager.MainForm.ShowContent(pValsMatrix);
245    }
246
247    private void pairwiseTestButton_Click(object sender, EventArgs e) {
248      string curItem = (string)groupCompComboBox.SelectedItem;
249      int colIndex = 0;
250
251      foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
252        if (col == curItem) {
253          break;
254        }
255        colIndex++;
256      }
257
258      DoubleMatrix pValsMatrix = new DoubleMatrix(5, stringConvertibleMatrixView.Content.Columns);
259      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
260      pValsMatrix.RowNames = new string[] { "p-Value of Mann-Whitney U", "p-Value of T-Test", "Necessary Sample Size for T-Test", "Cohen's d", "Hedges' g" };
261
262      double mwuBothtails;
263      double mwuLefttail;
264      double mwuRighttail;
265      double ttestLefttail;
266      for (int i = 0; i < data.Length; i++) {
267        alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
268        ttestLefttail = TTest.Test(data[colIndex], data[i]);
269        pValsMatrix[0, i] = mwuBothtails;
270        pValsMatrix[1, i] = ttestLefttail;
271        pValsMatrix[2, i] = TTest.GetOptimalSampleSize(data[colIndex], data[i]);
272        pValsMatrix[3, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], data[i]);
273        pValsMatrix[4, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], data[i]);
274      }
275
276      MainFormManager.MainForm.ShowContent(pValsMatrix);
277    }
278
279    private void infoLabel_DoubleClick(object sender, EventArgs e) {
280      using (InfoBox dialog = new InfoBox("Description of Statistical Tests", typeof(StatisticalTestingView).Namespace + ".InfoResources.StatisticalTestsInfo.rtf")) {
281        dialog.ShowDialog(this);
282      }
283    }
284
285    private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
286      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
287      boxplotView.Content = Content;
288      // TODO: enable as soon as we move to HeuristicLab.Optimization.Views
289      // boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
290      // boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
291      boxplotView.Show();
292    }
293
294    private void pairwiseCheckDataButton_Click(object sender, EventArgs e) {
295      string curItem = (string)groupCompComboBox.SelectedItem;
296      int colIndex = 0;
297
298      foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
299        if (col == curItem) {
300          break;
301        }
302        colIndex++;
303      }
304
305      double mwuBothtails;
306      double mwuLefttail;
307      double mwuRighttail;
308      int cnt = 0;
309
310      for (int i = 0; i < data.Length; i++) {
311        if (i != colIndex) {
312          alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
313          if (mwuBothtails > 0.05) {
314            cnt++;
315          }
316        }
317      }
318
319      double ratio = ((double)cnt) / (data.Length - 1) * 100.0;
320      equalDistsTextBox.Text = ratio.ToString() + " %";
321
322      if (cnt == 0) {
323        pairwiseLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
324      } else {
325        pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
326      }
327    }
328  }
329}
Note: See TracBrowser for help on using the repository browser.