PoissonTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Discrete;
  3. use MathPHP\Probability\Distribution\Discrete\Poisson;
  4. class PoissonTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pmf
  8. * @dataProvider dataProviderForPmf
  9. * @param int $k
  10. * @param float $λ
  11. * @param float $expectedPmf
  12. */
  13. public function testPmf(int $k, float $λ, float $expectedPmf)
  14. {
  15. // Given
  16. $poisson = new Poisson($λ);
  17. // When
  18. $pmf = $poisson->pmf($k);
  19. // Then
  20. $this->assertEqualsWithDelta($expectedPmf, $pmf, 0.001);
  21. }
  22. /**
  23. * @return array [k, λ, pmf]
  24. */
  25. public function dataProviderForPmf(): array
  26. {
  27. return [
  28. [3, 2, 0.180],
  29. [3, 5, 0.140373895814280564513],
  30. [8, 6, 0.103257733530844],
  31. [2, 0.45, 0.065],
  32. [16, 12, 0.0542933401099791],
  33. ];
  34. }
  35. /**
  36. * @test cdf
  37. * @dataProvider dataProviderForCdf
  38. * @param int $k
  39. * @param float $λ
  40. * @param float $expectedCdf
  41. */
  42. public function testCdf(int $k, float $λ, float $expectedCdf)
  43. {
  44. // Given
  45. $poisson = new Poisson($λ);
  46. // When
  47. $cdf = $poisson->cdf($k);
  48. // Then
  49. $this->assertEqualsWithDelta($expectedCdf, $cdf, 0.001);
  50. }
  51. /**
  52. * @return array[k, λ, cdf]
  53. */
  54. public function dataProviderForCdf(): array
  55. {
  56. return [
  57. [3, 2, 0.857123460498547048662],
  58. [3, 5, 0.2650],
  59. [8, 6, 0.8472374939845613089968],
  60. [2, 0.45, 0.99],
  61. [16, 12, 0.898708992560164],
  62. ];
  63. }
  64. /**
  65. * @test mean
  66. * @dataProvider dataProviderForMean
  67. * @param float $λ
  68. * @param float $μ
  69. */
  70. public function testMean(float $λ, float $μ)
  71. {
  72. // Given
  73. $poisson = new Poisson($λ);
  74. // When
  75. $mean = $poisson->mean();
  76. // Then
  77. $this->assertEqualsWithDelta($μ, $mean, 0.000001);
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function dataProviderForMean(): array
  83. {
  84. return [
  85. [0.1, 0.1],
  86. [1, 1],
  87. [2, 2],
  88. [5.6, 5.6],
  89. [848, 848],
  90. ];
  91. }
  92. /**
  93. * @test median
  94. * @dataProvider dataProviderForMedian
  95. * @param float $λ
  96. * @param float $expected
  97. */
  98. public function testMedian(float $λ, float $expected)
  99. {
  100. // Given
  101. $poisson = new Poisson($λ);
  102. // When
  103. $median = $poisson->median();
  104. // Then
  105. $this->assertEqualsWithDelta($expected, $median, 0.000001);
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function dataProviderForMedian(): array
  111. {
  112. return [
  113. [0.1, 0],
  114. [1, 1],
  115. [2, 2],
  116. [5.6, 5],
  117. [848, 848],
  118. ];
  119. }
  120. /**
  121. * @test mode
  122. * @dataProvider dataProviderForMode
  123. * @param float $λ
  124. * @param array $expected
  125. */
  126. public function testMode(float $λ, array $expected)
  127. {
  128. // Given
  129. $poisson = new Poisson($λ);
  130. // When
  131. $mode = $poisson->mode();
  132. // Then
  133. $this->assertEqualsWithDelta($expected, $mode, 0.000001);
  134. }
  135. /**
  136. * @return array
  137. */
  138. public function dataProviderForMode(): array
  139. {
  140. return [
  141. [0.1, [0, 0]],
  142. [1, [0, 1]],
  143. [2, [1, 2]],
  144. [5.6, [5, 5]],
  145. [848, [847, 848]],
  146. ];
  147. }
  148. /**
  149. * @test variance
  150. * @dataProvider dataProviderForVariance
  151. * @param float $λ
  152. * @param float $σ²
  153. */
  154. public function testVariance(float $λ, float $σ²)
  155. {
  156. // Given
  157. $poisson = new Poisson($λ);
  158. // When
  159. $variance = $poisson->variance();
  160. // Then
  161. $this->assertEqualsWithDelta($σ², $variance, 0.000001);
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function dataProviderForVariance(): array
  167. {
  168. return [
  169. [0.1, 0.1],
  170. [1, 1],
  171. [2, 2],
  172. [5.6, 5.6],
  173. [848, 848],
  174. ];
  175. }
  176. }