cbe49714035979f4b93aaf80324eb9def70bccc3.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /**
  28. * PHPExcel_Best_Fit
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared_Trend
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Best_Fit
  35. {
  36. /**
  37. * Indicator flag for a calculation error
  38. *
  39. * @var boolean
  40. **/
  41. protected $_error = False;
  42. /**
  43. * Algorithm type to use for best-fit
  44. *
  45. * @var string
  46. **/
  47. protected $_bestFitType = 'undetermined';
  48. /**
  49. * Number of entries in the sets of x- and y-value arrays
  50. *
  51. * @var int
  52. **/
  53. protected $_valueCount = 0;
  54. /**
  55. * X-value dataseries of values
  56. *
  57. * @var float[]
  58. **/
  59. protected $_xValues = array();
  60. /**
  61. * Y-value dataseries of values
  62. *
  63. * @var float[]
  64. **/
  65. protected $_yValues = array();
  66. /**
  67. * Flag indicating whether values should be adjusted to Y=0
  68. *
  69. * @var boolean
  70. **/
  71. protected $_adjustToZero = False;
  72. /**
  73. * Y-value series of best-fit values
  74. *
  75. * @var float[]
  76. **/
  77. protected $_yBestFitValues = array();
  78. protected $_goodnessOfFit = 1;
  79. protected $_stdevOfResiduals = 0;
  80. protected $_covariance = 0;
  81. protected $_correlation = 0;
  82. protected $_SSRegression = 0;
  83. protected $_SSResiduals = 0;
  84. protected $_DFResiduals = 0;
  85. protected $_F = 0;
  86. protected $_slope = 0;
  87. protected $_slopeSE = 0;
  88. protected $_intersect = 0;
  89. protected $_intersectSE = 0;
  90. protected $_Xoffset = 0;
  91. protected $_Yoffset = 0;
  92. public function getError() {
  93. return $this->_error;
  94. } // function getBestFitType()
  95. public function getBestFitType() {
  96. return $this->_bestFitType;
  97. } // function getBestFitType()
  98. /**
  99. * Return the Y-Value for a specified value of X
  100. *
  101. * @param float $xValue X-Value
  102. * @return float Y-Value
  103. */
  104. public function getValueOfYForX($xValue) {
  105. return False;
  106. } // function getValueOfYForX()
  107. /**
  108. * Return the X-Value for a specified value of Y
  109. *
  110. * @param float $yValue Y-Value
  111. * @return float X-Value
  112. */
  113. public function getValueOfXForY($yValue) {
  114. return False;
  115. } // function getValueOfXForY()
  116. /**
  117. * Return the original set of X-Values
  118. *
  119. * @return float[] X-Values
  120. */
  121. public function getXValues() {
  122. return $this->_xValues;
  123. } // function getValueOfXForY()
  124. /**
  125. * Return the Equation of the best-fit line
  126. *
  127. * @param int $dp Number of places of decimal precision to display
  128. * @return string
  129. */
  130. public function getEquation($dp=0) {
  131. return False;
  132. } // function getEquation()
  133. /**
  134. * Return the Slope of the line
  135. *
  136. * @param int $dp Number of places of decimal precision to display
  137. * @return string
  138. */
  139. public function getSlope($dp=0) {
  140. if ($dp != 0) {
  141. return round($this->_slope,$dp);
  142. }
  143. return $this->_slope;
  144. } // function getSlope()
  145. /**
  146. * Return the standard error of the Slope
  147. *
  148. * @param int $dp Number of places of decimal precision to display
  149. * @return string
  150. */
  151. public function getSlopeSE($dp=0) {
  152. if ($dp != 0) {
  153. return round($this->_slopeSE,$dp);
  154. }
  155. return $this->_slopeSE;
  156. } // function getSlopeSE()
  157. /**
  158. * Return the Value of X where it intersects Y = 0
  159. *
  160. * @param int $dp Number of places of decimal precision to display
  161. * @return string
  162. */
  163. public function getIntersect($dp=0) {
  164. if ($dp != 0) {
  165. return round($this->_intersect,$dp);
  166. }
  167. return $this->_intersect;
  168. } // function getIntersect()
  169. /**
  170. * Return the standard error of the Intersect
  171. *
  172. * @param int $dp Number of places of decimal precision to display
  173. * @return string
  174. */
  175. public function getIntersectSE($dp=0) {
  176. if ($dp != 0) {
  177. return round($this->_intersectSE,$dp);
  178. }
  179. return $this->_intersectSE;
  180. } // function getIntersectSE()
  181. /**
  182. * Return the goodness of fit for this regression
  183. *
  184. * @param int $dp Number of places of decimal precision to return
  185. * @return float
  186. */
  187. public function getGoodnessOfFit($dp=0) {
  188. if ($dp != 0) {
  189. return round($this->_goodnessOfFit,$dp);
  190. }
  191. return $this->_goodnessOfFit;
  192. } // function getGoodnessOfFit()
  193. public function getGoodnessOfFitPercent($dp=0) {
  194. if ($dp != 0) {
  195. return round($this->_goodnessOfFit * 100,$dp);
  196. }
  197. return $this->_goodnessOfFit * 100;
  198. } // function getGoodnessOfFitPercent()
  199. /**
  200. * Return the standard deviation of the residuals for this regression
  201. *
  202. * @param int $dp Number of places of decimal precision to return
  203. * @return float
  204. */
  205. public function getStdevOfResiduals($dp=0) {
  206. if ($dp != 0) {
  207. return round($this->_stdevOfResiduals,$dp);
  208. }
  209. return $this->_stdevOfResiduals;
  210. } // function getStdevOfResiduals()
  211. public function getSSRegression($dp=0) {
  212. if ($dp != 0) {
  213. return round($this->_SSRegression,$dp);
  214. }
  215. return $this->_SSRegression;
  216. } // function getSSRegression()
  217. public function getSSResiduals($dp=0) {
  218. if ($dp != 0) {
  219. return round($this->_SSResiduals,$dp);
  220. }
  221. return $this->_SSResiduals;
  222. } // function getSSResiduals()
  223. public function getDFResiduals($dp=0) {
  224. if ($dp != 0) {
  225. return round($this->_DFResiduals,$dp);
  226. }
  227. return $this->_DFResiduals;
  228. } // function getDFResiduals()
  229. public function getF($dp=0) {
  230. if ($dp != 0) {
  231. return round($this->_F,$dp);
  232. }
  233. return $this->_F;
  234. } // function getF()
  235. public function getCovariance($dp=0) {
  236. if ($dp != 0) {
  237. return round($this->_covariance,$dp);
  238. }
  239. return $this->_covariance;
  240. } // function getCovariance()
  241. public function getCorrelation($dp=0) {
  242. if ($dp != 0) {
  243. return round($this->_correlation,$dp);
  244. }
  245. return $this->_correlation;
  246. } // function getCorrelation()
  247. public function getYBestFitValues() {
  248. return $this->_yBestFitValues;
  249. } // function getYBestFitValues()
  250. protected function _calculateGoodnessOfFit($sumX,$sumY,$sumX2,$sumY2,$sumXY,$meanX,$meanY, $const) {
  251. $SSres = $SScov = $SScor = $SStot = $SSsex = 0.0;
  252. foreach($this->_xValues as $xKey => $xValue) {
  253. $bestFitY = $this->_yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
  254. $SSres += ($this->_yValues[$xKey] - $bestFitY) * ($this->_yValues[$xKey] - $bestFitY);
  255. if ($const) {
  256. $SStot += ($this->_yValues[$xKey] - $meanY) * ($this->_yValues[$xKey] - $meanY);
  257. } else {
  258. $SStot += $this->_yValues[$xKey] * $this->_yValues[$xKey];
  259. }
  260. $SScov += ($this->_xValues[$xKey] - $meanX) * ($this->_yValues[$xKey] - $meanY);
  261. if ($const) {
  262. $SSsex += ($this->_xValues[$xKey] - $meanX) * ($this->_xValues[$xKey] - $meanX);
  263. } else {
  264. $SSsex += $this->_xValues[$xKey] * $this->_xValues[$xKey];
  265. }
  266. }
  267. $this->_SSResiduals = $SSres;
  268. $this->_DFResiduals = $this->_valueCount - 1 - $const;
  269. if ($this->_DFResiduals == 0.0) {
  270. $this->_stdevOfResiduals = 0.0;
  271. } else {
  272. $this->_stdevOfResiduals = sqrt($SSres / $this->_DFResiduals);
  273. }
  274. if (($SStot == 0.0) || ($SSres == $SStot)) {
  275. $this->_goodnessOfFit = 1;
  276. } else {
  277. $this->_goodnessOfFit = 1 - ($SSres / $SStot);
  278. }
  279. $this->_SSRegression = $this->_goodnessOfFit * $SStot;
  280. $this->_covariance = $SScov / $this->_valueCount;
  281. $this->_correlation = ($this->_valueCount * $sumXY - $sumX * $sumY) / sqrt(($this->_valueCount * $sumX2 - pow($sumX,2)) * ($this->_valueCount * $sumY2 - pow($sumY,2)));
  282. $this->_slopeSE = $this->_stdevOfResiduals / sqrt($SSsex);
  283. $this->_intersectSE = $this->_stdevOfResiduals * sqrt(1 / ($this->_valueCount - ($sumX * $sumX) / $sumX2));
  284. if ($this->_SSResiduals != 0.0) {
  285. if ($this->_DFResiduals == 0.0) {
  286. $this->_F = 0.0;
  287. } else {
  288. $this->_F = $this->_SSRegression / ($this->_SSResiduals / $this->_DFResiduals);
  289. }
  290. } else {
  291. if ($this->_DFResiduals == 0.0) {
  292. $this->_F = 0.0;
  293. } else {
  294. $this->_F = $this->_SSRegression / $this->_DFResiduals;
  295. }
  296. }
  297. } // function _calculateGoodnessOfFit()
  298. protected function _leastSquareFit($yValues, $xValues, $const) {
  299. // calculate sums
  300. $x_sum = array_sum($xValues);
  301. $y_sum = array_sum($yValues);
  302. $meanX = $x_sum / $this->_valueCount;
  303. $meanY = $y_sum / $this->_valueCount;
  304. $mBase = $mDivisor = $xx_sum = $xy_sum = $yy_sum = 0.0;
  305. for($i = 0; $i < $this->_valueCount; ++$i) {
  306. $xy_sum += $xValues[$i] * $yValues[$i];
  307. $xx_sum += $xValues[$i] * $xValues[$i];
  308. $yy_sum += $yValues[$i] * $yValues[$i];
  309. if ($const) {
  310. $mBase += ($xValues[$i] - $meanX) * ($yValues[$i] - $meanY);
  311. $mDivisor += ($xValues[$i] - $meanX) * ($xValues[$i] - $meanX);
  312. } else {
  313. $mBase += $xValues[$i] * $yValues[$i];
  314. $mDivisor += $xValues[$i] * $xValues[$i];
  315. }
  316. }
  317. // calculate slope
  318. // $this->_slope = (($this->_valueCount * $xy_sum) - ($x_sum * $y_sum)) / (($this->_valueCount * $xx_sum) - ($x_sum * $x_sum));
  319. $this->_slope = $mBase / $mDivisor;
  320. // calculate intersect
  321. // $this->_intersect = ($y_sum - ($this->_slope * $x_sum)) / $this->_valueCount;
  322. if ($const) {
  323. $this->_intersect = $meanY - ($this->_slope * $meanX);
  324. } else {
  325. $this->_intersect = 0;
  326. }
  327. $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum,$meanX,$meanY,$const);
  328. } // function _leastSquareFit()
  329. /**
  330. * Define the regression
  331. *
  332. * @param float[] $yValues The set of Y-values for this regression
  333. * @param float[] $xValues The set of X-values for this regression
  334. * @param boolean $const
  335. */
  336. function __construct($yValues, $xValues=array(), $const=True) {
  337. // Calculate number of points
  338. $nY = count($yValues);
  339. $nX = count($xValues);
  340. // Define X Values if necessary
  341. if ($nX == 0) {
  342. $xValues = range(1,$nY);
  343. $nX = $nY;
  344. } elseif ($nY != $nX) {
  345. // Ensure both arrays of points are the same size
  346. $this->_error = True;
  347. return False;
  348. }
  349. $this->_valueCount = $nY;
  350. $this->_xValues = $xValues;
  351. $this->_yValues = $yValues;
  352. } // function __construct()
  353. } // class bestFit