WeightedLinearTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace MathPHP\Tests\Statistics\Regression;
  3. use MathPHP\Statistics\Regression\WeightedLinear;
  4. class WeightedLinearTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test getParameters
  8. * @dataProvider dataProviderForParameters
  9. * @param array $points
  10. * @param array $weights
  11. * @param float $m
  12. * @param float $b
  13. */
  14. public function testGetParameters(array $points, array $weights, float $m, float $b)
  15. {
  16. // Given
  17. $regression = new WeightedLinear($points, $weights);
  18. // When
  19. $parameters = $regression->getParameters();
  20. // Then
  21. $this->assertEqualsWithDelta($m, $parameters['m'], 0.0001);
  22. $this->assertEqualsWithDelta($b, $parameters['b'], 0.0001);
  23. }
  24. /**
  25. * @return array [points, weights, m, b]
  26. */
  27. public function dataProviderForParameters(): array
  28. {
  29. return [
  30. [
  31. [[1,2], [2,3], [4,5], [5,7], [6,8]],
  32. [1, 1, 1, 1, 1],
  33. 1.2209302325581, 0.60465116279069
  34. ],
  35. [
  36. [[1,2], [2,3], [4,5], [5,7], [6,8]],
  37. [1, 2, 3, 4, 5],
  38. 1.259717314, 0.439929329
  39. ],
  40. ];
  41. }
  42. /**
  43. * @test evaluate
  44. * @dataProvider dataProviderForEvaluate
  45. * @param array $points
  46. * @param array $weights
  47. * @param float $x
  48. * @param float $expected_y
  49. */
  50. public function testEvaluate(array $points, array $weights, float $x, float $expected_y)
  51. {
  52. // Given
  53. $regression = new WeightedLinear($points, $weights);
  54. // When
  55. $y = $regression->evaluate($x);
  56. // Then
  57. $this->assertEqualsWithDelta($expected_y, $y, 0.0001);
  58. }
  59. /**
  60. * @return array [points, weights, x, expected y]
  61. */
  62. public function dataProviderForEvaluate(): array
  63. {
  64. return [
  65. [
  66. [[1,2], [2,3], [4,5], [5,7], [6,8]],
  67. [1, 1, 1, 1, 1],
  68. 5,
  69. 6.709302,
  70. ],
  71. [
  72. [[1,2], [2,3], [4,5], [5,7], [6,8]],
  73. [1, 2, 3, 4, 5],
  74. 5,
  75. 6.738516,
  76. ],
  77. ];
  78. }
  79. }