LinearThroughPointTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace MathPHP\Tests\Statistics\Regression;
  3. use MathPHP\Statistics\Regression\LinearThroughPoint;
  4. class LinearThroughPointTest 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. $force = [0,0];
  14. // When
  15. $regression = new LinearThroughPoint($points, $force);
  16. // Then
  17. $this->assertInstanceOf(\MathPHP\Statistics\Regression\Regression::class, $regression);
  18. $this->assertInstanceOf(\MathPHP\Statistics\Regression\LinearThroughPoint::class, $regression);
  19. }
  20. /**
  21. * @test getPoints
  22. */
  23. public function testGetPoints()
  24. {
  25. // Given
  26. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  27. $force = [0,0];
  28. // When
  29. $regression = new LinearThroughPoint($points, $force);
  30. // Then
  31. $this->assertEquals($points, $regression->getPoints());
  32. }
  33. /**
  34. * @test getXs
  35. */
  36. public function testGetXs()
  37. {
  38. // Given
  39. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  40. $force = [0,0];
  41. // When
  42. $regression = new LinearThroughPoint($points, $force);
  43. // Then
  44. $this->assertEquals([1,2,4,5,6], $regression->getXs());
  45. }
  46. /**
  47. * @test getYs
  48. */
  49. public function testGetYs()
  50. {
  51. // Given
  52. $points = [ [1,2], [2,3], [4,5], [5,7], [6,8] ];
  53. $force = [0,0];
  54. // When
  55. $regression = new LinearThroughPoint($points, $force);
  56. // Then
  57. $this->assertEquals([2,3,5,7,8], $regression->getYs());
  58. }
  59. /**
  60. * @test getEquation - Equation matches pattern y = mx + b
  61. * @dataProvider dataProviderForEquation
  62. * @param array $points
  63. */
  64. public function testGetEquation(array $points)
  65. {
  66. // Given
  67. $force = [0,0];
  68. // When
  69. $regression = new LinearThroughPoint($points, $force);
  70. // Then
  71. $this->assertRegExp('/^y = [-]?\d+[.]\d+x [+\-] \d+[.]\d+$/', $regression->getEquation());
  72. }
  73. /**
  74. * @return array [points]
  75. */
  76. public function dataProviderForEquation(): array
  77. {
  78. return [
  79. [ [ [0,0], [1,1], [2,2], [3,3], [4,4] ] ],
  80. [ [ [1,2], [2,3], [4,5], [5,7], [6,8] ] ],
  81. [ [ [4,390], [9,580], [10,650], [14,730], [4,410], [7,530], [12,600], [22,790], [1,350], [3,400], [8,590], [11,640], [5,450], [6,520], [10,690], [11,690], [16,770], [13,700], [13,730], [10,640] ] ],
  82. ];
  83. }
  84. /**
  85. * @test getParameters
  86. * @dataProvider dataProviderForParameters
  87. * @param array $points
  88. * @param array $force_point
  89. * @param float $m
  90. * @param float $b
  91. */
  92. public function testGetParameters(array $points, array $force_point, float $m, float $b)
  93. {
  94. // Given
  95. $regression = new LinearThroughPoint($points, $force_point);
  96. // When
  97. $parameters = $regression->getParameters();
  98. // Then
  99. $this->assertEqualsWithDelta($m, $parameters['m'], 0.0001);
  100. $this->assertEqualsWithDelta($b, $parameters['b'], 0.0001);
  101. }
  102. /**
  103. * @return array [points, force_point, m, b]
  104. */
  105. public function dataProviderForParameters(): array
  106. {
  107. return [
  108. [
  109. [ [1,2], [2,3], [4,5], [5,7], [6,8] ], [0,0],
  110. 1.35365853658537, 0
  111. ],
  112. [
  113. [ [4,390], [9,580], [10,650], [14,730], [4,410], [7,530], [12,600], [22,790], [1,350], [3,400], [8,590], [11,640], [5,450], [6,520], [10,690], [11,690], [16,770], [13,700], [13,730], [10,640] ], [0,0],
  114. 54.9003101462118, 0
  115. ],
  116. [
  117. [ [11,15], [17,23], [23,31], [29,39] ], [0,0],
  118. 1.348314, 0
  119. ],
  120. [
  121. [ [100, 140], [200,230], [300,310], [400,400], [500,480] ], [300, 310],
  122. 0.85, 55
  123. ],
  124. ];
  125. }
  126. /**
  127. * @test getSampleSize
  128. * @dataProvider dataProviderForSampleSize
  129. * @param array $points
  130. * @param int $n
  131. */
  132. public function testGetSampleSize(array $points, int $n)
  133. {
  134. // Given
  135. $force = [0,0];
  136. // When
  137. $regression = new LinearThroughPoint($points, $force);
  138. // Then
  139. $this->assertEquals($n, $regression->getSampleSize());
  140. }
  141. /**
  142. * @return array [points, n]
  143. */
  144. public function dataProviderForSampleSize(): array
  145. {
  146. return [
  147. [
  148. [ [1,2], [2,3], [4,5], [5,7], [6,8] ], 5
  149. ],
  150. [
  151. [ [4,390], [9,580], [10,650], [14,730], [4,410], [7,530], [12,600], [22,790], [1,350], [3,400], [8,590], [11,640], [5,450], [6,520], [10,690], [11,690], [16,770], [13,700], [13,730], [10,640] ], 20
  152. ],
  153. ];
  154. }
  155. /**
  156. * @test evaluate
  157. * @dataProvider dataProviderForEvaluate
  158. * @param array $points
  159. * @param float $x
  160. * @param float $y
  161. */
  162. public function testEvaluate(array $points, float $x, float $y)
  163. {
  164. // Given
  165. $force = [0,0];
  166. // When
  167. $regression = new LinearThroughPoint($points, $force);
  168. // Then
  169. $this->assertEqualsWithDelta($y, $regression->evaluate($x), 0.0001);
  170. }
  171. /**
  172. * @return array [points, x, y]
  173. */
  174. public function dataProviderForEvaluate(): array
  175. {
  176. return [
  177. [
  178. [ [0,0], [1,1], [2,2], [3,3], [4,4] ], // y = x + 0
  179. 5, 5,
  180. ],
  181. [
  182. [ [0,0], [1,1], [2,2], [3,3], [4,4] ], // y = x + 0
  183. 18, 18,
  184. ],
  185. [
  186. [ [0,0], [1,2], [2,4], [3,6] ], // y = 2x + 0
  187. 4, 8,
  188. ],
  189. [
  190. [ [0,1], [1,3.5], [2,6] ], // y = 2.5x + 1
  191. 5, 15.5
  192. ],
  193. [
  194. [ [0,2], [1,1], [2,0], [3,-1] ], // y = -x + 2
  195. 4, -0.571428571
  196. ],
  197. ];
  198. }
  199. /**
  200. * @test ci
  201. * @dataProvider dataProviderForCI
  202. * @param array $points
  203. * @param float $x
  204. * @param float $p
  205. * @param float $ci
  206. * @throws \Exception
  207. */
  208. public function testCI(array $points, float $x, float $p, float $ci)
  209. {
  210. // Given
  211. $regression = new LinearThroughPoint($points);
  212. // Then
  213. $this->assertEqualsWithDelta($ci, $regression->ci($x, $p), .0000001);
  214. }
  215. /**
  216. * @return array [points, x, p, ci]
  217. */
  218. public function dataProviderForCI(): array
  219. {
  220. return [
  221. [
  222. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  223. 2, .05, 0.2644479205,
  224. ],
  225. [
  226. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  227. 3, .05, 0.3966718808,
  228. ],
  229. [
  230. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  231. 3, .1, 0.3045778477,
  232. ],
  233. ];
  234. }
  235. /**
  236. * @test PI
  237. * @dataProvider dataProviderForPI
  238. * @param array $points
  239. * @param float $x
  240. * @param float $p
  241. * @param float $q
  242. * @param float $pi
  243. * @throws \Exception
  244. */
  245. public function testPI(array $points, float $x, float $p, float $q, float $pi)
  246. {
  247. // Given
  248. $regression = new LinearThroughPoint($points);
  249. // Then
  250. $this->assertEqualsWithDelta($pi, $regression->pi($x, $p, $q), .0000001);
  251. }
  252. /**
  253. * @return array [points, x, p, q, pi]
  254. */
  255. public function dataProviderForPI(): array
  256. {
  257. return [
  258. [
  259. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  260. 2, .05, 1, 1.226194563,
  261. ],
  262. [
  263. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  264. 3, .05, 1, 1.261336191,
  265. ],
  266. [
  267. [ [1,2], [2,3], [4,5], [5,7], [6,8] ], // when q gets large, pi approaches ci.
  268. 3, .1, 10000000, 0.3045779864,
  269. ],
  270. ];
  271. }
  272. /**
  273. * @test sum of squares
  274. * @dataProvider dataProviderForSumSquares
  275. * @param array $points
  276. * @param array $force
  277. * @param array $sums
  278. */
  279. public function testSumSquares(array $points, array $force, array $sums)
  280. {
  281. // Given
  282. $regression = new LinearThroughPoint($points, $force);
  283. // Then
  284. $this->assertEqualsWithDelta($sums['sse'], $regression->sumOfSquaresResidual(), .0000001);
  285. $this->assertEqualsWithDelta($sums['ssr'], $regression->sumOfSquaresRegression(), .0000001);
  286. $this->assertEqualsWithDelta($sums['sst'], $regression->sumOfSquaresTotal(), .0000001);
  287. }
  288. /**
  289. * @return array [points, force, sums]
  290. */
  291. public function dataProviderForSumSquares(): array
  292. {
  293. return [
  294. [
  295. [ [1,2], [2,3], [4,5], [5,7], [6,8] ],
  296. [0,0],
  297. [
  298. 'sse' => 0.743902439,
  299. 'ssr' => 150.2560976,
  300. 'sst' => 151,
  301. ],
  302. ],
  303. [
  304. [ [2,3], [3,4], [5,6], [6,8], [7,9] ],
  305. [1,1],
  306. [
  307. 'sse' => 0.743902439,
  308. 'ssr' => 150.2560976,
  309. 'sst' => 151,
  310. ],
  311. ],
  312. ];
  313. }
  314. }