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
00035 define("NAN_HANDLING_REMOVE", 0);
00036 define("NAN_HANDLING_ZERO", 1);
00037
00038 class ilStatistics
00039 {
00048 var $nan_handling;
00049
00056 var $stat_data;
00057
00063 function ilStatistics($nan_handling = NAN_HANDLING_REMOVE)
00064 {
00065 $this->nan_handling = $nan_handling;
00066 $this->stat_data = array();
00067 }
00068
00078 function setNANHandling($nan_handling = NAN_HANDLING_REMOVE)
00079 {
00080 $this->nan_handling = $nan_handling;
00081 }
00082
00092 function getNANHandling()
00093 {
00094 return $this->nan_handling;
00095 }
00096
00103 function setData($stat_data)
00104 {
00105 $this->stat_data = array_values($stat_data);
00106 $this->validate();
00107 }
00108
00115 function getData()
00116 {
00117 return $this->stat_data;
00118 }
00119
00127 function min() {
00128 if (count($this->stat_data))
00129 {
00130 $min = min($this->stat_data);
00131 }
00132 else
00133 {
00134 $min = false;
00135 }
00136 return $min;
00137 }
00138
00146 function max() {
00147 if (count($this->stat_data))
00148 {
00149 $max = max($this->stat_data);
00150 }
00151 else
00152 {
00153 $max = false;
00154 }
00155 return $max;
00156 }
00157
00164 function count() {
00165 return count($this->stat_data);
00166 }
00167
00175 function sum_n($n) {
00176 $sum_n = false;
00177 if (count($this->stat_data))
00178 {
00179 $sum_n = 0;
00180 foreach ($this->stat_data as $value)
00181 {
00182 $sum_n += pow((double)$value, (double)$n);
00183 }
00184 }
00185 return $sum_n;
00186 }
00187
00194 function sum() {
00195 return $this->sum_n(1);
00196 }
00197
00198
00205 function sum2() {
00206 return $this->sum_n(2);
00207 }
00208
00216 function product_n($n) {
00217 $prod_n = false;
00218 if (count($this->stat_data))
00219 {
00220 if ($this->min() === 0)
00221 {
00222 return 0.0;
00223 }
00224 $prod_n = 1.0;
00225 foreach ($this->stat_data as $value)
00226 {
00227 $prod_n *= pow((double)$value, (double)$n);
00228 }
00229 }
00230 return $prod_n;
00231 }
00232
00240 function product($n) {
00241 return $this->product_n(1);
00242 }
00243
00251 function arithmetic_mean() {
00252 $sum = $this->sum();
00253 if ($sum === false)
00254 {
00255 return false;
00256 }
00257 $count = $this->count();
00258 if ($count == 0)
00259 {
00260 return false;
00261 }
00262 return (double)($sum/$count);
00263 }
00264
00276 function geometric_mean() {
00277 $prod = $this->product();
00278 if (($prod === false) or ($prod === 0))
00279 {
00280 return false;
00281 }
00282 $count = $this->count();
00283 if ($count == 0)
00284 {
00285 return false;
00286 }
00287 return pow((double)$prod, (double)(1/$count));
00288 }
00289
00297 function harmonic_mean() {
00298 $min = $this->min();
00299 if (($min === false) or ($min === 0))
00300 {
00301 return false;
00302 }
00303 $count = $this->count();
00304 if ($count == 0)
00305 {
00306 return false;
00307 }
00308 $sum = 0;
00309 foreach ($this->stat_data as $value)
00310 {
00311 $sum += 1/$value;
00312 }
00313 return $count/$sum;
00314 }
00315
00322 function median() {
00323 $median = false;
00324 if (count($this->stat_data))
00325 {
00326 $median = 0;
00327 $count = $this->count();
00328 if ((count($this->stat_data) % 2) == 0)
00329 {
00330 $median = ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
00331 }
00332 else
00333 {
00334 $median = $this->stat_data[(($count + 1) / 2) - 1];
00335 }
00336 }
00337 return $median;
00338 }
00339
00346 function rank($value)
00347 {
00348 if (!is_numeric($value))
00349 {
00350 return false;
00351 }
00352 $rank = array_search($value, $this->stat_data);
00353 if ($rank !== FALSE)
00354 {
00355 $rank = $this->count() - $rank;
00356 }
00357 return $rank;
00358 }
00359
00370 function rank_median()
00371 {
00372 $count = $this->count();
00373 if ($count == 0)
00374 {
00375 return false;
00376 }
00377
00378 if (($count % 2) == 0)
00379 {
00380 $rank_median = ($count + 1) / 2;
00381 }
00382 else
00383 {
00384 $rank_median = ($count + 1) / 2;
00385 }
00386 return $rank_median;
00387 }
00388
00396 function quantile($n) {
00397 $count = $this->count();
00398 if ($count == 0)
00399 {
00400 return false;
00401 }
00402 $nprod = ($n/100)*$count;
00403 if (intval($nprod) == $nprod)
00404 {
00405 $k = $nprod;
00406 if ($k == 0)
00407 {
00408 return $this->stat_data[$k];
00409 }
00410 else if ($k == $count)
00411 {
00412 return $this->stat_data[$k-1];
00413 }
00414 else
00415 {
00416 return ($this->stat_data[$k-1] + $this->stat_data[$k])/2;
00417 }
00418 }
00419 else
00420 {
00421 $k = ceil($nprod);
00422 return $this->stat_data[$k-1];
00423 }
00424 }
00425
00434 function validate()
00435 {
00436 $result = true;
00437 foreach ($this->stat_data as $key => $value)
00438 {
00439 if (!is_numeric($value)) {
00440 switch ($this->nan_handling) {
00441 case NAN_HANDLING_REMOVE:
00442 unset($this->stat_data[$key]);
00443 break;
00444 case NAN_HANDLING_ZERO:
00445 $this->stat_data[$key] = 0;
00446 break;
00447 default:
00448 $result = false;
00449 }
00450 }
00451 }
00452 sort($this->stat_data);
00453 return $result;
00454 }
00455 }
00456
00457 ?>