PowerBestFit.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared\Trend;
  3. class PowerBestFit extends BestFit
  4. {
  5. /**
  6. * Algorithm type to use for best-fit
  7. * (Name of this Trend class).
  8. *
  9. * @var string
  10. */
  11. protected $bestFitType = 'power';
  12. /**
  13. * Return the Y-Value for a specified value of X.
  14. *
  15. * @param float $xValue X-Value
  16. *
  17. * @return float Y-Value
  18. */
  19. public function getValueOfYForX($xValue)
  20. {
  21. return $this->getIntersect() * pow(($xValue - $this->xOffset), $this->getSlope());
  22. }
  23. /**
  24. * Return the X-Value for a specified value of Y.
  25. *
  26. * @param float $yValue Y-Value
  27. *
  28. * @return float X-Value
  29. */
  30. public function getValueOfXForY($yValue)
  31. {
  32. return pow((($yValue + $this->yOffset) / $this->getIntersect()), (1 / $this->getSlope()));
  33. }
  34. /**
  35. * Return the Equation of the best-fit line.
  36. *
  37. * @param int $dp Number of places of decimal precision to display
  38. *
  39. * @return string
  40. */
  41. public function getEquation($dp = 0)
  42. {
  43. $slope = $this->getSlope($dp);
  44. $intersect = $this->getIntersect($dp);
  45. return 'Y = ' . $intersect . ' * X^' . $slope;
  46. }
  47. /**
  48. * Return the Value of X where it intersects Y = 0.
  49. *
  50. * @param int $dp Number of places of decimal precision to display
  51. *
  52. * @return float
  53. */
  54. public function getIntersect($dp = 0)
  55. {
  56. if ($dp != 0) {
  57. return round(exp($this->intersect), $dp);
  58. }
  59. return exp($this->intersect);
  60. }
  61. /**
  62. * Execute the regression and calculate the goodness of fit for a set of X and Y data values.
  63. *
  64. * @param float[] $yValues The set of Y-values for this regression
  65. * @param float[] $xValues The set of X-values for this regression
  66. * @param bool $const
  67. */
  68. private function powerRegression($yValues, $xValues, $const)
  69. {
  70. foreach ($xValues as &$value) {
  71. if ($value < 0.0) {
  72. $value = 0 - log(abs($value));
  73. } elseif ($value > 0.0) {
  74. $value = log($value);
  75. }
  76. }
  77. unset($value);
  78. foreach ($yValues as &$value) {
  79. if ($value < 0.0) {
  80. $value = 0 - log(abs($value));
  81. } elseif ($value > 0.0) {
  82. $value = log($value);
  83. }
  84. }
  85. unset($value);
  86. $this->leastSquareFit($yValues, $xValues, $const);
  87. }
  88. /**
  89. * Define the regression and calculate the goodness of fit for a set of X and Y data values.
  90. *
  91. * @param float[] $yValues The set of Y-values for this regression
  92. * @param float[] $xValues The set of X-values for this regression
  93. * @param bool $const
  94. */
  95. public function __construct($yValues, $xValues = [], $const = true)
  96. {
  97. if (parent::__construct($yValues, $xValues) !== false) {
  98. $this->powerRegression($yValues, $xValues, $const);
  99. }
  100. }
  101. }