Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1886 added ttest and sample size determination for the ttest

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