Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00036 define("NAN_HANDLING_REMOVE", 0);
00037 define("NAN_HANDLING_ZERO", 1);
00038
00039 class ilStatistics
00040 {
00049 var $nan_handling;
00050
00057 var $stat_data;
00058
00064 function ilStatistics($nan_handling = NAN_HANDLING_REMOVE)
00065 {
00066 $this->nan_handling = $nan_handling;
00067 $this->stat_data = array();
00068 }
00069
00079 function setNANHandling($nan_handling = NAN_HANDLING_REMOVE)
00080 {
00081 $this->nan_handling = $nan_handling;
00082 }
00083
00093 function getNANHandling()
00094 {
00095 return $this->nan_handling;
00096 }
00097
00104 function setData($stat_data)
00105 {
00106 $this->stat_data = array_values($stat_data);
00107 $this->validate();
00108 }
00109
00116 function getData()
00117 {
00118 return $this->stat_data;
00119 }
00120
00128 function min() {
00129 if (count($this->stat_data))
00130 {
00131 $min = min($this->stat_data);
00132 }
00133 else
00134 {
00135 $min = false;
00136 }
00137 return $min;
00138 }
00139
00147 function max() {
00148 if (count($this->stat_data))
00149 {
00150 $max = max($this->stat_data);
00151 }
00152 else
00153 {
00154 $max = false;
00155 }
00156 return $max;
00157 }
00158
00165 function count() {
00166 return count($this->stat_data);
00167 }
00168
00176 function sum_n($n) {
00177 $sum_n = false;
00178 if (count($this->stat_data))
00179 {
00180 $sum_n = 0;
00181 foreach ($this->stat_data as $value)
00182 {
00183 $sum_n += pow((double)$value, (double)$n);
00184 }
00185 }
00186 return $sum_n;
00187 }
00188
00195 function sum() {
00196 return $this->sum_n(1);
00197 }
00198
00199
00206 function sum2() {
00207 return $this->sum_n(2);
00208 }
00209
00217 function product_n($n) {
00218 $prod_n = false;
00219 if (count($this->stat_data))
00220 {
00221 if ($this->min() === 0)
00222 {
00223 return 0.0;
00224 }
00225 $prod_n = 1.0;
00226 foreach ($this->stat_data as $value)
00227 {
00228 $prod_n *= pow((double)$value, (double)$n);
00229 }
00230 }
00231 return $prod_n;
00232 }
00233
00241 function product($n) {
00242 return $this->product_n(1);
00243 }
00244
00252 function arithmetic_mean() {
00253 $sum = $this->sum();
00254 if ($sum === false)
00255 {
00256 return false;
00257 }
00258 $count = $this->count();
00259 if ($count == 0)
00260 {
00261 return false;
00262 }
00263 return (double)($sum/$count);
00264 }
00265
00277 function geometric_mean() {
00278 $prod = $this->product();
00279 if (($prod === false) or ($prod === 0))
00280 {
00281 return false;
00282 }
00283 $count = $this->count();
00284 if ($count == 0)
00285 {
00286 return false;
00287 }
00288 return pow((double)$prod, (double)(1/$count));
00289 }
00290
00298 function harmonic_mean() {
00299 $min = $this->min();
00300 if (($min === false) or ($min === 0))
00301 {
00302 return false;
00303 }
00304 $count = $this->count();
00305 if ($count == 0)
00306 {
00307 return false;
00308 }
00309 $sum = 0;
00310 foreach ($this->stat_data as $value)
00311 {
00312 $sum += 1/$value;
00313 }
00314 return $count/$sum;
00315 }
00316
00323 function median() {
00324 $median = false;
00325 if (count($this->stat_data))
00326 {
00327 $median = 0;
00328 $count = $this->count();
00329 if ((count($this->stat_data) % 2) == 0)
00330 {
00331 $median = ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
00332 }
00333 else
00334 {
00335 $median = $this->stat_data[(($count + 1) / 2) - 1];
00336 }
00337 }
00338 return $median;
00339 }
00340
00347 function rank($value)
00348 {
00349 if (!is_numeric($value))
00350 {
00351 return false;
00352 }
00353 $rank = array_search($value, $this->stat_data);
00354 if ($rank !== FALSE)
00355 {
00356 $rank += 1;
00357 }
00358 return $rank;
00359 }
00360
00371 function rank_median()
00372 {
00373 $count = $this->count();
00374 if ($count == 0)
00375 {
00376 return false;
00377 }
00378
00379 if (($count % 2) == 0)
00380 {
00381 $rank_median = ($count*2 + 1) / 2;
00382 }
00383 else
00384 {
00385 $rank_median = ($count + 1) / 2;
00386 }
00387 return $rank_median;
00388 }
00389
00397 function quantile($n) {
00398 $count = $this->count();
00399 if ($count == 0)
00400 {
00401 return false;
00402 }
00403 $nprod = ($n/100)*$count;
00404 if (intval($nprod) == $nprod)
00405 {
00406 $k = $nprod;
00407 if ($k == 0)
00408 {
00409 return $this->stat_data[$k];
00410 }
00411 else if ($k == $count)
00412 {
00413 return $this->stat_data[$k-1];
00414 }
00415 else
00416 {
00417 return ($this->stat_data[$k-1] + $this->stat_data[$k])/2;
00418 }
00419 }
00420 else
00421 {
00422 $k = ceil($nprod);
00423 return $this->stat_data[$k-1];
00424 }
00425 }
00426
00435 function validate()
00436 {
00437 $result = true;
00438 foreach ($this->stat_data as $key => $value)
00439 {
00440 if (!is_numeric($value)) {
00441 switch ($this->nan_handling) {
00442 case NAN_HANDLING_REMOVE:
00443 unset($this->stat_data[$key]);
00444 break;
00445 case NAN_HANDLING_ZERO:
00446 $this->stat_data[$key] = 0;
00447 break;
00448 default:
00449 $result = false;
00450 }
00451 }
00452 }
00453 sort($this->stat_data);
00454 return $result;
00455 }
00456 }
00457
00458 ?>