ILIAS  release_8 Revision v8.24
class.ilStatistics.php
Go to the documentation of this file.
1<?php
2
24
33{
43
50 public $stat_data;
51
58 {
59 $this->nan_handling = $nan_handling;
60 $this->stat_data = array();
61 }
62
73 {
74 $this->nan_handling = $nan_handling;
75 }
76
86 public function getNANHandling(): int
87 {
89 }
90
97 public function setData($stat_data)
98 {
99 $this->stat_data = array_values($stat_data);
100 $this->validate();
101 }
102
109 public function getData(): array
110 {
111 return $this->stat_data;
112 }
113
121 public function min()
122 {
123 if (count($this->stat_data)) {
124 $min = min($this->stat_data);
125 } else {
126 $min = false;
127 }
128 return $min;
129 }
130
138 public function max()
139 {
140 if (count($this->stat_data)) {
141 $max = max($this->stat_data);
142 } else {
143 $max = false;
144 }
145 return $max;
146 }
147
154 public function count()
155 {
156 return count($this->stat_data);
157 }
158
166 public function sum_n($n)
167 {
168 $sum_n = false;
169 if (count($this->stat_data)) {
170 $sum_n = 0;
171 foreach ($this->stat_data as $value) {
172 $sum_n += pow((float) $value, (float) $n);
173 }
174 }
175 return $sum_n;
176 }
177
184 public function sum()
185 {
186 return $this->sum_n(1);
187 }
188
189
196 public function sum2()
197 {
198 return $this->sum_n(2);
199 }
200
208 public function product_n($n)
209 {
210 $prod_n = false;
211 if (count($this->stat_data)) {
212 if ($this->min() === 0) {
213 return 0.0;
214 }
215 $prod_n = 1.0;
216 foreach ($this->stat_data as $value) {
217 $prod_n *= pow((float) $value, (float) $n);
218 }
219 }
220 return $prod_n;
221 }
222
230 public function product($n)
231 {
232 return $this->product_n(1);
233 }
234
242 public function arithmetic_mean()
243 {
244 $sum = $this->sum();
245 if ($sum === false) {
246 return false;
247 }
248 $count = $this->count();
249 if ($count == 0) {
250 return false;
251 }
252 return (float) ($sum / $count);
253 }
254
266 public function geometric_mean()
267 {
268 $prod = $this->product(1);
269 if (($prod === false) or ($prod === 0)) {
270 return false;
271 }
272 $count = $this->count();
273 if ($count == 0) {
274 return false;
275 }
276 return pow((float) $prod, (float) (1 / $count));
277 }
278
286 public function harmonic_mean()
287 {
288 $min = $this->min();
289 if (($min === false) or ($min === 0)) {
290 return false;
291 }
292 $count = $this->count();
293 if ($count == 0) {
294 return false;
295 }
296 $sum = 0;
297 foreach ($this->stat_data as $value) {
298 $sum += 1 / $value;
299 }
300 return $count / $sum;
301 }
302
309 public function median()
310 {
311 $median = false;
312 if (count($this->stat_data)) {
313 $median = 0;
314 $count = $this->count();
315 if ((count($this->stat_data) % 2) == 0) {
316 $median = ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
317 } else {
318 $median = $this->stat_data[(($count + 1) / 2) - 1];
319 }
320 }
321 return $median;
322 }
323
330 public function rank($value)
331 {
332 if (!is_numeric($value)) {
333 return false;
334 }
335 $rank = array_search($value, $this->stat_data);
336 if ($rank !== false) {
337 $rank = $this->count() - $rank;
338 }
339 return $rank;
340 }
341
352 public function rank_median()
353 {
354 $count = $this->count();
355 if ($count == 0) {
356 return false;
357 }
358
359 if (($count % 2) == 0) {
360 $rank_median = ($count + 1) / 2;
361 } else {
362 $rank_median = ($count + 1) / 2;
363 }
364 return $rank_median;
365 }
366
374 public function quantile($n)
375 {
376 $count = $this->count();
377 if ($count == 0) {
378 return false;
379 }
380 $nprod = ($n / 100) * $count;
381 if (intval($nprod) == $nprod) {
382 $k = $nprod;
383 if ($k == 0) {
384 return $this->stat_data[$k];
385 } elseif ($k == $count) {
386 return $this->stat_data[$k - 1];
387 } else {
388 return ($this->stat_data[$k - 1] + $this->stat_data[$k]) / 2;
389 }
390 } else {
391 $k = ceil($nprod);
392 return $this->stat_data[$k - 1];
393 }
394 }
395
404 public function validate(): bool
405 {
406 $result = true;
407 foreach ($this->stat_data as $key => $value) {
408 if (!is_numeric($value)) {
409 switch ($this->nan_handling) {
411 unset($this->stat_data[$key]);
412 break;
414 $this->stat_data[$key] = 0;
415 break;
416 default:
417 $result = false;
418 }
419 }
420 }
421 sort($this->stat_data);
422 return $result;
423 }
424}
const NAN_HANDLING_REMOVE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const NAN_HANDLING_ZERO
This class provides mathematical functions for statistics.
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...
string $key
Consumer key/client ID value.
Definition: System.php:193