For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.8 KiB

  1. <?php
  2. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php';
  3. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/Matrix.php';
  4. /**
  5. * PHPExcel_Polynomial_Best_Fit
  6. *
  7. * Copyright (c) 2006 - 2015 PHPExcel
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * @category PHPExcel
  24. * @package PHPExcel_Shared_Trend
  25. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  26. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  27. * @version ##VERSION##, ##DATE##
  28. */
  29. class PHPExcel_Polynomial_Best_Fit extends PHPExcel_Best_Fit
  30. {
  31. /**
  32. * Algorithm type to use for best-fit
  33. * (Name of this trend class)
  34. *
  35. * @var string
  36. **/
  37. protected $bestFitType = 'polynomial';
  38. /**
  39. * Polynomial order
  40. *
  41. * @protected
  42. * @var int
  43. **/
  44. protected $order = 0;
  45. /**
  46. * Return the order of this polynomial
  47. *
  48. * @return int
  49. **/
  50. public function getOrder()
  51. {
  52. return $this->order;
  53. }
  54. /**
  55. * Return the Y-Value for a specified value of X
  56. *
  57. * @param float $xValue X-Value
  58. * @return float Y-Value
  59. **/
  60. public function getValueOfYForX($xValue)
  61. {
  62. $retVal = $this->getIntersect();
  63. $slope = $this->getSlope();
  64. foreach ($slope as $key => $value) {
  65. if ($value != 0.0) {
  66. $retVal += $value * pow($xValue, $key + 1);
  67. }
  68. }
  69. return $retVal;
  70. }
  71. /**
  72. * Return the X-Value for a specified value of Y
  73. *
  74. * @param float $yValue Y-Value
  75. * @return float X-Value
  76. **/
  77. public function getValueOfXForY($yValue)
  78. {
  79. return ($yValue - $this->getIntersect()) / $this->getSlope();
  80. }
  81. /**
  82. * Return the Equation of the best-fit line
  83. *
  84. * @param int $dp Number of places of decimal precision to display
  85. * @return string
  86. **/
  87. public function getEquation($dp = 0)
  88. {
  89. $slope = $this->getSlope($dp);
  90. $intersect = $this->getIntersect($dp);
  91. $equation = 'Y = ' . $intersect;
  92. foreach ($slope as $key => $value) {
  93. if ($value != 0.0) {
  94. $equation .= ' + ' . $value . ' * X';
  95. if ($key > 0) {
  96. $equation .= '^' . ($key + 1);
  97. }
  98. }
  99. }
  100. return $equation;
  101. }
  102. /**
  103. * Return the Slope of the line
  104. *
  105. * @param int $dp Number of places of decimal precision to display
  106. * @return string
  107. **/
  108. public function getSlope($dp = 0)
  109. {
  110. if ($dp != 0) {
  111. $coefficients = array();
  112. foreach ($this->_slope as $coefficient) {
  113. $coefficients[] = round($coefficient, $dp);
  114. }
  115. return $coefficients;
  116. }
  117. return $this->_slope;
  118. }
  119. public function getCoefficients($dp = 0)
  120. {
  121. return array_merge(array($this->getIntersect($dp)), $this->getSlope($dp));
  122. }
  123. /**
  124. * Execute the regression and calculate the goodness of fit for a set of X and Y data values
  125. *
  126. * @param int $order Order of Polynomial for this regression
  127. * @param float[] $yValues The set of Y-values for this regression
  128. * @param float[] $xValues The set of X-values for this regression
  129. * @param boolean $const
  130. */
  131. private function polynomialRegression($order, $yValues, $xValues, $const)
  132. {
  133. // calculate sums
  134. $x_sum = array_sum($xValues);
  135. $y_sum = array_sum($yValues);
  136. $xx_sum = $xy_sum = 0;
  137. for ($i = 0; $i < $this->valueCount; ++$i) {
  138. $xy_sum += $xValues[$i] * $yValues[$i];
  139. $xx_sum += $xValues[$i] * $xValues[$i];
  140. $yy_sum += $yValues[$i] * $yValues[$i];
  141. }
  142. /*
  143. * This routine uses logic from the PHP port of polyfit version 0.1
  144. * written by Michael Bommarito and Paul Meagher
  145. *
  146. * The function fits a polynomial function of order $order through
  147. * a series of x-y data points using least squares.
  148. *
  149. */
  150. for ($i = 0; $i < $this->valueCount; ++$i) {
  151. for ($j = 0; $j <= $order; ++$j) {
  152. $A[$i][$j] = pow($xValues[$i], $j);
  153. }
  154. }
  155. for ($i=0; $i < $this->valueCount; ++$i) {
  156. $B[$i] = array($yValues[$i]);
  157. }
  158. $matrixA = new Matrix($A);
  159. $matrixB = new Matrix($B);
  160. $C = $matrixA->solve($matrixB);
  161. $coefficients = array();
  162. for ($i = 0; $i < $C->m; ++$i) {
  163. $r = $C->get($i, 0);
  164. if (abs($r) <= pow(10, -9)) {
  165. $r = 0;
  166. }
  167. $coefficients[] = $r;
  168. }
  169. $this->intersect = array_shift($coefficients);
  170. $this->_slope = $coefficients;
  171. $this->calculateGoodnessOfFit($x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum);
  172. foreach ($this->xValues as $xKey => $xValue) {
  173. $this->yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
  174. }
  175. }
  176. /**
  177. * Define the regression and calculate the goodness of fit for a set of X and Y data values
  178. *
  179. * @param int $order Order of Polynomial for this regression
  180. * @param float[] $yValues The set of Y-values for this regression
  181. * @param float[] $xValues The set of X-values for this regression
  182. * @param boolean $const
  183. */
  184. public function __construct($order, $yValues, $xValues = array(), $const = true)
  185. {
  186. if (parent::__construct($yValues, $xValues) !== false) {
  187. if ($order < $this->valueCount) {
  188. $this->bestFitType .= '_'.$order;
  189. $this->order = $order;
  190. $this->polynomialRegression($order, $yValues, $xValues, $const);
  191. if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
  192. $this->_error = true;
  193. }
  194. } else {
  195. $this->_error = true;
  196. }
  197. }
  198. }
  199. }