PascalTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Discrete;
  3. use MathPHP\Probability\Distribution\Discrete\Pascal;
  4. class PascalTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pmf
  8. * @dataProvider dataProviderForPmf
  9. * @param int $r
  10. * @param float $p
  11. * @param int $x
  12. * @param float $expectedPmf
  13. * @throws \Exception
  14. */
  15. public function testPmf(int $r, float $p, int $x, float $expectedPmf)
  16. {
  17. // Given
  18. $pascal = new Pascal($r, $p);
  19. // When
  20. $pmf = $pascal->pmf($x);
  21. // Then
  22. $this->assertEqualsWithDelta($expectedPmf, $pmf, 0.001);
  23. }
  24. /**
  25. * @return array [r, p, x, pmf]
  26. * Data generated with R stats dnbinom(x, r, p)
  27. */
  28. public function dataProviderForPmf(): array
  29. {
  30. return [
  31. [1, 0.5, 2, 0.125],
  32. [1, 0.4, 2, 0.144],
  33. [2, 0.5, 3, 0.125],
  34. [2, 0.3, 3, 0.12348],
  35. [4, 0.95, 2, 0.02036266],
  36. [7, 0.6, 4, 0.1504936],
  37. [1, 0.2, 3, 0.1024],
  38. [1, 0.2, 7, 0.04194304],
  39. [40, 0.35, 65, 0.02448896],
  40. ];
  41. }
  42. /**
  43. * @test cdf
  44. * @dataProvider dataProviderForCdf
  45. * @param int $r
  46. * @param float $p
  47. * @param int $x
  48. * @param float $expectedCdf
  49. * @throws \Exception
  50. */
  51. public function testCdf(int $r, float $p, int $x, float $expectedCdf)
  52. {
  53. // Given
  54. $pascal = new Pascal($r, $p);
  55. // When
  56. $cdf = $pascal->cdf($x);
  57. // Then
  58. $this->assertEqualsWithDelta($expectedCdf, $cdf, 0.0000001);
  59. }
  60. /**
  61. * @return array [r, p, x, cdf]
  62. * Data generated with R stats pnbinom(x, r, p)
  63. */
  64. public function dataProviderForCdf(): array
  65. {
  66. return [
  67. [1, 0.5, 2, 0.875],
  68. [1, 0.4, 2, 0.784],
  69. [2, 0.5, 3, 0.8125],
  70. [2, 0.3, 3, 0.47178],
  71. [4, 0.95, 2, 0.9977702],
  72. [7, 0.6, 4, 0.5327742],
  73. [1, 0.2, 3, 0.5904],
  74. [1, 0.2, 7, 0.8322278],
  75. [40, 0.35, 65, 0.2845497],
  76. [3, 0.35, 0, 0.042875],
  77. ];
  78. }
  79. /**
  80. * @test mean
  81. * @dataProvider dataProviderForMean
  82. * @param int $r
  83. * @param float $p
  84. * @param float $μ
  85. */
  86. public function testMean(int $r, float $p, float $μ)
  87. {
  88. // Given
  89. $pascal = new Pascal($r, $p);
  90. // When
  91. $mean = $pascal->mean();
  92. // Then
  93. $this->assertEqualsWithDelta($μ, $mean, 0.00000001);
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function dataProviderForMean(): array
  99. {
  100. return [
  101. [4, 0.05, 0.21052631578947],
  102. ];
  103. }
  104. /**
  105. * @test mode
  106. * @dataProvider dataProviderForMode
  107. * @param int $r
  108. * @param float $p
  109. * @param float $expected
  110. */
  111. public function testMode(int $r, float $p, float $expected)
  112. {
  113. // Given
  114. $pascal = new Pascal($r, $p);
  115. // When
  116. $mode = $pascal->mode();
  117. // Then
  118. $this->assertEqualsWithDelta($expected, $mode, 0.00000001);
  119. }
  120. /**
  121. * @return array
  122. */
  123. public function dataProviderForMode(): array
  124. {
  125. return [
  126. [0, 0.05, 0],
  127. [0, 0.95, 0],
  128. [1, 0.05, 0],
  129. [1, 0.95, 0],
  130. [2, 0.05, 0],
  131. [2, 0.5, 1],
  132. [2, 0.9, 9],
  133. ];
  134. }
  135. /**
  136. * @test variance
  137. * @dataProvider dataProviderForVariance
  138. * @param int $r
  139. * @param float $p
  140. * @param float σ²
  141. */
  142. public function testVariance(int $r, float $p, float $σ²)
  143. {
  144. // Given
  145. $pascal = new Pascal($r, $p);
  146. // When
  147. $variance = $pascal->variance();
  148. // Then
  149. $this->assertEqualsWithDelta($σ², $variance, 0.00000001);
  150. }
  151. /**
  152. * @return array
  153. */
  154. public function dataProviderForVariance(): array
  155. {
  156. return [
  157. [4, 0.05, 0.22160664819945],
  158. ];
  159. }
  160. }