LinearBestFitTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Shared\Trend;
  3. use PhpOffice\PhpSpreadsheet\Shared\Trend\LinearBestFit;
  4. use PHPUnit\Framework\TestCase;
  5. class LinearBestFitTest extends TestCase
  6. {
  7. const LBF_PRECISION = 1.0E-8;
  8. /**
  9. * @dataProvider providerLinearBestFit
  10. *
  11. * @param mixed $expectedSlope
  12. * @param mixed $expectedIntersect
  13. * @param mixed $expectedGoodnessOfFit
  14. * @param mixed $yValues
  15. * @param mixed $xValues
  16. * @param mixed $expectedEquation
  17. */
  18. public function testLinearBestFit(
  19. $expectedSlope,
  20. $expectedIntersect,
  21. $expectedGoodnessOfFit,
  22. $expectedEquation,
  23. $yValues,
  24. $xValues
  25. ): void {
  26. $bestFit = new LinearBestFit($yValues, $xValues);
  27. $slope = $bestFit->getSlope(1);
  28. self::assertEqualsWithDelta($expectedSlope[0], $slope, self::LBF_PRECISION);
  29. $slope = $bestFit->getSlope();
  30. self::assertEqualsWithDelta($expectedSlope[1], $slope, self::LBF_PRECISION);
  31. $intersect = $bestFit->getIntersect(1);
  32. self::assertEqualsWithDelta($expectedIntersect[0], $intersect, self::LBF_PRECISION);
  33. $intersect = $bestFit->getIntersect();
  34. self::assertEqualsWithDelta($expectedIntersect[1], $intersect, self::LBF_PRECISION);
  35. $equation = $bestFit->getEquation(2);
  36. self::assertEquals($expectedEquation, $equation);
  37. self::assertSame($expectedGoodnessOfFit[0], $bestFit->getGoodnessOfFit(6));
  38. self::assertSame($expectedGoodnessOfFit[1], $bestFit->getGoodnessOfFit());
  39. }
  40. public function providerLinearBestFit(): array
  41. {
  42. return require 'tests/data/Shared/Trend/LinearBestFit.php';
  43. }
  44. }