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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Views;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis.Statistics {
|
---|
34 | [View("RunCollection Statistical Testing")]
|
---|
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 | }
|
---|
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 FillCompComboBox() {
|
---|
132 | string parameterName = (string)groupComboBox.SelectedItem;
|
---|
133 | if (parameterName != null) {
|
---|
134 | string resultName = (string)resultComboBox.SelectedItem;
|
---|
135 | if (resultName != null) {
|
---|
136 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
137 | var columnNames = GetColumnNames(runs).ToList();
|
---|
138 | groupCompComboBox.Items.Clear();
|
---|
139 | columnNames.ForEach(x => groupCompComboBox.Items.Add(x));
|
---|
140 | if (groupCompComboBox.Items.Count > 0) groupCompComboBox.SelectedItem = groupCompComboBox.Items[0];
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void RebuildDataTable() {
|
---|
146 | string parameterName = (string)groupComboBox.SelectedItem;
|
---|
147 | if (parameterName != null) {
|
---|
148 | string resultName = (string)resultComboBox.SelectedItem;
|
---|
149 |
|
---|
150 | var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
|
---|
151 | var columnNames = GetColumnNames(runs);
|
---|
152 | var groups = GetGroups(columnNames, runs);
|
---|
153 | data = new double[columnNames.Count()][];
|
---|
154 |
|
---|
155 | DoubleMatrix dt = new DoubleMatrix(groups.Select(x => x.Count()).Max(), columnNames.Count());
|
---|
156 | dt.ColumnNames = columnNames;
|
---|
157 |
|
---|
158 | int i = 0;
|
---|
159 | int j = 0;
|
---|
160 | foreach (string columnName in columnNames) {
|
---|
161 | j = 0;
|
---|
162 | data[i] = new double[groups[i].Count()];
|
---|
163 | foreach (IRun run in groups[i]) {
|
---|
164 | dt[j, i] = (double)((dynamic)run.Results[resultName]).Value;
|
---|
165 | data[i][j] = dt[j, i];
|
---|
166 | j++;
|
---|
167 | }
|
---|
168 | i++;
|
---|
169 | }
|
---|
170 |
|
---|
171 | stringConvertibleMatrixView.Content = dt;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | private List<IEnumerable<IRun>> GetGroups(string[] columnNames, IEnumerable<IRun> runs) {
|
---|
176 | List<IEnumerable<IRun>> runCols = new List<IEnumerable<IRun>>();
|
---|
177 | string parameterName = (string)groupComboBox.SelectedItem;
|
---|
178 |
|
---|
179 | foreach (string cn in columnNames) {
|
---|
180 | var tmpRuns = runs.Where(x => ((string)((dynamic)x.Parameters[parameterName]).Value.ToString()) == cn);
|
---|
181 | runCols.Add(tmpRuns);
|
---|
182 | }
|
---|
183 |
|
---|
184 | return runCols;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void ResetUI() {
|
---|
188 | normalityLabel.Image = null;
|
---|
189 | groupCompLabel.Image = null;
|
---|
190 | pValTextBox.Text = string.Empty;
|
---|
191 | equalDistsTextBox.Text = string.Empty;
|
---|
192 | }
|
---|
193 |
|
---|
194 | private void testButton_Click(object sender, EventArgs e) {
|
---|
195 | double pval = KruskalWallis.Test(data);
|
---|
196 | pValTextBox.Text = pval.ToString();
|
---|
197 | if (pval < 0.05) {
|
---|
198 | groupCompLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
|
---|
199 | } else {
|
---|
200 | groupCompLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | private void normalDistButton_Click(object sender, EventArgs e) {
|
---|
205 | double val;
|
---|
206 | List<double> res = new List<double>();
|
---|
207 |
|
---|
208 | for (int i = 0; i < data.Length; i++) {
|
---|
209 | alglib.jarqueberatest(data[i], data[i].Length, out val);
|
---|
210 | res.Add(val);
|
---|
211 | }
|
---|
212 |
|
---|
213 | for (int i = 0; i < res.Count(); i++) {
|
---|
214 | if (res[i] < 0.1) {
|
---|
215 | stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Red;
|
---|
216 | normalityLabel.Image = HeuristicLab.Common.Resources.VSImageLibrary.Warning;
|
---|
217 | } else {
|
---|
218 | stringConvertibleMatrixView.DataGridView.Columns[i].DefaultCellStyle.ForeColor = Color.Green;
|
---|
219 | normalityLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void resultComboBox_SelectedValueChanged(object sender, EventArgs e) {
|
---|
225 | RebuildDataTable();
|
---|
226 | ResetUI();
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void groupComboBox_SelectedValueChanged(object sender, EventArgs e) {
|
---|
230 | FillCompComboBox();
|
---|
231 | RebuildDataTable();
|
---|
232 | ResetUI();
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void normalityDetails_Click(object sender, EventArgs e) {
|
---|
236 | DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
|
---|
237 | pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
|
---|
238 | pValsMatrix.RowNames = new string[] { "p-Value" };
|
---|
239 |
|
---|
240 | double val;
|
---|
241 | for (int i = 0; i < data.Length; i++) {
|
---|
242 | alglib.jarqueberatest(data[i], data[i].Length, out val);
|
---|
243 | pValsMatrix[0, i] = val;
|
---|
244 | }
|
---|
245 |
|
---|
246 | MainFormManager.MainForm.ShowContent(pValsMatrix);
|
---|
247 | }
|
---|
248 |
|
---|
249 | private void pairwiseTestButton_Click(object sender, EventArgs e) {
|
---|
250 | string curItem = (string)groupCompComboBox.SelectedItem;
|
---|
251 | int colIndex = 0;
|
---|
252 |
|
---|
253 | foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
|
---|
254 | if (col == curItem) {
|
---|
255 | break;
|
---|
256 | }
|
---|
257 | colIndex++;
|
---|
258 | }
|
---|
259 |
|
---|
260 | DoubleMatrix pValsMatrix = new DoubleMatrix(5, stringConvertibleMatrixView.Content.Columns);
|
---|
261 | pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
|
---|
262 | pValsMatrix.RowNames = new string[] { "p-Value of MannWhitneyU", "p-Value of T-Test", "Necessary Sample Size for T-Test", "Cohen's d", "Hedges' g" };
|
---|
263 |
|
---|
264 | double mwuBothtails;
|
---|
265 | double mwuLefttail;
|
---|
266 | double mwuRighttail;
|
---|
267 | double ttestLefttail;
|
---|
268 | for (int i = 0; i < data.Length; i++) {
|
---|
269 | alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
|
---|
270 | ttestLefttail = TTest.Test(data[colIndex], data[i]);
|
---|
271 | pValsMatrix[0, i] = mwuBothtails;
|
---|
272 | pValsMatrix[1, i] = ttestLefttail;
|
---|
273 | pValsMatrix[2, i] = TTest.GetOptimalSampleSize(data[colIndex], data[i]);
|
---|
274 | pValsMatrix[3, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], data[i]);
|
---|
275 | pValsMatrix[4, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], data[i]);
|
---|
276 | }
|
---|
277 |
|
---|
278 | MainFormManager.MainForm.ShowContent(pValsMatrix);
|
---|
279 | }
|
---|
280 |
|
---|
281 | private void infoLabel_DoubleClick(object sender, EventArgs e) {
|
---|
282 | using (InfoBox dialog = new InfoBox("Description of statistical tests", typeof(StatisticalTestingView).Namespace + ".InfoResources.StatisticalTestsInfo.rtf")) {
|
---|
283 | dialog.ShowDialog(this);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
288 | RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
|
---|
289 | boxplotView.Content = Content;
|
---|
290 | // TODO: enable as soon as we move to HeuristicLab.Optimization.Views
|
---|
291 | // boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
|
---|
292 | // boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
|
---|
293 | boxplotView.Show();
|
---|
294 | }
|
---|
295 |
|
---|
296 | private void pairwiseCheckDataButton_Click(object sender, EventArgs e) {
|
---|
297 | string curItem = (string)groupCompComboBox.SelectedItem;
|
---|
298 | int colIndex = 0;
|
---|
299 |
|
---|
300 | foreach (string col in stringConvertibleMatrixView.Content.ColumnNames) {
|
---|
301 | if (col == curItem) {
|
---|
302 | break;
|
---|
303 | }
|
---|
304 | colIndex++;
|
---|
305 | }
|
---|
306 |
|
---|
307 | double mwuBothtails;
|
---|
308 | double mwuLefttail;
|
---|
309 | double mwuRighttail;
|
---|
310 | int cnt = 0;
|
---|
311 |
|
---|
312 | for (int i = 0; i < data.Length; i++) {
|
---|
313 | if (i != colIndex) {
|
---|
314 | alglib.mannwhitneyutest(data[colIndex], data[colIndex].Length, data[i], data[i].Length, out mwuBothtails, out mwuLefttail, out mwuRighttail);
|
---|
315 | if (mwuBothtails > 0.05) {
|
---|
316 | cnt++;
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | double ratio = ((double)cnt) / data.Length * 100.0;
|
---|
322 | equalDistsTextBox.Text = ratio.ToString() + " %";
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|