ILIAS  release_8 Revision v8.24
ilStatistics Class Reference

This class provides mathematical functions for statistics. More...

+ Collaboration diagram for ilStatistics:

Public Member Functions

 __construct ($nan_handling=NAN_HANDLING_REMOVE)
 Constructor of ilStatistics class. More...
 
 setNANHandling ($nan_handling=NAN_HANDLING_REMOVE)
 Set the handling of elements which are not a number. More...
 
 getNANHandling ()
 Get the handling of elements which are not a number. More...
 
 setData ($stat_data)
 Sets the data and checks for invalid values. More...
 
 getData ()
 Returns the numeric value array containing the data. More...
 
 min ()
 Calculates the minimum value. More...
 
 max ()
 Calculates the maximum value. More...
 
 count ()
 Calculates number of data values. More...
 
 sum_n ($n)
 Calculates the sum of x_1^n + x_2^n + ... + x_i^n. More...
 
 sum ()
 Calculates the sum of x_1 + x_2 + ... + x_i. More...
 
 sum2 ()
 Calculates the sum of x_1^2 + x_2^2 + ... + x_i^2. More...
 
 product_n ($n)
 Calculates the product of x_1^n * x_2^n * ... * x_i^n. More...
 
 product ($n)
 Calculates the product of x_1 * x_2 * ... * x_i. More...
 
 arithmetic_mean ()
 Arithmetic mean of the data values xbar = (1/n)*∑x_i. More...
 
 geometric_mean ()
 Geometric mean of the data values geometric_mean = (x_1 * x_2 * ... * x_n)^(1/n) More...
 
 harmonic_mean ()
 Harmonic mean of the data values harmonic_mean = n/(1/x_1 + 1/x_2 + ... + 1/x_n) More...
 
 median ()
 Median of the data values. More...
 
 rank ($value)
 Returns the rank of a given value. More...
 
 rank_median ()
 Returns the rank of the median. More...
 
 quantile ($n)
 n-Quantile of the data values More...
 
 validate ()
 Validates the numeric data and handles values which are not a number according to the $nan_handling variable. More...
 

Data Fields

 $nan_handling
 
 $stat_data
 

Detailed Description

This class provides mathematical functions for statistics.

It works on an array of numeric values.

Author
Helmut Schottmüller hscho.nosp@m.ttm@.nosp@m.tzi.d.nosp@m.e
Version
$Id$

Definition at line 32 of file class.ilStatistics.php.

Constructor & Destructor Documentation

◆ __construct()

ilStatistics::__construct (   $nan_handling = NAN_HANDLING_REMOVE)

Constructor of ilStatistics class.

@access public

Definition at line 57 of file class.ilStatistics.php.

58 {
59 $this->nan_handling = $nan_handling;
60 $this->stat_data = array();
61 }

References $nan_handling.

Member Function Documentation

◆ arithmetic_mean()

ilStatistics::arithmetic_mean ( )

Arithmetic mean of the data values xbar = (1/n)*∑x_i.

Returns
mixed The arithmetic mean or false, if there is an error or no values @access public

Definition at line 242 of file class.ilStatistics.php.

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 }
sum()
Calculates the sum of x_1 + x_2 + ... + x_i.
count()
Calculates number of data values.

References count(), and sum().

+ Here is the call graph for this function:

◆ count()

ilStatistics::count ( )

Calculates number of data values.

Returns
mixed The number of data values @access public

Definition at line 154 of file class.ilStatistics.php.

155 {
156 return count($this->stat_data);
157 }

References count().

Referenced by arithmetic_mean(), count(), geometric_mean(), harmonic_mean(), max(), median(), min(), product_n(), quantile(), rank(), rank_median(), and sum_n().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ geometric_mean()

ilStatistics::geometric_mean ( )

Geometric mean of the data values geometric_mean = (x_1 * x_2 * ... * x_n)^(1/n)

The geometric mean of a set of positive data is defined as the product of all the members of the set, raised to a power equal to the reciprocal of the number of members.

Returns
mixed The geometric mean or false, if there is an error or no values @access public

Definition at line 266 of file class.ilStatistics.php.

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 }
product($n)
Calculates the product of x_1 * x_2 * ... * x_i.

References count(), and product().

+ Here is the call graph for this function:

◆ getData()

ilStatistics::getData ( )

Returns the numeric value array containing the data.

Returns
array An array containing the sorted numeric data @access public

Definition at line 109 of file class.ilStatistics.php.

109 : array
110 {
111 return $this->stat_data;
112 }

References $stat_data.

◆ getNANHandling()

ilStatistics::getNANHandling ( )

Get the handling of elements which are not a number.

Returns NAN_HANDLING_REMOVE if all elements which are not a number will be removed. Returns NAN_HANDLING_ZERO if all elements which are not a number will be set to zero.

Returns
integer A constant defining the handling of elements which are not a number @access public

Definition at line 86 of file class.ilStatistics.php.

86 : int
87 {
89 }

References $nan_handling.

◆ harmonic_mean()

ilStatistics::harmonic_mean ( )

Harmonic mean of the data values harmonic_mean = n/(1/x_1 + 1/x_2 + ... + 1/x_n)

Returns
mixed The harmonic mean or false, if there is an error or no values @access public

Definition at line 286 of file class.ilStatistics.php.

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 }
min()
Calculates the minimum value.

References count(), and min().

+ Here is the call graph for this function:

◆ max()

ilStatistics::max ( )

Calculates the maximum value.

Returns
mixed The maximum value or false, if no maximum exists
See also
min() @access public

Definition at line 138 of file class.ilStatistics.php.

139 {
140 if (count($this->stat_data)) {
141 $max = max($this->stat_data);
142 } else {
143 $max = false;
144 }
145 return $max;
146 }
max()
Calculates the maximum value.

References count(), and max().

Referenced by max().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ median()

ilStatistics::median ( )

Median of the data values.

Returns
mixed The median or false, if there are no data values @access public

Definition at line 309 of file class.ilStatistics.php.

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 }

References count().

+ Here is the call graph for this function:

◆ min()

ilStatistics::min ( )

Calculates the minimum value.

Returns
mixed The minimum value or false, if no minimum exists
See also
max() @access public

Definition at line 121 of file class.ilStatistics.php.

122 {
123 if (count($this->stat_data)) {
124 $min = min($this->stat_data);
125 } else {
126 $min = false;
127 }
128 return $min;
129 }

References count(), and min().

Referenced by harmonic_mean(), min(), and product_n().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ product()

ilStatistics::product (   $n)

Calculates the product of x_1 * x_2 * ... * x_i.

Parameters
numeric$nThe exponent
Returns
mixed The product of x_1 * x_2 * ... * x_i or false, if no values exist @access public

Definition at line 230 of file class.ilStatistics.php.

231 {
232 return $this->product_n(1);
233 }
product_n($n)
Calculates the product of x_1^n * x_2^n * ... * x_i^n.

References product_n().

Referenced by geometric_mean().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ product_n()

ilStatistics::product_n (   $n)

Calculates the product of x_1^n * x_2^n * ... * x_i^n.

Parameters
numeric$nThe exponent
Returns
mixed The product of x_1^n * x_2^n * ... * x_i^n or false, if no values exist @access public

Definition at line 208 of file class.ilStatistics.php.

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 }

References count(), and min().

Referenced by product().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quantile()

ilStatistics::quantile (   $n)

n-Quantile of the data values

Parameters
double$nA value between 0 an 100 calculating the n-Quantile
Returns
mixed The n-quantile or false, if there are no data values @access public

Definition at line 374 of file class.ilStatistics.php.

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 }

References count().

+ Here is the call graph for this function:

◆ rank()

ilStatistics::rank (   $value)

Returns the rank of a given value.

Returns
mixed The rank, if the value exists in the data, otherwise false @access public

Definition at line 330 of file class.ilStatistics.php.

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 }

References count().

+ Here is the call graph for this function:

◆ rank_median()

ilStatistics::rank_median ( )

Returns the rank of the median.

This method is different from the rank method because the median could be the arithmetic mean of the two middle values when the data size is even. In this case the median could a value which is not part of the data set.

Returns
mixed The rank of the median, otherwise false @access public

Definition at line 352 of file class.ilStatistics.php.

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 }

References count().

+ Here is the call graph for this function:

◆ setData()

ilStatistics::setData (   $stat_data)

Sets the data and checks for invalid values.

Parameters
array$stat_dataAn array containing the numeric data @access public

Definition at line 97 of file class.ilStatistics.php.

98 {
99 $this->stat_data = array_values($stat_data);
100 $this->validate();
101 }
validate()
Validates the numeric data and handles values which are not a number according to the $nan_handling v...

References $stat_data, and validate().

+ Here is the call graph for this function:

◆ setNANHandling()

ilStatistics::setNANHandling (   $nan_handling = NAN_HANDLING_REMOVE)

Set the handling of elements which are not a number.

If set to NAN_HANDLING_REMOVE, all elements which are not a number will be removed, if set to NAN_HANDLING_ZERO, all elements which are not a number will be set to zero.

Parameters
integer$nan_handlingA constant defining the handling of elements which are not a number @access public

Definition at line 72 of file class.ilStatistics.php.

73 {
74 $this->nan_handling = $nan_handling;
75 }

References $nan_handling.

◆ sum()

ilStatistics::sum ( )

Calculates the sum of x_1 + x_2 + ... + x_i.

Returns
mixed The sum of x_1 + x_2 + ... + x_i or false, if no values exist @access public

Definition at line 184 of file class.ilStatistics.php.

185 {
186 return $this->sum_n(1);
187 }
sum_n($n)
Calculates the sum of x_1^n + x_2^n + ... + x_i^n.

References sum_n().

Referenced by arithmetic_mean().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sum2()

ilStatistics::sum2 ( )

Calculates the sum of x_1^2 + x_2^2 + ... + x_i^2.

Returns
mixed The sum of x_1^2 + x_2^2 + ... + x_i^2 or false, if no values exist @access public

Definition at line 196 of file class.ilStatistics.php.

197 {
198 return $this->sum_n(2);
199 }

References sum_n().

+ Here is the call graph for this function:

◆ sum_n()

ilStatistics::sum_n (   $n)

Calculates the sum of x_1^n + x_2^n + ... + x_i^n.

Parameters
numeric$nThe exponent
Returns
mixed The sum of x_1^n + x_2^n + ... + x_i^n or false, if no values exist @access public

Definition at line 166 of file class.ilStatistics.php.

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 }

References count().

Referenced by sum(), and sum2().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validate()

ilStatistics::validate ( )

Validates the numeric data and handles values which are not a number according to the $nan_handling variable.

After validation the data is sorted.

Returns
boolean Returns true on success, otherwise false @access private

Definition at line 404 of file class.ilStatistics.php.

404 : 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 }
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
string $key
Consumer key/client ID value.
Definition: System.php:193

References ILIAS\LTI\ToolProvider\$key, NAN_HANDLING_REMOVE, and NAN_HANDLING_ZERO.

Referenced by setData().

+ Here is the caller graph for this function:

Field Documentation

◆ $nan_handling

ilStatistics::$nan_handling

Definition at line 42 of file class.ilStatistics.php.

Referenced by __construct(), getNANHandling(), and setNANHandling().

◆ $stat_data

ilStatistics::$stat_data

Definition at line 50 of file class.ilStatistics.php.

Referenced by getData(), and setData().


The documentation for this class was generated from the following file: