PowerLawTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace MathPHP\Tests\Statistics\Regression;
  3. use MathPHP\Statistics\Regression\PowerLaw;
  4. class PowerLawTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test constructor
  8. */
  9. public function testConstructor()
  10. {
  11. // Given
  12. $points = [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ];
  13. // When
  14. $regression = new PowerLaw($points);
  15. // Then
  16. $this->assertInstanceOf(\MathPHP\Statistics\Regression\Regression::class, $regression);
  17. $this->assertInstanceOf(\MathPHP\Statistics\Regression\PowerLaw::class, $regression);
  18. }
  19. /**
  20. * @test getEquation - Equation matches pattern y = axᵇ
  21. * @dataProvider dataProviderForEquation
  22. * @param array $points
  23. */
  24. public function testGetEquation(array $points)
  25. {
  26. // Given
  27. $regression = new PowerLaw($points);
  28. // Then
  29. $this->assertRegExp('/^y = \d+[.]\d+x\^\d+[.]\d+$/', $regression->getEquation());
  30. }
  31. /**
  32. * @return array [points]
  33. */
  34. public function dataProviderForEquation(): array
  35. {
  36. return [
  37. [
  38. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  39. ]
  40. ];
  41. }
  42. /**
  43. * @test getParameters
  44. * @dataProvider dataProviderForParameters
  45. * @param array $points
  46. * @param float $a
  47. * @param float $b
  48. */
  49. public function testGetParameters(array $points, float $a, float $b)
  50. {
  51. // Given
  52. $regression = new PowerLaw($points);
  53. // When
  54. $parameters = $regression->getParameters();
  55. // Then
  56. $this->assertEqualsWithDelta($a, $parameters['a'], 0.0001);
  57. $this->assertEqualsWithDelta($b, $parameters['b'], 0.0001);
  58. }
  59. /**
  60. * @return array [points, a, b]
  61. */
  62. public function dataProviderForParameters(): array
  63. {
  64. return [
  65. [
  66. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  67. 56.48338, 0.2641538,
  68. ],
  69. ];
  70. }
  71. /**
  72. * @test evaluate
  73. * @dataProvider dataProviderForEvaluate
  74. * @param array $points
  75. * @param float $x
  76. * @param float $y
  77. */
  78. public function testEvaluate(array $points, float $x, float $y)
  79. {
  80. // Given
  81. $regression = new PowerLaw($points);
  82. // Then
  83. $this->assertEqualsWithDelta($y, $regression->evaluate($x), 0.0001);
  84. }
  85. /**
  86. * @return array [points, x, y]
  87. */
  88. public function dataProviderForEvaluate(): array
  89. {
  90. // y = 56.48338x^0.2641538
  91. return [
  92. [
  93. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  94. 83, 181.4898448,
  95. ],
  96. [
  97. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  98. 71, 174.1556182,
  99. ],
  100. [
  101. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  102. 64, 169.4454327,
  103. ],
  104. [
  105. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  106. 57, 164.3393562,
  107. ],
  108. [
  109. [ [ 83, 183 ], [ 71, 168 ], [ 64, 171 ], [ 69, 178 ], [ 69, 176 ], [ 64, 172 ], [ 68, 165 ], [ 59, 158 ], [ 81, 183 ], [ 91, 182 ], [ 57, 163 ], [ 65, 175 ], [ 58, 164 ], [ 62, 175 ] ],
  110. 91, 185.955396,
  111. ],
  112. ];
  113. }
  114. }