LaplaceTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Laplace;
  4. class LaplaceTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $μ
  11. * @param float $b
  12. * @param float $expected_pdf
  13. */
  14. public function testPdf(float $x, float $μ, float $b, float $expected_pdf)
  15. {
  16. // Given
  17. $laplace = new Laplace($μ, $b);
  18. // When
  19. $pdf = $laplace->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expected_pdf, $pdf, 0.000001);
  22. }
  23. /**
  24. * @return array [x, μ, b, pdf]
  25. */
  26. public function dataProviderForPdf(): array
  27. {
  28. return [
  29. [1, 0, 1, 0.1839397206],
  30. [1.1, 0, 1, 0.1664355418],
  31. [1.2, 0, 1, 0.150597106],
  32. [5, 0, 1, 0.0033689735],
  33. [1, 2, 1.4, 0.174836307],
  34. [1.1, 2, 1.4, 0.1877814373],
  35. [2.9, 2, 1.4, 0.1877814373],
  36. ];
  37. }
  38. /**
  39. * @test cdf
  40. * @dataProvider dataProviderForCdf
  41. * @param float $x
  42. * @param float $μ
  43. * @param float $b
  44. * @param float $expected_cdf
  45. */
  46. public function testCdf(float $x, float $μ, float $b, float $expected_cdf)
  47. {
  48. // Given
  49. $laplace = new Laplace($μ, $b);
  50. // When
  51. $cdf = $laplace->cdf($x);
  52. // Then
  53. $this->assertEqualsWithDelta($expected_cdf, $cdf, 0.000001);
  54. }
  55. /**
  56. * @return array [x, μ, b, cdf]
  57. */
  58. public function dataProviderForCdf(): array
  59. {
  60. return [
  61. [1, 0, 1, 0.8160602794],
  62. [1.1, 0, 1, 0.8335644582],
  63. [1.2, 0, 1, 0.849402894],
  64. [5, 0, 1, 0.9966310265],
  65. [1, 2, 1.4, 0.2447708298],
  66. [1.1, 2, 1.4, 0.2628940122],
  67. [2.9, 2, 1.4, 0.7371059878],
  68. ];
  69. }
  70. /**
  71. * @test mean is always μ
  72. */
  73. public function testMean()
  74. {
  75. foreach (\range(-5, 5) as $μ) {
  76. foreach (\range(1, 3) as $b) {
  77. // Given
  78. $laplace = new Laplace($μ, $b);
  79. // When
  80. $mean = $laplace->mean();
  81. // Then
  82. $this->assertEquals($μ, $mean);
  83. }
  84. }
  85. }
  86. /**
  87. * @test median is always μ
  88. */
  89. public function testMedian()
  90. {
  91. foreach (\range(-5, 5) as $μ) {
  92. foreach (\range(1, 3) as $b) {
  93. // Given
  94. $laplace = new Laplace($μ, $b);
  95. // When
  96. $median = $laplace->median();
  97. // Then
  98. $this->assertEquals($μ, $median);
  99. }
  100. }
  101. }
  102. /**
  103. * @test mode is always μ
  104. */
  105. public function testMode()
  106. {
  107. foreach (\range(-5, 5) as $μ) {
  108. foreach (\range(1, 3) as $b) {
  109. // Given
  110. $laplace = new Laplace($μ, $b);
  111. // When
  112. $mode = $laplace->mode();
  113. // Then
  114. $this->assertEquals($μ, $mode);
  115. }
  116. }
  117. }
  118. /**
  119. * @test variance
  120. * @dataProvider dataProviderForVariance
  121. * @param float $μ
  122. * @param float $b
  123. * @param float $expected
  124. */
  125. public function testVariance(float $μ, float $b, float $expected)
  126. {
  127. // Given
  128. $laplace = new Laplace($μ, $b);
  129. // When
  130. $variance = $laplace->variance();
  131. // Then
  132. $this->assertEqualsWithDelta($expected, $variance, 0.000001);
  133. }
  134. /**
  135. * @return array [μ, b, variance]
  136. */
  137. public function dataProviderForVariance(): array
  138. {
  139. return [
  140. [1, 1, 2],
  141. [2, 1, 2],
  142. [3, 1, 2],
  143. [1, 2, 8],
  144. [2, 2, 8],
  145. [4, 3, 18],
  146. ];
  147. }
  148. /**
  149. * @test inverse
  150. * @dataProvider dataProviderForInverse
  151. * @param float $p
  152. * @param float $μ
  153. * @param float $b
  154. * @param $expected_inverse
  155. */
  156. public function testInverse(float $p, float $μ, float $b, $expected_inverse)
  157. {
  158. // Given
  159. $laplace = new Laplace($μ, $b);
  160. // When
  161. $inverse = $laplace->inverse($p);
  162. // Then
  163. $this->assertEqualsWithDelta($expected_inverse, $inverse, 0.00001);
  164. }
  165. /**
  166. * @return array [p, μ, b, inverse]
  167. * Generated with R (rmutil) qlaplace(p, location, dispersion)
  168. */
  169. public function dataProviderForInverse(): array
  170. {
  171. return [
  172. [0, 0, 1, -\INF],
  173. [0.1, 0, 1, -1.609438],
  174. [0.3, 0, 1, -0.5108256],
  175. [0.5, 0, 1, 0],
  176. [0.7, 0, 1, 0.5108256],
  177. [0.9, 0, 1, 1.609438],
  178. [1, 0, 1, \INF],
  179. [0, 1, 1, -\INF],
  180. [0.1, 1, 1, -0.6094379],
  181. [0.2, 1, 1, 0.08370927],
  182. [0.3, 1, 1, 0.4891744],
  183. [0.5, 1, 1, 1],
  184. [0.7, 1, 1, 1.510826],
  185. [0.9, 1, 1, 2.609438],
  186. [1, 1, 1, \INF],
  187. [0, -1, 1, -\INF],
  188. [0.1, -1, 1, -2.609438],
  189. [0.3, -1, 1, -1.510826],
  190. [0.5, -1, 1, -1],
  191. [0.7, -1, 1, -0.4891744],
  192. [0.9, -1, 1, 0.6094379],
  193. [1, -1, 1, \INF],
  194. [0, 2, 4, -\INF],
  195. [0.1, 2, 4, -4.437752],
  196. [0.3, 2, 4, -0.0433025],
  197. [0.5, 2, 4, 2],
  198. [0.7, 2, 4, 4.043302],
  199. [0.9, 2, 4, 8.437752],
  200. [1, 2, 4, \INF],
  201. [0, 13, 9, -\INF],
  202. [0.1, 13, 9, -1.484941],
  203. [0.3, 13, 9, 8.402569],
  204. [0.5, 13, 9, 13],
  205. [0.7, 13, 9, 17.59743],
  206. [0.9, 13, 9, 27.48494],
  207. [1, 13, 9, \INF],
  208. ];
  209. }
  210. /**
  211. * @test rand
  212. */
  213. public function testRand()
  214. {
  215. foreach (\range(-3, 3) as $μ) {
  216. foreach (\range(1, 3) as $b) {
  217. // Given
  218. $laplace = new Laplace($μ, $b);
  219. // When
  220. $random = $laplace->rand();
  221. // Then
  222. $this->assertTrue(\is_numeric($random));
  223. }
  224. }
  225. }
  226. }