ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilStatistics.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
15 define("NAN_HANDLING_REMOVE", 0);
16 define("NAN_HANDLING_ZERO", 1);
17
19{
29
37
44 {
45 $this->nan_handling = $nan_handling;
46 $this->stat_data = array();
47 }
48
59 {
60 $this->nan_handling = $nan_handling;
61 }
62
72 function getNANHandling()
73 {
75 }
76
84 {
85 $this->stat_data = array_values($stat_data);
86 $this->validate();
87 }
88
95 function getData()
96 {
97 return $this->stat_data;
98 }
99
107 function min() {
108 if (count($this->stat_data))
109 {
110 $min = min($this->stat_data);
111 }
112 else
113 {
114 $min = false;
115 }
116 return $min;
117 }
118
126 function max() {
127 if (count($this->stat_data))
128 {
129 $max = max($this->stat_data);
130 }
131 else
132 {
133 $max = false;
134 }
135 return $max;
136 }
137
144 function count() {
145 return count($this->stat_data);
146 }
147
155 function sum_n($n) {
156 $sum_n = false;
157 if (count($this->stat_data))
158 {
159 $sum_n = 0;
160 foreach ($this->stat_data as $value)
161 {
162 $sum_n += pow((double)$value, (double)$n);
163 }
164 }
165 return $sum_n;
166 }
167
174 function sum() {
175 return $this->sum_n(1);
176 }
177
178
185 function sum2() {
186 return $this->sum_n(2);
187 }
188
196 function product_n($n) {
197 $prod_n = false;
198 if (count($this->stat_data))
199 {
200 if ($this->min() === 0)
201 {
202 return 0.0;
203 }
204 $prod_n = 1.0;
205 foreach ($this->stat_data as $value)
206 {
207 $prod_n *= pow((double)$value, (double)$n);
208 }
209 }
210 return $prod_n;
211 }
212
220 function product($n) {
221 return $this->product_n(1);
222 }
223
231 function arithmetic_mean() {
232 $sum = $this->sum();
233 if ($sum === false)
234 {
235 return false;
236 }
237 $count = $this->count();
238 if ($count == 0)
239 {
240 return false;
241 }
242 return (double)($sum/$count);
243 }
244
256 function geometric_mean() {
257 $prod = $this->product();
258 if (($prod === false) or ($prod === 0))
259 {
260 return false;
261 }
262 $count = $this->count();
263 if ($count == 0)
264 {
265 return false;
266 }
267 return pow((double)$prod, (double)(1/$count));
268 }
269
277 function harmonic_mean() {
278 $min = $this->min();
279 if (($min === false) or ($min === 0))
280 {
281 return false;
282 }
283 $count = $this->count();
284 if ($count == 0)
285 {
286 return false;
287 }
288 $sum = 0;
289 foreach ($this->stat_data as $value)
290 {
291 $sum += 1/$value;
292 }
293 return $count/$sum;
294 }
295
302 function median() {
303 $median = false;
304 if (count($this->stat_data))
305 {
306 $median = 0;
307 $count = $this->count();
308 if ((count($this->stat_data) % 2) == 0)
309 {
310 $median = ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
311 }
312 else
313 {
314 $median = $this->stat_data[(($count + 1) / 2) - 1];
315 }
316 }
317 return $median;
318 }
319
326 function rank($value)
327 {
328 if (!is_numeric($value))
329 {
330 return false;
331 }
332 $rank = array_search($value, $this->stat_data);
333 if ($rank !== FALSE)
334 {
335 $rank = $this->count() - $rank;
336 }
337 return $rank;
338 }
339
350 function rank_median()
351 {
352 $count = $this->count();
353 if ($count == 0)
354 {
355 return false;
356 }
357
358 if (($count % 2) == 0)
359 {
360 $rank_median = ($count + 1) / 2;
361 }
362 else
363 {
364 $rank_median = ($count + 1) / 2;
365 }
366 return $rank_median;
367 }
368
376 function quantile($n) {
377 $count = $this->count();
378 if ($count == 0)
379 {
380 return false;
381 }
382 $nprod = ($n/100)*$count;
383 if (intval($nprod) == $nprod)
384 {
385 $k = $nprod;
386 if ($k == 0)
387 {
388 return $this->stat_data[$k];
389 }
390 else if ($k == $count)
391 {
392 return $this->stat_data[$k-1];
393 }
394 else
395 {
396 return ($this->stat_data[$k-1] + $this->stat_data[$k])/2;
397 }
398 }
399 else
400 {
401 $k = ceil($nprod);
402 return $this->stat_data[$k-1];
403 }
404 }
405
414 function validate()
415 {
416 $result = true;
417 foreach ($this->stat_data as $key => $value)
418 {
419 if (!is_numeric($value)) {
420 switch ($this->nan_handling) {
422 unset($this->stat_data[$key]);
423 break;
425 $this->stat_data[$key] = 0;
426 break;
427 default:
428 $result = false;
429 }
430 }
431 }
432 sort($this->stat_data);
433 return $result;
434 }
435}
436
437?>
$result
$n
Definition: RandomTest.php:80
const NAN_HANDLING_REMOVE
This class provides mathematical functions for statistics.
const NAN_HANDLING_ZERO
sum()
Calculates the sum of x_1 + x_2 + ... + x_i.
setNANHandling($nan_handling=NAN_HANDLING_REMOVE)
Set the handling of elements which are not a number.
product_n($n)
Calculates the product of x_1^n * x_2^n * ... * x_i^n.
product($n)
Calculates the product of x_1 * x_2 * ... * x_i.
ilStatistics($nan_handling=NAN_HANDLING_REMOVE)
Constructor of ilStatistics class.
quantile($n)
n-Quantile of the data values
arithmetic_mean()
Arithmetic mean of the data values xbar = (1/n)*∑x_i.
getData()
Returns the numeric value array containing the data.
sum_n($n)
Calculates the sum of x_1^n + x_2^n + ... + x_i^n.
setData($stat_data)
Sets the data and checks for invalid values.
harmonic_mean()
Harmonic mean of the data values harmonic_mean = n/(1/x_1 + 1/x_2 + ... + 1/x_n)
geometric_mean()
Geometric mean of the data values geometric_mean = (x_1 * x_2 * ... * x_n)^(1/n)
median()
Median of the data values.
rank($value)
Returns the rank of a given value.
getNANHandling()
Get the handling of elements which are not a number.
rank_median()
Returns the rank of the median.
min()
Calculates the minimum value.
max()
Calculates the maximum value.
count()
Calculates number of data values.
sum2()
Calculates the sum of x_1^2 + x_2^2 + ... + x_i^2.
validate()
Validates the numeric data and handles values which are not a number according to the $nan_handling v...