NoncentralTTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\NoncentralT;
  4. class NoncentralTTest extends \PHPUnit\Framework\TestCase
  5. {
  6. private const ε = .000001;
  7. /**
  8. * @test pdf
  9. * @dataProvider dataProviderForPdf
  10. * @param float $t
  11. * @param int $ν degrees of freedom > 0
  12. * @param float $μ Noncentrality parameter
  13. * @param float $expected
  14. */
  15. public function testPdf(float $t, int $ν, float $μ, float $expected)
  16. {
  17. // Given
  18. $noncentral_t = new NoncentralT($ν, $μ);
  19. $tol = \abs(self::ε * $expected);
  20. // When
  21. $pdf = $noncentral_t->pdf($t);
  22. // Then
  23. $this->assertEqualsWithDelta($expected, $pdf, $tol);
  24. }
  25. /**
  26. * @return array [t, ν, μ, pdf]
  27. */
  28. public function dataProviderForPdf(): array
  29. {
  30. return [
  31. [0, 25, -2, 0.053453889],
  32. [-2, 2, -2, 0.25505237245],
  33. [8, 50, 10, .09559962614195],
  34. [12, 50, 10, .10778431492038],
  35. [-1, 1, 0, 0.1591549430918953357689],
  36. [0, 1, 0, 0.3183098861837906715378],
  37. [1, 1, 0, 0.1591549430918953357689],
  38. [2, 1, 0, 0.06366197723675813430755],
  39. [3, 1, 0, 0.03183098861837906715378],
  40. [-2.8, 1, 1, 0.00793047581730952096428],
  41. [-1, 1, 1, 0.0438603083831399257109],
  42. [0, 1, 1, 0.1930647052601078150839],
  43. [1, 1, 1, 0.2635559531170011242344],
  44. [2, 1, 1, 0.1437974534152678701355],
  45. [2.8, 1, 1, 0.0882563624149698842293],
  46. [3, 1, 1, 0.0789682653062082740843],
  47. [3.25, 1, 1, 0.0691488842822591489281],
  48. [5, 1, 1, 0.03212097322886888814071],
  49. [10, 1, 1, 0.008482965907309839007312],
  50. [-1, 3, 2, 0.00527823780849162254],
  51. [0, 3, 2, 0.049742834812291363635],
  52. [1, 3, 2, 0.236548732514145537757],
  53. [2, 3, 2, 0.28778335780668807139],
  54. [3, 3, 2, 0.181695020544847197199],
  55. //[-1, 5, 5, 1.5871922128754056E-8], // Slightly outside of tolerance
  56. [0, 5, 5, 1.41466247476928302415E-6],
  57. [1, 5, 5, 3.11315287270833112945E-4],
  58. [2, 5, 5, 0.0163549748052644937958],
  59. [3, 5, 5, 0.108534331537738072681],
  60. [3.5, 5, 5, 0.165432129771609351798],
  61. [5, 5, 5, 0.205723200508389201985],
  62. [8, 5, 5, 0.0691178126030179718418],
  63. [10, 5, 5, 0.0283367802665771249083],
  64. ];
  65. }
  66. /**
  67. * @test cdf
  68. * @dataProvider dataProviderForCdf
  69. * @param float $t
  70. * @param int $ν degrees of freedom > 0
  71. * @param float $μ Noncentrality parameter
  72. * @param float $expected
  73. */
  74. public function testCdf(float $t, int $ν, float $μ, float $expected)
  75. {
  76. // Given
  77. $noncentral_t = new NoncentralT($ν, $μ);
  78. $tol = \abs(self::ε * $expected);
  79. // When
  80. $cdf = $noncentral_t->cdf($t);
  81. // Then
  82. $this->assertEqualsWithDelta($expected, $cdf, $tol);
  83. }
  84. /**
  85. * @return array [t, ν, μ, cdf]
  86. */
  87. public function dataProviderForCdf(): array
  88. {
  89. return [
  90. [0, 25, -2, 0.97724986805],
  91. [2, 2, 2, 0.4204754808637],
  92. [8, 50, 10, 0.05611822106520649788],
  93. [12, 50, 10, 0.8939939602826094285],
  94. [-1, 1, 0, 0.25],
  95. [0, 1, 0, 0.5],
  96. [1, 1, 0, 0.75],
  97. [2, 1, 0, 0.8524163823495667258246],
  98. [3, 1, 0, 0.8975836176504332741754],
  99. [-2.8, 1, 1, 0.0232136140512707476863],
  100. [-1, 1, 1, 0.05748009179432582500346],
  101. [0, 1, 1, 0.1586552539314570514148],
  102. [1, 1, 1, 0.4220200303926276373138],
  103. [2, 1, 1, 0.6228719644602816531633],
  104. [2.8, 1, 1, 0.7134035920837329779863],
  105. [3, 1, 1, 0.730102598610563360141],
  106. [3.25, 1, 1, 0.7485796324415677422027],
  107. [5, 1, 1, 0.8313197187246860636513],
  108. [10, 1, 1, 0.9141028166626217410293],
  109. //[-1, 3, 2, 0.003005247595398505997], // Slightly outside of tolerance
  110. //[0, 3, 2, 0.02275013194817920720028], // Slightly outside of tolerance
  111. [1, 3, 2, 0.157349433970036534264],
  112. [2, 3, 2, 0.443075782217602879851],
  113. [3, 3, 2, 0.680027174055522443575],
  114. //[-1, 5, 5, 4.914260406713E-9], // Outside of tolerance
  115. //[0, 5, 5, 2.866515718791939116738E-7], // Slightly outside of tolerance
  116. //[1, 5, 5, 5.9336218471589503E-5], // Slightly outside of tolerance
  117. [2, 5, 5, 0.00479699066830130346],
  118. [3, 5, 5, 0.060515014206464689666],
  119. [3.5, 5, 5, 0.129477929356560427512],
  120. [5, 5, 5, 0.433982979486368857942],
  121. [8, 5, 5, 0.8356579020754351168709],
  122. [10, 5, 5, 0.926988481803982025489],
  123. ];
  124. }
  125. /**
  126. * @test mean
  127. * @dataProvider dataProviderForMean
  128. * @param int $ν degrees of freedom > 0
  129. * @param float $μ Noncentrality parameter
  130. * @param float $expected
  131. */
  132. public function testMean(int $ν, float $μ, float $expected)
  133. {
  134. // Given
  135. $noncentral_t = new NoncentralT($ν, $μ);
  136. $tol = \abs(self::ε * $expected);
  137. // When
  138. $mean = $noncentral_t->mean();
  139. // Then
  140. $this->assertEqualsWithDelta($expected, $mean, $tol);
  141. }
  142. /**
  143. * @return array [ν, μ, mean]
  144. */
  145. public function dataProviderForMean(): array
  146. {
  147. return [
  148. [25, -2, -2.06260931008523],
  149. [2, -2, -3.54490770181103],
  150. [50, 10, 10.1531919459648],
  151. [10, 10, 10.8372230793914],
  152. ];
  153. }
  154. /**
  155. * @test mean not a number if ν = 1
  156. * @dataProvider dataProviderForMeanNan
  157. * @param int $ν
  158. * @param float $μ
  159. */
  160. public function testMeanNAN(int $ν, float $μ)
  161. {
  162. // Given
  163. $noncentral_t = new NoncentralT($ν, $μ);
  164. // When
  165. $mean = $noncentral_t->mean();
  166. // Then
  167. $this->assertNan($mean);
  168. }
  169. /**
  170. * @return array [ν, μ]
  171. */
  172. public function dataProviderForMeanNan(): array
  173. {
  174. return [
  175. [1, 1],
  176. [1, 2],
  177. [1, -1],
  178. ];
  179. }
  180. /**
  181. * @test median (temporary version that is just the mean)
  182. * @dataProvider dataProviderForMean
  183. * @todo Rewrite test using actual median values once median calculation is implemented
  184. * @param int $ν degrees of freedom > 0
  185. * @param float $μ Noncentrality parameter
  186. * @param float $expected
  187. */
  188. public function testMedianTemporaryValue(int $ν, float $μ, float $expected)
  189. {
  190. // Given
  191. $noncentral_t = new NoncentralT($ν, $μ);
  192. $tol = \abs(self::ε * $expected);
  193. // When
  194. $median = $noncentral_t->median();
  195. // Then
  196. $this->assertEqualsWithDelta($expected, $median, $tol);
  197. }
  198. }