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