GammaTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Gamma;
  4. class GammaTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x x ∈ (0,1)
  10. * @param float $k shape parameter α > 0
  11. * @param float $θ scale parameter θ > 0
  12. * @param float $expectedPdf
  13. */
  14. public function testPdf(float $x, float $k, float $θ, float $expectedPdf)
  15. {
  16. // Given
  17. $gamma = new Gamma($k, $θ);
  18. // When
  19. $pdf = $gamma->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expectedPdf, $pdf, 0.00000001);
  22. }
  23. /**
  24. * Data provider for PDF
  25. * Test data created with calculator http://keisan.casio.com/exec/system/1180573217
  26. * Additional data generated with R dgamma(x, shape = k, scale = θ)
  27. * @return array [x, k, θ, pdf]
  28. */
  29. public function dataProviderForPdf(): array
  30. {
  31. return [
  32. [1, 1, 1, 0.3678794411714423215955],
  33. [1, 2, 1, 0.3678794411714423215955],
  34. [1, 1, 2, 0.3032653298563167118019],
  35. [2, 2, 2, 0.1839397205857211607978],
  36. [2, 4, 1, 0.180447044315483589192],
  37. [4, 2, 5, 0.07189263425875545462882],
  38. [18, 2, 5, 0.01967308016205064377713],
  39. [75, 2, 5, 9.177069615054773651144E-7],
  40. [0.1, 0.1, 0.1, 0.386691694403023771966],
  41. [15, 0.1, 0.1, 8.2986014463775253874E-68],
  42. [4, 0.5, 6, 0.05912753695472959648351],
  43. [1, 4, 5, 0.0002183282],
  44. [2, 4, 5, 0.001430016],
  45. [3, 4, 5, 0.003951444],
  46. [5, 4, 5, 0.01226265],
  47. [15, 4, 5, 0.04480836],
  48. [115, 4, 5, 4.161876e-08],
  49. ];
  50. }
  51. /**
  52. * @test cdf
  53. * @dataProvider dataProviderForCdf
  54. * @param float $x x ∈ (0,1)
  55. * @param float $k shape parameter α > 0
  56. * @param float $θ scale parameter θ > 0
  57. * @param float $expectedCdf
  58. */
  59. public function testCdf(float $x, float $k, float $θ, float $expectedCdf)
  60. {
  61. // Given
  62. $gamma = new Gamma($k, $θ);
  63. // When
  64. $cdf = $gamma->cdf($x);
  65. // Then
  66. $this->assertEqualsWithDelta($expectedCdf, $cdf, 0.000001);
  67. }
  68. /**
  69. * Data provider for CDF
  70. * Test data created with calculator http://keisan.casio.com/exec/system/1180573217
  71. * Additional data generated with R pgamma(x, shape = k, scale = θ)
  72. * @return array [x, k, θ, cdf]
  73. */
  74. public function dataProviderForCdf(): array
  75. {
  76. return [
  77. [1, 1, 1, 0.6321205588285576784045],
  78. [1, 2, 1, 0.264241117657115356809],
  79. [1, 1, 2, 0.3934693402873665763962],
  80. [2, 2, 2, 0.264241117657115356809],
  81. [2, 4, 1, 0.142876539501452951338],
  82. [4, 2, 5, 0.1912078645890011354258],
  83. [18, 2, 5, 0.8743108767424542203128],
  84. [75, 2, 5, 0.9999951055628719707874],
  85. [0.1, 0.1, 0.1, 0.975872656273672222617],
  86. [15, 0.1, 0.1, 1],
  87. [4, 0.5, 6, 0.7517869210100764165283],
  88. [1, 4, 5, 5.684024e-05],
  89. [2, 4, 5, 0.0007762514],
  90. [3, 4, 5, 0.003358069],
  91. [5, 4, 5, 0.01898816],
  92. [15, 4, 5, 0.3527681],
  93. [115, 4, 5, 0.9999998],
  94. ];
  95. }
  96. /**
  97. * @test mean returns the expected average
  98. * @dataProvider dataProviderForMean
  99. * @param float $k
  100. * @param float $θ
  101. * @param float $μ
  102. */
  103. public function testMean(float $k, float $θ, float $μ)
  104. {
  105. // Given
  106. $gamma = new Gamma($k, $θ);
  107. // When
  108. $mean = $gamma->mean();
  109. // Then
  110. $this->assertEqualsWithDelta($μ, $mean, 0.0001);
  111. }
  112. /**
  113. * Data provider for mean
  114. * @return array [k, θ, μ]
  115. */
  116. public function dataProviderForMean(): array
  117. {
  118. return [
  119. [1, 1, 1.0],
  120. [1, 2, 2.0],
  121. [2, 1, 2.0],
  122. [9, 0.5, 4.5],
  123. ];
  124. }
  125. /**
  126. * @test median returns the expected approximation of the average
  127. * @dataProvider dataProviderForMedian
  128. * @param float $k
  129. * @param float $θ
  130. * @param float $expectedApproximation
  131. */
  132. public function testMedian(float $k, float $θ, float $expectedApproximation)
  133. {
  134. // Given
  135. $gamma = new Gamma($k, $θ);
  136. // When
  137. $median = $gamma->median();
  138. // Then
  139. $this->assertEqualsWithDelta($expectedApproximation, $median, 0.000001);
  140. }
  141. /**
  142. * Data provider for median
  143. * @return array [k, θ, μ]
  144. */
  145. public function dataProviderForMedian(): array
  146. {
  147. return [
  148. [1, 1, 0.6875],
  149. [1, 2, 1.375],
  150. [2, 1, 1.6774193548387],
  151. [9, 0.5, 4.33455882352943],
  152. ];
  153. }
  154. /**
  155. * @test mode
  156. * @dataProvider dataProviderForMode
  157. * @param float $k
  158. * @param float $θ
  159. * @param float $expected
  160. */
  161. public function testMode(float $k, float $θ, float $expected)
  162. {
  163. // Given
  164. $gamma = new Gamma($k, $θ);
  165. // When
  166. $mode = $gamma->mode();
  167. // Then
  168. $this->assertEqualsWithDelta($expected, $mode, 0.000001);
  169. }
  170. /**
  171. * Data provider for mode
  172. * @return array [k, θ, μ]
  173. */
  174. public function dataProviderForMode(): array
  175. {
  176. return [
  177. [1, 1, 0],
  178. [1, 2, 0],
  179. [2, 1, 1],
  180. [2, 2, 2],
  181. [2, 3, 3],
  182. [3, 1, 2],
  183. [3, 2, 4],
  184. [3, 3, 6],
  185. ];
  186. }
  187. /**
  188. * @test mode is not a number if k < 1
  189. * @dataProvider dataProviderForModeNan
  190. * @param float $k
  191. * @param float $θ
  192. */
  193. public function testModeNan(float $k, float $θ)
  194. {
  195. // Given
  196. $gamma = new Gamma($k, $θ);
  197. // When
  198. $mode = $gamma->mode();
  199. // Then
  200. $this->assertNan($mode);
  201. }
  202. /**
  203. * Data provider for mode NAN
  204. * @return array [k, θ]
  205. */
  206. public function dataProviderForModeNan(): array
  207. {
  208. return [
  209. [0.1, 1],
  210. [0.5, 3],
  211. [0.9, 6],
  212. ];
  213. }
  214. /**
  215. * @test variance
  216. * @dataProvider dataProviderForVariance
  217. * @param float $k
  218. * @param float $θ
  219. * @param float $expected
  220. */
  221. public function testVariance(float $k, float $θ, float $expected)
  222. {
  223. // Given
  224. $gamma = new Gamma($k, $θ);
  225. // When
  226. $variance = $gamma->variance();
  227. // Then
  228. $this->assertEqualsWithDelta($expected, $variance, 0.000001);
  229. }
  230. /**
  231. * Data provider for variance
  232. * @return array [k, θ, variance]
  233. */
  234. public function dataProviderForVariance(): array
  235. {
  236. return [
  237. [1, 1, 1],
  238. [1, 2, 4],
  239. [2, 1, 2],
  240. [2, 2, 8],
  241. [2, 3, 18],
  242. [3, 1, 3],
  243. [3, 2, 12],
  244. [3, 3, 27],
  245. ];
  246. }
  247. }