HypergeometricTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Discrete;
  3. use MathPHP\Probability\Distribution\Discrete\Hypergeometric;
  4. class HypergeometricTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pmf returns expected probability
  8. * @dataProvider dataProviderForPmf
  9. * @param int $N population size
  10. * @param int $K number of success states in the population
  11. * @param int $n number of draws
  12. * @param int $k number of observed successes
  13. * @param float $expectedPmf
  14. */
  15. public function testPmf(int $N, int $K, int $n, int $k, float $expectedPmf)
  16. {
  17. // Given
  18. $hypergeometric = new Hypergeometric($N, $K, $n);
  19. // When
  20. $pmf = $hypergeometric->pmf($k);
  21. // Then
  22. $this->assertEqualsWithDelta($expectedPmf, $pmf, 0.0000001);
  23. }
  24. /**
  25. * Test data made with: http://stattrek.com/m/online-calculator/hypergeometric.aspx
  26. * @return array
  27. */
  28. public function dataProviderForPmf(): array
  29. {
  30. return [
  31. [50, 5, 10, 4, 0.00396458305801507],
  32. [50, 5, 10, 5, 0.000118937491740452],
  33. [100, 80, 50, 40, 0.196871217706549],
  34. [100, 80, 50, 35, 0.00889760379503624],
  35. [48, 6, 15, 2, 0.350128003786331],
  36. [48, 6, 15, 0, 0.0902552187538097],
  37. [48, 6, 15, 6, 0.000407855201543217],
  38. [100, 30, 20, 5, 0.19182559242904654583],
  39. ];
  40. }
  41. /**
  42. * @test cdf returns expected probability
  43. * @dataProvider dataProviderForCdf
  44. * @param int $N population size
  45. * @param int $K number of success states in the population
  46. * @param int $n number of draws
  47. * @param int $k number of observed successes
  48. * @param float $expectedCdf
  49. */
  50. public function testCdf(int $N, int $K, int $n, int $k, float $expectedCdf)
  51. {
  52. // Given
  53. $hypergeometric = new Hypergeometric($N, $K, $n);
  54. // When
  55. $cdf = $hypergeometric->cdf($k);
  56. // Then
  57. $this->assertEqualsWithDelta($expectedCdf, $cdf, 0.0000001);
  58. }
  59. /**
  60. * Test data made with: http://stattrek.com/m/online-calculator/hypergeometric.aspx
  61. * @return array
  62. */
  63. public function dataProviderForCdf(): array
  64. {
  65. return [
  66. [50, 5, 10, 4, 0.000118937],
  67. [100, 80, 50, 40, 0.401564391],
  68. [100, 80, 50, 35, 0.988582509],
  69. [48, 6, 15, 2, 0.269510717],
  70. [48, 6, 15, 0, 0.909744781],
  71. [100, 30, 20, 5, 0.599011207],
  72. ];
  73. }
  74. /**
  75. * @test mean returns expected average
  76. * @dataProvider dataProviderForMean
  77. * @param int $N population size
  78. * @param int $K number of success states in the population
  79. * @param int $n number of draws
  80. * @param float $μ
  81. */
  82. public function testMean(int $N, int $K, int $n, float $μ)
  83. {
  84. // Given
  85. $hypergeometric = new Hypergeometric($N, $K, $n);
  86. // When
  87. $mean = $hypergeometric->mean();
  88. // Then
  89. $this->assertEqualsWithDelta($μ, $mean, 0.0000001);
  90. }
  91. /**
  92. * Test data made with: http://keisan.casio.com/exec/system/1180573201
  93. * @return array
  94. */
  95. public function dataProviderForMean(): array
  96. {
  97. return [
  98. [50, 5, 10, 1],
  99. [100, 80, 50, 40],
  100. [48, 6, 15, 1.875],
  101. [100, 30, 20, 6],
  102. ];
  103. }
  104. /**
  105. * @test mode
  106. * @dataProvider dataProviderForMode
  107. * @param int $N population size
  108. * @param int $K number of success states in the population
  109. * @param int $n number of draws
  110. * @param array $expectedMode
  111. */
  112. public function testMode(int $N, int $K, int $n, array $expectedMode)
  113. {
  114. // Given
  115. $hypergeometric = new Hypergeometric($N, $K, $n);
  116. // When
  117. $mode = $hypergeometric->mode();
  118. // Then
  119. $this->assertEqualsWithDelta($expectedMode, $mode, 0.0000001);
  120. }
  121. /**
  122. * @return array [N, K, n, mode]
  123. */
  124. public function dataProviderForMode(): array
  125. {
  126. return [
  127. [50, 5, 10, [1, 1]],
  128. [100, 80, 50, [40, 40]],
  129. [48, 6, 15, [2, 2]],
  130. [100, 30, 20, [6, 6]],
  131. ];
  132. }
  133. /**
  134. * @test variance
  135. * @dataProvider dataProviderForVariance
  136. * @param int $N population size
  137. * @param int $K number of success states in the population
  138. * @param int $n number of draws
  139. * @param float $σ²
  140. */
  141. public function testVariance(int $N, int $K, int $n, float $σ²)
  142. {
  143. // Given
  144. $hypergeometric = new Hypergeometric($N, $K, $n);
  145. // When
  146. $variance = $hypergeometric->variance();
  147. // Then
  148. $this->assertEqualsWithDelta($σ², $variance, 0.0000001);
  149. }
  150. /**
  151. * @return array [N, K, n, σ²]
  152. */
  153. public function dataProviderForVariance(): array
  154. {
  155. return [
  156. [50, 5, 10, 0.73469387755102],
  157. [100, 80, 50, 4.040404040404],
  158. ];
  159. }
  160. }