Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Statistics.Views/3.3/StatisticalTestsView.cs @ 11871

Last change on this file since 11871 was 11837, checked in by ascheibe, 10 years ago

#2031

  • fixed a problem where an exception was thrown when the same progress bar was removed multiple times
  • remove data points from histogram before adding new ones
File size: 17.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Data;
31using HeuristicLab.MainForm;
32using HeuristicLab.Optimization;
33using HeuristicLab.Optimization.Views;
34
35namespace HeuristicLab.Analysis.Statistics.Views {
36  [View("Statistical Tests", "HeuristicLab.Analysis.Statistics.Views.InfoResources.StatisticalTestsInfo.rtf")]
37  [Content(typeof(RunCollection), false)]
38  public sealed partial class StatisticalTestsView : ItemView, IConfigureableView {
39    private double significanceLevel = 0.05;
40    private const int requiredSampleSize = 5;
41    private double[][] data;
42
43    public double SignificanceLevel {
44      get { return significanceLevel; }
45      set {
46        if (!significanceLevel.IsAlmost(value)) {
47          significanceLevel = value;
48          ResetUI();
49          CalculateValues();
50        }
51      }
52    }
53
54    public new RunCollection Content {
55      get { return (RunCollection)base.Content; }
56      set { base.Content = value; }
57    }
58
59    public override bool ReadOnly {
60      get { return true; }
61      set { /*not needed because results are always readonly */}
62    }
63
64    public StatisticalTestsView() {
65      InitializeComponent();
66    }
67
68    public void ShowConfiguration() {
69      using (StatisticalTestsConfigurationDialog dlg = new StatisticalTestsConfigurationDialog(this)) {
70        dlg.ShowDialog(this);
71      }
72    }
73
74    protected override void OnContentChanged() {
75      base.OnContentChanged();
76
77      if (Content != null) {
78        UpdateResultComboBox();
79        UpdateGroupsComboBox();
80        RebuildDataTable();
81      }
82      UpdateCaption();
83    }
84
85    private void UpdateCaption() {
86      Caption = Content != null ? Content.OptimizerName + " Statistical Tests" : ViewAttribute.GetViewName(GetType());
87    }
88
89    #region events
90    protected override void RegisterContentEvents() {
91      base.RegisterContentEvents();
92      Content.ColumnsChanged += Content_ColumnsChanged;
93      Content.RowsChanged += Content_RowsChanged;
94      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
95      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
96    }
97
98    protected override void DeregisterContentEvents() {
99      base.DeregisterContentEvents();
100      Content.ColumnsChanged -= Content_ColumnsChanged;
101      Content.RowsChanged -= Content_RowsChanged;
102      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
103      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
104    }
105
106    void Content_RowsChanged(object sender, EventArgs e) {
107      RebuildDataTable();
108    }
109
110    void Content_ColumnsChanged(object sender, EventArgs e) {
111      RebuildDataTable();
112    }
113
114    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
115      RebuildDataTable();
116    }
117
118    void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
119      if (!Content.UpdateOfRunsInProgress) {
120        RebuildDataTable();
121      }
122    }
123    #endregion
124
125    private void UpdateGroupsComboBox() {
126      groupComboBox.Items.Clear();
127
128      var parameters = (from run in Content
129                        where run.Visible
130                        from param in run.Parameters
131                        select param.Key).Distinct().ToArray();
132
133      foreach (var p in parameters) {
134        var variations = (from run in Content
135                          where run.Visible && run.Parameters.ContainsKey(p) &&
136                          (run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
137                          run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
138                          select ((dynamic)run.Parameters[p]).Value).Distinct();
139
140        if (variations.Count() > 1) {
141          groupComboBox.Items.Add(p);
142        }
143      }
144
145      if (groupComboBox.Items.Count > 0) {
146        //try to select something different than "Seed" or "Algorithm Name" as this makes no sense
147        //and takes a long time to group
148        List<int> possibleIndizes = new List<int>();
149        for (int i = 0; i < groupComboBox.Items.Count; i++) {
150          if (groupComboBox.Items[i].ToString() != "Seed"
151            && groupComboBox.Items[i].ToString() != "Algorithm Name") {
152            possibleIndizes.Add(i);
153          }
154        }
155
156        if (possibleIndizes.Count > 0) {
157          groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
158        } else {
159          groupComboBox.SelectedItem = groupComboBox.Items[0];
160        }
161      }
162    }
163
164    private string[] GetColumnNames(IEnumerable<IRun> runs) {
165      string parameterName = (string)groupComboBox.SelectedItem;
166      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
167      return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
168    }
169
170    private void UpdateResultComboBox() {
171      resultComboBox.Items.Clear();
172      var results = (from run in Content
173                     where run.Visible
174                     from result in run.Results
175                     where result.Value is IntValue || result.Value is DoubleValue
176                     select result.Key).Distinct().ToArray();
177
178      resultComboBox.Items.AddRange(results);
179      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
180    }
181
182    private void FillCompComboBox() {
183      string parameterName = (string)groupComboBox.SelectedItem;
184      if (parameterName != null) {
185        string resultName = (string)resultComboBox.SelectedItem;
186        if (resultName != null) {
187          var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
188          var columnNames = GetColumnNames(runs).ToList();
189          groupCompComboBox.Items.Clear();
190          columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
191          if (groupCompComboBox.Items.Count > 0) groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
192        }
193      }
194    }
195
196    private void RebuildDataTable() {
197      string parameterName = (string)groupComboBox.SelectedItem;
198      if (parameterName != null) {
199        string resultName = (string)resultComboBox.SelectedItem;
200
201        var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
202        var columnNames = GetColumnNames(runs);
203        var groups = GetGroups(columnNames, runs);
204        data = new double[columnNames.Count()][];
205
206        DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
207        dt.ColumnNames = columnNames;
208        DataTable histogramDataTable = new DataTable(resultName);
209
210        for (int i = 0; i < columnNames.Count(); i++) {
211          int j = 0;
212          data[i] = new double[groups[i].Count()];
213          DataRow row = new DataRow(columnNames[i]);
214          row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
215          histogramDataTable.Rows.Add(row);
216
217          foreach (IRun run in groups[i]) {
218            dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
219            data[i][j] = dt[j, i];
220            row.Values.Add(dt[j, i]);
221            j++;
222          }
223        }
224
225        GenerateChart(histogramDataTable);
226        stringConvertibleMatrixView.Content = dt;
227      }
228    }
229
230    private void GenerateChart(DataTable histogramTable) {
231      histogramControl.ClearPoints();
232      foreach (var row in histogramTable.Rows) {
233        histogramControl.AddPoints(row.Name, row.Values, true);
234      }
235    }
236
237    private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
238      List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
239      string parameterName = (string)groupComboBox.SelectedItem;
240
241      foreach (string cn in columnNames) {
242        var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
243        runCols.Add(tmpRuns);
244      }
245
246      return runCols;
247    }
248
249    private void ResetUI() {
250      normalityLabel.Image = null;
251      normalityTextLabel.Text = string.Empty;
252      groupCompLabel.Image = null;
253      groupComTextLabel.Text = string.Empty;
254      pairwiseLabel.Image = null;
255      pairwiseTextLabel.Text = string.Empty;
256
257      pValTextBox.Text = string.Empty;
258      equalDistsTextBox.Text = string.Empty;
259    }
260
261    private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
262      RebuildDataTable();
263      ResetUI();
264      CalculateValues();
265    }
266
267    private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
268      RebuildDataTable();
269      FillCompComboBox();
270      ResetUI();
271      CalculateValues();
272    }
273
274    private bool VerifyDataLength(bool showMessage) {
275      if (data == null || data.Length == 0)
276        return false;
277
278      //alglib needs at least 5 samples for computation
279      if (data.Any(x => x.Length <= requiredSampleSize)) {
280        if (showMessage)
281          MessageBox.Show(this, "You need at least " + requiredSampleSize
282            + " samples per group for computing hypothesis tests.", "HeuristicLab", MessageBoxButtons.OK,
283            MessageBoxIcon.Error);
284        return false;
285      }
286      return true;
287    }
288
289    private void CalculateValues() {
290      if (!VerifyDataLength(true))
291        return;
292
293      if (data != null) {
294        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>()
295          .AddOperationProgressToView(this, "Calculating...");
296
297        string curItem = (string)groupCompComboBox.SelectedItem;
298        Task.Factory.StartNew(() => CalculateValuesAsync(curItem));
299      }
300    }
301
302    private void CalculateValuesAsync(string groupName) {
303      CalculateAllGroupsTest();
304      CalculateNormalityTest();
305      CalculatePairwiseTest(groupName);
306
307      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
308    }
309
310    private void CalculatePairwise(string groupName) {
311      if (!VerifyDataLength(false))
312        return;
313
314      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(pairwiseTestGroupBox, "Calculating...");
315      Task.Factory.StartNew(() => CalculatePairwiseAsync(groupName));
316    }
317
318    private void CalculatePairwiseAsync(string groupName) {
319      CalculatePairwiseTest(groupName);
320
321      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(pairwiseTestGroupBox);
322    }
323
324    private void CalculateAllGroupsTest() {
325      double pval = KruskalWallisTest.Test(data);
326      pValTextBox.Text = pval.ToString();
327      if (pval < significanceLevel) {
328        this.Invoke(new Action(() => {
329          groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Default;
330          groupComTextLabel.Text = "There are groups with different distributions";
331        }));
332      } else {
333        this.Invoke(new Action(() => {
334          groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
335          groupComTextLabel.Text = "Groups have an equal distribution";
336        }));
337      }
338    }
339
340    private void CalculateNormalityTest() {
341      double val;
342      List<double> res = new List<double>();
343      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
344      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
345      pValsMatrix.RowNames = new string[] { "p-Value" };
346
347      for (int i = 0; i < data.Length; i++) {
348        alglib.jarqueberatest(data[i], data[i].Length, out val);
349        res.Add(val);
350        pValsMatrix[0, i] = val;
351      }
352
353      // p-value is below significance level and thus the null hypothesis (data is normally distributed) is rejected
354      if (res.Any(x => x < significanceLevel)) {
355        this.Invoke(new Action(() => {
356          normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
357          normalityTextLabel.Text = "Some groups may not be normally distributed";
358        }));
359      } else {
360        this.Invoke(new Action(() => {
361          normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Default;
362          normalityTextLabel.Text = "All sample data is normally distributed";
363        }));
364      }
365
366      this.Invoke(new Action(() => {
367        normalityStringConvertibleMatrixView.Content = pValsMatrix;
368        normalityStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
369      }));
370    }
371
372    private void ShowPairwiseResult(int nrOfEqualDistributions) {
373      double ratio = ((double)nrOfEqualDistributions) / (data.Length - 1) * 100.0;
374      equalDistsTextBox.Text = ratio.ToString() + " %";
375
376      if (nrOfEqualDistributions == 0) {
377        this.Invoke(new Action(() => {
378          pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Default;
379          pairwiseTextLabel.Text = "All groups have different distributions";
380        }));
381      } else {
382        this.Invoke(new Action(() => {
383          pairwiseLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
384          pairwiseTextLabel.Text = "Some groups have equal distributions";
385        }));
386      }
387    }
388
389    private void CalculatePairwiseTest(string groupName) {
390      var columnNames = stringConvertibleMatrixView.Content.ColumnNames.ToList();
391      int colIndex = columnNames.IndexOf(groupName);
392      columnNames = columnNames.Where(x => x != groupName).ToList();
393
394      double[][] newData = FilterDataForPairwiseTest(colIndex);
395
396      var rowNames = new string[] { "p-Value of Mann-Whitney U", "Adjusted p-Value of Mann-Whitney U",
397            "p-Value of T-Test", "Adjusted p-Value of T-Test", "Cohen's d", "Hedges' g" };
398
399      DoubleMatrix pValsMatrix = new DoubleMatrix(rowNames.Length, columnNames.Count());
400      pValsMatrix.ColumnNames = columnNames;
401      pValsMatrix.RowNames = rowNames;
402
403      double mwuBothTails;
404      double tTestBothTails;
405      double[] mwuPValues = new double[newData.Length];
406      double[] tTestPValues = new double[newData.Length];
407      bool[] decision = null;
408      double[] adjustedMwuPValues = null;
409      double[] adjustedTtestPValues = null;
410      int cnt = 0;
411
412      for (int i = 0; i < newData.Length; i++) {
413        mwuBothTails = PairwiseTest.MannWhitneyUTest(data[colIndex], newData[i]);
414        tTestBothTails = PairwiseTest.TTest(data[colIndex], newData[i]);
415        mwuPValues[i] = mwuBothTails;
416        tTestPValues[i] = tTestBothTails;
417
418        if (mwuBothTails > significanceLevel) {
419          cnt++;
420        }
421      }
422
423      adjustedMwuPValues = BonferroniHolm.Calculate(significanceLevel, mwuPValues, out decision);
424      adjustedTtestPValues = BonferroniHolm.Calculate(significanceLevel, tTestPValues, out decision);
425
426      for (int i = 0; i < newData.Length; i++) {
427        pValsMatrix[0, i] = mwuPValues[i];
428        pValsMatrix[1, i] = adjustedMwuPValues[i];
429        pValsMatrix[2, i] = tTestPValues[i];
430        pValsMatrix[3, i] = adjustedTtestPValues[i];
431        pValsMatrix[4, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], newData[i]);
432        pValsMatrix[5, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], newData[i]);
433      }
434
435      this.Invoke(new Action(() => {
436        pairwiseStringConvertibleMatrixView.Content = pValsMatrix;
437        pairwiseStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
438      }));
439
440      ShowPairwiseResult(cnt);
441    }
442
443    private double[][] FilterDataForPairwiseTest(int columnToRemove) {
444      double[][] newData = new double[data.Length - 1][];
445
446      int i = 0;
447      int l = 0;
448      while (i < data.Length) {
449        if (i != columnToRemove) {
450          double[] row = new double[data[i].Length - 1];
451          newData[l] = row;
452
453          int j = 0, k = 0;
454          while (j < row.Length) {
455            if (i != columnToRemove) {
456              newData[l][j] = data[i][k];
457              j++;
458              k++;
459            } else {
460              k++;
461            }
462          }
463          i++;
464          l++;
465        } else {
466          i++;
467        }
468      }
469      return newData;
470    }
471
472    private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
473      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
474      boxplotView.Content = Content;
475      boxplotView.SetXAxis(groupComboBox.SelectedItem.ToString());
476      boxplotView.SetYAxis(resultComboBox.SelectedItem.ToString());
477
478      boxplotView.Show();
479    }
480
481    private void groupCompComboBox_SelectedValueChanged(object sender, EventArgs e) {
482      string curItem = (string)groupCompComboBox.SelectedItem;
483      CalculatePairwise(curItem);
484    }
485  }
486}
Note: See TracBrowser for help on using the repository browser.