CauchyTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\Cauchy;
  4. class CauchyTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $x₀
  11. * @param float $γ
  12. * @param float $expected_pdf
  13. */
  14. public function testPdf(float $x, float $x₀, float $γ, float $expected_pdf)
  15. {
  16. // Given
  17. $cauchy = new Cauchy($x₀, $γ);
  18. // When
  19. $pdf = $cauchy->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expected_pdf, $pdf, 0.000000001);
  22. }
  23. /**
  24. * @return array [x, x₀, γ, pdf]
  25. * Generated with http://keisan.casio.com/exec/system/1180573169
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [1, 0, 1, 0.1591549430918953357689],
  31. [1, 0, 2, 0.1273239544735162686151],
  32. [1, 0, 3, 0.09549296585513720146133],
  33. [1, 0, 4, 0.07489644380795074624418],
  34. [1, 0, 5, 0.06121343965072897529573],
  35. [1, 0, 6, 0.05161781938115524403315],
  36. [0, 1, 1, 0.1591549430918953357689],
  37. [0, 1, 2, 0.1273239544735162686151],
  38. [0, 1, 3, 0.09549296585513720146133],
  39. [0, 1, 4, 0.07489644380795074624418],
  40. [0, 1, 5, 0.06121343965072897529573],
  41. [0, 1, 6, 0.05161781938115524403315],
  42. [1, 1, 1, 0.3183098861837906715378],
  43. [2, 3, 4, 0.07489644380795074624418],
  44. [4, 3, 2, 0.1273239544735162686151],
  45. [5, 5, 5, 0.06366197723675813430755],
  46. [-20, 7.3, 4.3, 0.001792050735277566691472],
  47. [-3, 7.3, 4.3, 0.01098677565090945486926],
  48. [-2, 7.3, 4.3, 0.01303803115441322049545],
  49. [-1, 7.3, 4.3, 0.01566413951236323973006],
  50. [0, 7.3, 4.3, 0.01906843843118277915314],
  51. [1, 7.3, 4.3, 0.02352582520780852333469],
  52. [2, 7.3, 4.3, 0.0293845536837762964279],
  53. [3, 7.3, 4.3, 0.03701277746323147343462],
  54. [20, 7.3, 4.3, 0.007613374739071642494229],
  55. ];
  56. }
  57. /**
  58. * @test cdf
  59. * @dataProvider dataProviderForCdf
  60. * @param float $x
  61. * @param float $x₀
  62. * @param float $γ
  63. * @param float $expected_cdf
  64. */
  65. public function testCdf(float $x, float $x₀, float $γ, float $expected_cdf)
  66. {
  67. // Given
  68. $cauchy = new Cauchy($x₀, $γ);
  69. // When
  70. $cdf = $cauchy->cdf($x);
  71. // Then
  72. $this->assertEqualsWithDelta($expected_cdf, $cdf, 0.000000001);
  73. }
  74. /**
  75. * @test inverse of CDF is original support x
  76. * @dataProvider dataProviderForCdf
  77. * @param float $x
  78. * @param float $x₀
  79. * @param float $γ
  80. */
  81. public function testInverseOfCdf(float $x, float $x₀, float $γ)
  82. {
  83. // Given
  84. $cauchy = new Cauchy($x₀, $γ);
  85. $cdf = $cauchy->cdf($x);
  86. // When
  87. $inverse_of_cdf = $cauchy->inverse($cdf);
  88. // Then
  89. $this->assertEqualsWithDelta($x, $inverse_of_cdf, 0.000000001);
  90. }
  91. /**
  92. * @return array [x, x₀, γ, cdf]
  93. * Generated with http://keisan.casio.com/exec/system/1180573169
  94. */
  95. public function dataProviderForCdf(): array
  96. {
  97. return [
  98. [1, 0, 1, 0.75],
  99. [1, 0, 2, 0.6475836176504332741754],
  100. [1, 0, 3, 0.6024163823495667258246],
  101. [1, 0, 4, 0.5779791303773693254605],
  102. [1, 0, 5, 0.5628329581890011838138],
  103. [1, 0, 6, 0.5525684567112534299508],
  104. [0, 1, 1, 0.25],
  105. [0, 1, 2, 0.3524163823495667258246],
  106. [0, 1, 3, 0.3975836176504332741754],
  107. [0, 1, 4, 0.4220208696226306745395],
  108. [0, 1, 5, 0.4371670418109988161863],
  109. [0, 1, 6, 0.4474315432887465700492],
  110. [1, 1, 1, 0.5],
  111. [2, 3, 4, 0.4220208696226306745395],
  112. [4, 3, 2, 0.6475836176504332741754],
  113. [5, 5, 5, 0.5],
  114. [-20, 7.3, 4.3, 0.04972817023155424541129],
  115. [-3, 7.3, 4.3, 0.1258852891111436766445],
  116. [-2, 7.3, 4.3, 0.1378566499474175095298],
  117. [-1, 7.3, 4.3, 0.1521523453170898354801],
  118. [0, 7.3, 4.3, 0.1694435179635968563959],
  119. [1, 7.3, 4.3, 0.1906393755555404651458],
  120. [2, 7.3, 4.3, 0.2169618719223694455636],
  121. [3, 7.3, 4.3, 0.25],
  122. [20, 7.3, 4.3, 0.8960821670587991836005],
  123. ];
  124. }
  125. /**
  126. * @dataProvider dataProviderForInverse
  127. * @param float $p
  128. * @param float $x₀
  129. * @param float $γ
  130. * @param float $expected_inverse
  131. */
  132. public function testInverse(float $p, float $x₀, float $γ, float $expected_inverse)
  133. {
  134. // Given
  135. $cauchy = new Cauchy($x₀, $γ);
  136. // When
  137. $inverse = $cauchy->inverse($p);
  138. // Then
  139. $this->assertEqualsWithDelta($expected_inverse, $inverse, 0.000001);
  140. }
  141. /**
  142. * @return array [$p, $x₀, $γ, expected_inverse]
  143. * Generated with R (stats) qcauchy(p, location, scale)
  144. */
  145. public function dataProviderForInverse(): array
  146. {
  147. return [
  148. [0.1, 1, 1, -2.077684],
  149. [0.3, 1, 1, 0.2734575],
  150. [0.5, 1, 1, 1],
  151. [0.7, 1, 1, 1.726543],
  152. [0.9, 1, 1, 4.077684],
  153. [0.1, 2, 3, -7.233051],
  154. [0.3, 2, 3, -0.1796276],
  155. [0.5, 2, 3, 2],
  156. [0.7, 2, 3, 4.179628],
  157. [0.9, 2, 3, 11.23305],
  158. ];
  159. }
  160. /**
  161. * @test mean is not a number
  162. * @dataProvider dataProviderForAverages
  163. * @param float $x₀
  164. * @param float $γ
  165. */
  166. public function testMean(float $x₀, float $γ)
  167. {
  168. // Given
  169. $cauchy = new Cauchy($x₀, $γ);
  170. // When
  171. $mean = $cauchy->mean();
  172. // Then
  173. $this->assertNan($mean);
  174. }
  175. /**
  176. * @test median is $x₀
  177. * @dataProvider dataProviderForAverages
  178. * @param float $x₀
  179. * @param float $γ
  180. */
  181. public function testMedian(float $x₀, float $γ)
  182. {
  183. // Given
  184. $cauchy = new Cauchy($x₀, $γ);
  185. // When
  186. $median = $cauchy->median();
  187. // Then
  188. $this->assertEquals($x₀, $median);
  189. }
  190. /**
  191. * @test mode is $x₀
  192. * @dataProvider dataProviderForAverages
  193. * @param float $x₀
  194. * @param float $γ
  195. */
  196. public function testMode(float $x₀, float $γ)
  197. {
  198. // Given
  199. $cauchy = new Cauchy($x₀, $γ);
  200. // When
  201. $mode = $cauchy->mode();
  202. // Then
  203. $this->assertEquals($x₀, $mode);
  204. }
  205. /**
  206. * @test variance is not a number
  207. * @dataProvider dataProviderForAverages
  208. * @param float $x₀
  209. * @param float $γ
  210. */
  211. public function testVariance(float $x₀, float $γ)
  212. {
  213. // Given
  214. $cauchy = new Cauchy($x₀, $γ);
  215. // When
  216. $variance = $cauchy->variance();
  217. // Then
  218. $this->assertNan($variance);
  219. }
  220. /**
  221. * @return array [x₀, γ]
  222. */
  223. public function dataProviderForAverages(): array
  224. {
  225. return [
  226. [-1, 0.1],
  227. [-1, 1],
  228. [-1, 2],
  229. [0, 0.1],
  230. [0, 1],
  231. [0, 2],
  232. [1, 0.1],
  233. [1, 1],
  234. [1, 2],
  235. [2, 3],
  236. [5, 3],
  237. ];
  238. }
  239. /**
  240. * @test rand
  241. */
  242. public function testRand()
  243. {
  244. foreach (\range(-5, 5) as $x₀) {
  245. foreach (\range(1, 10) as $γ) {
  246. // Given
  247. $cauchy = new Cauchy($x₀, $γ);
  248. foreach (\range(1, 3) as $_) {
  249. // When
  250. $random = $cauchy->rand();
  251. // Then
  252. $this->assertTrue(\is_numeric($random));
  253. }
  254. }
  255. }
  256. }
  257. }