#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Analysis.Statistics; using HeuristicLab.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Analysis.Tests { [TestClass] public class KruskalWallisUnitTests { //tests the sample data from R's Kruskal.Test() help page [TestMethod] [TestCategory("Analysis.Statistics")] [TestProperty("Time", "short")] public void KruskalWallisTest1() { double[][] data = new double[3][]; data[0] = new double[] { 2.9, 3.0, 2.5, 2.6, 3.2 }; data[1] = new double[] { 3.8, 2.7, 4.0, 2.4 }; data[2] = new double[] { 2.8, 3.4, 3.7, 2.2, 2.0 }; Assert.AreEqual(0.68, Math.Round(KruskalWallisTest.Test(data), 2)); } //compares groups of uniformly distributed random variables [TestMethod] [TestCategory("Analysis.Statistics")] [TestProperty("Time", "short")] public void KruskalWallisTest2() { double[][] data = new double[3][]; data[0] = new double[] { 977,3848,2803,4097,3155,2745,25,3384,2587,1332,1407,2842,4089,4083,608,3246,1634 ,3182,2856,1614,2627,1116,2659,1166,3299,2322,3681,2184,2470,4682,3305,2553,239,2847 ,2733,8,2773,3215,1345,1037,1760,2729,984,648,1875,4852,1002,4106,3934,4542,3718 ,67,4539,1862,1886,1299,4544,3463,4052,1934,728,2492,799,730,1896,468,4724,1329 ,510,3952,2392,2812,330,94,1726,2542,926,1472,4014,812,1243,2274,2394,3120,1745 ,2597,4331,2562,4814,239,1194,4843,4577,3277,3482,1381,4697,3878,500,2182}; data[1] = new double[] { 4106,3279,4464,1460,1023,4459,4723,2934,1818,26,2539,1816,2246,1185,1266,1137,1921 ,1340,4655,4636,578,2970,4208,2658,110,1063,2081,1735,3308,3900,1483,929,2214,2810 ,4568,327,3046,1085,4585,535,3303,1511,213,491,492,167,1520,1469,122,2514,2206 ,2218,974,1480,3959,1158,2753,1273,3870,2356,4707,492,3606,562,445,866,3714,1603 ,3139,4941,3824,1124,2130,3701,3437,1771,2837,1,3800,3465,3407,4281,444,363,2926 ,2353,4429,3357,4834,1039,4238,139,3050,4099,2003,4253,2004,3849,4321,380 }; data[2] = new double[] { 233,1577,198,2311,2281,1957,737,3508,1974,1819,3778,1237,3497,616,1286,1901,3114 ,3982,1756,1058,2874,2401,1921,3146,1313,1604,621,2365,46,3108,2714,81,4367,853 ,1387,73,4132,2052,3440,2814,520,4434,4183,4055,1655,4417,3845,2389,1500,3029,3733 ,3834,3681,1283,2875,1752,1258,279,1302,4301,4402,229,2256,3490,977,3615,1293,4345 ,2005,298,2962,2118,3379,3121,4237,4646,1186,568,2181,2843,4721,1609,224,391,1893 ,3605,4162,493,330,4595,2699,4457,3981,3499,1773,653,3176,134,3205,4537 }; Assert.AreEqual(0.95, Math.Round(KruskalWallisTest.Test(data), 2)); } //compares groups of normally distributed random variables [TestMethod] [TestCategory("Analysis.Statistics")] [TestProperty("Time", "short")] public void KruskalWallisTest3() { double[][] data = new double[3][]; data[0] = new double[] { 0.264589812,0.161585300,0.890091755,0.342488339,-1.596719857,0.120133657 ,2.241639009,0.085758340,-0.857294600,1.677233367,1.256788739,-1.094681878 ,1.243547091,-0.420561525,-0.762303565,0.108380241,-1.583143952,-0.327616045 ,1.848906992,2.265051780,-0.101968019,0.958290323,0.531773709,0.923724862 ,-0.345119203,1.502269741,-0.441750726,-0.041689146,-0.794916891,-1.407491978 ,0.260857731,-0.583565945,-0.246297801,0.289167991,1.475766373,0.089479435 ,-0.419836298,0.929193796,1.321124907,0.984626214,-1.106529215,1.893772460 ,-0.116651087,-0.855576968,-0.235878058,0.122260772,0.505024585,0.557638701 ,1.042759510,-0.301076354,0.786736923,0.768976235,-0.004049338,0.577436400 ,1.281303178,0.081638643,0.323528409,-0.037552073,0.700950894,0.051583445 ,-0.002369374,1.025067100,-0.829806908,0.759951587,-0.126659012,0.139548228 ,1.776592281,-1.047810226,0.002268369,1.706443821,0.491150412,0.785840754 ,-0.108426124,-0.364622100,-1.863405685,0.475659013,1.410155770,1.619192440 ,0.145232489,0.390564220,0.078523202,1.246437519,-1.851728231,-0.702955879 }; data[1] = new double[] { 0.88684045,-1.64580307,1.35554664,0.99774740,-0.28673422,0.07218712,-0.57748503 ,0.46563867,0.18205386,0.37696309,-0.67810720,-0.22694641,0.04085999,-0.01930002 ,-1.14389989,-0.04909415,-0.05459455,-0.17639915,-0.16265625,0.07556000,-0.44700873 ,-0.91181687,0.68196982,1.24826347,0.43507746,-0.09482412,0.78624477,0.34046540 ,-0.62211625,-1.12255940,-0.63737143,0.68804212,-0.71520904,-1.53110842,-1.05479994 ,-1.24221848,-0.45931126,-0.45559477,1.76679857,-0.77232782,1.13517674,-1.39495738 ,-0.34197961,0.30465514,-0.53298790,0.50683810,-0.58359581,-0.25960696,-1.22998083 ,0.65792853,1.26266806,1.74247915,0.58659782,0.30592475,-0.18126620,1.34782875 ,0.15637320,0.37607185,-0.68933176,-1.24838659,0.65059380,-0.81253053,-1.69024871 ,0.07260499,-1.63974258,0.16926860,1.57151501,-1.59405343,-0.07934909,-0.02262132 ,-0.77291344,0.59094265,-0.27438763,-0.95374866,-0.22790924,-0.21198915,0.68324522 ,-0.23775701,-1.16306273,-1.33217708,-0.11785916,0.85999469,0.01001755,-0.01307264 }; data[2] = new double[] { 1.211025191,0.958893913,-0.195330882,-2.169217166,0.001336114,1.216295531 ,-0.973521915,0.365480774,-0.489803183,-1.673553315,2.003532021,1.423367551 ,0.092679901,-1.491714972,-1.565832859,0.689337215,-1.204939599,0.598992068 ,-1.261605940,0.890733253,3.334496751,0.817455786,0.441394621,-2.374263091 ,-0.635907080,-1.283172539,-0.130218566,-0.169552228,0.177057474,-0.398655531 ,1.151068075,1.700511574,-0.193563851,-0.970337200,-1.299506394,0.571352533 ,0.304304277,-0.962776922,-0.160333430,0.727834694,-0.175105398,-2.029508526 ,1.491111387,-1.584083346,0.662427657,1.500680474,0.820281659,-0.707746044 ,-0.180426669,-1.259842785,0.050592502,0.476649763,0.368184886,1.482703173 ,1.863034189,-0.833241981,1.602468694,0.155115911,-2.463551102,2.045183510 ,-0.078774687,0.463642193,1.263807207,-0.635996999,-0.563708248,0.475730571 ,-0.162671474,-0.682790861,-0.573510544,0.077701772,0.751698338,0.408727821 ,-0.374242494,-0.002329019,-0.330893667,-0.488596854,-0.697768835,-1.356949810 ,-0.947125493,2.087444922,0.271276104,-1.027604792,-0.364097727,-0.741089219}; Assert.AreEqual(0.0253, Math.Round(KruskalWallisTest.Test(data), 4)); } //compares groups of normally and uniformly distributed random variables [TestMethod] [TestCategory("Analysis.Statistics")] [TestProperty("Time", "short")] public void KruskalWallisTest4() { double[][] data = new double[3][]; data[0] = new double[] { 977,3848,2803,4097,3155,2745,25,3384,2587,1332,1407,2842,4089,4083,608,3246,1634 ,3182,2856,1614,2627,1116,2659,1166,3299,2322,3681,2184,2470,4682,3305,2553,239,2847 ,2733,8,2773,3215,1345,1037,1760,2729,984,648,1875,4852,1002,4106,3934,4542,3718 ,67,4539,1862,1886,1299,4544,3463,4052,1934,728,2492,799,730,1896,468,4724,1329 ,510,3952,2392,2812,330,94,1726,2542,926,1472,4014,812,1243,2274,2394,3120,1745 ,2597,4331,2562,4814,239,1194,4843,4577,3277,3482,1381,4697,3878,500,2182}; data[1] = new double[] { 0.88684045,-1.64580307,1.35554664,0.99774740,-0.28673422,0.07218712,-0.57748503 ,0.46563867,0.18205386,0.37696309,-0.67810720,-0.22694641,0.04085999,-0.01930002 ,-1.14389989,-0.04909415,-0.05459455,-0.17639915,-0.16265625,0.07556000,-0.44700873 ,-0.91181687,0.68196982,1.24826347,0.43507746,-0.09482412,0.78624477,0.34046540 ,-0.62211625,-1.12255940,-0.63737143,0.68804212,-0.71520904,-1.53110842,-1.05479994 ,-1.24221848,-0.45931126,-0.45559477,1.76679857,-0.77232782,1.13517674,-1.39495738 ,-0.34197961,0.30465514,-0.53298790,0.50683810,-0.58359581,-0.25960696,-1.22998083 ,0.65792853,1.26266806,1.74247915,0.58659782,0.30592475,-0.18126620,1.34782875 ,0.15637320,0.37607185,-0.68933176,-1.24838659,0.65059380,-0.81253053,-1.69024871 ,0.07260499,-1.63974258,0.16926860,1.57151501,-1.59405343,-0.07934909,-0.02262132 ,-0.77291344,0.59094265,-0.27438763,-0.95374866,-0.22790924,-0.21198915,0.68324522 ,-0.23775701,-1.16306273,-1.33217708,-0.11785916,0.85999469,0.01001755,-0.01307264 }; data[2] = new double[] { 1.211025191,0.958893913,-0.195330882,-2.169217166,0.001336114,1.216295531 ,-0.973521915,0.365480774,-0.489803183,-1.673553315,2.003532021,1.423367551 ,0.092679901,-1.491714972,-1.565832859,0.689337215,-1.204939599,0.598992068 ,-1.261605940,0.890733253,3.334496751,0.817455786,0.441394621,-2.374263091 ,-0.635907080,-1.283172539,-0.130218566,-0.169552228,0.177057474,-0.398655531 ,1.151068075,1.700511574,-0.193563851,-0.970337200,-1.299506394,0.571352533 ,0.304304277,-0.962776922,-0.160333430,0.727834694,-0.175105398,-2.029508526 ,1.491111387,-1.584083346,0.662427657,1.500680474,0.820281659,-0.707746044 ,-0.180426669,-1.259842785,0.050592502,0.476649763,0.368184886,1.482703173 ,1.863034189,-0.833241981,1.602468694,0.155115911,-2.463551102,2.045183510 ,-0.078774687,0.463642193,1.263807207,-0.635996999,-0.563708248,0.475730571 ,-0.162671474,-0.682790861,-0.573510544,0.077701772,0.751698338,0.408727821 ,-0.374242494,-0.002329019,-0.330893667,-0.488596854,-0.697768835,-1.356949810 ,-0.947125493,2.087444922,0.271276104,-1.027604792,-0.364097727,-0.741089219}; var result = KruskalWallisTest.Test(data); Assert.IsTrue(result.IsAlmost(2.2E-16)); } //compares groups containing the same random variables [TestMethod] [TestCategory("Analysis.Statistics")] [TestProperty("Time", "short")] public void KruskalWallisTest5() { double[][] data = new double[4][]; data[0] = new double[] { 1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28 ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758}; data[1] = new double[] { 1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28 ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758}; data[2] = new double[] {1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28 ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758 }; data[3] = new double[] {1477,764,4731,1048,822,974,3302,2258,1946,3067,974,206,2439,3022,2714,2559,28 ,2654,4588,3363,4750,1440,1892,3629,816,4317,34,2119,3290,1758 }; Assert.AreEqual(1, Math.Round(KruskalWallisTest.Test(data), 2)); } } }