polynomialBestFitClass.php 7.2 KB

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