exponentialBestFitClass.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. require_once(PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php');
  3. /**
  4. * PHPExcel_Exponential_Best_Fit
  5. *
  6. * Copyright (c) 2006 - 2015 PHPExcel
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * @category PHPExcel
  23. * @package PHPExcel_Shared_Trend
  24. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  26. * @version ##VERSION##, ##DATE##
  27. */
  28. class PHPExcel_Exponential_Best_Fit extends PHPExcel_Best_Fit
  29. {
  30. /**
  31. * Algorithm type to use for best-fit
  32. * (Name of this trend class)
  33. *
  34. * @var string
  35. **/
  36. protected $bestFitType = 'exponential';
  37. /**
  38. * Return the Y-Value for a specified value of X
  39. *
  40. * @param float $xValue X-Value
  41. * @return float Y-Value
  42. **/
  43. public function getValueOfYForX($xValue)
  44. {
  45. return $this->getIntersect() * pow($this->getSlope(), ($xValue - $this->xOffset));
  46. }
  47. /**
  48. * Return the X-Value for a specified value of Y
  49. *
  50. * @param float $yValue Y-Value
  51. * @return float X-Value
  52. **/
  53. public function getValueOfXForY($yValue)
  54. {
  55. return log(($yValue + $this->yOffset) / $this->getIntersect()) / log($this->getSlope());
  56. }
  57. /**
  58. * Return the Equation of the best-fit line
  59. *
  60. * @param int $dp Number of places of decimal precision to display
  61. * @return string
  62. **/
  63. public function getEquation($dp = 0)
  64. {
  65. $slope = $this->getSlope($dp);
  66. $intersect = $this->getIntersect($dp);
  67. return 'Y = ' . $intersect . ' * ' . $slope . '^X';
  68. }
  69. /**
  70. * Return the Slope of the line
  71. *
  72. * @param int $dp Number of places of decimal precision to display
  73. * @return string
  74. **/
  75. public function getSlope($dp = 0)
  76. {
  77. if ($dp != 0) {
  78. return round(exp($this->_slope), $dp);
  79. }
  80. return exp($this->_slope);
  81. }
  82. /**
  83. * Return the Value of X where it intersects Y = 0
  84. *
  85. * @param int $dp Number of places of decimal precision to display
  86. * @return string
  87. **/
  88. public function getIntersect($dp = 0)
  89. {
  90. if ($dp != 0) {
  91. return round(exp($this->intersect), $dp);
  92. }
  93. return exp($this->intersect);
  94. }
  95. /**
  96. * Execute the regression and calculate the goodness of fit for a set of X and Y data values
  97. *
  98. * @param float[] $yValues The set of Y-values for this regression
  99. * @param float[] $xValues The set of X-values for this regression
  100. * @param boolean $const
  101. */
  102. private function exponentialRegression($yValues, $xValues, $const)
  103. {
  104. foreach ($yValues as &$value) {
  105. if ($value < 0.0) {
  106. $value = 0 - log(abs($value));
  107. } elseif ($value > 0.0) {
  108. $value = log($value);
  109. }
  110. }
  111. unset($value);
  112. $this->leastSquareFit($yValues, $xValues, $const);
  113. }
  114. /**
  115. * Define the regression and calculate the goodness of fit for a set of X and Y data values
  116. *
  117. * @param float[] $yValues The set of Y-values for this regression
  118. * @param float[] $xValues The set of X-values for this regression
  119. * @param boolean $const
  120. */
  121. public function __construct($yValues, $xValues = array(), $const = true)
  122. {
  123. if (parent::__construct($yValues, $xValues) !== false) {
  124. $this->exponentialRegression($yValues, $xValues, $const);
  125. }
  126. }
  127. }