Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StatisticalAnalysis/3.2/MannWhitneyWilcoxonTestControl.cs @ 1548

Last change on this file since 1548 was 1548, checked in by abeham, 15 years ago

fixes a bug in MannWhitneyWilcoxonTest, regarding unsorted arrays (#573)
using a 2-tailed test is now properly used under the normal approximation method
some small enhancements to the control

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31
32namespace HeuristicLab.StatisticalAnalysis {
33  public partial class MannWhitneyWilcoxonTestControl : EditorBase {
34    public MannWhitneyWilcoxonTestControl()
35      : base() {
36      InitializeComponent();
37      alphaTextBox.Text = ((double)0.05).ToString();
38    }
39
40    private void ArrayToString<T>(T[] array, TextBox p) {
41      StringBuilder s = new StringBuilder();
42      foreach (T element in array)
43        s.Append(element + "; ");
44      s.Remove(s.Length - 2, 2);
45      p.Text = s.ToString();
46    }
47
48    private double[] ConvertStringToArray(TextBox p) {
49      string t = p.Text;
50      string[] s = t.Split(new char[] { ';', ' ', '\t' });
51      List<double> tmp = new List<double>();
52      for (int i = 0; i < s.Length; i++) {
53        try {
54          double val = double.Parse(s[i]);
55          tmp.Add(val);
56        }
57        catch (FormatException) { }
58      }
59      if (tmp.Count > 0) return tmp.ToArray();
60      else return null;
61    }
62
63    private void testApproximateButton_Click(object sender, EventArgs e) {
64      double[] p1 = ConvertStringToArray(p1TextBox);
65      double[] p2 = ConvertStringToArray(p2TextBox);
66      if (p1.Length < 10 || p2.Length < 10) {
67        MessageBox.Show("Sample size is too small to approximate, provide at least 10 samples in each population.");
68        return;
69      }
70      double alpha = Double.Parse(alphaTextBox.Text);
71      double pVal = MannWhitneyWilcoxonTest.TwoTailedTest(p1, p2);
72      if (pVal <= alpha) {
73        resultLabel.Text = "The hypothesis H0 can be rejected at " + alpha.ToString() + ", the p-Value is " + pVal.ToString() + "\nThe samples are likely not to stem from the same distribution.";
74      } else {
75        resultLabel.Text = "The hypothesis H0 cannot be rejected at " + alpha.ToString();
76      }
77    }
78
79    private void testExactButton_Click(object sender, EventArgs e) {
80      double[] p1 = ConvertStringToArray(p1TextBox);
81      double[] p2 = ConvertStringToArray(p2TextBox);
82      if (p1.Length > 20 || p2.Length > 20) {
83        double[] tmp = new double[Math.Min(20, p1.Length)];
84        for (int i = 0; i < Math.Min(20, p1.Length); i++) tmp[i] = p1[i];
85        p1 = tmp;
86        tmp = new double[Math.Min(20, p2.Length)];
87        for (int i = 0; i < Math.Min(20, p2.Length); i++) tmp[i] = p2[i];
88        p2 = tmp;
89        MessageBox.Show("Caution: Sample size is too large for exact calculation. Only the first 20 samples are used for the test!");
90      }
91      double alpha = Double.Parse(alphaTextBox.Text);
92      if (MannWhitneyWilcoxonTest.TwoTailedTest(p1, p2, alpha)) {
93        resultLabel.Text = "The hypothesis H0 can be rejected at " + alpha.ToString() + "\nThe samples are likely not to stem from the same distribution.";
94      } else {
95        resultLabel.Text = "The hypothesis H0 cannot be rejected at " + alpha.ToString();
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.