ExponentialTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Exponential;
  4. use MathPHP\Exception\OutOfBoundsException;
  5. class ExponentialTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test pdf
  9. * @dataProvider dataProviderForPdf
  10. * @param float $λ
  11. * @param float $x
  12. * @param float $expected_pdf
  13. */
  14. public function testPdf(float $λ, float $x, float $expected_pdf)
  15. {
  16. // Given
  17. $exponential = new Exponential($λ);
  18. // When
  19. $pdf = $exponential->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expected_pdf, $pdf, 0.000001);
  22. }
  23. /**
  24. * @return array [λ, x, pdf]
  25. * Generated with R dexp(x, λ)
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [0.1, -100, 0],
  31. [0.1, -2, 0],
  32. [0.1, -1, 0],
  33. [0.1, 0, 0.1],
  34. [0.1, 0.1, 0.09900498],
  35. [0.1, 0.5, 0.09512294],
  36. [0.1, 1, 0.09048374],
  37. [0.1, 2, 0.08187308],
  38. [0.1, 3, 0.07408182],
  39. [0.1, 10, 0.03678794],
  40. [0.1, 50, 0.0006737947],
  41. [1, -100, 0],
  42. [1, -2, 0],
  43. [1, -1, 0],
  44. [1, 0, 1],
  45. [1, 0.1, 0.9048374],
  46. [1, 0.5, 0.6065307],
  47. [1, 1, 0.3678794],
  48. [1, 2, 0.1353353],
  49. [1, 3, 0.04978707],
  50. [1, 4, 0.01831564],
  51. [2, -100, 0],
  52. [2, -2, 0],
  53. [2, -1, 0],
  54. [2, 0, 2],
  55. [2, 0.1, 1.637462],
  56. [2, 0.5, 0.7357589],
  57. [2, 1, 0.2706706],
  58. [2, 2, 0.03663128],
  59. [2, 3, 0.004957504],
  60. ];
  61. }
  62. /**
  63. * @test cdf
  64. * @dataProvider dataProviderForCdf
  65. * @param number $λ
  66. * @param number $x
  67. * @param number $expected_cdf
  68. */
  69. public function testCdf($λ, $x, $expected_cdf)
  70. {
  71. // Given
  72. $exponential = new Exponential($λ);
  73. // When
  74. $cdf = $exponential->cdf($x);
  75. // Then
  76. $this->assertEqualsWithDelta($expected_cdf, $cdf, 0.0000001);
  77. }
  78. /**
  79. * @return array [λ, x, cdf]
  80. * Generated with R pexp(x, λ)
  81. */
  82. public function dataProviderForCdf(): array
  83. {
  84. return [
  85. [0.1, -100, 0],
  86. [0.1, -2, 0],
  87. [0.1, -1, 0],
  88. [0.1, 0, 0],
  89. [0.1, 0.1, 0.009950166],
  90. [0.1, 0.5, 0.04877058],
  91. [0.1, 1, 0.09516258],
  92. [0.1, 2, 0.1812692],
  93. [0.1, 3, 0.2591818],
  94. [0.1, 10, 0.6321206],
  95. [0.1, 50, 0.9932621],
  96. [1, -100, 0],
  97. [1, -2, 0],
  98. [1, -1, 0],
  99. [1, 0, 0],
  100. [1, 0.1, 0.09516258],
  101. [1, 0.5, 0.3934693],
  102. [1, 1, 0.6321206],
  103. [1, 2, 0.8646647],
  104. [1, 3, 0.9502129],
  105. [1, 4, 0.9816844],
  106. [2, -100, 0],
  107. [2, -2, 0],
  108. [2, -1, 0],
  109. [2, 0, 0],
  110. [2, 0.1, 0.1812692],
  111. [2, 0.5, 0.6321206],
  112. [2, 1, 0.8646647],
  113. [2, 2, 0.9816844],
  114. [2, 3, 0.9975212],
  115. [1 / 3, 2, 0.4865829],
  116. [1 / 3, 4, 0.7364029],
  117. [1 / 5, 4, 0.550671],
  118. ];
  119. }
  120. /**
  121. * @test mean
  122. * @dataProvider dataProviderForMean
  123. * @param number $λ
  124. * @param number $μ
  125. */
  126. public function testMean($λ, $μ)
  127. {
  128. // Given
  129. $exponential = new Exponential($λ);
  130. // When
  131. $mean = $exponential->mean();
  132. // then
  133. $this->assertEqualsWithDelta($μ, $mean, 0.0001);
  134. }
  135. /**
  136. * @return array [λ, μ]
  137. */
  138. public function dataProviderForMean(): array
  139. {
  140. return [
  141. [1, 1],
  142. [2, 0.5],
  143. [3, 0.33333],
  144. [4, 0.25],
  145. ];
  146. }
  147. /**
  148. * @test median
  149. * @dataProvider dataProviderForMedian
  150. * @param number $λ
  151. * @param number $expectedMedian
  152. */
  153. public function testMedian($λ, $expectedMedian)
  154. {
  155. // Given
  156. $exponential = new Exponential($λ);
  157. // When
  158. $median = $exponential->median();
  159. // then
  160. $this->assertEqualsWithDelta($expectedMedian, $median, 0.0000001);
  161. }
  162. /**
  163. * @return array [λ, median]
  164. */
  165. public function dataProviderForMedian(): array
  166. {
  167. return [
  168. [1, 0.69314718055995],
  169. [2, 0.34657359027997],
  170. [3, 0.23104906018665],
  171. [4, 0.17328679513999],
  172. ];
  173. }
  174. /**
  175. * @test mode
  176. * @dataProvider dataProviderForMedian
  177. * @param number $λ
  178. */
  179. public function testMode($λ)
  180. {
  181. // Given
  182. $exponential = new Exponential($λ);
  183. // When
  184. $mode = $exponential->mode();
  185. // then
  186. $this->assertEquals(0, $mode);
  187. }
  188. /**
  189. * @test variance
  190. * @dataProvider dataProviderForVariance
  191. * @param number $λ
  192. * @param number $expectedVariance
  193. */
  194. public function testVariance($λ, $expectedVariance)
  195. {
  196. // Given
  197. $exponential = new Exponential($λ);
  198. // When
  199. $variance = $exponential->variance();
  200. // then
  201. $this->assertEqualsWithDelta($expectedVariance, $variance, 0.0000001);
  202. }
  203. /**
  204. * @return array [λ, variance]
  205. */
  206. public function dataProviderForVariance(): array
  207. {
  208. return [
  209. [1, 1],
  210. [2, 0.25],
  211. [3, 0.111111111111111],
  212. [4, 0.0625],
  213. ];
  214. }
  215. /**
  216. * @test inverse of cdf is x
  217. * @dataProvider dataProviderForInverse
  218. * @param float $λ
  219. * @param float $p
  220. * @param float $expectedInverse
  221. * @throws \Exception
  222. */
  223. public function testInverse(float $λ, float $p, float $expectedInverse)
  224. {
  225. // Given
  226. $exponential = new Exponential($λ);
  227. // When
  228. $inverse = $exponential->inverse($p);
  229. // Then
  230. $this->assertEqualsWithDelta($expectedInverse, $inverse, 0.00001);
  231. }
  232. /**
  233. * @test inverse of cdf is original p
  234. * @dataProvider dataProviderForInverse
  235. * @param float $λ
  236. * @param float $p
  237. * @throws \Exception
  238. */
  239. public function testInverseOfCdf(float $λ, float $p)
  240. {
  241. // Given
  242. $exponential = new Exponential($λ);
  243. $cdf = $exponential->cdf($p);
  244. // When
  245. $inverse_of_cdf = $exponential->inverse($cdf);
  246. // Then
  247. $this->assertEqualsWithDelta($p, $inverse_of_cdf, 0.000001);
  248. }
  249. /**
  250. * @return array [λ, p, cdf]
  251. * Generated with R (stats) qexp(p, λ)
  252. */
  253. public function dataProviderForInverse(): array
  254. {
  255. return [
  256. [0.1, 0, 0],
  257. [0.1, 0.1, 1.053605],
  258. [0.1, 0.3, 3.566749],
  259. [0.1, 0.5, 6.931472],
  260. [0.1, 0.7, 12.03973],
  261. [0.1, 0.9, 23.02585],
  262. [0.1, 1, \INF],
  263. [1, 0, 0],
  264. [1, 0.1, 0.1053605],
  265. [1, 0.3, 0.3566749],
  266. [1, 0.5, 0.6931472],
  267. [1, 0.7, 1.203973],
  268. [1, 0.9, 2.302585],
  269. [1, 1, \INF],
  270. [2, 0, 0],
  271. [2, 0.1, 0.05268026],
  272. [2, 0.3, 0.1783375],
  273. [2, 0.5, 0.3465736],
  274. [2, 0.7, 0.6019864],
  275. [2, 0.9, 1.151293],
  276. [2, 1, \INF],
  277. [1 / 3, 0, 0],
  278. [1 / 3, 0.1, 0.3160815],
  279. [1 / 3, 0.3, 1.070025],
  280. [1 / 3, 0.5, 2.079442],
  281. [1 / 3, 0.7, 3.611918],
  282. [1 / 3, 0.9, 6.907755],
  283. [1 / 3, 1, \INF],
  284. [4, 0, 0],
  285. [4, 0.1, 0.02634013],
  286. [4, 0.3, 0.08916874],
  287. [4, 0.5, 0.1732868],
  288. [4, 0.7, 0.3009932],
  289. [4, 0.9, 0.5756463],
  290. [4, 1, \INF],
  291. ];
  292. }
  293. /**
  294. * @test inverse throws OutOfBounds exceptions for bad p values
  295. * @dataProvider dataProviderForInverseOutOfBoundsP
  296. * @param float $p
  297. * @throws \Exception
  298. */
  299. public function testInverseOutOfBoundsException(float $p)
  300. {
  301. // Given
  302. $λ = 1;
  303. $exponential = new Exponential($λ);
  304. // Then
  305. $this->expectException(OutOfBoundsException::class);
  306. // When
  307. $exponential->inverse($p);
  308. }
  309. /**
  310. * @return array [p]
  311. */
  312. public function dataProviderForInverseOutOfBoundsP(): array
  313. {
  314. return [
  315. [-1],
  316. [-0.01],
  317. [1.01],
  318. [2],
  319. ];
  320. }
  321. /**
  322. * @test rand
  323. */
  324. public function testRand()
  325. {
  326. foreach (\range(1, 4) as $λ) {
  327. foreach (\range(1, 20) as $_) {
  328. // Given
  329. $exponential = new Exponential($λ);
  330. // When
  331. $random = $exponential->rand();
  332. // Then
  333. $this->assertTrue(\is_numeric($random));
  334. }
  335. }
  336. }
  337. }