WeibullTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Weibull;
  4. class WeibullTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $k
  11. * @param float $λ
  12. * @param float $expected_pdf
  13. */
  14. public function testPdf(float $x, float $k, float $λ, float $expected_pdf)
  15. {
  16. // Given
  17. $weibull = new Weibull($k, $λ);
  18. // When
  19. $pdf = $weibull->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expected_pdf, $pdf, 0.0000001);
  22. }
  23. /**
  24. * @return array [x, k, λ. pdf]
  25. * Generated with R dweibull(x, shape, scale)
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [-1, 1, 1, 0],
  31. [0, 1, 1, 1],
  32. [1, 1, 1, 0.3678794],
  33. [2, 1, 1, 0.1353353],
  34. [3, 1, 1, 0.04978707],
  35. [4, 1, 1, 0.01831564],
  36. [5, 1, 1, 0.006737947],
  37. [10, 1, 1, 4.539993e-05],
  38. [-1, 1, 2, 0],
  39. [0, 1, 2, 0.5],
  40. [1, 1, 2, 0.3032653],
  41. [2, 1, 2, 0.1839397],
  42. [3, 1, 2, 0.1115651],
  43. [4, 1, 2, 0.06766764],
  44. [5, 1, 2, 0.0410425],
  45. [10, 1, 2, 0.003368973],
  46. [-1, 2, 1, 0],
  47. [0, 2, 1, 0],
  48. [1, 2, 1, 0.7357589],
  49. [2, 2, 1, 0.07326256],
  50. [3, 2, 1, 0.0007404588],
  51. [4, 2, 1, 9.002814e-07],
  52. [5, 2, 1, 1.388794e-10],
  53. [10, 2, 1, 7.440152e-43],
  54. [3, 4, 5, 0.1517956],
  55. [3, 5, 5, 0.1199042],
  56. [33, 34, 45, 2.711453e-05],
  57. ];
  58. }
  59. /**
  60. * @test cdf
  61. * @dataProvider dataProviderForCdf
  62. * @param float $x
  63. * @param float $k
  64. * @param float $λ
  65. * @param float $expected_cdf
  66. */
  67. public function testCdf(float $x, float $k, float $λ, float $expected_cdf)
  68. {
  69. // Given
  70. $weibull = new Weibull($k, $λ);
  71. // When
  72. $cdf = $weibull->cdf($x);
  73. // Then
  74. $this->assertEqualsWithDelta($expected_cdf, $cdf, 0.0000001);
  75. }
  76. /**
  77. * @return array [x, k, λ, cdf]
  78. * Generated with R pweibull(x, shape, scale)
  79. */
  80. public function dataProviderForCdf(): array
  81. {
  82. return [
  83. [-1, 1, 1, 0],
  84. [0, 1, 1, 0],
  85. [1, 1, 1, 0.6321206],
  86. [2, 1, 1, 0.8646647],
  87. [3, 1, 1, 0.9502129],
  88. [4, 1, 1, 0.9816844],
  89. [5, 1, 1, 0.9932621],
  90. [10, 1, 1, 0.9999546],
  91. [-1, 1, 2, 0],
  92. [0, 1, 2, 0],
  93. [1, 1, 2, 0.3934693],
  94. [2, 1, 2, 0.6321206],
  95. [3, 1, 2, 0.7768698],
  96. [4, 1, 2, 0.8646647],
  97. [5, 1, 2, 0.917915],
  98. [10, 1, 2, 0.9932621],
  99. [-1, 2, 1, 0],
  100. [0, 2, 1, 0],
  101. [1, 2, 1, 0.6321206],
  102. [2, 2, 1, 0.9816844],
  103. [3, 2, 1, 0.9998766],
  104. [4, 2, 1, 0.9999999],
  105. [5, 2, 1, 1],
  106. [3, 4, 5, 0.1215533],
  107. [3, 5, 5, 0.07481356],
  108. [33, 34, 45, 2.631739e-05],
  109. ];
  110. }
  111. /**
  112. * @dataProvider dataProviderForInverse
  113. * @param float $p
  114. * @param float $k
  115. * @param float $λ
  116. * @param float $expected_inverse
  117. */
  118. public function testInverse(float $p, float $k, float $λ, float $expected_inverse)
  119. {
  120. // Given
  121. $weibull = new Weibull($k, $λ);
  122. // When
  123. $inverse = $weibull->inverse($p);
  124. // Then
  125. $this->assertEqualsWithDelta($expected_inverse, $inverse, 0.000001);
  126. }
  127. /**
  128. * @return array [x, k, λ, inverse]
  129. * Generated with R (stats) qweibull(p, shape, scale)
  130. */
  131. public function dataProviderForInverse(): array
  132. {
  133. return [
  134. [0, 1, 1, 0],
  135. [0.1, 1, 1, 0.1053605],
  136. [0.3, 1, 1, 0.3566749],
  137. [0.5, 1, 1, 0.6931472],
  138. [0.7, 1, 1, 1.203973],
  139. [0.9, 1, 1, 2.302585],
  140. [1, 1, 1, \INF],
  141. [0, 2, 3, 0],
  142. [0.1, 2, 3, 0.9737785],
  143. [0.3, 2, 3, 1.791668],
  144. [0.5, 2, 3, 2.497664],
  145. [0.7, 2, 3, 3.291771],
  146. [0.9, 2, 3, 4.552281],
  147. [1, 2, 3, \INF],
  148. ];
  149. }
  150. /**
  151. * @test inverse of cdf is x
  152. * @dataProvider dataProviderForInverseOfCdf
  153. * @param float $x
  154. * @param float $k
  155. * @param float $λ
  156. */
  157. public function testInverseOfCdf(float $x, float $k, float $λ)
  158. {
  159. // Given
  160. $weibull = new Weibull($k, $λ);
  161. $cdf = $weibull->cdf($x);
  162. // When
  163. $inverse_of_cdf = $weibull->inverse($cdf);
  164. // Then
  165. $this->assertEqualsWithDelta($x, $inverse_of_cdf, 0.000001);
  166. }
  167. /**
  168. * @return array [x, k, λ]
  169. */
  170. public function dataProviderForInverseOfCdf(): array
  171. {
  172. return [
  173. [1, 1, 1],
  174. [2, 1, 1],
  175. [3, 1, 1],
  176. [4, 1, 1],
  177. [5, 1, 1],
  178. [10, 1, 1],
  179. [1, 1, 2],
  180. [2, 1, 2],
  181. [3, 1, 2],
  182. [4, 1, 2],
  183. [5, 1, 2],
  184. [10, 1, 2],
  185. [1, 2, 1],
  186. [2, 2, 1],
  187. [3, 2, 1],
  188. [4, 2, 1],
  189. [5, 2, 1],
  190. [3, 4, 5],
  191. [3, 5, 5],
  192. [33, 34, 45],
  193. ];
  194. }
  195. /**
  196. * @test mean
  197. * @dataProvider dataProviderForMean
  198. * @param float $k
  199. * @param float $λ
  200. * @param float $μ
  201. */
  202. public function testMean(float $k, float $λ, float $μ)
  203. {
  204. // Given
  205. $weibull = new Weibull($k, $λ);
  206. // When
  207. $mean = $weibull->mean();
  208. // Then
  209. $this->assertEqualsWithDelta($μ, $mean, 0.0001);
  210. }
  211. /**
  212. * @return array [k, λ, μ]
  213. */
  214. public function dataProviderForMean(): array
  215. {
  216. return [
  217. [1, 1, 1],
  218. [1, 2, 2],
  219. [2, 1, 0.88622692545275801365],
  220. [2, 2, 1.77245386],
  221. [22, 27, 26.34458072],
  222. ];
  223. }
  224. /**
  225. * @test median
  226. * @dataProvider dataProviderForMedian
  227. * @param float $k
  228. * @param float $λ
  229. * @param float $μ
  230. */
  231. public function testMedian(float $k, float $λ, float $μ)
  232. {
  233. // Given
  234. $weibull = new Weibull($k, $λ);
  235. // When
  236. $median = $weibull->median();
  237. // Then
  238. $this->assertEqualsWithDelta($μ, $median, 0.0001);
  239. }
  240. /**
  241. * Data generated with https://captaincalculator.com/math/statistics/weibull-distribution-calculator/
  242. * @return array [k, λ, μ]
  243. */
  244. public function dataProviderForMedian(): array
  245. {
  246. return [
  247. [1, 1, 0.69314718055995],
  248. [2, 1, 0.83255461],
  249. [1, 2, 1.38629436],
  250. [2, 2, 1.66510922],
  251. [22, 27, 26.55391482],
  252. ];
  253. }
  254. /**
  255. * @test mode
  256. * @dataProvider dataProviderForMode
  257. * @param float $k
  258. * @param float $λ
  259. * @param float $μ
  260. */
  261. public function testMode(float $k, float $λ, float $μ)
  262. {
  263. // Given
  264. $weibull = new Weibull($k, $λ);
  265. // When
  266. $mode = $weibull->mode();
  267. // Then
  268. $this->assertEqualsWithDelta($μ, $mode, 0.0001);
  269. }
  270. /**
  271. * Data generated with https://captaincalculator.com/math/statistics/weibull-distribution-calculator/
  272. * @return array [k, λ, μ]
  273. */
  274. public function dataProviderForMode(): array
  275. {
  276. return [
  277. [0.5, 1, 0],
  278. [1, 1, 0],
  279. [2, 1, 0.70710678],
  280. [1, 2, 0],
  281. [2, 2, 1.41421356],
  282. [22, 27, 26.94296757],
  283. ];
  284. }
  285. /**
  286. * @test rand
  287. */
  288. public function testRand()
  289. {
  290. foreach (\range(1, 10) as $k) {
  291. foreach (\range(1, 10) as $λ) {
  292. // Given
  293. $weibull = new Weibull($k, $λ);
  294. foreach (\range(1, 3) as $_) {
  295. // When
  296. $random = $weibull->rand();
  297. // Then
  298. $this->assertTrue(\is_numeric($random));
  299. }
  300. }
  301. }
  302. }
  303. }