LogisticTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Logistic;
  4. class LogisticTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $μ
  11. * @param float $s
  12. * @param float $expected_pdf
  13. */
  14. public function testPdf(float $x, float $μ, float $s, float $expected_pdf)
  15. {
  16. // Given
  17. $logistic = new Logistic($μ, $s);
  18. // When
  19. $pdf = $logistic->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expected_pdf, $pdf, 0.000001);
  22. }
  23. /**
  24. * @return array [x, μ, s, pdf]
  25. * Generated with R (stats) dlogis(x, μ, s)
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [0, 0, 1, 0.25],
  31. [1, 0, 1, 0.1966119],
  32. [2, 0, 1, 0.1049936],
  33. [3, 0, 1, 0.04517666],
  34. [4, 0, 1, 0.01766271],
  35. [5, 0, 1, 0.006648057],
  36. [10, 0, 1, 4.539581e-05],
  37. [0, 1, 1, 0.1966119],
  38. [1, 1, 1, 0.25],
  39. [2, 1, 1, 0.1966119],
  40. [3, 1, 1, 0.1049936],
  41. [4, 1, 1, 0.04517666],
  42. [5, 1, 1, 0.01766271],
  43. [10, 1, 1, 0.0001233793],
  44. [-5, 0, 0.7, 0.001127488648],
  45. [-4.2, 0, 0.7, 0.003523584702],
  46. [-3.5, 0, 0.7, 0.009497223815],
  47. [-3.0, 0, 0.7, 0.01913226324],
  48. [-2.0, 0, 0.7, 0.07337619322],
  49. [-0.1, 0, 0.7, 0.3553268797],
  50. [0, 0, 0.7, 0.3571428571],
  51. [0.1, 0, 0.7, 0.3553268797],
  52. [3.5, 0, 0.7, 0.009497223815],
  53. [4.2, 0, 0.7, 0.003523584702],
  54. [5, 0, 0.7, 0.001127488648],
  55. [-5, 2, 1.5, 0.006152781498],
  56. [-3.7, 2, 1.5, 0.01426832061],
  57. [0, 2, 1.5, 0.1100606731],
  58. [3.7, 2, 1.5, 0.1228210582],
  59. [5, 2, 1.5, 0.06999572],
  60. ];
  61. }
  62. /**
  63. * @test cdf
  64. * @dataProvider dataProviderForCdf
  65. * @param float $x
  66. * @param float $μ
  67. * @param float $s
  68. * @param float $expected_cdf
  69. */
  70. public function testCdf(float $x, float $μ, float $s, float $expected_cdf)
  71. {
  72. // Given
  73. $logistic = new Logistic($μ, $s);
  74. // When
  75. $cdf = $logistic->cdf($x);
  76. // Then
  77. $this->assertEqualsWithDelta($expected_cdf, $cdf, 0.000001);
  78. }
  79. /**
  80. * @test inverse of cdf is x
  81. * @dataProvider dataProviderForCdf
  82. * @param float $x
  83. * @param float $μ
  84. * @param float $s
  85. */
  86. public function testInverseOfCdf(float $x, float $μ, float $s)
  87. {
  88. // Given
  89. $logistic = new Logistic($μ, $s);
  90. $cdf = $logistic->cdf($x);
  91. // When
  92. $inverse_of_cdf = $logistic->inverse($cdf);
  93. // Then
  94. $this->assertEqualsWithDelta($x, $inverse_of_cdf, 0.000001);
  95. }
  96. /**
  97. * @return array [x, μ, s, cdf]
  98. * Generated with R (stats) plogis(x, μ, s)
  99. */
  100. public function dataProviderForCdf(): array
  101. {
  102. return [
  103. [0, 0, 1, 0.5],
  104. [1, 0, 1, 0.7310586],
  105. [2, 0, 1, 0.8807971],
  106. [3, 0, 1, 0.9525741],
  107. [4, 0, 1, 0.9820138],
  108. [5, 0, 1, 0.9933071],
  109. [10, 0, 1, 0.9999546],
  110. [0, 1, 1, 0.2689414],
  111. [1, 1, 1, 0.5],
  112. [2, 1, 1, 0.7310586],
  113. [3, 1, 1, 0.8807971],
  114. [4, 1, 1, 0.9525741],
  115. [5, 1, 1, 0.9820138],
  116. [10, 1, 1, 0.9998766],
  117. [-4.8, 0, 0.7, 0.001050809752],
  118. [-3.5, 0, 0.7, 0.006692850924],
  119. [-3.0, 0, 0.7, 0.01357691694],
  120. [-2.0, 0, 0.7, 0.05431326613],
  121. [-0.1, 0, 0.7, 0.4643463292],
  122. [0, 0, 0.7, 0.5],
  123. [0.1, 0, 0.7, 0.5356536708],
  124. [3.5, 0, 0.7, 0.9933071491],
  125. [4.2, 0, 0.7, 0.9975273768],
  126. [5, 0, 0.7, 0.9992101341],
  127. [-5, 2, 1.5, 0.009315959345],
  128. [-3.7, 2, 1.5, 0.02188127094],
  129. [0, 2, 1.5, 0.2086085273],
  130. [3.7, 2, 1.5, 0.7564535292],
  131. [5, 2, 1.5, 0.880797078],
  132. ];
  133. }
  134. /**
  135. * @test mean
  136. */
  137. public function testMean()
  138. {
  139. foreach (\range(-3, 3) as $μ) {
  140. foreach (\range(1, 3) as $s) {
  141. // Given
  142. $logistic = new Logistic($μ, $s);
  143. // When
  144. $mean = $logistic->mean();
  145. // Then
  146. $this->assertEquals($μ, $mean);
  147. }
  148. }
  149. }
  150. /**
  151. * @test median
  152. */
  153. public function testMedian()
  154. {
  155. foreach (\range(-3, 3) as $μ) {
  156. foreach (\range(1, 3) as $s) {
  157. // Given
  158. $logistic = new Logistic($μ, $s);
  159. // When
  160. $median = $logistic->median();
  161. // Then
  162. $this->assertEquals($μ, $median);
  163. }
  164. }
  165. }
  166. /**
  167. * @test mode
  168. */
  169. public function testMode()
  170. {
  171. foreach (\range(-3, 3) as $μ) {
  172. foreach (\range(1, 3) as $s) {
  173. // Given
  174. $logistic = new Logistic($μ, $s);
  175. // When
  176. $mode = $logistic->mode();
  177. // Then
  178. $this->assertEquals($μ, $mode);
  179. }
  180. }
  181. }
  182. /**
  183. * @test variance
  184. * @dataProvider dataProviderForVariance
  185. * @param float $μ
  186. * @param float $s
  187. * @param float $expected
  188. */
  189. public function testVariance(float $μ, float $s, float $expected)
  190. {
  191. // Given
  192. $logistic = new Logistic($μ, $s);
  193. // When
  194. $variance = $logistic->variance();
  195. // Then
  196. $this->assertEqualsWithDelta($expected, $variance, 0.000001);
  197. }
  198. /**
  199. * @return array
  200. */
  201. public function dataProviderForVariance(): array
  202. {
  203. return [
  204. [0, 1, 3.28986813369645],
  205. [0, 2, 13.15947253478581],
  206. [0, 3, 29.60881320326808],
  207. [5, 4, 52.63789013914325],
  208. ];
  209. }
  210. /**
  211. * @test inverse
  212. * @dataProvider dataProviderForInverse
  213. * @param float $p
  214. * @param float $μ
  215. * @param float $s
  216. * @param $expected_inverse
  217. */
  218. public function testInverse(float $p, float $μ, float $s, $expected_inverse)
  219. {
  220. // Given
  221. $logistic = new Logistic($μ, $s);
  222. // When
  223. $inverse = $logistic->inverse($p);
  224. // Then
  225. $this->assertEqualsWithDelta($expected_inverse, $inverse, 0.00001);
  226. }
  227. /**
  228. * @return array [p, μ, s, inverse]
  229. * Generated with R (stats) qlogis(p, location, scale)
  230. */
  231. public function dataProviderForInverse(): array
  232. {
  233. return [
  234. [0, -1, 1, -\INF],
  235. [0.1, -1, 1, -3.197225],
  236. [0.3, -1, 1, -1.847298],
  237. [0.5, -1, 1, -1],
  238. [0.7, -1, 1, -0.1527021],
  239. [0.9, -1, 1, 1.197225],
  240. [1, -1, 1, \INF],
  241. [0, 0, 1, -\INF],
  242. [0.1, 0, 1, -2.197225],
  243. [0.3, 0, 1, -0.8472979],
  244. [0.5, 0, 1, 0],
  245. [0.7, 0, 1, 0.8472979],
  246. [0.9, 0, 1, 2.197225],
  247. [1, 0, 1, \INF],
  248. [0, 1, 1, -\INF],
  249. [0.1, 1, 1, -1.197225],
  250. [0.3, 1, 1, 0.1527021],
  251. [0.5, 1, 1, 1],
  252. [0.7, 1, 1, 1.847298],
  253. [0.9, 1, 1, 3.197225],
  254. [1, 1, 1, \INF],
  255. [0, 2, 5, -\INF],
  256. [0.1, 2, 5, -8.986123],
  257. [0.3, 2, 5, -2.236489],
  258. [0.5, 2, 5, 2],
  259. [0.7, 2, 5, 6.236489],
  260. [0.9, 2, 5, 12.98612],
  261. [1, 2, 5, \INF],
  262. ];
  263. }
  264. /**
  265. * @test rand
  266. */
  267. public function testRand()
  268. {
  269. foreach (\range(-3, 3) as $μ) {
  270. foreach (\range(1, 3) as $s) {
  271. // Given
  272. $logistic = new Logistic($μ, $s);
  273. // When
  274. $rand = $logistic->rand();
  275. // Then
  276. $this->assertTrue(\is_numeric($rand));
  277. }
  278. }
  279. }
  280. }