TDistributionTableTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Table;
  3. use MathPHP\Probability\Distribution\Table\TDistribution;
  4. use MathPHP\Exception;
  5. class TDistributionTableTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test one-sided T value from confidence level
  9. * @dataProvider dataProviderForOneSidedCL
  10. * @param mixed $ν
  11. * @param mixed $cl
  12. * @param float $t
  13. * @throws \Exception
  14. */
  15. public function testGetOneSidedTValueFromConfidenceLevel($ν, $cl, float $t)
  16. {
  17. // When
  18. $value = TDistribution::getOneSidedTValueFromConfidenceLevel($ν, $cl);
  19. // Then
  20. $this->assertEquals($t, $value);
  21. }
  22. public function dataProviderForOneSidedCL(): array
  23. {
  24. return [
  25. [ 1, 0, 0 ],
  26. [ 1, 75, 1.000 ],
  27. [ 1, 80, 1.376 ],
  28. [ 1, 97.5, 12.71 ],
  29. [ 1, 99.9, 318.3 ],
  30. [ 1, 99.95, 636.6 ],
  31. [ 10, 0, 0 ],
  32. [ 10, 75, 0.700 ],
  33. [ 10, 80, 0.879 ],
  34. [ 10, 95, 1.812 ],
  35. [ 10, 99.5, 3.169 ],
  36. [ 10, 99.95, 4.587 ],
  37. [ 'infinity', 0, 0 ],
  38. [ 'infinity', 75, 0.674 ],
  39. [ 'infinity', 97.5, 1.960 ],
  40. [ 'infinity', 99.95, 3.291 ],
  41. ];
  42. }
  43. /**
  44. * @test two-sided T value from confidence level
  45. * @dataProvider dataProviderForTwoSidedCL
  46. * @param mixed $ν
  47. * @param mixed $cl
  48. * @param float $t
  49. * @throws \Exception
  50. */
  51. public function testGetTwoSidedTValueFromConfidenceLevel($ν, $cl, float $t)
  52. {
  53. // When
  54. $value = TDistribution::getTwoSidedTValueFromConfidenceLevel($ν, $cl);
  55. // Then
  56. $this->assertEquals($t, $value);
  57. }
  58. public function dataProviderForTwoSidedCL(): array
  59. {
  60. return [
  61. [ 1, 0, 0 ],
  62. [ 1, 50, 1.000 ],
  63. [ 1, 60, 1.376 ],
  64. [ 1, 95, 12.71 ],
  65. [ 1, 99.8, 318.3 ],
  66. [ 1, 99.9, 636.6 ],
  67. [ 10, 0, 0 ],
  68. [ 10, 50, 0.700 ],
  69. [ 10, 60, 0.879 ],
  70. [ 10, 90, 1.812 ],
  71. [ 10, 99, 3.169 ],
  72. [ 10, 99.9, 4.587 ],
  73. [ 'infinity', 0, 0 ],
  74. [ 'infinity', 50, 0.674 ],
  75. [ 'infinity', 95, 1.960 ],
  76. [ 'infinity', 99.9, 3.291 ],
  77. ];
  78. }
  79. /**
  80. * @test one-sided value from alpha
  81. * @dataProvider dataProviderForOneSidedAlpha
  82. * @param mixed $ν
  83. * @param mixed $α
  84. * @param float $t
  85. * @throws \Exception
  86. */
  87. public function testGetOneSidedTValueFromAlpha($ν, $α, float $t)
  88. {
  89. // When
  90. $value = TDistribution::getOneSidedTValueFromAlpha($ν, $α);
  91. // Then
  92. $this->assertEquals($t, $value);
  93. }
  94. public function dataProviderForOneSidedAlpha(): array
  95. {
  96. return [
  97. [ 1, '0.50', 0 ],
  98. [ 1, 0.25, 1.000 ],
  99. [ 1, '0.20', 1.376 ],
  100. [ 1, 0.025, 12.71 ],
  101. [ 1, 0.001, 318.3 ],
  102. [ 1, 0.0005, 636.6 ],
  103. [ 10, '0.50', 0 ],
  104. [ 10, 0.25, 0.700 ],
  105. [ 10, '0.20', 0.879 ],
  106. [ 10, 0.05, 1.812 ],
  107. [ 10, 0.005, 3.169 ],
  108. [ 10, 0.0005, 4.587 ],
  109. [ 'infinity', '0.50', 0 ],
  110. [ 'infinity', 0.25, 0.674 ],
  111. [ 'infinity', 0.025, 1.960 ],
  112. [ 'infinity', 0.0005, 3.291 ],
  113. ];
  114. }
  115. /**
  116. * @test two-sided value from alpha
  117. * @dataProvider dataProviderForTwoSidedAlpha
  118. * @param mixed $ν
  119. * @param mixed $α
  120. * @param float $t
  121. * @throws \Exception
  122. */
  123. public function testGetTwoSidedTValueFromAlpha($ν, $α, float $t)
  124. {
  125. // When
  126. $value = TDistribution::getTwoSidedTValueFromAlpha($ν, $α);
  127. // Then
  128. $this->assertEquals($t, $value);
  129. }
  130. public function dataProviderForTwoSidedAlpha(): array
  131. {
  132. return [
  133. [ 1, '1.00', 0 ],
  134. [ 1, '0.50', 1.000 ],
  135. [ 1, '0.40', 1.376 ],
  136. [ 1, 0.05, 12.71 ],
  137. [ 1, 0.002, 318.3 ],
  138. [ 1, 0.001, 636.6 ],
  139. [ 10, '1.00', 0 ],
  140. [ 10, '0.50', 0.700 ],
  141. [ 10, '0.40', 0.879 ],
  142. [ 10, '0.10', 1.812 ],
  143. [ 10, 0.01, 3.169 ],
  144. [ 10, 0.001, 4.587 ],
  145. [ 'infinity', '1.00', 0 ],
  146. [ 'infinity', '0.50', 0.674 ],
  147. [ 'infinity', 0.05, 1.960 ],
  148. [ 'infinity', 0.001, 3.291 ],
  149. ];
  150. }
  151. /**
  152. * @test
  153. * @throws \Exception
  154. */
  155. public function testGetOneSidedTValueFromConfidenceLevelExceptionBadDF()
  156. {
  157. // Then
  158. $this->expectException(Exception\BadDataException::class);
  159. // When
  160. TDistribution::getOneSidedTValueFromConfidenceLevel(1234, 99);
  161. }
  162. /**
  163. * @test
  164. * @throws \Exception
  165. */
  166. public function testGetTwoSidedTValueFromConfidenceLevelExceptionBadDF()
  167. {
  168. // Then
  169. $this->expectException(Exception\BadDataException::class);
  170. // When
  171. TDistribution::getTwoSidedTValueFromConfidenceLevel(1234, 99);
  172. }
  173. /**
  174. * @test
  175. * @throws \Exception
  176. */
  177. public function testGetOneSidedTValueFromAlphaExceptionBadDF()
  178. {
  179. // Then
  180. $this->expectException(Exception\BadDataException::class);
  181. // When
  182. TDistribution::getOneSidedTValueFromAlpha(1234, 0.05);
  183. }
  184. /**
  185. * @test
  186. * @throws \Exception
  187. */
  188. public function testGetTwoSidedTValueFromAlphaExceptionBadDF()
  189. {
  190. // Then
  191. $this->expectException(Exception\BadDataException::class);
  192. // When
  193. TDistribution::getTwoSidedTValueFromAlpha(1234, 0.05);
  194. }
  195. /**
  196. * @test
  197. * @throws \Exception
  198. */
  199. public function testGetOneSidedTValueFromConfidenceLevelExceptionBadCL()
  200. {
  201. // Then
  202. $this->expectException(Exception\BadDataException::class);
  203. // When
  204. TDistribution::getOneSidedTValueFromConfidenceLevel(1, 155);
  205. }
  206. /**
  207. * @test
  208. * @throws \Exception
  209. */
  210. public function testGetTwoSidedTValueFromConfidenceLevelExceptionBadCL()
  211. {
  212. // Then
  213. $this->expectException(Exception\BadDataException::class);
  214. // When
  215. TDistribution::getTwoSidedTValueFromConfidenceLevel(1, 155);
  216. }
  217. /**
  218. * @test
  219. * @throws \Exception
  220. */
  221. public function testGetOneSidedTValueFromAlphaExceptionBadAlpha()
  222. {
  223. // Then
  224. $this->expectException(Exception\BadDataException::class);
  225. // When
  226. TDistribution::getOneSidedTValueFromAlpha(1, 999);
  227. }
  228. /**
  229. * @test
  230. * @throws \Exception
  231. */
  232. public function testGetTwoSidedTValueFromAlphaExceptionBadAlpha()
  233. {
  234. // Then
  235. $this->expectException(Exception\BadDataException::class);
  236. // When
  237. TDistribution::getTwoSidedTValueFromAlpha(1, 999);
  238. }
  239. }