TheilSenTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace MathPHP\Tests\Statistics\Regression;
  3. use MathPHP\Statistics\Regression\TheilSen;
  4. class TheilSenTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test constructor
  8. */
  9. public function testConstructor()
  10. {
  11. // Given
  12. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  13. // When
  14. $regression = new TheilSen($points);
  15. // Then
  16. $this->assertInstanceOf(\MathPHP\Statistics\Regression\Regression::class, $regression);
  17. $this->assertInstanceOf(\MathPHP\Statistics\Regression\TheilSen::class, $regression);
  18. }
  19. /**
  20. * @test getPoints
  21. */
  22. public function testGetPoints()
  23. {
  24. // Given
  25. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  26. // When
  27. $regression = new TheilSen($points);
  28. // Then
  29. $this->assertEquals($points, $regression->getPoints());
  30. }
  31. /**
  32. * @test getXs
  33. */
  34. public function testGetXs()
  35. {
  36. // Given
  37. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  38. // When
  39. $regression = new TheilSen($points);
  40. // Then
  41. $this->assertEquals([1,2,4,5,6], $regression->getXs());
  42. }
  43. /**
  44. * @test getYs
  45. */
  46. public function testGetYs()
  47. {
  48. // Given
  49. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  50. // When
  51. $regression = new TheilSen($points);
  52. // Then
  53. $this->assertEquals([2,3,5,7,8], $regression->getYs());
  54. }
  55. /**
  56. * @test getEquation - Equation matches pattern y = mx + b
  57. * @dataProvider dataProviderForEquation
  58. * @param array $points
  59. */
  60. public function testGetEquation(array $points)
  61. {
  62. // Given
  63. $regression = new TheilSen($points);
  64. // Then
  65. $this->assertRegExp('/^y = \d+[.]\d+x [+] \d+[.]\d+$/', $regression->getEquation());
  66. }
  67. /**
  68. * @return array [points]
  69. */
  70. public function dataProviderForEquation(): array
  71. {
  72. return [
  73. [ [ [0,0], [1,1], [2,2], [3,3], [4,4] ] ],
  74. ];
  75. }
  76. /**
  77. * @test getParameters
  78. * @dataProvider dataProviderForParameters
  79. * @param array $points
  80. * @param float $m
  81. * @param float $b
  82. */
  83. public function testGetParameters(array $points, float $m, float $b)
  84. {
  85. // Given
  86. $regression = new TheilSen($points);
  87. // When
  88. $parameters = $regression->getParameters();
  89. // Then
  90. $this->assertEqualsWithDelta($m, $parameters['m'], 0.0001);
  91. $this->assertEqualsWithDelta($b, $parameters['b'], 0.0001);
  92. }
  93. /**
  94. * @return array [points, m, b]
  95. */
  96. public function dataProviderForParameters(): array
  97. {
  98. return [
  99. [
  100. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  101. 1.225, 0.1
  102. ],
  103. ];
  104. }
  105. /**
  106. * @test getSampleSize
  107. * @dataProvider dataProviderForSampleSize
  108. * @param array $points
  109. * @param int $n
  110. */
  111. public function testGetSampleSize(array $points, int $n)
  112. {
  113. // Given
  114. $regression = new TheilSen($points);
  115. // Then
  116. $this->assertEquals($n, $regression->getSampleSize());
  117. }
  118. /**
  119. * @return array [points, n]
  120. */
  121. public function dataProviderForSampleSize()
  122. {
  123. return [
  124. [
  125. [ [1,2], [2,3], [4,5], [5,7], [6,8] ], 5
  126. ],
  127. ];
  128. }
  129. /**
  130. * @test evaluate
  131. * @dataProvider dataProviderForEvaluate
  132. * @param array $points
  133. * @param float $x
  134. * @param float $y
  135. */
  136. public function testEvaluate(array $points, float $x, float $y)
  137. {
  138. // Given
  139. $regression = new TheilSen($points);
  140. // Then
  141. $this->assertEquals($y, $regression->evaluate($x));
  142. }
  143. /**
  144. * @return array [points, x, y]
  145. */
  146. public function dataProviderForEvaluate(): array
  147. {
  148. return [
  149. [
  150. [ [0,0], [1,1], [2,2], [3,3], [4,4] ], // y = x + 0
  151. 5, 5,
  152. ],
  153. [
  154. [ [0,0], [1,1], [2,2], [3,3], [4,4] ], // y = x + 0
  155. 18, 18,
  156. ],
  157. [
  158. [ [0,0], [1,2], [2,4], [3,6] ], // y = 2x + 0
  159. 4, 8,
  160. ],
  161. [
  162. [ [0,1], [1,3.5], [2,6] ], // y = 2.5x + 1
  163. 5, 13.5
  164. ],
  165. [
  166. [ [0,2], [1,1], [2,0], [3,-1] ], // y = -x - 2
  167. 4, -2
  168. ],
  169. ];
  170. }
  171. }