OutlierTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. namespace MathPHP\Tests\Statistics;
  3. use MathPHP\Exception;
  4. use MathPHP\Statistics\Outlier;
  5. class OutlierTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test Grubbs' statistic for two-sided test
  9. * @dataProvider dataProviderForGrubbsStatisticTwoSided
  10. * @param array $data
  11. * @param float $expectedG
  12. * @throws \Exception
  13. */
  14. public function testGrubbsStatisticTwoSided(array $data, float $expectedG)
  15. {
  16. // When
  17. $G = Outlier::grubbsStatistic($data, 'two');
  18. // Then
  19. $this->assertEqualsWithDelta($expectedG, $G, 0.0001);
  20. }
  21. /**
  22. * Calculated with R outliers grubbs.test(z, type=10, two.sided=TRUE)
  23. * @return array
  24. */
  25. public function dataProviderForGrubbsStatisticTwoSided(): array
  26. {
  27. return [
  28. [
  29. [199.31, 199.53, 200.19, 200.82, 201.92, 201.95, 202.18, 245.57],
  30. 2.4688000,
  31. ],
  32. [
  33. [145, 125, 190, 135, 220, 130, 210, 3, 165, 165, 150],
  34. 2.52390,
  35. ],
  36. [
  37. [1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7, 46, -48],
  38. 3.04240,
  39. ],
  40. ];
  41. }
  42. /**
  43. * @test Grubbs' statistic for one-sided lower test
  44. * @dataProvider dataProviderForGrubbsStatisticOneSidedLower
  45. * @param array $data
  46. * @param float $expectedG
  47. * @throws \Exception
  48. */
  49. public function testGrubbsStatisticOneSidedLower(array $data, float $expectedG)
  50. {
  51. // When
  52. $G = Outlier::grubbsStatistic($data, 'lower');
  53. // Then
  54. $this->assertEqualsWithDelta($expectedG, $G, 0.0001);
  55. }
  56. /**
  57. * Calculated by (μ - min) / sd
  58. * @return array
  59. */
  60. public function dataProviderForGrubbsStatisticOneSidedLower(): array
  61. {
  62. return [
  63. 'min 199.31, mean 206.4338, sd 15.85256' => [
  64. [199.31, 199.53, 200.19, 200.82, 201.92, 201.95, 202.18, 245.57],
  65. 0.449378523090277,
  66. ],
  67. 'min 3, mean 148.9091, sd 57.81082' => [
  68. [145, 125, 190, 135, 220, 130, 210, 3, 165, 165, 150],
  69. 2.523906424437501,
  70. ],
  71. 'min -48, mean 3, sd 16.76305' => [
  72. [1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7, 46, -48],
  73. 3.042405767446855,
  74. ],
  75. ];
  76. }
  77. /**
  78. * @test Grubbs' statistic for one-sided upper test
  79. * @dataProvider dataProviderForGrubbsStatisticOneSidedUpper
  80. * @param array $data
  81. * @param float $expectedG
  82. * @throws \Exception
  83. */
  84. public function testGrubbsStatisticOneSidedUpper(array $data, float $expectedG)
  85. {
  86. // When
  87. $G = Outlier::grubbsStatistic($data, 'upper');
  88. // Then
  89. $this->assertEqualsWithDelta($expectedG, $G, 0.0001);
  90. }
  91. /**
  92. * cakcykated by (max - μ) / sd
  93. * @return array
  94. */
  95. public function dataProviderForGrubbsStatisticOneSidedUpper(): array
  96. {
  97. return [
  98. 'max 245.57, mean 206.4338, sd 15.85256' => [
  99. [199.31, 199.53, 200.19, 200.82, 201.92, 201.95, 202.18, 245.57],
  100. 2.468762143149119,
  101. ],
  102. 'max 220, mean 148.9091, sd 57.81082' => [
  103. [145, 125, 190, 135, 220, 130, 210, 3, 165, 165, 150],
  104. 1.229716167319543,
  105. ],
  106. 'max 46, mean 3, sd 16.76305' => [
  107. [1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7, 46, -48],
  108. 2.56520,
  109. ],
  110. ];
  111. }
  112. /**
  113. * @test Critical value for two-sided test
  114. * @dataProvider dataProviderForCriticalValueOneSided
  115. * @param float $𝛼
  116. * @param int $n
  117. * @param float $expectedCriticalValue
  118. * @throws Exception\BadParameterException
  119. */
  120. public function testCriticalGrubsOneSided(float $𝛼, int $n, float $expectedCriticalValue)
  121. {
  122. // Given
  123. $oneSided = Outlier::ONE_SIDED;
  124. // When
  125. $criticalValue = Outlier::grubbsCriticalValue($𝛼, $n, $oneSided);
  126. // Then
  127. $this->assertEqualsWithDelta($expectedCriticalValue, $criticalValue, 0.001);
  128. }
  129. /**
  130. * Reference table: http://www.statistics4u.com/fundstat_eng/ee_grubbs_outliertest.html
  131. * @return array (𝛼, n, critical value)
  132. * @todo [0.05, 400, 3.6339], [0.05, 500, 3.6952], [0.05, 600, 3.7442]
  133. * @todo [0.01, 400, 4.0166], [0.01, 500, 4.0749], [0.01, 600, 4.1214],
  134. */
  135. public function dataProviderForCriticalValueOneSided(): array
  136. {
  137. return [
  138. // 𝛼 = 0.05
  139. [0.05, 3, 1.1531],
  140. [0.05, 4, 1.4625],
  141. [0.05, 5, 1.6714],
  142. [0.05, 6, 1.8221],
  143. [0.05, 7, 1.9381],
  144. [0.05, 8, 2.0317],
  145. [0.05, 9, 2.1096],
  146. [0.05, 10, 2.1761],
  147. [0.05, 11, 2.2339],
  148. [0.05, 12, 2.2850],
  149. [0.05, 13, 2.3305],
  150. [0.05, 14, 2.3717],
  151. [0.05, 15, 2.4090],
  152. [0.05, 16, 2.4433],
  153. [0.05, 17, 2.4748],
  154. [0.05, 18, 2.5040],
  155. [0.05, 19, 2.5312],
  156. [0.05, 20, 2.5566],
  157. [0.05, 25, 2.6629],
  158. [0.05, 30, 2.7451],
  159. [0.05, 40, 2.8675],
  160. [0.05, 50, 2.9570],
  161. [0.05, 60, 3.0269],
  162. [0.05, 70, 3.0839],
  163. [0.05, 80, 3.1319],
  164. [0.05, 90, 3.1733],
  165. [0.05, 100, 3.2095],
  166. [0.05, 120, 3.2706],
  167. [0.05, 140, 3.3208],
  168. [0.05, 160, 3.3633],
  169. [0.05, 180, 3.4001],
  170. [0.05, 200, 3.4324],
  171. [0.05, 300, 3.5525],
  172. // 𝛼 = 0.01
  173. [0.01, 3, 1.1546],
  174. [0.01, 4, 1.4925],
  175. [0.01, 5, 1.7489],
  176. [0.01, 6, 1.9442],
  177. [0.01, 7, 2.0973],
  178. [0.01, 8, 2.2208],
  179. [0.01, 9, 2.3231],
  180. [0.01, 10, 2.4097],
  181. [0.01, 11, 2.4843],
  182. [0.01, 12, 2.5494],
  183. [0.01, 13, 2.6070],
  184. [0.01, 14, 2.6585],
  185. [0.01, 15, 2.7049],
  186. [0.01, 16, 2.7470],
  187. [0.01, 17, 2.7854],
  188. [0.01, 18, 2.8208],
  189. [0.01, 19, 2.8535],
  190. [0.01, 20, 2.8838],
  191. [0.01, 25, 3.0086],
  192. [0.01, 30, 3.1029],
  193. [0.01, 40, 3.2395],
  194. [0.01, 50, 3.3366],
  195. [0.01, 60, 3.4111],
  196. [0.01, 70, 3.4710],
  197. [0.01, 80, 3.5208],
  198. [0.01, 90, 3.5632],
  199. [0.01, 100, 3.6002],
  200. [0.01, 120, 3.6619],
  201. [0.01, 140, 3.7121],
  202. [0.01, 160, 3.7542],
  203. [0.01, 180, 3.7904],
  204. [0.01, 200, 3.8220],
  205. [0.01, 300, 3.9385],
  206. ];
  207. }
  208. /**
  209. * @test Critical value for two-sided test
  210. * @dataProvider dataProviderForCriticalValueTwoSided
  211. * @param float $𝛼
  212. * @param int $n
  213. * @param float $expectedCriticalValue
  214. * @throws Exception\BadParameterException
  215. */
  216. public function testCriticalGrubsTwoSided(float $𝛼, int $n, float $expectedCriticalValue)
  217. {
  218. // Given
  219. $twoSided = Outlier::TWO_SIDED;
  220. // When
  221. $criticalValue = Outlier::grubbsCriticalValue($𝛼, $n, $twoSided);
  222. // Then
  223. $this->assertEqualsWithDelta($expectedCriticalValue, $criticalValue, 0.001);
  224. }
  225. /**
  226. * Reference table: http://www.statistics4u.com/fundstat_eng/ee_grubbs_outliertest.html
  227. * @return array (𝛼, n, critical value)
  228. * @todo [0.05, 400, 3.8032], [0.05, 500, 3.8631], [0.05, 600, 3.9109]
  229. * @todo [0.01, 400, 4.1707], [0.01, 500, 4.2283], [0.01, 600, 4.2740]
  230. */
  231. public function dataProviderForCriticalValueTwoSided(): array
  232. {
  233. return [
  234. // 𝛼 = 0.05
  235. [0.05, 3, 1.1543],
  236. [0.05, 4, 1.4812],
  237. [0.05, 5, 1.7150],
  238. [0.05, 6, 1.8871],
  239. [0.05, 7, 2.0200],
  240. [0.05, 8, 2.1266],
  241. [0.05, 9, 2.2150],
  242. [0.05, 10, 2.2900],
  243. [0.05, 11, 2.3547],
  244. [0.05, 12, 2.4116],
  245. [0.05, 13, 2.4620],
  246. [0.05, 14, 2.5073],
  247. [0.05, 15, 2.5483],
  248. [0.05, 16, 2.5857],
  249. [0.05, 17, 2.6200],
  250. [0.05, 18, 2.6516],
  251. [0.05, 19, 2.6809],
  252. [0.05, 20, 2.7082],
  253. [0.05, 25, 2.8217],
  254. [0.05, 30, 2.9085],
  255. [0.05, 40, 3.0361],
  256. [0.05, 50, 3.1282],
  257. [0.05, 60, 3.1997],
  258. [0.05, 70, 3.2576],
  259. [0.05, 80, 3.3061],
  260. [0.05, 90, 3.3477],
  261. [0.05, 100, 3.3841],
  262. [0.05, 120, 3.4451],
  263. [0.05, 140, 3.4951],
  264. [0.05, 160, 3.5373],
  265. [0.05, 180, 3.5736],
  266. [0.05, 200, 3.6055],
  267. [0.05, 300, 3.7236],
  268. // 𝛼 = 0.01
  269. [0.01, 3, 1.1547],
  270. [0.01, 4, 1.4962],
  271. [0.01, 5, 1.7637],
  272. [0.01, 6, 1.9728],
  273. [0.01, 7, 2.1391],
  274. [0.01, 8, 2.2744],
  275. [0.01, 9, 2.3868],
  276. [0.01, 10, 2.4821],
  277. [0.01, 11, 2.5641],
  278. [0.01, 12, 2.6357],
  279. [0.01, 13, 2.6990],
  280. [0.01, 14, 2.7554],
  281. [0.01, 15, 2.8061],
  282. [0.01, 16, 2.8521],
  283. [0.01, 17, 2.8940],
  284. [0.01, 18, 2.9325],
  285. [0.01, 19, 2.9680],
  286. [0.01, 20, 3.0008],
  287. [0.01, 25, 3.1353],
  288. [0.01, 30, 3.2361],
  289. [0.01, 40, 3.3807],
  290. [0.01, 50, 3.4825],
  291. [0.01, 60, 3.5599],
  292. [0.01, 70, 3.6217],
  293. [0.01, 80, 3.6729],
  294. [0.01, 90, 3.7163],
  295. [0.01, 100, 3.7540],
  296. [0.01, 120, 3.8167],
  297. [0.01, 140, 3.8673],
  298. [0.01, 160, 3.9097],
  299. [0.01, 180, 3.9460],
  300. [0.01, 200, 3.9777],
  301. [0.01, 300, 4.0935],
  302. ];
  303. }
  304. /**
  305. * @test Grubbs statistic error when test type is invalid
  306. * @throws \Exception
  307. */
  308. public function testGrubbsStatisticTestTypeException()
  309. {
  310. // Given
  311. $irrelevantData = [1, 2, 3, 4];
  312. $wrongTypeOfTest = 'wrong type of test';
  313. // Then
  314. $this->expectException(Exception\BadParameterException::class);
  315. // When
  316. $G = Outlier::grubbsStatistic($irrelevantData, $wrongTypeOfTest);
  317. }
  318. /**
  319. * @test Critical value tails must be 1 or 2
  320. * @dataProvider dataProviderForInvalidTails
  321. * @param string $typeOfTest
  322. * @throws \Exception
  323. */
  324. public function testCriticalValueException(string $typeOfTest)
  325. {
  326. // Given
  327. $𝛼 = 0.05;
  328. $n = 10;
  329. // Then
  330. $this->expectException(Exception\BadParameterException::class);
  331. // When
  332. $criticalValue = Outlier::grubbsCriticalValue($𝛼, $n, $typeOfTest);
  333. }
  334. /**
  335. * @return array
  336. */
  337. public function dataProviderForInvalidTails(): array
  338. {
  339. return [
  340. ['zero'],
  341. ['three'],
  342. ['ten'],
  343. ];
  344. }
  345. }