ILIAS  eassessment Revision 61809
 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  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  }
252 
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()
272 
273 
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];
285 
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  }
294 
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;
298 
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  }
306 
307  $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum,$meanX,$meanY,$const);
308  } // function _leastSquareFit()
309 
310 
311  function __construct($yValues, $xValues=array(), $const=True) {
312  // Calculate number of points
313  $nY = count($yValues);
314  $nX = count($xValues);
315 
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  }
325 
326  $this->_valueCount = $nY;
327  $this->_xValues = $xValues;
328  $this->_yValues = $yValues;
329  } // function __construct()
330 
331 } // class bestFit