Changeset 1548


Ignore:
Timestamp:
04/10/09 10:45:35 (11 years ago)
Author:
abeham
Message:

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

Location:
trunk/sources/HeuristicLab.StatisticalAnalysis/3.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StatisticalAnalysis/3.2/MannWhitneyWilcoxonTest.cs

    r1530 r1548  
    5050    /// <returns>The p-value of the test</returns>
    5151    public static double TwoTailedTest(double[] p1, double[] p2) {
     52      Array.Sort<double>(p1);
     53      Array.Sort<double>(p2);
    5254      double rank = MannWhitneyWilcoxonTest.CalculateRankSumForP1(p1, p2);
    5355      return MannWhitneyWilcoxonTest.ApproximatePValue(rank, p1.Length, p2.Length, true);
     
    7072    /// <returns>True if H0 (p1 equals p2) can be rejected, False otherwise</returns>
    7173    public static bool TwoTailedTest(double[] p1, double[] p2, double alpha) {
     74      Array.Sort<double>(p1);
     75      Array.Sort<double>(p2);
    7276      return MannWhitneyWilcoxonTest.TwoTailedTest(p1, p2, alpha, 20);
    7377    }
     
    120124    /// <returns>The p-value of the test</returns>
    121125    public static double TwoTailedTest(int[] p1, int[] p2) {
     126      Array.Sort<int>(p1);
     127      Array.Sort<int>(p2);
    122128      double rank = MannWhitneyWilcoxonTest.CalculateRankSumForP1(p1, p2);
    123129      return MannWhitneyWilcoxonTest.ApproximatePValue(rank, p1.Length, p2.Length, true);
     
    181187      double U1 = rank - (double)(nRank * (nRank + 1) / 2);
    182188      double U2 = (double)(nRank * nOther) - U1;
    183       if (alpha <= 0.01) {
     189      if (alpha < 0.05) {
    184190        return (Math.Min(U1, U2) <= table001[nRank - 1, nOther - 1]);
    185       } else if (alpha <= 0.05) {
     191      } else if (alpha < 0.1) {
    186192        return (Math.Min(U1, U2) <= table005[nRank - 1, nOther - 1]);
    187       } else if (alpha <= 0.1) {
     193      } else if (Math.Abs(alpha - 0.1) < 1e-07) {
    188194        return (Math.Min(U1, U2) <= table01[nRank - 1, nOther - 1]);
    189195      } else throw new ArgumentException("ERROR in MannWhitneyWilcoxonTest: alpha must be <= 0.1");
     
    197203      double sigma = Math.Sqrt(nRank * nOther * (nRank + nOther + 1) / 12);
    198204      double z = (Math.Min(U1, U2) - mu) / sigma;
    199       return ProbabilityOfZValue(z);
     205      if (twoTailed) {
     206        return 2 * ProbabilityOfZValue(z);
     207      } else return ProbabilityOfZValue(z);
    200208    }
    201209
  • trunk/sources/HeuristicLab.StatisticalAnalysis/3.2/MannWhitneyWilcoxonTestControl.cs

    r1530 r1548  
    4848    private double[] ConvertStringToArray(TextBox p) {
    4949      string t = p.Text;
    50       string[] s = t.Split(new char[] { ';' });
    51       double[] tmp = new double[s.Length];
    52       try {
    53         for (int i = 0; i < s.Length; i++) {
    54           tmp[i] = double.Parse(s[i]);
     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);
    5556        }
     57        catch (FormatException) { }
    5658      }
    57       catch (FormatException) {
    58         return null;
    59       }
    60       return tmp;
     59      if (tmp.Count > 0) return tmp.ToArray();
     60      else return null;
    6161    }
    6262
     
    8181      double[] p2 = ConvertStringToArray(p2TextBox);
    8282      if (p1.Length > 20 || p2.Length > 20) {
    83         MessageBox.Show("Sample size is too large for exact calculation, do not use more than 20 samples in each population.");
    84         return;
     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!");
    8590      }
    8691      double alpha = Double.Parse(alphaTextBox.Text);
Note: See TracChangeset for help on using the changeset viewer.