9707ad092af13edb2735faa248082c1593289d75.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Shared_Trend
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php';
  28. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/Matrix.php';
  29. /**
  30. * PHPExcel_Polynomial_Best_Fit
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Shared_Trend
  34. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Polynomial_Best_Fit extends PHPExcel_Best_Fit
  37. {
  38. /**
  39. * Algorithm type to use for best-fit
  40. * (Name of this trend class)
  41. *
  42. * @var string
  43. **/
  44. protected $_bestFitType = 'polynomial';
  45. /**
  46. * Polynomial order
  47. *
  48. * @protected
  49. * @var int
  50. **/
  51. protected $_order = 0;
  52. /**
  53. * Return the order of this polynomial
  54. *
  55. * @return int
  56. **/
  57. public function getOrder() {
  58. return $this->_order;
  59. } // function getOrder()
  60. /**
  61. * Return the Y-Value for a specified value of X
  62. *
  63. * @param float $xValue X-Value
  64. * @return float Y-Value
  65. **/
  66. public function getValueOfYForX($xValue) {
  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. } // function getValueOfYForX()
  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. return ($yValue - $this->getIntersect()) / $this->getSlope();
  84. } // function getValueOfXForY()
  85. /**
  86. * Return the Equation of the best-fit line
  87. *
  88. * @param int $dp Number of places of decimal precision to display
  89. * @return string
  90. **/
  91. public function getEquation($dp=0) {
  92. $slope = $this->getSlope($dp);
  93. $intersect = $this->getIntersect($dp);
  94. $equation = 'Y = '.$intersect;
  95. foreach($slope as $key => $value) {
  96. if ($value != 0.0) {
  97. $equation .= ' + '.$value.' * X';
  98. if ($key > 0) {
  99. $equation .= '^'.($key + 1);
  100. }
  101. }
  102. }
  103. return $equation;
  104. } // function getEquation()
  105. /**
  106. * Return the Slope of the line
  107. *
  108. * @param int $dp Number of places of decimal precision to display
  109. * @return string
  110. **/
  111. public function getSlope($dp=0) {
  112. if ($dp != 0) {
  113. $coefficients = array();
  114. foreach($this->_slope as $coefficient) {
  115. $coefficients[] = round($coefficient,$dp);
  116. }
  117. return $coefficients;
  118. }
  119. return $this->_slope;
  120. } // function getSlope()
  121. public function getCoefficients($dp=0) {
  122. return array_merge(array($this->getIntersect($dp)),$this->getSlope($dp));
  123. } // function getCoefficients()
  124. /**
  125. * Execute the regression and calculate the goodness of fit for a set of X and Y data values
  126. *
  127. * @param int $order Order of Polynomial for this regression
  128. * @param float[] $yValues The set of Y-values for this regression
  129. * @param float[] $xValues The set of X-values for this regression
  130. * @param boolean $const
  131. */
  132. private function _polynomial_regression($order, $yValues, $xValues, $const) {
  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. } // function _polynomial_regression()
  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. function __construct($order, $yValues, $xValues=array(), $const=True) {
  185. if (parent::__construct($yValues, $xValues) !== False) {
  186. if ($order < $this->_valueCount) {
  187. $this->_bestFitType .= '_'.$order;
  188. $this->_order = $order;
  189. $this->_polynomial_regression($order, $yValues, $xValues, $const);
  190. if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
  191. $this->_error = True;
  192. }
  193. } else {
  194. $this->_error = True;
  195. }
  196. }
  197. } // function __construct()
  198. } // class polynomialBestFit