ExponentialBestFit.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared\Trend;
  3. class ExponentialBestFit 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 = 'exponential';
  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($this->getSlope(), ($xValue - $this->xOffset));
  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 log(($yValue + $this->yOffset) / $this->getIntersect()) / log($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 . ' * ' . $slope . '^X';
  46. }
  47. /**
  48. * Return the Slope of the line.
  49. *
  50. * @param int $dp Number of places of decimal precision to display
  51. *
  52. * @return float
  53. */
  54. public function getSlope($dp = 0)
  55. {
  56. if ($dp != 0) {
  57. return round(exp($this->slope), $dp);
  58. }
  59. return exp($this->slope);
  60. }
  61. /**
  62. * Return the Value of X where it intersects Y = 0.
  63. *
  64. * @param int $dp Number of places of decimal precision to display
  65. *
  66. * @return float
  67. */
  68. public function getIntersect($dp = 0)
  69. {
  70. if ($dp != 0) {
  71. return round(exp($this->intersect), $dp);
  72. }
  73. return exp($this->intersect);
  74. }
  75. /**
  76. * Execute the regression and calculate the goodness of fit for a set of X and Y data values.
  77. *
  78. * @param float[] $yValues The set of Y-values for this regression
  79. * @param float[] $xValues The set of X-values for this regression
  80. * @param bool $const
  81. */
  82. private function exponentialRegression($yValues, $xValues, $const)
  83. {
  84. foreach ($yValues as &$value) {
  85. if ($value < 0.0) {
  86. $value = 0 - log(abs($value));
  87. } elseif ($value > 0.0) {
  88. $value = log($value);
  89. }
  90. }
  91. unset($value);
  92. $this->leastSquareFit($yValues, $xValues, $const);
  93. }
  94. /**
  95. * Define the regression and calculate the goodness of fit for a set of X and Y data values.
  96. *
  97. * @param float[] $yValues The set of Y-values for this regression
  98. * @param float[] $xValues The set of X-values for this regression
  99. * @param bool $const
  100. */
  101. public function __construct($yValues, $xValues = [], $const = true)
  102. {
  103. if (parent::__construct($yValues, $xValues) !== false) {
  104. $this->exponentialRegression($yValues, $xValues, $const);
  105. }
  106. }
  107. }