ZipfTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Discrete;
  3. use MathPHP\Exception;
  4. use MathPHP\Probability\Distribution\Discrete\Zipf;
  5. class ZipfTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test pmf
  9. * @dataProvider dataProviderForPmf
  10. * @param int $k
  11. * @param number $s
  12. * @param int $N
  13. * @param float $expectedPmf
  14. *
  15. * R code to replicate:
  16. * library(sads)
  17. * dzipf(x=k, N=N, s=s)
  18. */
  19. public function testPmf(int $k, $s, int $N, float $expectedPmf)
  20. {
  21. // Given
  22. $zipf = new Zipf($s, $N);
  23. // When
  24. $pmf = $zipf->pmf($k);
  25. // Then
  26. $this->assertEqualsWithDelta($expectedPmf, $pmf, 0.001);
  27. }
  28. /**
  29. * @return array [k, s, N, pmf]
  30. */
  31. public function dataProviderForPmf(): array
  32. {
  33. return [
  34. [1, 3, 10, 0.8350508],
  35. [2, 3, 10, 0.1043813],
  36. [3, 3, 10, 0.03092781],
  37. [4, 3, 10, 0.01304767],
  38. [4, 2, 10, 0.04032862],
  39. [4, 1, 10, 0.08535429],
  40. [4, 1, 8, 0.09198423],
  41. ];
  42. }
  43. /**
  44. * @test pmfthrows a BadDataException if k > N
  45. * @throws \Exception
  46. */
  47. public function testBadPmfK()
  48. {
  49. // Given
  50. $k = 11;
  51. $zipf = new Zipf(3, 10);
  52. // Then
  53. $this->expectException(Exception\OutOfBoundsException::class);
  54. // When
  55. $pmf = $zipf->pmf($k);
  56. }
  57. /**
  58. * @test cdf
  59. * @dataProvider dataProviderForCdf
  60. * @param int $k
  61. * @param number $s
  62. * @param int $N
  63. * @param float $expectedCdf
  64. *
  65. * R code to replicate:
  66. * library(sads)
  67. * pzipf(q=x, N=N, s=s)
  68. */
  69. public function testCdf(int $k, $s, int $N, float $expectedCdf)
  70. {
  71. // Given
  72. $zipf = new Zipf($s, $N);
  73. // When
  74. $cdf = $zipf->cdf($k);
  75. // Then
  76. $this->assertEqualsWithDelta($expectedCdf, $cdf, 0.001);
  77. }
  78. /**
  79. * @test pmfthrows a BadDataException if k > N
  80. * @throws \Exception
  81. */
  82. public function testBadCdfK()
  83. {
  84. // Given
  85. $k = 11;
  86. $zipf = new Zipf(3, 10);
  87. // Then
  88. $this->expectException(Exception\OutOfBoundsException::class);
  89. // When
  90. $pmf = $zipf->cdf($k);
  91. }
  92. /**
  93. * @return array[k, s, N, cdf]
  94. */
  95. public function dataProviderForCdf(): array
  96. {
  97. return [
  98. [1, 3, 10, 0.8350508],
  99. [2, 3, 10, 0.9394321],
  100. [3, 3, 10, 0.9703599],
  101. [4, 3, 10, 0.9834076],
  102. [4, 2, 10, 0.9185964],
  103. [4, 1, 10, 0.7112857],
  104. [4, 1, 8, 0.7665353],
  105. ];
  106. }
  107. /**
  108. * @test mode
  109. * @dataProvider dataProviderForMode
  110. * @param number $s
  111. * @param int $N
  112. * @param int $expected_mode
  113. */
  114. public function testMode($s, int $N, int $expected_mode)
  115. {
  116. // Given
  117. $zipf = new Zipf($s, $N);
  118. // When
  119. $mode = $zipf->mode();
  120. // Then
  121. $this->assertEquals($expected_mode, $mode);
  122. }
  123. /**
  124. * @return array[s, N, mode]
  125. */
  126. public function dataProviderForMode(): array
  127. {
  128. return [
  129. [3, 10, 1],
  130. [2, 10, 1],
  131. [1, 10, 1],
  132. [1, 8, 1],
  133. ];
  134. }
  135. /**
  136. * @test mean
  137. * @dataProvider dataProviderForMean
  138. * @param number $s
  139. * @param int $N
  140. * @param float $expected_mean
  141. *
  142. * R code to replicate:
  143. * library(sads)
  144. * k <- 1:N
  145. * sum(dzipf(x=k, N=N, s=s) * k)
  146. */
  147. public function testMean($s, int $N, float $expected_mean)
  148. {
  149. // Given
  150. $zipf = new Zipf($s, $N);
  151. // When
  152. $mean = $zipf->mean();
  153. // Then
  154. $this->assertEqualsWithDelta($expected_mean, $mean, .001);
  155. }
  156. /**
  157. * @return array[s, N, mean]
  158. */
  159. public function dataProviderForMean(): array
  160. {
  161. return [
  162. [3, 10, 1.294135],
  163. [2, 10, 1.88994],
  164. [1, 10, 3.414172],
  165. [1, 8, 2.943495],
  166. ];
  167. }
  168. }