forked from s-wagner/HeuristicLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatisticalTestsView.cs
540 lines (460 loc) · 19.3 KB
/
StatisticalTestsView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Common.Resources;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Views;
namespace HeuristicLab.Analysis.Statistics.Views {
[View("Statistical Tests", "HeuristicLab.Analysis.Statistics.Views.InfoResources.StatisticalTestsInfo.rtf")]
[Content(typeof(RunCollection), false)]
public sealed partial class StatisticalTestsView : ItemView, IConfigureableView {
private double significanceLevel = 0.05;
private const int requiredSampleSize = 5;
private double[][] data;
private bool suppressUpdates;
private bool initializing;
public double SignificanceLevel {
get { return significanceLevel; }
set {
if (!significanceLevel.IsAlmost(value)) {
significanceLevel = value;
ResetUI();
CalculateValues();
}
}
}
public new RunCollection Content {
get { return (RunCollection)base.Content; }
set { base.Content = value; }
}
public override bool ReadOnly {
get { return true; }
set { /*not needed because results are always readonly */}
}
public StatisticalTestsView() {
InitializeComponent();
}
public void ShowConfiguration() {
using (StatisticalTestsConfigurationDialog dlg = new StatisticalTestsConfigurationDialog(this)) {
dlg.ShowDialog(this);
}
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
UpdateUI();
} else {
ResetUI();
}
UpdateCaption();
}
private void UpdateUI() {
initializing = true;
UpdateResultComboBox();
UpdateGroupsComboBox();
RebuildDataTable();
FillCompComboBox();
ResetUI();
CalculateValues();
initializing = false;
}
private void UpdateCaption() {
Caption = Content != null ? Content.OptimizerName + " Statistical Tests" : ViewAttribute.GetViewName(GetType());
}
#region events
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ColumnsChanged += Content_ColumnsChanged;
Content.RowsChanged += Content_RowsChanged;
Content.CollectionReset += Content_CollectionReset;
Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ColumnsChanged -= Content_ColumnsChanged;
Content.RowsChanged -= Content_RowsChanged;
Content.CollectionReset -= Content_CollectionReset;
Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
}
void Content_RowsChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_RowsChanged, sender, e);
else {
UpdateUI();
}
}
void Content_ColumnsChanged(object sender, EventArgs e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ColumnsChanged, sender, e);
else {
UpdateUI();
}
}
private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
if (suppressUpdates) return;
if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IRun>>)Content_CollectionReset, sender, e);
else {
UpdateUI();
}
}
void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_UpdateOfRunsInProgressChanged, sender, e);
else {
suppressUpdates = Content.UpdateOfRunsInProgress;
if (!suppressUpdates) UpdateUI();
}
}
private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
boxplotView.Content = Content;
boxplotView.SetXAxis(groupComboBox.SelectedItem.ToString());
boxplotView.SetYAxis(resultComboBox.SelectedItem.ToString());
boxplotView.Show();
}
private void groupCompComboBox_SelectedValueChanged(object sender, EventArgs e) {
if (initializing || suppressUpdates) return;
string curItem = (string)groupCompComboBox.SelectedItem;
CalculatePairwise(curItem);
}
private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
if (initializing || suppressUpdates) return;
RebuildDataTable();
ResetUI();
CalculateValues();
}
private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
if (initializing || suppressUpdates) return;
RebuildDataTable();
FillCompComboBox();
ResetUI();
CalculateValues();
}
#endregion
private void UpdateGroupsComboBox() {
string selectedItem = (string)groupComboBox.SelectedItem;
groupComboBox.Items.Clear();
var parameters = (from run in Content
where run.Visible
from param in run.Parameters
select param.Key).Distinct().ToArray();
foreach (var p in parameters) {
var variations = (from run in Content
where run.Visible && run.Parameters.ContainsKey(p) &&
(run.Parameters[p] is IntValue || run.Parameters[p] is DoubleValue ||
run.Parameters[p] is StringValue || run.Parameters[p] is BoolValue)
select ((dynamic)run.Parameters[p]).Value).Distinct();
if (variations.Count() > 1) {
groupComboBox.Items.Add(p);
}
}
if (groupComboBox.Items.Count > 0) {
//try to select something different than "Seed" or "Algorithm Name" as this makes no sense
//and takes a long time to group
List<int> possibleIndizes = new List<int>();
for (int i = 0; i < groupComboBox.Items.Count; i++) {
if (groupComboBox.Items[i].ToString() != "Seed"
&& groupComboBox.Items[i].ToString() != "Algorithm Name") {
possibleIndizes.Add(i);
}
}
if (selectedItem != null && groupComboBox.Items.Contains(selectedItem)) {
groupComboBox.SelectedItem = selectedItem;
} else if (possibleIndizes.Count > 0) {
groupComboBox.SelectedItem = groupComboBox.Items[possibleIndizes.First()];
}
}
}
private string[] GetColumnNames(IEnumerable<IRun> runs) {
string parameterName = (string)groupComboBox.SelectedItem;
var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
return r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct().Select(x => (string)x.ToString()).ToArray();
}
private void UpdateResultComboBox() {
string selectedItem = (string)resultComboBox.SelectedItem;
resultComboBox.Items.Clear();
var results = (from run in Content
where run.Visible
from result in run.Results
where result.Value is IntValue || result.Value is DoubleValue
select result.Key).Distinct().ToArray();
resultComboBox.Items.AddRange(results);
if (selectedItem != null && resultComboBox.Items.Contains(selectedItem)) {
resultComboBox.SelectedItem = selectedItem;
} else if (resultComboBox.Items.Count > 0) {
resultComboBox.SelectedItem = resultComboBox.Items[0];
}
}
private void FillCompComboBox() {
string selectedItem = (string)groupCompComboBox.SelectedItem;
string parameterName = (string)groupComboBox.SelectedItem;
if (parameterName != null) {
string resultName = (string)resultComboBox.SelectedItem;
if (resultName != null) {
var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
var columnNames = GetColumnNames(runs).ToList();
groupCompComboBox.Items.Clear();
columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
if (selectedItem != null && groupCompComboBox.Items.Contains(selectedItem)) {
groupCompComboBox.SelectedItem = selectedItem;
} else if (groupCompComboBox.Items.Count > 0) {
groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
}
}
}
}
private void RebuildDataTable() {
string parameterName = (string)groupComboBox.SelectedItem;
if (parameterName != null) {
string resultName = (string)resultComboBox.SelectedItem;
var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
var columnNames = GetColumnNames(runs);
var groups = GetGroups(columnNames, runs);
data = new double[columnNames.Count()][];
if (!groups.Any() || !columnNames.Any()) {
return;
}
DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
dt.ColumnNames = columnNames;
DataTable histogramDataTable = new DataTable(resultName);
for (int i = 0; i < columnNames.Count(); i++) {
int j = 0;
data[i] = new double[groups[i].Count()];
DataRow row = new DataRow(columnNames[i]);
row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
histogramDataTable.Rows.Add(row);
foreach (IRun run in groups[i]) {
dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
data[i][j] = dt[j, i];
row.Values.Add(dt[j, i]);
j++;
}
}
GenerateChart(histogramDataTable);
stringConvertibleMatrixView.Content = dt;
}
}
private void GenerateChart(DataTable histogramTable) {
histogramControl.ClearPoints();
foreach (var row in histogramTable.Rows) {
histogramControl.AddPoints(row.Name, row.Values, true);
}
}
private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
string parameterName = (string)groupComboBox.SelectedItem;
foreach (string cn in columnNames) {
var tmpRuns = runs.Where(x =>
x.Parameters.ContainsKey(parameterName) &&
(((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn));
runCols.Add(tmpRuns);
}
return runCols;
}
private void ResetUI() {
normalityLabel.Image = null;
normalityTextLabel.Text = string.Empty;
groupCompLabel.Image = null;
groupComTextLabel.Text = string.Empty;
pairwiseLabel.Image = null;
pairwiseTextLabel.Text = string.Empty;
pValTextBox.Text = string.Empty;
equalDistsTextBox.Text = string.Empty;
}
private bool VerifyDataLength(bool showMessage) {
if (data == null || data.Length < 2)
return false;
//alglib needs at least 5 samples for computation
if (data.Any(x => x.Length < requiredSampleSize)) {
if (showMessage)
MessageBox.Show(this, "You need at least " + requiredSampleSize
+ " samples per group for computing hypothesis tests.", "HeuristicLab", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
return true;
}
private void CalculateValues() {
if (!VerifyDataLength(true))
return;
if (data != null && data.All(x => x != null)) {
Progress.Show(this, "Calculating...", ProgressMode.Indeterminate);
string curItem = (string)groupCompComboBox.SelectedItem;
Task.Factory.StartNew(() => CalculateValuesAsync(curItem));
}
}
private void CalculateValuesAsync(string groupName) {
CalculateAllGroupsTest();
CalculateNormalityTest();
CalculatePairwiseTest(groupName);
Progress.Hide(this);
}
private void CalculatePairwise(string groupName) {
if (groupName == null) return;
if (!VerifyDataLength(false))
return;
Progress.ShowOnControl(pairwiseTestGroupBox, "Calculating...", ProgressMode.Indeterminate);
Task.Factory.StartNew(() => CalculatePairwiseAsync(groupName));
}
private void CalculatePairwiseAsync(string groupName) {
CalculatePairwiseTest(groupName);
Progress.HideFromControl(pairwiseTestGroupBox);
}
private void CalculateAllGroupsTest() {
double pval = KruskalWallisTest.Test(data);
DisplayAllGroupsTextResults(pval);
}
private void DisplayAllGroupsTextResults(double pval) {
if (InvokeRequired) {
Invoke((Action<double>)DisplayAllGroupsTextResults, pval);
} else {
pValTextBox.Text = pval.ToString();
if (pval < significanceLevel) {
groupCompLabel.Image = VSImageLibrary.Default;
groupComTextLabel.Text = "There are groups with different distributions";
} else {
groupCompLabel.Image = VSImageLibrary.Warning;
groupComTextLabel.Text = "Groups have an equal distribution";
}
}
}
private void CalculateNormalityTest() {
double val;
List<double> res = new List<double>();
DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
pValsMatrix.RowNames = new[] { "p-Value" };
for (int i = 0; i < data.Length; i++) {
alglib.jarqueberatest(data[i], data[i].Length, out val);
res.Add(val);
pValsMatrix[0, i] = val;
}
// p-value is below significance level and thus the null hypothesis (data is normally distributed) is rejected
if (res.Any(x => x < significanceLevel)) {
Invoke(new Action(() => {
normalityLabel.Image = VSImageLibrary.Warning;
normalityTextLabel.Text = "Some groups may not be normally distributed";
}));
} else {
Invoke(new Action(() => {
normalityLabel.Image = VSImageLibrary.Default;
normalityTextLabel.Text = "All sample data is normally distributed";
}));
}
Invoke(new Action(() => {
normalityStringConvertibleMatrixView.Content = pValsMatrix;
normalityStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}));
}
private void ShowPairwiseResult(int nrOfEqualDistributions) {
double ratio = ((double)nrOfEqualDistributions) / (data.Length - 1) * 100.0;
equalDistsTextBox.Text = ratio + " %";
if (nrOfEqualDistributions == 0) {
Invoke(new Action(() => {
pairwiseLabel.Image = VSImageLibrary.Default;
pairwiseTextLabel.Text = "All groups have different distributions";
}));
} else {
Invoke(new Action(() => {
pairwiseLabel.Image = VSImageLibrary.Warning;
pairwiseTextLabel.Text = "Some groups have equal distributions";
}));
}
}
private void CalculatePairwiseTest(string groupName) {
var columnNames = stringConvertibleMatrixView.Content.ColumnNames.ToList();
int colIndex = columnNames.IndexOf(groupName);
columnNames = columnNames.Where(x => x != groupName).ToList();
double[][] newData = FilterDataForPairwiseTest(colIndex);
var rowNames = new[] { "p-Value of Mann-Whitney U", "Adjusted p-Value of Mann-Whitney U",
"p-Value of T-Test", "Adjusted p-Value of T-Test", "Cohen's d", "Hedges' g" };
DoubleMatrix pValsMatrix = new DoubleMatrix(rowNames.Length, columnNames.Count());
pValsMatrix.ColumnNames = columnNames;
pValsMatrix.RowNames = rowNames;
double mwuBothTails;
double tTestBothTails;
double[] mwuPValues = new double[newData.Length];
double[] tTestPValues = new double[newData.Length];
bool[] decision = null;
double[] adjustedMwuPValues = null;
double[] adjustedTtestPValues = null;
int cnt = 0;
for (int i = 0; i < newData.Length; i++) {
mwuBothTails = PairwiseTest.MannWhitneyUTest(data[colIndex], newData[i]);
tTestBothTails = PairwiseTest.TTest(data[colIndex], newData[i]);
mwuPValues[i] = mwuBothTails;
tTestPValues[i] = tTestBothTails;
if (mwuBothTails > significanceLevel) {
cnt++;
}
}
adjustedMwuPValues = BonferroniHolm.Calculate(significanceLevel, mwuPValues, out decision);
adjustedTtestPValues = BonferroniHolm.Calculate(significanceLevel, tTestPValues, out decision);
for (int i = 0; i < newData.Length; i++) {
pValsMatrix[0, i] = mwuPValues[i];
pValsMatrix[1, i] = adjustedMwuPValues[i];
pValsMatrix[2, i] = tTestPValues[i];
pValsMatrix[3, i] = adjustedTtestPValues[i];
pValsMatrix[4, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], newData[i]);
pValsMatrix[5, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], newData[i]);
}
Invoke(new Action(() => {
pairwiseStringConvertibleMatrixView.Content = pValsMatrix;
pairwiseStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}));
ShowPairwiseResult(cnt);
}
private double[][] FilterDataForPairwiseTest(int columnToRemove) {
double[][] newData = new double[data.Length - 1][];
int i = 0;
int l = 0;
while (i < data.Length) {
if (i != columnToRemove) {
double[] row = new double[data[i].Length - 1];
newData[l] = row;
int j = 0, k = 0;
while (j < row.Length) {
if (i != columnToRemove) {
newData[l][j] = data[i][k];
j++;
k++;
} else {
k++;
}
}
i++;
l++;
} else {
i++;
}
}
return newData;
}
}
}