Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/StatisticalTestingView.cs @ 9329

Last change on this file since 9329 was 9329, checked in by ascheibe, 12 years ago

#1886 calculate Cohen's D and Hedges' G for pairwise comparison and updated documentation

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
28using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.Optimization;
33
34namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
35  [View("Statistical Testing View")]
36  [Content(typeof(RunCollection), false)]
37  public sealed partial class StatisticalTestingView : ItemView {
38    private double[][] data;
39
40    public StatisticalTestingView() {
41      InitializeComponent();
42    }
43
44    public new RunCollection Content {
45      get { return (RunCollection)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public override bool ReadOnly {
50      get { return true; }
51      set { /*not needed because results are always readonly */}
52    }
53
54    protected override void OnContentChanged() {
55      base.OnContentChanged();
56
57      if (Content != null) {
58        UpdateResultComboBox();
59        UpdateGroupsComboBox();
60        RebuildDataTable();
61      }
62    }
63
64    #region events
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67    }
68
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71    }
72    #endregion
73
74    private void UpdateGroupsComboBox() {
75      groupComboBox.Items.Clear();
76
77      var parameters = (from run in Content
78                        where run.Visible
79                        from param in run.Parameters
80                        select param.Key).Distinct().ToArray();
81
82      foreach (var p in parameters) {
83        var variations = (from run in Content
84                          where run.Visible && run.Parameters.ContainsKey(p) &&
85                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
86                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
87                          select ((dynamic)run.Parameters[p]).Value).Distinct();
88
89        if (variations.Count() > 1) {
90          groupComboBox.Items.Add(p);
91        }
92      }
93
94      if (groupComboBox.Items.Count > 0) {
95        //try to select something different than "Seed" or "Algorithm Name" as this makes no sense
96        //and takes a long time to group
97        List<int> possibleIndizes = new List<int>();
98        for (int i = 0; i < groupComboBox.Items.Count; i++) {
99          if (groupComboBox.Items[i].ToString() != "Seed"
100            && groupComboBox.Items[i].ToString() != "Algorithm Name") {
101            possibleIndizes.Add(i);
102          }
103        }
104
105        if (possibleIndizes.Count > 0) {
106          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
107        } else {
108          groupComboBox.SelectedItem = groupComboBox.Items[0];
109        }
110      }
111    }
112
113    private string[] GetColumnNames(IEnumerable<IRun> runs) {
114      string parameterName = (string)groupComboBox.SelectedItem;
115      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
116      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
117    }
118
119    private void UpdateResultComboBox() {
120      resultComboBox.Items.Clear();
121      var results = (from run in Content
122                     where run.Visible
123                     from result in run.Results
124                     where result.Value is IntValue || result.Value is DoubleValue
125                     select result.Key).Distinct().ToArray();
126
127      resultComboBox.Items.AddRange(results);
128      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
129    }
130
131    private void RebuildDataTable() {
132      string parameterName = (string)groupComboBox.SelectedItem;
133      if (parameterName != null) {
134        string resultName = (string)resultComboBox.SelectedItem;
135
136        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
137        var columnNames = GetColumnNames(runs);
138        var groups = GetGroups(columnNames, runs);
139        data = new double[columnNames.Count()][];
140
141        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
142        dt.ColumnNames = columnNames;
143
144        int i = 0;
145        int j = 0;
146        foreach (string columnName in columnNames) {
147          j = 0;
148          data[i] = new double[groups[i].Count()];
149          foreach (IRun run in groups[i]) {
150            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
151            data[i][j] = dt[j, i];
152            j++;
153          }
154          i++;
155        }
156
157        stringConvertibleMatrixView.Content = dt;
158      }
159    }
160
161    private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
162      List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
163      string parameterName = (string)groupComboBox.SelectedItem;
164
165      foreach (string cn in columnNames) {
166        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
167        runCols.Add(tmpRuns);
168      }
169
170      return runCols;
171    }
172
173    private void testButton_Click(object sender, EventArgs e) {
174      double pval = KruskalWallis.Test(data);
175      pValTextBox.Text = pval.ToString();
176    }
177
178    private void normalDistButton_Click(object sender, EventArgs e) {
179      double val;
180      List<double> res = new List<double>();
181
182      for (int i = 0; i < data.Length; i++) {
183        alglib.jarqueberatest(data[i], data[i].Length, out val);
184        res.Add(val);
185      }
186
187      for (int i = 0; i < res.Count(); i++) {
188        if (res[i] < 0.1) {
189          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Red;
190        } else {
191          stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Green;
192        }
193      }
194    }
195
196    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
197      RebuildDataTable();
198      pValTextBox.Text = string.Empty;
199    }
200
201    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
202      RebuildDataTable();
203      pValTextBox.Text = string.Empty;
204    }
205
206    private void normalityDetails_Click(object sender, EventArgs e) {
207      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
208      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
209      pValsMatrix.RowNames = new string[] { "p-Value" };
210
211      double val;
212      for (int i = 0; i < data.Length; i++) {
213        alglib.jarqueberatest(data[i], data[i].Length, out val);
214        pValsMatrix[0, i] = val;
215      }
216
217      MainFormManager.MainForm.ShowContent(pValsMatrix);
218    }
219
220    private void pairwiseTestButton_Click(object sender, EventArgs e) {
221      var selectedCells = stringConvertibleMatrixView.DataGridView.SelectedCells;
222      if (selectedCells.Count < 1) {
223        MessageBox.Show("Please selected one cell/column for pairwise comparision. ", "HeuristicLab", MessageBoxButtons.OK, MessageBoxIcon.Error);
224        return;
225      }
226
227      int colIndex = selectedCells[0].ColumnIndex;
228      foreach (DataGridViewCell selC in selectedCells) {
229        if (colIndex != selC.ColumnIndex) {
230          MessageBox.Show("Please selected only one column for pairwise comparision. ", "HeuristicLab", MessageBoxButtons.OK, MessageBoxIcon.Error);
231          return;
232        }
233      }
234
235      DoubleMatrix pValsMatrix = new DoubleMatrix(3, stringConvertibleMatrixView.Content.Columns);
236      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
237      pValsMatrix.RowNames = new string[] { "p-Value", "Cohen's d", "Hedges' g" };
238
239      double bothtails;
240      double lefttail;
241      double righttail;
242      for (int i = 0; i < data.Length; i++) {
243        alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out bothtails, out lefttail, out righttail);
244        pValsMatrix[0, i] = bothtails;
245        pValsMatrix[1, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], data[i]);
246        pValsMatrix[2, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], data[i]);
247      }
248
249      MainFormManager.MainForm.ShowContent(pValsMatrix);
250    }
251
252    private void infoLabel_DoubleClick(object sender, EventArgs e) {
253      using (TextDialog dialog = new TextDialog("Description of statistical tests", toolTip1.GetToolTip(this.infoLabel), true)) {
254        dialog.ShowDialog(this);
255      }
256    }
257  }
258}
Note: See TracBrowser for help on using the repository browser.