ParetoTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Pareto;
  4. class ParetoTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $a
  11. * @param float $b
  12. * @param float $expected_pdf
  13. */
  14. public function testPdf(float $x, float $a, float $b, float $expected_pdf)
  15. {
  16. // Given
  17. $pareto = new Pareto($a, $b);
  18. // When
  19. $pdf = $pareto->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expected_pdf, $pdf, 0.00001);
  22. }
  23. /**
  24. * @return array
  25. * Generated with R dpareto(x, b a) from package EnvStats
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [0.1, 1, 1, 0],
  31. [1, 1, 1, 1],
  32. [2, 1, 1, 0.25],
  33. [3, 1, 1, 0.1111111],
  34. [4, 1, 1, 0.0625],
  35. [5, 1, 1, 0.04],
  36. [10, 1, 1, 0.01],
  37. [0.1, 2, 1, 0],
  38. [1, 2, 1, 2],
  39. [2, 2, 1, 0.25],
  40. [3, 2, 1, 0.07407407],
  41. [4, 2, 1, 0.03125],
  42. [5, 2, 1, 0.016],
  43. [10, 2, 1, 0.002],
  44. [0.1, 2, 1, 0],
  45. [1, 1, 2, 0],
  46. [2, 1, 2, 0.5],
  47. [3, 1, 2, 0.2222222],
  48. [4, 1, 2, 0.125],
  49. [5, 1, 2, 0.08],
  50. [10, 1, 2, 0.02],
  51. [0.1, 2, 2, 0],
  52. [1, 2, 2, 0],
  53. [2, 2, 2, 1],
  54. [3, 2, 2, 0.2962963],
  55. [4, 2, 2, 0.125],
  56. [5, 2, 2, 0.064],
  57. [10, 2, 2, 0.008],
  58. [4, 8, 2, 0.0078125],
  59. [5, 8, 2, 0.001048576],
  60. [9, 4, 5, 0.04233772],
  61. ];
  62. }
  63. /**
  64. * @test cdf
  65. * @dataProvider dataProviderForCdf
  66. * @param float $x
  67. * @param float $a
  68. * @param float $b
  69. * @param float $expected_cdf
  70. */
  71. public function testCdf(float $x, float $a, float $b, float $expected_cdf)
  72. {
  73. // Given
  74. $pareto = new Pareto($a, $b);
  75. // When
  76. $cdf = $pareto->cdf($x);
  77. // Then
  78. $this->assertEqualsWithDelta($expected_cdf, $cdf, 0.00001);
  79. }
  80. /**
  81. * @return array
  82. * Generated with R ppareto(x, b a) from package EnvStats
  83. */
  84. public function dataProviderForCdf(): array
  85. {
  86. return [
  87. [0.1, 1, 1, 0],
  88. [1, 1, 1, 0],
  89. [2, 1, 1, 0.5],
  90. [3, 1, 1, 0.6666667],
  91. [4, 1, 1, 0.75],
  92. [5, 1, 1, 0.8],
  93. [10, 1, 1, 0.9],
  94. [0.1, 2, 1, 0],
  95. [1, 2, 1, 0],
  96. [2, 2, 1, 0.75],
  97. [3, 2, 1, 0.8888889],
  98. [4, 2, 1, 0.9375],
  99. [5, 2, 1, 0.96],
  100. [10, 2, 1, 0.99],
  101. [0.1, 2, 1, 0],
  102. [1, 1, 2, 0],
  103. [2, 1, 2, 0],
  104. [3, 1, 2, 0.3333333],
  105. [4, 1, 2, 0.5],
  106. [5, 1, 2, 0.6],
  107. [10, 1, 2, 0.8],
  108. [0.1, 2, 2, 0],
  109. [1, 2, 2, 0],
  110. [2, 2, 2, 0],
  111. [3, 2, 2, 0.5555556],
  112. [4, 2, 2, 0.75],
  113. [5, 2, 2, 0.84],
  114. [10, 2, 2, 0.96],
  115. [4, 8, 2, 0.9960938],
  116. [5, 8, 2, 0.9993446],
  117. [9, 4, 5, 0.9047401],
  118. ];
  119. }
  120. /**
  121. * @test inverse
  122. * @dataProvider dataProviderForInverse
  123. * @param float $p
  124. * @param float $a
  125. * @param float $b
  126. * @param float $expected_inverse
  127. */
  128. public function testInverse(float $p, float $a, float $b, float $expected_inverse)
  129. {
  130. // Given
  131. $pareto = new Pareto($a, $b);
  132. // When
  133. $inverse = $pareto->inverse($p);
  134. // Then
  135. $this->assertEqualsWithDelta($expected_inverse, $inverse, 0.00001);
  136. }
  137. /**
  138. * @return array
  139. * Generated with https://solvemymath.com/online_math_calculator/statistics/continuous_distributions/pareto/quantile_pareto.php
  140. */
  141. public function dataProviderForInverse(): array
  142. {
  143. return [
  144. [0, 1, 1, -\INF],
  145. [0.1, 1, 1, 1.1111111],
  146. [0.2, 1, 1, 1.25],
  147. [0.3, 1, 1, 1.42857],
  148. [0.4, 1, 1, 1.666666667],
  149. [0.5, 1, 1, 2],
  150. [0.6, 1, 1, 2.5],
  151. [0.7, 1, 1, 3.3333333],
  152. [0.8, 1, 1, 5],
  153. [0.9, 1, 1, 10],
  154. [1, 1, 1, \INF],
  155. [0, 2, 2, -\INF],
  156. [0.1, 2, 2, 2.108185],
  157. [0.2, 2, 2, 2.2360679],
  158. [0.3, 2, 2, 2.390457218],
  159. [0.4, 2, 2, 2.5819888974],
  160. [0.5, 2, 2, 2.8284271247],
  161. [0.6, 2, 2, 3.1622776601],
  162. [0.7, 2, 2, 3.6514837167],
  163. [0.8, 2, 2, 4.4721359549],
  164. [0.9, 2, 2, 6.3245553203],
  165. [1, 2, 2, \INF],
  166. [0, 4, 6, -\INF],
  167. [0.1, 4, 6, 6.1601405764],
  168. [0.2, 4, 6, 6.3442275806],
  169. [0.5, 4, 6, 7.1352426900],
  170. [0.9, 4, 6, 10.669676460],
  171. [1, 4, 6, \INF],
  172. ];
  173. }
  174. /**
  175. * @test inverse of CDF is x
  176. * @dataProvider dataProviderForInverseOfCdf
  177. * @param float $x
  178. * @param float $a
  179. * @param float $b
  180. */
  181. public function testInverseOfCdf(float $x, float $a, float $b)
  182. {
  183. // Given
  184. $pareto = new Pareto($a, $b);
  185. $cdf = $pareto->cdf($x);
  186. // When
  187. $inverse_of_cdf = $pareto->inverse($cdf);
  188. // Then
  189. $this->assertEqualsWithDelta($x, $inverse_of_cdf, 0.000001);
  190. }
  191. /**
  192. * @return array
  193. */
  194. public function dataProviderForInverseOfCdf(): array
  195. {
  196. return [
  197. [2, 1, 1, 0.5],
  198. [3, 1, 1, 0.6666667],
  199. [4, 1, 1, 0.75],
  200. [5, 1, 1, 0.8],
  201. [10, 1, 1, 0.9],
  202. [2, 2, 1, 0.75],
  203. [3, 2, 1, 0.8888889],
  204. [4, 2, 1, 0.9375],
  205. [5, 2, 1, 0.96],
  206. [10, 2, 1, 0.99],
  207. [3, 1, 2, 0.3333333],
  208. [4, 1, 2, 0.5],
  209. [5, 1, 2, 0.6],
  210. [10, 1, 2, 0.8],
  211. [3, 2, 2, 0.5555556],
  212. [4, 2, 2, 0.75],
  213. [5, 2, 2, 0.84],
  214. [10, 2, 2, 0.96],
  215. [4, 8, 2, 0.9960938],
  216. [5, 8, 2, 0.9993446],
  217. [9, 4, 5, 0.9047401],
  218. ];
  219. }
  220. /**
  221. * @test mean
  222. * @dataProvider dataProviderForMean
  223. * @param float $a
  224. * @param float $b
  225. * @param float $μ
  226. */
  227. public function testMean(float $a, float $b, float $μ)
  228. {
  229. // Given
  230. $pareto = new Pareto($a, $b);
  231. // When
  232. $mean = $pareto->mean();
  233. // Then
  234. $this->assertEqualsWithDelta($μ, $mean, 0.0001);
  235. }
  236. /**
  237. * @return array [a, b, μ]
  238. */
  239. public function dataProviderForMean(): array
  240. {
  241. return [
  242. [1, 2, \INF],
  243. [0.4, 2, \INF],
  244. [0.001, 2, \INF],
  245. [2, 1, 2],
  246. [3, 1, 1.5],
  247. [3, 2, 3],
  248. ];
  249. }
  250. /**
  251. * @test median
  252. * @dataProvider dataProviderForMedian
  253. * @param float $a
  254. * @param float $b
  255. * @param float $expected_median
  256. */
  257. public function testMedian(float $a, float $b, float $expected_median)
  258. {
  259. // Given
  260. $pareto = new Pareto($a, $b);
  261. // When
  262. $median = $pareto->median();
  263. // Then
  264. $this->assertEqualsWithDelta($expected_median, $median, 0.0000001);
  265. }
  266. /**
  267. * @return array [a, b, median]
  268. */
  269. public function dataProviderForMedian(): array
  270. {
  271. return [
  272. [1, 1, 2],
  273. [1, 2, 1.414213562373095],
  274. [2, 1, 4],
  275. [4, 5, 4.59479341998814],
  276. ];
  277. }
  278. /**
  279. * @test mode
  280. * @dataProvider dataProviderForMode
  281. * @param float $a
  282. * @param float $b
  283. * @param float $expected
  284. */
  285. public function testMode(float $a, float $b, float $expected)
  286. {
  287. // Given
  288. $pareto = new Pareto($a, $b);
  289. // When
  290. $mode = $pareto->mode();
  291. // Then
  292. $this->assertEqualsWithDelta($expected, $mode, 0.0000001);
  293. }
  294. /**
  295. * @return array [a, b, mode]
  296. */
  297. public function dataProviderForMode(): array
  298. {
  299. return [
  300. [1, 1, 1],
  301. [2, 2, 2],
  302. [2, 1, 2],
  303. [4, 5, 4],
  304. ];
  305. }
  306. /**
  307. * @test variance
  308. * @dataProvider dataProviderForVariance
  309. * @param float $a
  310. * @param float $b
  311. * @param float $expected
  312. */
  313. public function testVariance(float $a, float $b, float $expected)
  314. {
  315. // Given
  316. $pareto = new Pareto($a, $b);
  317. // When
  318. $variance = $pareto->variance();
  319. // Then
  320. $this->assertEqualsWithDelta($expected, $variance, 0.0000001);
  321. }
  322. /**
  323. * @return array [a, b, σ²]
  324. */
  325. public function dataProviderForVariance(): array
  326. {
  327. return [
  328. [1, 1, \INF],
  329. [2, 2, \INF],
  330. [2, 1, \INF],
  331. [3, 1, 0.75],
  332. [3, 2, 3],
  333. [4, 3, 2],
  334. ];
  335. }
  336. }