Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StatisticalTesting/Statistics.UnitTests/KruskalWallisUnitTests.cs @ 10736

Last change on this file since 10736 was 9721, checked in by ascheibe, 11 years ago

#2031 added unit tests for the Kruskal Wallis test

File size: 12.9 KB
Line 
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
22using System;
23using HeuristicLab.Analysis.Statistics;
24using HeuristicLab.Common;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26
27namespace Statistics.UnitTests {
28  [TestClass]
29  public class KruskalWallisUnitTests {
30    //tests the sample data from R's Kruskal.Test() help page
31    [TestMethod]
32    public void KruskalWallisTest1() {
33      double[][] data = new double[3][];
34      data[0] = new double[] { 2.9, 3.0, 2.5, 2.6, 3.2 };
35      data[1] = new double[] { 3.8, 2.7, 4.0, 2.4 };
36      data[2] = new double[] { 2.8, 3.4, 3.7, 2.2, 2.0 };
37
38      Assert.AreEqual(0.68, Math.Round(KruskalWallis.Test(data), 2));
39    }
40
41    //compares groups of uniformly distributed random variables
42    [TestMethod]
43    public void KruskalWallisTest2() {
44      double[][] data = new double[3][];
45      data[0] = new double[] { 977,3848,2803,4097,3155,2745,25,3384,2587,1332,1407,2842,4089,4083,608,3246,1634
46                              ,3182,2856,1614,2627,1116,2659,1166,3299,2322,3681,2184,2470,4682,3305,2553,239,2847
47                              ,2733,8,2773,3215,1345,1037,1760,2729,984,648,1875,4852,1002,4106,3934,4542,3718
48                              ,67,4539,1862,1886,1299,4544,3463,4052,1934,728,2492,799,730,1896,468,4724,1329
49                              ,510,3952,2392,2812,330,94,1726,2542,926,1472,4014,812,1243,2274,2394,3120,1745
50                              ,2597,4331,2562,4814,239,1194,4843,4577,3277,3482,1381,4697,3878,500,2182};
51      data[1] = new double[] { 4106,3279,4464,1460,1023,4459,4723,2934,1818,26,2539,1816,2246,1185,1266,1137,1921
52                              ,1340,4655,4636,578,2970,4208,2658,110,1063,2081,1735,3308,3900,1483,929,2214,2810
53                              ,4568,327,3046,1085,4585,535,3303,1511,213,491,492,167,1520,1469,122,2514,2206
54                              ,2218,974,1480,3959,1158,2753,1273,3870,2356,4707,492,3606,562,445,866,3714,1603
55                              ,3139,4941,3824,1124,2130,3701,3437,1771,2837,1,3800,3465,3407,4281,444,363,2926
56                              ,2353,4429,3357,4834,1039,4238,139,3050,4099,2003,4253,2004,3849,4321,380 };
57      data[2] = new double[] { 233,1577,198,2311,2281,1957,737,3508,1974,1819,3778,1237,3497,616,1286,1901,3114
58                              ,3982,1756,1058,2874,2401,1921,3146,1313,1604,621,2365,46,3108,2714,81,4367,853
59                              ,1387,73,4132,2052,3440,2814,520,4434,4183,4055,1655,4417,3845,2389,1500,3029,3733
60                              ,3834,3681,1283,2875,1752,1258,279,1302,4301,4402,229,2256,3490,977,3615,1293,4345
61                              ,2005,298,2962,2118,3379,3121,4237,4646,1186,568,2181,2843,4721,1609,224,391,1893
62                              ,3605,4162,493,330,4595,2699,4457,3981,3499,1773,653,3176,134,3205,4537 };
63
64      Assert.AreEqual(0.95, Math.Round(KruskalWallis.Test(data), 2));
65    }
66
67    //compares groups of normally distributed random variables
68    [TestMethod]
69    public void KruskalWallisTest3() {
70      double[][] data = new double[3][];
71      data[0] = new double[] { 0.264589812,0.161585300,0.890091755,0.342488339,-1.596719857,0.120133657
72                              ,2.241639009,0.085758340,-0.857294600,1.677233367,1.256788739,-1.094681878
73                              ,1.243547091,-0.420561525,-0.762303565,0.108380241,-1.583143952,-0.327616045
74                              ,1.848906992,2.265051780,-0.101968019,0.958290323,0.531773709,0.923724862
75                              ,-0.345119203,1.502269741,-0.441750726,-0.041689146,-0.794916891,-1.407491978
76                              ,0.260857731,-0.583565945,-0.246297801,0.289167991,1.475766373,0.089479435
77                              ,-0.419836298,0.929193796,1.321124907,0.984626214,-1.106529215,1.893772460
78                              ,-0.116651087,-0.855576968,-0.235878058,0.122260772,0.505024585,0.557638701
79                              ,1.042759510,-0.301076354,0.786736923,0.768976235,-0.004049338,0.577436400
80                              ,1.281303178,0.081638643,0.323528409,-0.037552073,0.700950894,0.051583445
81                              ,-0.002369374,1.025067100,-0.829806908,0.759951587,-0.126659012,0.139548228
82                              ,1.776592281,-1.047810226,0.002268369,1.706443821,0.491150412,0.785840754
83                              ,-0.108426124,-0.364622100,-1.863405685,0.475659013,1.410155770,1.619192440
84                              ,0.145232489,0.390564220,0.078523202,1.246437519,-1.851728231,-0.702955879 };
85      data[1] = new double[] { 0.88684045,-1.64580307,1.35554664,0.99774740,-0.28673422,0.07218712,-0.57748503
86                              ,0.46563867,0.18205386,0.37696309,-0.67810720,-0.22694641,0.04085999,-0.01930002
87                              ,-1.14389989,-0.04909415,-0.05459455,-0.17639915,-0.16265625,0.07556000,-0.44700873
88                              ,-0.91181687,0.68196982,1.24826347,0.43507746,-0.09482412,0.78624477,0.34046540
89                              ,-0.62211625,-1.12255940,-0.63737143,0.68804212,-0.71520904,-1.53110842,-1.05479994
90                              ,-1.24221848,-0.45931126,-0.45559477,1.76679857,-0.77232782,1.13517674,-1.39495738
91                              ,-0.34197961,0.30465514,-0.53298790,0.50683810,-0.58359581,-0.25960696,-1.22998083
92                              ,0.65792853,1.26266806,1.74247915,0.58659782,0.30592475,-0.18126620,1.34782875
93                              ,0.15637320,0.37607185,-0.68933176,-1.24838659,0.65059380,-0.81253053,-1.69024871
94                              ,0.07260499,-1.63974258,0.16926860,1.57151501,-1.59405343,-0.07934909,-0.02262132
95                              ,-0.77291344,0.59094265,-0.27438763,-0.95374866,-0.22790924,-0.21198915,0.68324522
96                              ,-0.23775701,-1.16306273,-1.33217708,-0.11785916,0.85999469,0.01001755,-0.01307264 };
97      data[2] = new double[] { 1.211025191,0.958893913,-0.195330882,-2.169217166,0.001336114,1.216295531
98                              ,-0.973521915,0.365480774,-0.489803183,-1.673553315,2.003532021,1.423367551
99                              ,0.092679901,-1.491714972,-1.565832859,0.689337215,-1.204939599,0.598992068
100                              ,-1.261605940,0.890733253,3.334496751,0.817455786,0.441394621,-2.374263091
101                              ,-0.635907080,-1.283172539,-0.130218566,-0.169552228,0.177057474,-0.398655531
102                              ,1.151068075,1.700511574,-0.193563851,-0.970337200,-1.299506394,0.571352533
103                              ,0.304304277,-0.962776922,-0.160333430,0.727834694,-0.175105398,-2.029508526
104                              ,1.491111387,-1.584083346,0.662427657,1.500680474,0.820281659,-0.707746044
105                              ,-0.180426669,-1.259842785,0.050592502,0.476649763,0.368184886,1.482703173
106                              ,1.863034189,-0.833241981,1.602468694,0.155115911,-2.463551102,2.045183510
107                              ,-0.078774687,0.463642193,1.263807207,-0.635996999,-0.563708248,0.475730571
108                              ,-0.162671474,-0.682790861,-0.573510544,0.077701772,0.751698338,0.408727821
109                              ,-0.374242494,-0.002329019,-0.330893667,-0.488596854,-0.697768835,-1.356949810
110                              ,-0.947125493,2.087444922,0.271276104,-1.027604792,-0.364097727,-0.741089219};
111
112      Assert.AreEqual(0.0253, Math.Round(KruskalWallis.Test(data), 4));
113    }
114
115    //compares groups of normally and uniformly distributed random variables
116    [TestMethod]
117    public void KruskalWallisTest4() {
118      double[][] data = new double[3][];
119      data[0] = new double[] { 977,3848,2803,4097,3155,2745,25,3384,2587,1332,1407,2842,4089,4083,608,3246,1634
120                              ,3182,2856,1614,2627,1116,2659,1166,3299,2322,3681,2184,2470,4682,3305,2553,239,2847
121                              ,2733,8,2773,3215,1345,1037,1760,2729,984,648,1875,4852,1002,4106,3934,4542,3718
122                              ,67,4539,1862,1886,1299,4544,3463,4052,1934,728,2492,799,730,1896,468,4724,1329
123                              ,510,3952,2392,2812,330,94,1726,2542,926,1472,4014,812,1243,2274,2394,3120,1745
124                              ,2597,4331,2562,4814,239,1194,4843,4577,3277,3482,1381,4697,3878,500,2182};
125      data[1] = new double[] { 0.88684045,-1.64580307,1.35554664,0.99774740,-0.28673422,0.07218712,-0.57748503
126                              ,0.46563867,0.18205386,0.37696309,-0.67810720,-0.22694641,0.04085999,-0.01930002
127                              ,-1.14389989,-0.04909415,-0.05459455,-0.17639915,-0.16265625,0.07556000,-0.44700873
128                              ,-0.91181687,0.68196982,1.24826347,0.43507746,-0.09482412,0.78624477,0.34046540
129                              ,-0.62211625,-1.12255940,-0.63737143,0.68804212,-0.71520904,-1.53110842,-1.05479994
130                              ,-1.24221848,-0.45931126,-0.45559477,1.76679857,-0.77232782,1.13517674,-1.39495738
131                              ,-0.34197961,0.30465514,-0.53298790,0.50683810,-0.58359581,-0.25960696,-1.22998083
132                              ,0.65792853,1.26266806,1.74247915,0.58659782,0.30592475,-0.18126620,1.34782875
133                              ,0.15637320,0.37607185,-0.68933176,-1.24838659,0.65059380,-0.81253053,-1.69024871
134                              ,0.07260499,-1.63974258,0.16926860,1.57151501,-1.59405343,-0.07934909,-0.02262132
135                              ,-0.77291344,0.59094265,-0.27438763,-0.95374866,-0.22790924,-0.21198915,0.68324522
136                              ,-0.23775701,-1.16306273,-1.33217708,-0.11785916,0.85999469,0.01001755,-0.01307264 };
137      data[2] = new double[] { 1.211025191,0.958893913,-0.195330882,-2.169217166,0.001336114,1.216295531
138                              ,-0.973521915,0.365480774,-0.489803183,-1.673553315,2.003532021,1.423367551
139                              ,0.092679901,-1.491714972,-1.565832859,0.689337215,-1.204939599,0.598992068
140                              ,-1.261605940,0.890733253,3.334496751,0.817455786,0.441394621,-2.374263091
141                              ,-0.635907080,-1.283172539,-0.130218566,-0.169552228,0.177057474,-0.398655531
142                              ,1.151068075,1.700511574,-0.193563851,-0.970337200,-1.299506394,0.571352533
143                              ,0.304304277,-0.962776922,-0.160333430,0.727834694,-0.175105398,-2.029508526
144                              ,1.491111387,-1.584083346,0.662427657,1.500680474,0.820281659,-0.707746044
145                              ,-0.180426669,-1.259842785,0.050592502,0.476649763,0.368184886,1.482703173
146                              ,1.863034189,-0.833241981,1.602468694,0.155115911,-2.463551102,2.045183510
147                              ,-0.078774687,0.463642193,1.263807207,-0.635996999,-0.563708248,0.475730571
148                              ,-0.162671474,-0.682790861,-0.573510544,0.077701772,0.751698338,0.408727821
149                              ,-0.374242494,-0.002329019,-0.330893667,-0.488596854,-0.697768835,-1.356949810
150                              ,-0.947125493,2.087444922,0.271276104,-1.027604792,-0.364097727,-0.741089219};
151
152      var result = KruskalWallis.Test(data);
153      Assert.IsTrue(result.IsAlmost(2.2E-16));
154    }
155
156    //compares groups containing the same random variables
157    [TestMethod]
158    public void KruskalWallisTest5() {
159      double[][] data = new double[4][];
160      data[0] = new double[] { 1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28
161                              ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758};
162      data[1] = new double[] { 1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28
163                              ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758};
164      data[2] = new double[] {1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28
165                              ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758 };
166      data[3] = new double[] {1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28
167                              ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758 };
168
169      Assert.AreEqual(1, Math.Round(KruskalWallis.Test(data), 2));
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.