LogLogisticTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\LogLogistic;
  4. class LogLogisticTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $α
  11. * @param float $β
  12. * @param float $expectedPdf
  13. */
  14. public function testPdf(float $x, float $α, float $β, float $expectedPdf)
  15. {
  16. // Given
  17. $logLogistic = new LogLogistic($α, $β);
  18. // When
  19. $pdf = $logLogistic->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expectedPdf, $pdf, 0.000001);
  22. }
  23. /**
  24. * @return array [x, α, β, pdf]
  25. * Generated with R dllogis(x, scale = α, shape = β) [From eha package]
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [0, 1, 1, 1],
  31. [1, 1, 1, 0.25],
  32. [2, 1, 1, 0.1111111],
  33. [3, 1, 1, 0.0625],
  34. [4, 1, 1, 0.04],
  35. [5, 1, 1, 0.02777778],
  36. [10, 1, 1, 0.008264463],
  37. [0, 1, 2, 0],
  38. [1, 1, 2, 0.5],
  39. [2, 1, 2, 0.16],
  40. [3, 1, 2, 0.06],
  41. [4, 1, 2, 0.02768166],
  42. [5, 1, 2, 0.0147929],
  43. [10, 1, 2, 0.001960592],
  44. [0, 2, 2, 0],
  45. [1, 2, 2, 0.32],
  46. [2, 2, 2, 0.25],
  47. [3, 2, 2, 0.1420118],
  48. [4, 2, 2, 0.08],
  49. [5, 2, 2, 0.04756243],
  50. [10, 2, 2, 0.00739645],
  51. [4, 2, 3, 0.07407407],
  52. ];
  53. }
  54. /**
  55. * @test cdf
  56. * @dataProvider dataProviderForCdf
  57. * @param float $x
  58. * @param float $α
  59. * @param float $β
  60. * @param float $expectedPdf
  61. */
  62. public function testCdf(float $x, float $α, float $β, float $expectedPdf)
  63. {
  64. // Given
  65. $logLogistic = new LogLogistic($α, $β);
  66. // When
  67. $cdf = $logLogistic->cdf($x);
  68. // Then
  69. $this->assertEqualsWithDelta($expectedPdf, $cdf, 0.000001);
  70. }
  71. /**
  72. * @test inverse of cdf is x
  73. * @dataProvider dataProviderForCdf
  74. * @param float $x
  75. * @param float $α
  76. * @param float $β
  77. */
  78. public function testInverse(float $x, float $α, float $β)
  79. {
  80. // Given
  81. $logLogistic = new LogLogistic($α, $β);
  82. $cdf = $logLogistic->cdf($x);
  83. // When
  84. $inverseOfCdf = $logLogistic->inverse($cdf);
  85. // Then
  86. $this->assertEqualsWithDelta($x, $inverseOfCdf, 0.000001);
  87. }
  88. /**
  89. * @return array [x, α, β, cdf]
  90. * Generated with R pllogis(x, scale = α, shape = β) [From eha package]
  91. */
  92. public function dataProviderForCdf(): array
  93. {
  94. return [
  95. [0, 1, 1, 0],
  96. [1, 1, 1, 0.5],
  97. [2, 1, 1, 0.6666667],
  98. [3, 1, 1, 0.75],
  99. [4, 1, 1, 0.8],
  100. [5, 1, 1, 0.8333333],
  101. [10, 1, 1, 0.9090909],
  102. [0, 1, 2, 0],
  103. [1, 1, 2, 0.5],
  104. [2, 1, 2, 0.8],
  105. [3, 1, 2, 0.9],
  106. [4, 1, 2, 0.9411765],
  107. [5, 1, 2, 0.9615385],
  108. [10, 1, 2, 0.990099],
  109. [0, 2, 2, 0],
  110. [1, 2, 2, 0.2],
  111. [2, 2, 2, 0.5],
  112. [3, 2, 2, 0.6923077],
  113. [4, 2, 2, 0.8],
  114. [5, 2, 2, 0.862069],
  115. [10, 2, 2, 0.9615385],
  116. [4, 2, 3, 0.8888889],
  117. ];
  118. }
  119. /**
  120. * @test mean
  121. * @dataProvider dataProviderForMean
  122. * @param float $α
  123. * @param float $β
  124. * @param float $μ
  125. */
  126. public function testMean(float $α, float $β, float $μ)
  127. {
  128. // Given
  129. $logLogistic = new LogLogistic($α, $β);
  130. // When
  131. $mean = $logLogistic->mean();
  132. // Then
  133. $this->assertEqualsWithDelta($μ, $mean, 0.00001);
  134. }
  135. /**
  136. * @return array [α, β, μ]
  137. */
  138. public function dataProviderForMean(): array
  139. {
  140. return [
  141. [1, 2, 1.570795],
  142. [2, 2, 3.14159],
  143. [3, 3, 3.62759751692],
  144. [5, 4, 5.55360266602],
  145. ];
  146. }
  147. /**
  148. * @test mean is not a number when shape is not greater than 1
  149. * @dataProvider dataProviderForMeanNan
  150. * @param float $α
  151. * @param float $β
  152. */
  153. public function testMeanNan(float $α, float $β)
  154. {
  155. $logLogistic = new LogLogistic($α, $β);
  156. $this->assertNan($logLogistic->mean());
  157. }
  158. /**
  159. * @return array [α, β]
  160. */
  161. public function dataProviderForMeanNan(): array
  162. {
  163. return [
  164. [1, 1],
  165. [2, 1],
  166. [3, 1],
  167. [5, 1],
  168. ];
  169. }
  170. /**
  171. * @test median
  172. * @dataProvider dataProviderForMean
  173. * @param float $α
  174. * @param float $β
  175. */
  176. public function testMedian(float $α, float $β)
  177. {
  178. // Given
  179. $logLogistic = new LogLogistic($α, $β);
  180. // When
  181. $median = $logLogistic->median();
  182. // Then
  183. $this->assertEqualsWithDelta($α, $median, 0.00001);
  184. }
  185. /**
  186. * @test mode
  187. * @dataProvider dataProviderForMode
  188. * @param float $α
  189. * @param float $β
  190. * @param float $expected
  191. */
  192. public function testMode(float $α, float $β, float $expected)
  193. {
  194. // Given
  195. $logLogistic = new LogLogistic($α, $β);
  196. // When
  197. $mode = $logLogistic->mode();
  198. // Then
  199. $this->assertEqualsWithDelta($expected, $mode, 0.00001);
  200. }
  201. /**
  202. * @return array
  203. */
  204. public function dataProviderForMode(): array
  205. {
  206. return [
  207. [1, 0.2, 0],
  208. [2, 0.9, 0],
  209. [3, 1, 0],
  210. [1, 2, 0.577350269189623],
  211. [1, 3, 0.793700525984102],
  212. [2, 3, 1.5874010519682],
  213. ];
  214. }
  215. /**
  216. * @test variance
  217. * @dataProvider dataProviderForVariance
  218. * @param float $α
  219. * @param float $β
  220. * @param float $expected
  221. */
  222. public function testVariance(float $α, float $β, float $expected)
  223. {
  224. // Given
  225. $logLogistic = new LogLogistic($α, $β);
  226. // When
  227. $variance = $logLogistic->variance();
  228. // Then
  229. $this->assertEqualsWithDelta($expected, $variance, 0.00001);
  230. }
  231. /**
  232. * @return array
  233. */
  234. public function dataProviderForVariance(): array
  235. {
  236. return [
  237. [1, 3, -473.39731252713666],
  238. [2, 4, -79.39739526887552],
  239. ];
  240. }
  241. /**
  242. * @test variance is not a number when β ≤ 2
  243. * @dataProvider dataProviderForVarianceNan
  244. * @param float $α
  245. * @param float $β
  246. */
  247. public function testVarianceNan(float $α, float $β)
  248. {
  249. // Given
  250. $logLogistic = new LogLogistic($α, $β);
  251. // When
  252. $variance = $logLogistic->variance();
  253. // Then
  254. $this->assertNan($variance);
  255. }
  256. /**
  257. * @return array
  258. */
  259. public function dataProviderForVarianceNan(): array
  260. {
  261. return [
  262. [1, 1],
  263. [2, 2],
  264. ];
  265. }
  266. }