DiracDeltaTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace MathPHP\Tests\Probability\Distribution\Continuous;
  3. use MathPHP\Probability\Distribution\Continuous\DiracDelta;
  4. class DiracDeltaTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test pdf
  8. * @dataProvider dataProviderForPdf
  9. * @param float $x
  10. * @param float $expectedPdf
  11. */
  12. public function testPdf(float $x, float $expectedPdf)
  13. {
  14. // Given
  15. $dirac = new DiracDelta();
  16. // When
  17. $pdf = $dirac->pdf($x);
  18. // Then
  19. $this->assertEquals($expectedPdf, $pdf);
  20. }
  21. /**
  22. * @return array [x, pdf]
  23. */
  24. public function dataProviderForPdf(): array
  25. {
  26. return [
  27. [-100, 0],
  28. [-12, 0],
  29. [-2, 0],
  30. [-1, 0],
  31. [-0.5, 0],
  32. [0, \INF],
  33. [0.5, 0],
  34. [1, 0],
  35. [2, 0],
  36. [12, 0],
  37. [100, 0],
  38. ];
  39. }
  40. /**
  41. * @testCase cdf
  42. * @dataProvider dataProviderForCdf
  43. * @param float $x
  44. * @param int $expectedCdf
  45. */
  46. public function testCdf(float $x, int $expectedCdf)
  47. {
  48. // Given
  49. $dirac = new DiracDelta();
  50. // When
  51. $cdf = $dirac->cdf($x);
  52. // Then
  53. $this->assertSame($expectedCdf, $cdf);
  54. }
  55. /**
  56. * @return array [x, cdf]
  57. */
  58. public function dataProviderForCdf(): array
  59. {
  60. return [
  61. [-100, 0],
  62. [-12, 0],
  63. [-2, 0],
  64. [-1, 0],
  65. [-0.5, 0],
  66. [0, 1],
  67. [0.5, 1],
  68. [1, 1],
  69. [2, 1],
  70. [12, 1],
  71. [100, 1],
  72. ];
  73. }
  74. /**
  75. * @testCase inverse is always 0
  76. */
  77. public function testInverse()
  78. {
  79. // Given
  80. $diracDelta = new DiracDelta();
  81. foreach (\range(-10, 10, 0.5) as $p) {
  82. // When
  83. $inverse = $diracDelta->inverse($p);
  84. // Then
  85. $this->assertEquals(0, $inverse);
  86. }
  87. }
  88. /**
  89. * @testCase rand is always 0
  90. */
  91. public function testRand()
  92. {
  93. // Given
  94. $diracDelta = new DiracDelta();
  95. foreach (\range(-10, 10, 0.5) as $_) {
  96. // When
  97. $rand = $diracDelta->rand();
  98. // Then
  99. $this->assertEquals(0, $rand);
  100. }
  101. }
  102. /**
  103. * @testCase mean is always 0
  104. */
  105. public function testMean()
  106. {
  107. // Given
  108. $diracDelta = new DiracDelta();
  109. foreach (\range(-10, 10, 0.5) as $_) {
  110. // When
  111. $mean = $diracDelta->mean();
  112. // Then
  113. $this->assertEquals(0, $mean);
  114. }
  115. }
  116. /**
  117. * @testCase median is always 0
  118. */
  119. public function testMedian()
  120. {
  121. // Given
  122. $diracDelta = new DiracDelta();
  123. foreach (\range(-10, 10, 0.5) as $_) {
  124. // When
  125. $median = $diracDelta->median();
  126. // Then
  127. $this->assertEquals(0, $median);
  128. }
  129. }
  130. /**
  131. * @testCase mode is always 0
  132. */
  133. public function testMode()
  134. {
  135. // Given
  136. $diracDelta = new DiracDelta();
  137. foreach (\range(-10, 10, 0.5) as $_) {
  138. // When
  139. $mode = $diracDelta->mode();
  140. // Then
  141. $this->assertEquals(0, $mode);
  142. }
  143. }
  144. }