Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2031

  • redesigned statistical testing view
  • improved sample size influence view
File size: 15.0 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.Threading.Tasks;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Views;
32
33namespace HeuristicLab.Analysis.Statistics {
34  [View("Statistical Tests")]
35  [Content(typeof(RunCollection), false)]
36  public sealed partial class StatisticalTestingView : ItemView {
37    private double[][] data;
38
39    public StatisticalTestingView() {
40      InitializeComponent();
41    }
42
43    public new RunCollection Content {
44      get { return (RunCollection)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public override bool ReadOnly {
49      get { return true; }
50      set { /*not needed because results are always readonly */}
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55
56      if (Content != null) {
57        UpdateResultComboBox();
58        UpdateGroupsComboBox();
59        FillCompComboBox();
60        RebuildDataTable();
61      }
62      UpdateCaption();
63    }
64
65    private void UpdateCaption() {
66      Caption = Content != null ? Content.OptimizerName + " Statistical Tests" : ViewAttribute.GetViewName(GetType());
67    }
68
69    #region events
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
73      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
74      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
75      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
76    }
77
78    protected override void DeregisterContentEvents() {
79      base.DeregisterContentEvents();
80      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
81      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
82      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
83      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
84    }
85
86    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
87      RebuildDataTable();
88    }
89
90    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
91      RebuildDataTable();
92    }
93
94    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
95      RebuildDataTable();
96    }
97
98    void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
99      if (!Content.UpdateOfRunsInProgress) {
100        RebuildDataTable();
101      }
102    }
103    #endregion
104
105    private void UpdateGroupsComboBox() {
106      groupComboBox.Items.Clear();
107
108      var parameters = (from run in Content
109                        where run.Visible
110                        from param in run.Parameters
111                        select param.Key).Distinct().ToArray();
112
113      foreach (var p in parameters) {
114        var variations = (from run in Content
115                          where run.Visible && run.Parameters.ContainsKey(p) &&
116                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
117                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
118                          select ((dynamic)run.Parameters[p]).Value).Distinct();
119
120        if (variations.Count() > 1) {
121          groupComboBox.Items.Add(p);
122        }
123      }
124
125      if (groupComboBox.Items.Count > 0) {
126        //try to select something different than "Seed" or "Algorithm Name" as this makes no sense
127        //and takes a long time to group
128        List<int> possibleIndizes = new List<int>();
129        for (int i = 0; i < groupComboBox.Items.Count; i++) {
130          if (groupComboBox.Items[i].ToString() != "Seed"
131            && groupComboBox.Items[i].ToString() != "Algorithm Name") {
132            possibleIndizes.Add(i);
133          }
134        }
135
136        if (possibleIndizes.Count > 0) {
137          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
138        } else {
139          groupComboBox.SelectedItem = groupComboBox.Items[0];
140        }
141      }
142    }
143
144    private string[] GetColumnNames(IEnumerable<IRun> runs) {
145      string parameterName = (string)groupComboBox.SelectedItem;
146      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
147      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
148    }
149
150    private void UpdateResultComboBox() {
151      resultComboBox.Items.Clear();
152      var results = (from run in Content
153                     where run.Visible
154                     from result in run.Results
155                     where result.Value is IntValue || result.Value is DoubleValue
156                     select result.Key).Distinct().ToArray();
157
158      resultComboBox.Items.AddRange(results);
159      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
160    }
161
162    private void FillCompComboBox() {
163      string parameterName = (string)groupComboBox.SelectedItem;
164      if (parameterName != null) {
165        string resultName = (string)resultComboBox.SelectedItem;
166        if (resultName != null) {
167          var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
168          var columnNames = GetColumnNames(runs).ToList();
169          groupCompComboBox.Items.Clear();
170          columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
171          if (groupCompComboBox.Items.Count > 0) groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
172        }
173      }
174    }
175
176    private void RebuildDataTable() {
177      string parameterName = (string)groupComboBox.SelectedItem;
178      if (parameterName != null) {
179        string resultName = (string)resultComboBox.SelectedItem;
180
181        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
182        var columnNames = GetColumnNames(runs);
183        var groups = GetGroups(columnNames, runs);
184        data = new double[columnNames.Count()][];
185
186        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
187        dt.ColumnNames = columnNames;
188
189        int i = 0;
190        int j = 0;
191        foreach (string columnName in columnNames) {
192          j = 0;
193          data[i] = new double[groups[i].Count()];
194          foreach (IRun run in groups[i]) {
195            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
196            data[i][j] = dt[j, i];
197            j++;
198          }
199          i++;
200        }
201
202        stringConvertibleMatrixView.Content = dt;
203      }
204    }
205
206    private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
207      List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
208      string parameterName = (string)groupComboBox.SelectedItem;
209
210      foreach (string cn in columnNames) {
211        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
212        runCols.Add(tmpRuns);
213      }
214
215      return runCols;
216    }
217
218    private void ResetUI() {
219      normalityLabel.Image = null;
220      groupCompLabel.Image = null;
221      pairwiseLabel.Image = null;
222      pValTextBox.Text = string.Empty;
223      equalDistsTextBox.Text = string.Empty;
224    }
225
226    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
227      RebuildDataTable();
228      ResetUI();
229      CalculateValues();
230    }
231
232    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
233      FillCompComboBox();
234      RebuildDataTable();
235      ResetUI();
236      CalculateValues();
237    }
238
239    private void CalculateValues() {
240      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Calculating...");
241      Task.Factory.StartNew(CalculateValuesAsync);
242    }
243
244    private void CalculateValuesAsync() {
245      TestAllGroups();
246      CalculateNormality();
247      CalculateNormalityDetails();
248      CalculatePairwiseTest();
249      CalculatePairwiseTestDetails();
250
251      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
252    }
253
254    private void CalculatePairwise() {
255      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Calculating...");
256      Task.Factory.StartNew(CalculatePairwiseAsync);
257    }
258
259    private void CalculatePairwiseAsync() {
260      CalculatePairwiseTest();
261      CalculatePairwiseTestDetails();
262
263      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
264    }
265
266    private void TestAllGroups() {
267      double pval = KruskalWallis.Test(data);
268      pValTextBox.Text = pval.ToString();
269      if (pval < 0.05) {
270        this.Invoke(new Action(() => { groupCompLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default; }));
271      } else {
272        this.Invoke(new Action(() => { groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning; }));
273      }
274    }
275
276    private void CalculateNormality() {
277      double val;
278      List<double> res = new List<double>();
279
280      for (int i = 0; i < data.Length; i++) {
281        alglib.jarqueberatest(data[i], data[i].Length, out val);
282        res.Add(val);
283      }
284
285      for (int i = 0; i < res.Count(); i++) {
286        if (res[i] < 0.1) {
287          this.Invoke(new Action(() => { normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning; }));
288        } else {
289          this.Invoke(new Action(() => { normalityLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default; }));
290        }
291      }
292    }
293
294    private void CalculateNormalityDetails() {
295      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
296      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
297      pValsMatrix.RowNames = new string[] { "p-Value" };
298
299      double val;
300      for (int i = 0; i < data.Length; i++) {
301        alglib.jarqueberatest(data[i], data[i].Length, out val);
302        pValsMatrix[0, i] = val;
303      }
304
305      this.Invoke(new Action(() => { normalityStringConvertibleMatrixView.Content = pValsMatrix; }));
306    }
307
308    private void CalculatePairwiseTest() {
309      string curItem = (string)groupCompComboBox.SelectedItem;
310      int colIndex = 0;
311
312      foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
313        if (col == curItem) {
314          break;
315        }
316        colIndex++;
317      }
318
319      double mwuBothtails;
320      double mwuLefttail;
321      double mwuRighttail;
322      int cnt = 0;
323
324      for (int i = 0; i < data.Length; i++) {
325        if (i != colIndex) {
326          alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
327          if (mwuBothtails > 0.05) {
328            cnt++;
329          }
330        }
331      }
332
333      double ratio = ((double)cnt) / (data.Length - 1) * 100.0;
334      equalDistsTextBox.Text = ratio.ToString() + " %";
335
336      if (cnt == 0) {
337        this.Invoke(new Action(() => { pairwiseLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default; }));
338      } else {
339        this.Invoke(new Action(() => { pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning; }));
340      }
341    }
342
343    private void CalculatePairwiseTestDetails() {
344      string curItem = (string)groupCompComboBox.SelectedItem;
345      int colIndex = 0;
346
347      foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
348        if (col == curItem) {
349          break;
350        }
351        colIndex++;
352      }
353
354      DoubleMatrix pValsMatrix = new DoubleMatrix(5, stringConvertibleMatrixView.Content.Columns);
355      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
356      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" };
357
358      double mwuBothtails;
359      double mwuLefttail;
360      double mwuRighttail;
361      double ttestLefttail;
362      for (int i = 0; i < data.Length; i++) {
363        alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
364        ttestLefttail = TTest.Test(data[colIndex], data[i]);
365        pValsMatrix[0, i] = mwuBothtails;
366        pValsMatrix[1, i] = ttestLefttail;
367        pValsMatrix[2, i] = TTest.GetOptimalSampleSize(data[colIndex], data[i]);
368        pValsMatrix[3, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], data[i]);
369        pValsMatrix[4, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], data[i]);
370      }
371
372      this.Invoke(new Action(() => { pairwiseStringConvertibleMatrixView.Content = pValsMatrix; }));
373    }
374
375    private void infoLabel_DoubleClick(object sender, EventArgs e) {
376      using (InfoBox dialog = new InfoBox("Description of Statistical Tests", typeof(StatisticalTestingView).Namespace + ".InfoResources.StatisticalTestsInfo.rtf")) {
377        dialog.ShowDialog(this);
378      }
379    }
380
381    private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
382      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
383      boxplotView.Content = Content;
384      // TODO: enable as soon as we move to HeuristicLab.Optimization.Views
385      // boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
386      // boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
387      boxplotView.Show();
388    }
389
390    private void groupCompComboBox_SelectedValueChanged(object sender, EventArgs e) {
391      CalculatePairwise();
392    }
393  }
394}
Note: See TracBrowser for help on using the repository browser.