ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
bestFitClass.php
Go to the documentation of this file.
1 <?php
37 {
38  protected $_error = False;
39 
40  protected $_bestFitType = 'undetermined';
41 
42  protected $_valueCount = 0;
43 
44  protected $_xValues = array();
45 
46  protected $_yValues = array();
47 
48  protected $_adjustToZero = False;
49 
50  protected $_yBestFitValues = array();
51 
52  protected $_goodnessOfFit = 1;
53 
54  protected $_stdevOfResiduals = 0;
55 
56  protected $_covariance = 0;
57 
58  protected $_correlation = 0;
59 
60  protected $_SSRegression = 0;
61 
62  protected $_SSResiduals = 0;
63 
64  protected $_DFResiduals = 0;
65 
66  protected $_F = 0;
67 
68  protected $_slope = 0;
69 
70  protected $_slopeSE = 0;
71 
72  protected $_intersect = 0;
73 
74  protected $_intersectSE = 0;
75 
76  protected $_Xoffset = 0;
77 
78  protected $_Yoffset = 0;
79 
80 
81  public function getError() {
82  return $this->_error;
83  } // function getBestFitType()
84 
85 
86  public function getBestFitType() {
87  return $this->_bestFitType;
88  } // function getBestFitType()
89 
90 
91  public function getValueOfYForX($xValue) {
92  return False;
93  } // function getValueOfYForX()
94 
95 
96  public function getValueOfXForY($yValue) {
97  return False;
98  } // function getValueOfXForY()
99 
100 
101  public function getXValues() {
102  return $this->_xValues;
103  } // function getValueOfXForY()
104 
105 
106  public function getEquation($dp=0) {
107  return False;
108  } // function getEquation()
109 
110 
111  public function getSlope($dp=0) {
112  if ($dp != 0) {
113  return round($this->_slope,$dp);
114  }
115  return $this->_slope;
116  } // function getSlope()
117 
118 
119  public function getSlopeSE($dp=0) {
120  if ($dp != 0) {
121  return round($this->_slopeSE,$dp);
122  }
123  return $this->_slopeSE;
124  } // function getSlopeSE()
125 
126 
127  public function getIntersect($dp=0) {
128  if ($dp != 0) {
129  return round($this->_intersect,$dp);
130  }
131  return $this->_intersect;
132  } // function getIntersect()
133 
134 
135  public function getIntersectSE($dp=0) {
136  if ($dp != 0) {
137  return round($this->_intersectSE,$dp);
138  }
139  return $this->_intersectSE;
140  } // function getIntersectSE()
141 
142 
143  public function getGoodnessOfFit($dp=0) {
144  if ($dp != 0) {
145  return round($this->_goodnessOfFit,$dp);
146  }
147  return $this->_goodnessOfFit;
148  } // function getGoodnessOfFit()
149 
150 
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()
157 
158 
159  public function getStdevOfResiduals($dp=0) {
160  if ($dp != 0) {
161  return round($this->_stdevOfResiduals,$dp);
162  }
164  } // function getStdevOfResiduals()
165 
166 
167  public function getSSRegression($dp=0) {
168  if ($dp != 0) {
169  return round($this->_SSRegression,$dp);
170  }
171  return $this->_SSRegression;
172  } // function getSSRegression()
173 
174 
175  public function getSSResiduals($dp=0) {
176  if ($dp != 0) {
177  return round($this->_SSResiduals,$dp);
178  }
179  return $this->_SSResiduals;
180  } // function getSSResiduals()
181 
182 
183  public function getDFResiduals($dp=0) {
184  if ($dp != 0) {
185  return round($this->_DFResiduals,$dp);
186  }
187  return $this->_DFResiduals;
188  } // function getDFResiduals()
189 
190 
191  public function getF($dp=0) {
192  if ($dp != 0) {
193  return round($this->_F,$dp);
194  }
195  return $this->_F;
196  } // function getF()
197 
198 
199  public function getCovariance($dp=0) {
200  if ($dp != 0) {
201  return round($this->_covariance,$dp);
202  }
203  return $this->_covariance;
204  } // function getCovariance()
205 
206 
207  public function getCorrelation($dp=0) {
208  if ($dp != 0) {
209  return round($this->_correlation,$dp);
210  }
211  return $this->_correlation;
212  } // function getCorrelation()
213 
214 
215  public function getYBestFitValues() {
216  return $this->_yBestFitValues;
217  } // function getYBestFitValues()
218 
219 
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);
224 
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  }
238 
239  $this->_SSResiduals = $SSres;
240  $this->_DFResiduals = $this->_valueCount - 1 - $const;
241 
242  $this->_stdevOfResiduals = sqrt($SSres / $this->_DFResiduals);
243  if (($SStot == 0.0) || ($SSres == $SStot)) {
244  $this->_goodnessOfFit = 1;
245  } else {
246  $this->_goodnessOfFit = 1 - ($SSres / $SStot);
247  }
248 
249  $this->_SSRegression = $this->_goodnessOfFit * $SStot;
250  $this->_covariance = $SScov / $this->_valueCount;
251  $this->_correlation = ($this->_valueCount * $sumXY - $sumX * $sumY) / sqrt(($this->_valueCount * $sumX2 - pow($sumX,2)) * ($this->_valueCount * $sumY2 - pow($sumY,2)));
252  $this->_slopeSE = $this->_stdevOfResiduals / sqrt($SSsex);
253  $this->_intersectSE = $this->_stdevOfResiduals * sqrt(1 / ($this->_valueCount - ($sumX * $sumX) / $sumX2));
254  if ($this->_SSResiduals != 0.0) {
255  $this->_F = $this->_SSRegression / ($this->_SSResiduals / $this->_DFResiduals);
256  } else {
257  $this->_F = $this->_SSRegression / $this->_DFResiduals;
258  }
259  } // function _calculateGoodnessOfFit()
260 
261 
262  protected function _leastSquareFit($yValues, $xValues, $const) {
263  // calculate sums
264  $x_sum = array_sum($xValues);
265  $y_sum = array_sum($yValues);
266  $meanX = $x_sum / $this->_valueCount;
267  $meanY = $y_sum / $this->_valueCount;
268  $mBase = $mDivisor = $xx_sum = $xy_sum = $yy_sum = 0.0;
269  for($i = 0; $i < $this->_valueCount; ++$i) {
270  $xy_sum += $xValues[$i] * $yValues[$i];
271  $xx_sum += $xValues[$i] * $xValues[$i];
272  $yy_sum += $yValues[$i] * $yValues[$i];
273 
274  if ($const) {
275  $mBase += ($xValues[$i] - $meanX) * ($yValues[$i] - $meanY);
276  $mDivisor += ($xValues[$i] - $meanX) * ($xValues[$i] - $meanX);
277  } else {
278  $mBase += $xValues[$i] * $yValues[$i];
279  $mDivisor += $xValues[$i] * $xValues[$i];
280  }
281  }
282 
283  // calculate slope
284 // $this->_slope = (($this->_valueCount * $xy_sum) - ($x_sum * $y_sum)) / (($this->_valueCount * $xx_sum) - ($x_sum * $x_sum));
285  $this->_slope = $mBase / $mDivisor;
286 
287  // calculate intersect
288 // $this->_intersect = ($y_sum - ($this->_slope * $x_sum)) / $this->_valueCount;
289  if ($const) {
290  $this->_intersect = $meanY - ($this->_slope * $meanX);
291  } else {
292  $this->_intersect = 0;
293  }
294 
295  $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum,$meanX,$meanY,$const);
296  } // function _leastSquareFit()
297 
298 
299  function __construct($yValues, $xValues=array(), $const=True) {
300  // Calculate number of points
301  $nY = count($yValues);
302  $nX = count($xValues);
303 
304  // Define X Values if necessary
305  if ($nX == 0) {
306  $xValues = range(1,$nY);
307  $nX = $nY;
308  } elseif ($nY != $nX) {
309  // Ensure both arrays of points are the same size
310  $this->_error = True;
311  return False;
312  }
313 
314  $this->_valueCount = $nY;
315  $this->_xValues = $xValues;
316  $this->_yValues = $yValues;
317  } // function __construct()
318 
319 } // class bestFit