FTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\F;
  4. class FTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param int $x
  10. * @param int $d₁
  11. * @param int $d₂
  12. * @param float $expectedPdf
  13. */
  14. public function testPdf(int $x, int $d₁, int $d₂, float $expectedPdf)
  15. {
  16. // Given
  17. $f = new F($d₁, $d₂);
  18. // When
  19. $pdf = $f->pdf($x);
  20. // Then
  21. $this->assertEqualsWithDelta($expectedPdf, $pdf, 0.00001);
  22. }
  23. /**
  24. * @return array [x, d₁, d₂, pdf]
  25. * Generated with R df(x, d₁, d₂)
  26. */
  27. public function dataProviderForPdf(): array
  28. {
  29. return [
  30. [1, 1, 1, 0.1591549],
  31. [2, 1, 1, 0.07502636],
  32. [3, 1, 1, 0.04594407],
  33. [4, 1, 1, 0.03183099],
  34. [5, 1, 1, 0.02372542],
  35. [10, 1, 1, 0.009150766],
  36. [1, 2, 1, 0.1924501],
  37. [2, 2, 1, 0.08944272],
  38. [3, 2, 1, 0.05399492],
  39. [4, 2, 1, 0.03703704],
  40. [5, 2, 1, 0.02741012],
  41. [10, 2, 1, 0.01039133],
  42. [1, 1, 2, 0.1924501],
  43. [2, 1, 2, 0.08838835],
  44. [3, 1, 2, 0.05163978],
  45. [4, 1, 2, 0.03402069],
  46. [5, 1, 2, 0.02414726],
  47. [10, 1, 2, 0.007607258],
  48. [1, 2, 2, 0.25],
  49. [2, 2, 2, 0.1111111],
  50. [3, 2, 2, 0.0625],
  51. [4, 2, 2, 0.04],
  52. [5, 2, 2, 0.02777778],
  53. [10, 2, 2, 0.008264463],
  54. [5, 3, 7, 0.01667196],
  55. [7, 6, 2, 0.016943],
  56. [7, 20, 14, 0.0002263343],
  57. [45, 2, 3, 0.0001868942],
  58. ];
  59. }
  60. /**
  61. * @test cdf
  62. * @dataProvider dataProviderForCdf
  63. * @param int $x
  64. * @param int $d₁
  65. * @param int $d₂
  66. * @param float $expectedCdf
  67. */
  68. public function testCdf(int $x, int $d₁, int $d₂, float $expectedCdf)
  69. {
  70. // Given
  71. $f = new F($d₁, $d₂);
  72. // When
  73. $cdf = $f->cdf($x);
  74. // Then
  75. $this->assertEqualsWithDelta($expectedCdf, $cdf, 0.00001);
  76. }
  77. /**
  78. * @return array [x, d₁, d₂, cdf]
  79. * Generated with R pf(x, d₁, d₂)
  80. */
  81. public function dataProviderForCdf(): array
  82. {
  83. return [
  84. [0, 1, 1, 0],
  85. [0, 1, 2, 0],
  86. [0, 2, 1, 0],
  87. [0, 2, 2, 0],
  88. [0, 2, 3, 0],
  89. [1, 1, 1, 0.5],
  90. [2, 1, 1, 0.6081734],
  91. [3, 1, 1, 0.6666667],
  92. [4, 1, 1, 0.7048328],
  93. [5, 1, 1, 0.7322795],
  94. [10, 1, 1, 0.8050178],
  95. [1, 2, 1, 0.4226497],
  96. [2, 2, 1, 0.5527864],
  97. [3, 2, 1, 0.6220355],
  98. [4, 2, 1, 0.6666667],
  99. [5, 2, 1, 0.6984887],
  100. [10, 2, 1, 0.7817821],
  101. [1, 1, 2, 0.5773503],
  102. [2, 1, 2, 0.7071068],
  103. [3, 1, 2, 0.7745967],
  104. [4, 1, 2, 0.8164966],
  105. [5, 1, 2, 0.8451543],
  106. [10, 1, 2, 0.9128709],
  107. [1, 2, 2, 0.5],
  108. [2, 2, 2, 0.6666667],
  109. [3, 2, 2, 0.75],
  110. [4, 2, 2, 0.8],
  111. [5, 2, 2, 0.8333333],
  112. [10, 2, 2, 0.9090909],
  113. [5, 3, 7, 0.9633266],
  114. [7, 6, 2, 0.8697408],
  115. [7, 20, 14, 0.9997203],
  116. [45, 2, 3, 0.9942063],
  117. ];
  118. }
  119. /**
  120. * @test mean
  121. * @dataProvider dataProviderForMean
  122. * @param int $d₁
  123. * @param int $d₂
  124. * @param float $μ
  125. */
  126. public function testMean(int $d₁, int $d₂, float $μ)
  127. {
  128. // Given
  129. $f = new F($d₁, $d₂);
  130. // When
  131. $mean = $f->mean();
  132. // Then
  133. $this->assertEqualsWithDelta($μ, $mean, 0.0001);
  134. }
  135. /**
  136. * @return array [d₁, d₂, $μ]
  137. */
  138. public function dataProviderForMean(): array
  139. {
  140. return [
  141. [1, 3, 3,],
  142. [1, 4, 2],
  143. [1, 5, 1.66666667],
  144. [1, 6, 1.5],
  145. ];
  146. }
  147. /**
  148. * @test mean is not a number if d₂ ≤ 2
  149. * @dataProvider dataProviderForMeanNan
  150. * @param int $d₁
  151. * @param int $d₂
  152. */
  153. public function testMeanNAN(int $d₁, int $d₂)
  154. {
  155. // Given
  156. $f = new F($d₁, $d₂);
  157. // When
  158. $mean = $f->mean();
  159. // Then
  160. $this->assertNan($mean);
  161. }
  162. /**
  163. * @return array [d₁, d₂]
  164. */
  165. public function dataProviderForMeanNan(): array
  166. {
  167. return [
  168. [1, 1],
  169. [1, 2],
  170. ];
  171. }
  172. /**
  173. * @test mode
  174. * @dataProvider dataProviderForMode
  175. * @param int $d₁
  176. * @param int $d₂
  177. * @param float $μ
  178. */
  179. public function testMode(int $d₁, int $d₂, float $μ)
  180. {
  181. // Given
  182. $f = new F($d₁, $d₂);
  183. // When
  184. $mode = $f->mode();
  185. // Then
  186. $this->assertEqualsWithDelta($μ, $mode, 0.0001);
  187. }
  188. /**
  189. * @return array [d₁, d₂, μ]
  190. */
  191. public function dataProviderForMode(): array
  192. {
  193. return [
  194. [3, 1, 0.11111111],
  195. [3, 2, 0.16666667],
  196. [3, 3, 0.2],
  197. [3, 4, 0.22222222],
  198. [4, 1, 0.16666667],
  199. [4, 2, 0.25],
  200. ];
  201. }
  202. /**
  203. * @test mode is not defined for d₁ <= 2
  204. * @dataProvider dataProviderForModeNan
  205. * @param int $d₁
  206. * @param int $d₂
  207. */
  208. public function testModeNan(int $d₁, int $d₂)
  209. {
  210. // Given
  211. $f = new F($d₁, $d₂);
  212. // When
  213. $mode = $f->mode();
  214. // Then
  215. $this->assertNan($mode);
  216. }
  217. /**
  218. * @return array [d₁, d]
  219. */
  220. public function dataProviderForModeNan(): array
  221. {
  222. return [
  223. [1, 1],
  224. [1, 5],
  225. [2, 2],
  226. [2, 4],
  227. ];
  228. }
  229. /**
  230. * @test variance
  231. * @dataProvider dataProviderForVariance
  232. * @param int $d₁
  233. * @param int $d₂
  234. * @param float $expected
  235. */
  236. public function testVariance(int $d₁, int $d₂, float $expected)
  237. {
  238. // Given
  239. $f = new F($d₁, $d₂);
  240. // When
  241. $variance = $f->variance();
  242. // Then
  243. $this->assertEqualsWithDelta($expected, $variance, 0.0001);
  244. }
  245. /**
  246. * @return array [d₁, d₂, variance]
  247. */
  248. public function dataProviderForVariance(): array
  249. {
  250. return [
  251. [1, 5, 22.22222222],
  252. [2, 5, 13.88888889],
  253. [3, 5, 11.11111111],
  254. [4, 5, 9.72222222],
  255. [5, 5, 8.88888889],
  256. [6, 5, 8.33333333],
  257. [5, 7, 2.61333333],
  258. [9, 8, 1.48148148],
  259. ];
  260. }
  261. /**
  262. * @test variance is not defined for d₂ <= 4
  263. * @dataProvider dataProviderForVarianceNan
  264. * @param int $d₁
  265. * @param int $d₂
  266. */
  267. public function testVarianceNan(int $d₁, int $d₂)
  268. {
  269. // Given
  270. $f = new F($d₁, $d₂);
  271. // When
  272. $variance = $f->variance();
  273. // Then
  274. $this->assertNan($variance);
  275. }
  276. /**
  277. * @return array [d₁, d]
  278. */
  279. public function dataProviderForVarianceNan(): array
  280. {
  281. return [
  282. [1, 1],
  283. [1, 2],
  284. [2, 3],
  285. [5, 4],
  286. ];
  287. }
  288. /**
  289. * @test median (temporary version that is just the mean)
  290. * @dataProvider dataProviderForMean
  291. * @todo Rewrite test using actual median values once median calculation is implemented
  292. * @param int $d₁
  293. * @param int $d₂
  294. * @param float $μ
  295. */
  296. public function testMedianTemporaryVersion(int $d₁, int $d₂, float $μ)
  297. {
  298. // Given
  299. $f = new F($d₁, $d₂);
  300. // When
  301. $mean = $f->median();
  302. // Then
  303. $this->assertEqualsWithDelta($μ, $mean, 0.0001);
  304. }
  305. }