polynomialBestFitClass.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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_Best_Fit
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  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_Best_Fit
  34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Polynomial_Best_Fit extends PHPExcel_Best_Fit
  37. {
  38. protected $_bestFitType = 'polynomial';
  39. protected $_order = 0;
  40. public function getOrder() {
  41. return $this->_order;
  42. } // function getOrder()
  43. public function getValueOfYForX($xValue) {
  44. $retVal = $this->getIntersect();
  45. $slope = $this->getSlope();
  46. foreach($slope as $key => $value) {
  47. if ($value != 0.0) {
  48. $retVal += $value * pow($xValue, $key + 1);
  49. }
  50. }
  51. return $retVal;
  52. } // function getValueOfYForX()
  53. public function getValueOfXForY($yValue) {
  54. return ($yValue - $this->getIntersect()) / $this->getSlope();
  55. } // function getValueOfXForY()
  56. public function getEquation($dp=0) {
  57. $slope = $this->getSlope($dp);
  58. $intersect = $this->getIntersect($dp);
  59. $equation = 'Y = '.$intersect;
  60. foreach($slope as $key => $value) {
  61. if ($value != 0.0) {
  62. $equation .= ' + '.$value.' * X';
  63. if ($key > 0) {
  64. $equation .= '^'.($key + 1);
  65. }
  66. }
  67. }
  68. return $equation;
  69. } // function getEquation()
  70. public function getSlope($dp=0) {
  71. if ($dp != 0) {
  72. $coefficients = array();
  73. foreach($this->_slope as $coefficient) {
  74. $coefficients[] = round($coefficient,$dp);
  75. }
  76. return $coefficients;
  77. }
  78. return $this->_slope;
  79. } // function getSlope()
  80. public function getCoefficients($dp=0) {
  81. return array_merge(array($this->getIntersect($dp)),$this->getSlope($dp));
  82. } // function getCoefficients()
  83. private function _polynomial_regression($order, $yValues, $xValues, $const) {
  84. // calculate sums
  85. $x_sum = array_sum($xValues);
  86. $y_sum = array_sum($yValues);
  87. $xx_sum = $xy_sum = 0;
  88. for($i = 0; $i < $this->_valueCount; ++$i) {
  89. $xy_sum += $xValues[$i] * $yValues[$i];
  90. $xx_sum += $xValues[$i] * $xValues[$i];
  91. $yy_sum += $yValues[$i] * $yValues[$i];
  92. }
  93. /*
  94. * This routine uses logic from the PHP port of polyfit version 0.1
  95. * written by Michael Bommarito and Paul Meagher
  96. *
  97. * The function fits a polynomial function of order $order through
  98. * a series of x-y data points using least squares.
  99. *
  100. */
  101. for ($i = 0; $i < $this->_valueCount; ++$i) {
  102. for ($j = 0; $j <= $order; ++$j) {
  103. $A[$i][$j] = pow($xValues[$i], $j);
  104. }
  105. }
  106. for ($i=0; $i < $this->_valueCount; ++$i) {
  107. $B[$i] = array($yValues[$i]);
  108. }
  109. $matrixA = new Matrix($A);
  110. $matrixB = new Matrix($B);
  111. $C = $matrixA->solve($matrixB);
  112. $coefficients = array();
  113. for($i = 0; $i < $C->m; ++$i) {
  114. $r = $C->get($i, 0);
  115. if (abs($r) <= pow(10, -9)) {
  116. $r = 0;
  117. }
  118. $coefficients[] = $r;
  119. }
  120. $this->_intersect = array_shift($coefficients);
  121. $this->_slope = $coefficients;
  122. $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum);
  123. foreach($this->_xValues as $xKey => $xValue) {
  124. $this->_yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
  125. }
  126. } // function _polynomial_regression()
  127. function __construct($order, $yValues, $xValues=array(), $const=True) {
  128. if (parent::__construct($yValues, $xValues) !== False) {
  129. if ($order < $this->_valueCount) {
  130. $this->_bestFitType .= '_'.$order;
  131. $this->_order = $order;
  132. $this->_polynomial_regression($order, $yValues, $xValues, $const);
  133. if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
  134. $this->_error = True;
  135. }
  136. } else {
  137. $this->_error = True;
  138. }
  139. }
  140. } // function __construct()
  141. } // class polynomialBestFit