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