ShiftedGeometricTest.php 4.7 KB

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