File Source for bestFitClass.php

PHPExcel_Shared_Best_Fit

# Source for file bestFitClass.php

Documentation is available at bestFitClass.php

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. */
29. /**
30. * PHPExcel_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_Best_Fit
37. {
38. protected $_error = False;
40. protected $_bestFitType = 'undetermined';
42. protected $_valueCount = 0;
44. protected $_xValues = array();
46. protected $_yValues = array();
48. protected $_adjustToZero = False;
50. protected $_yBestFitValues = array();
52. protected $_goodnessOfFit = 1;
54. protected $_stdevOfResiduals = 0;
56. protected $_covariance = 0;
58. protected $_correlation = 0;
60. protected $_SSRegression = 0;
62. protected $_SSResiduals = 0;
64. protected $_DFResiduals = 0;
66. protected $_F = 0;
68. protected $_slope = 0;
70. protected $_slopeSE = 0;
72. protected $_intersect = 0;
74. protected $_intersectSE = 0;
76. protected $_Xoffset = 0;
78. protected $_Yoffset = 0;
81. public function getError() {
82. return $this->_error;
83. } // function getBestFitType()
86. public function getBestFitType() {
87. return $this->_bestFitType;
88. } // function getBestFitType()
91. public function getValueOfYForX($xValue) {
92. return False;
93. } // function getValueOfYForX()
96. public function getValueOfXForY($yValue) {
97. return False;
98. } // function getValueOfXForY()
101. public function getXValues() {
102. return $this->_xValues;
103. } // function getValueOfXForY()
106. public function getEquation($dp=0) {
107. return False;
108. } // function getEquation()
111. public function getSlope($dp=0) {
112. if ($dp != 0) {
113. return round($this->_slope,$dp);
114. }
115. return $this->_slope;
116. } // function getSlope()
119. public function getSlopeSE($dp=0) {
120. if ($dp != 0) {
121. return round($this->_slopeSE,$dp);
122. }
123. return $this->_slopeSE;
124. } // function getSlopeSE()
127. public function getIntersect($dp=0) {
128. if ($dp != 0) {
129. return round($this->_intersect,$dp);
130. }
131. return $this->_intersect;
132. } // function getIntersect()
135. public function getIntersectSE($dp=0) {
136. if ($dp != 0) {
137. return round($this->_intersectSE,$dp);
138. }
139. return $this->_intersectSE;
140. } // function getIntersectSE()
143. public function getGoodnessOfFit($dp=0) {
144. if ($dp != 0) {
145. return round($this->_goodnessOfFit,$dp);
146. }
147. return $this->_goodnessOfFit;
148. } // function getGoodnessOfFit()
151. public function getGoodnessOfFitPercent($dp=0) {
152. if ($dp != 0) {
153. return round($this->_goodnessOfFit * 100,$dp);
154. }
155. return $this->_goodnessOfFit * 100;
156. } // function getGoodnessOfFitPercent()
159. public function getStdevOfResiduals($dp=0) {
160. if ($dp != 0) {
161. return round($this->_stdevOfResiduals,$dp);
162. }
163. return $this->_stdevOfResiduals;
164. } // function getStdevOfResiduals()
167. public function getSSRegression($dp=0) {
168. if ($dp != 0) {
169. return round($this->_SSRegression,$dp);
170. }
171. return $this->_SSRegression;
172. } // function getSSRegression()
175. public function getSSResiduals($dp=0) {
176. if ($dp != 0) {
177. return round($this->_SSResiduals,$dp);
178. }
179. return $this->_SSResiduals;
180. } // function getSSResiduals()
183. public function getDFResiduals($dp=0) {
184. if ($dp != 0) {
185. return round($this->_DFResiduals,$dp);
186. }
187. return $this->_DFResiduals;
188. } // function getDFResiduals()
191. public function getF($dp=0) {
192. if ($dp != 0) {
193. return round($this->_F,$dp);
194. }
195. return $this->_F;
196. } // function getF()
199. public function getCovariance($dp=0) {
200. if ($dp != 0) {
201. return round($this->_covariance,$dp);
202. }
203. return $this->_covariance;
204. } // function getCovariance()
207. public function getCorrelation($dp=0) {
208. if ($dp != 0) {
209. return round($this->_correlation,$dp);
210. }
211. return $this->_correlation;
212. } // function getCorrelation()
215. public function getYBestFitValues() {
216. return $this->_yBestFitValues;
217. } // function getYBestFitValues()
220. protected function _calculateGoodnessOfFit($sumX,$sumY,$sumX2,$sumY2,$sumXY,$meanX,$meanY, $const) {
221. $SSres = $SScov = $SScor = $SStot = $SSsex = 0.0;
222. foreach($this->_xValues as $xKey => $xValue) {
223. $bestFitY = $this->_yBestFitValues[$xKey] = $this-> getValueOfYForX($xValue);
225. $SSres += ($this->_yValues[$xKey] - $bestFitY) * ($this->_yValues[$xKey] - $bestFitY);
226. if ($const) {
227. $SStot += ($this->_yValues[$xKey] - $meanY) * ($this->_yValues[$xKey] - $meanY);
228. } else {
229. $SStot += $this->_yValues[$xKey] * $this->_yValues[$xKey];
230. }
231. $SScov += ($this->_xValues[$xKey] - $meanX) * ($this->_yValues[$xKey] - $meanY);
232. if ($const) {
233. $SSsex += ($this->_xValues[$xKey] - $meanX) * ($this->_xValues[$xKey] - $meanX);
234. } else {
235. $SSsex += $this->_xValues[$xKey] * $this->_xValues[$xKey];
236. }
237. }
239. $this->_SSResiduals = $SSres;
240. $this->_DFResiduals = $this->_valueCount - 1 - $const;
242. if ($this->_DFResiduals == 0.0) {
243. $this->_stdevOfResiduals = 0.0;
244. } else {
245. $this->_stdevOfResiduals = sqrt($SSres / $this->_DFResiduals);
246. }
247. if (($SStot == 0.0) || ($SSres == $SStot)) {
248. $this->_goodnessOfFit = 1;
249. } else {
250. $this->_goodnessOfFit = 1 - ($SSres / $SStot);
251. }
253. $this->_SSRegression = $this->_goodnessOfFit * $SStot;
254. $this->_covariance = $SScov / $this->_valueCount;
255. $this->_correlation = ($this->_valueCount * $sumXY - $sumX * $sumY) / sqrt(($this->_valueCount * $sumX2 - pow($sumX,2)) * ($this->_valueCount * $sumY2 - pow($sumY,2)));
256. $this->_slopeSE = $this->_stdevOfResiduals / sqrt($SSsex);
257. $this->_intersectSE = $this->_stdevOfResiduals * sqrt(1 / ($this->_valueCount - ($sumX * $sumX) / $sumX2));
258. if ($this->_SSResiduals != 0.0) {
259. if ($this->_DFResiduals == 0.0) {
260. $this->_F = 0.0;
261. } else {
262. $this->_F = $this->_SSRegression / ($this->_SSResiduals / $this->_DFResiduals);
263. }
264. } else {
265. if ($this->_DFResiduals == 0.0) {
266. $this->_F = 0.0;
267. } else {
268. $this->_F = $this->_SSRegression / $this->_DFResiduals;
269. }
270. }
271. } // function _calculateGoodnessOfFit()
274. protected function _leastSquareFit($yValues, $xValues, $const) {
275. // calculate sums
276. $x_sum = array_sum($xValues);
277. $y_sum = array_sum($yValues);
278. $meanX = $x_sum / $this->_valueCount;
279. $meanY = $y_sum / $this->_valueCount;
280. $mBase = $mDivisor = $xx_sum = $xy_sum = $yy_sum = 0.0;
281. for($i = 0; $i < $this->_valueCount; ++$i) {
282. $xy_sum += $xValues[$i] * $yValues[$i];
283. $xx_sum += $xValues[$i] * $xValues[$i];
284. $yy_sum += $yValues[$i] * $yValues[$i];
286. if ($const) {
287. $mBase += ($xValues[$i] - $meanX) * ($yValues[$i] - $meanY);
288. $mDivisor += ($xValues[$i] - $meanX) * ($xValues[$i] - $meanX);
289. } else {
290. $mBase += $xValues[$i] * $yValues[$i];
291. $mDivisor += $xValues[$i] * $xValues[$i];
292. }
293. }
295. // calculate slope
296. // $this->_slope = (($this->_valueCount * $xy_sum) - ($x_sum * $y_sum)) / (($this->_valueCount * $xx_sum) - ($x_sum * $x_sum));
297. $this->_slope = $mBase / $mDivisor;
299. // calculate intersect
300. // $this->_intersect = ($y_sum - ($this->_slope * $x_sum)) / $this->_valueCount;
301. if ($const) {
302. $this->_intersect = $meanY - ($this->_slope * $meanX);
303. } else {
304. $this->_intersect = 0;
305. }
307. $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum,$meanX,$meanY,$const);
308. } // function _leastSquareFit()
311. function __construct($yValues, $xValues=array(), $const=True) {
312. // Calculate number of points
313. $nY = count($yValues);
314. $nX = count($xValues);
316. // Define X Values if necessary
317. if ($nX == 0) {
318. $xValues = range(1,$nY);
319. $nX = $nY;
320. } elseif ($nY != $nX) {
321. // Ensure both arrays of points are the same size
322. $this->_error = True;
323. return False;
324. }
326. $this->_valueCount = $nY;
327. $this->_xValues = $xValues;
328. $this->_yValues = $yValues;
329. } // function __construct()
331. } // class bestFit

Documentation generated on Sun, 27 Feb 2011 16:28:30 -0800 by phpDocumentor 1.4.3