ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
36 public $stat_data;
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 public function getNANHandling()
73 {
75 }
76
83 public function setData($stat_data)
84 {
85 $this->stat_data = array_values($stat_data);
86 $this->validate();
87 }
88
95 public function getData()
96 {
97 return $this->stat_data;
98 }
99
107 public function min()
108 {
109 if (count($this->stat_data)) {
110 $min = min($this->stat_data);
111 } else {
112 $min = false;
113 }
114 return $min;
115 }
116
124 public function max()
125 {
126 if (count($this->stat_data)) {
127 $max = max($this->stat_data);
128 } else {
129 $max = false;
130 }
131 return $max;
132 }
133
140 public function count()
141 {
142 return count($this->stat_data);
143 }
144
152 public function sum_n($n)
153 {
154 $sum_n = false;
155 if (count($this->stat_data)) {
156 $sum_n = 0;
157 foreach ($this->stat_data as $value) {
158 $sum_n += pow((double) $value, (double) $n);
159 }
160 }
161 return $sum_n;
162 }
163
170 public function sum()
171 {
172 return $this->sum_n(1);
173 }
174
175
182 public function sum2()
183 {
184 return $this->sum_n(2);
185 }
186
194 public function product_n($n)
195 {
196 $prod_n = false;
197 if (count($this->stat_data)) {
198 if ($this->min() === 0) {
199 return 0.0;
200 }
201 $prod_n = 1.0;
202 foreach ($this->stat_data as $value) {
203 $prod_n *= pow((double) $value, (double) $n);
204 }
205 }
206 return $prod_n;
207 }
208
216 public function product($n)
217 {
218 return $this->product_n(1);
219 }
220
228 public function arithmetic_mean()
229 {
230 $sum = $this->sum();
231 if ($sum === false) {
232 return false;
233 }
234 $count = $this->count();
235 if ($count == 0) {
236 return false;
237 }
238 return (double) ($sum / $count);
239 }
240
252 public function geometric_mean()
253 {
254 $prod = $this->product();
255 if (($prod === false) or ($prod === 0)) {
256 return false;
257 }
258 $count = $this->count();
259 if ($count == 0) {
260 return false;
261 }
262 return pow((double) $prod, (double) (1 / $count));
263 }
264
272 public function harmonic_mean()
273 {
274 $min = $this->min();
275 if (($min === false) or ($min === 0)) {
276 return false;
277 }
278 $count = $this->count();
279 if ($count == 0) {
280 return false;
281 }
282 $sum = 0;
283 foreach ($this->stat_data as $value) {
284 $sum += 1 / $value;
285 }
286 return $count / $sum;
287 }
288
295 public function median()
296 {
297 $median = false;
298 if (count($this->stat_data)) {
299 $median = 0;
300 $count = $this->count();
301 if ((count($this->stat_data) % 2) == 0) {
302 $median = ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
303 } else {
304 $median = $this->stat_data[(($count + 1) / 2) - 1];
305 }
306 }
307 return $median;
308 }
309
316 public function rank($value)
317 {
318 if (!is_numeric($value)) {
319 return false;
320 }
321 $rank = array_search($value, $this->stat_data);
322 if ($rank !== false) {
323 $rank = $this->count() - $rank;
324 }
325 return $rank;
326 }
327
338 public function rank_median()
339 {
340 $count = $this->count();
341 if ($count == 0) {
342 return false;
343 }
344
345 if (($count % 2) == 0) {
346 $rank_median = ($count + 1) / 2;
347 } else {
348 $rank_median = ($count + 1) / 2;
349 }
350 return $rank_median;
351 }
352
360 public function quantile($n)
361 {
362 $count = $this->count();
363 if ($count == 0) {
364 return false;
365 }
366 $nprod = ($n / 100) * $count;
367 if (intval($nprod) == $nprod) {
368 $k = $nprod;
369 if ($k == 0) {
370 return $this->stat_data[$k];
371 } elseif ($k == $count) {
372 return $this->stat_data[$k - 1];
373 } else {
374 return ($this->stat_data[$k - 1] + $this->stat_data[$k]) / 2;
375 }
376 } else {
377 $k = ceil($nprod);
378 return $this->stat_data[$k - 1];
379 }
380 }
381
390 public function validate()
391 {
392 $result = true;
393 foreach ($this->stat_data as $key => $value) {
394 if (!is_numeric($value)) {
395 switch ($this->nan_handling) {
397 unset($this->stat_data[$key]);
398 break;
400 $this->stat_data[$key] = 0;
401 break;
402 default:
403 $result = false;
404 }
405 }
406 }
407 sort($this->stat_data);
408 return $result;
409 }
410}
$result
$n
Definition: RandomTest.php:85
An exception for terminatinating execution or to throw for unit testing.
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.
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)
__construct($nan_handling=NAN_HANDLING_REMOVE)
Constructor of ilStatistics class.
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...