ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilStatistics.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35  define("NAN_HANDLING_REMOVE", 0);
36  define("NAN_HANDLING_ZERO", 1);
37 
39 {
49 
57 
64  {
65  $this->nan_handling = $nan_handling;
66  $this->stat_data = array();
67  }
68 
79  {
80  $this->nan_handling = $nan_handling;
81  }
82 
92  function getNANHandling()
93  {
94  return $this->nan_handling;
95  }
96 
103  function setData($stat_data)
104  {
105  $this->stat_data = array_values($stat_data);
106  $this->validate();
107  }
108 
115  function getData()
116  {
117  return $this->stat_data;
118  }
119 
127  function min() {
128  if (count($this->stat_data))
129  {
130  $min = min($this->stat_data);
131  }
132  else
133  {
134  $min = false;
135  }
136  return $min;
137  }
138 
146  function max() {
147  if (count($this->stat_data))
148  {
149  $max = max($this->stat_data);
150  }
151  else
152  {
153  $max = false;
154  }
155  return $max;
156  }
157 
164  function count() {
165  return count($this->stat_data);
166  }
167 
175  function sum_n($n) {
176  $sum_n = false;
177  if (count($this->stat_data))
178  {
179  $sum_n = 0;
180  foreach ($this->stat_data as $value)
181  {
182  $sum_n += pow((double)$value, (double)$n);
183  }
184  }
185  return $sum_n;
186  }
187 
194  function sum() {
195  return $this->sum_n(1);
196  }
197 
198 
205  function sum2() {
206  return $this->sum_n(2);
207  }
208 
216  function product_n($n) {
217  $prod_n = false;
218  if (count($this->stat_data))
219  {
220  if ($this->min() === 0)
221  {
222  return 0.0;
223  }
224  $prod_n = 1.0;
225  foreach ($this->stat_data as $value)
226  {
227  $prod_n *= pow((double)$value, (double)$n);
228  }
229  }
230  return $prod_n;
231  }
232 
240  function product($n) {
241  return $this->product_n(1);
242  }
243 
251  function arithmetic_mean() {
252  $sum = $this->sum();
253  if ($sum === false)
254  {
255  return false;
256  }
257  $count = $this->count();
258  if ($count == 0)
259  {
260  return false;
261  }
262  return (double)($sum/$count);
263  }
264 
276  function geometric_mean() {
277  $prod = $this->product();
278  if (($prod === false) or ($prod === 0))
279  {
280  return false;
281  }
282  $count = $this->count();
283  if ($count == 0)
284  {
285  return false;
286  }
287  return pow((double)$prod, (double)(1/$count));
288  }
289 
297  function harmonic_mean() {
298  $min = $this->min();
299  if (($min === false) or ($min === 0))
300  {
301  return false;
302  }
303  $count = $this->count();
304  if ($count == 0)
305  {
306  return false;
307  }
308  $sum = 0;
309  foreach ($this->stat_data as $value)
310  {
311  $sum += 1/$value;
312  }
313  return $count/$sum;
314  }
315 
322  function median() {
323  $median = false;
324  if (count($this->stat_data))
325  {
326  $median = 0;
327  $count = $this->count();
328  if ((count($this->stat_data) % 2) == 0)
329  {
330  $median = ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
331  }
332  else
333  {
334  $median = $this->stat_data[(($count + 1) / 2) - 1];
335  }
336  }
337  return $median;
338  }
339 
346  function rank($value)
347  {
348  if (!is_numeric($value))
349  {
350  return false;
351  }
352  $rank = array_search($value, $this->stat_data);
353  if ($rank !== FALSE)
354  {
355  $rank = $this->count() - $rank;
356  }
357  return $rank;
358  }
359 
370  function rank_median()
371  {
372  $count = $this->count();
373  if ($count == 0)
374  {
375  return false;
376  }
377 
378  if (($count % 2) == 0)
379  {
380  $rank_median = ($count + 1) / 2;
381  }
382  else
383  {
384  $rank_median = ($count + 1) / 2;
385  }
386  return $rank_median;
387  }
388 
396  function quantile($n) {
397  $count = $this->count();
398  if ($count == 0)
399  {
400  return false;
401  }
402  $nprod = ($n/100)*$count;
403  if (intval($nprod) == $nprod)
404  {
405  $k = $nprod;
406  if ($k == 0)
407  {
408  return $this->stat_data[$k];
409  }
410  else if ($k == $count)
411  {
412  return $this->stat_data[$k-1];
413  }
414  else
415  {
416  return ($this->stat_data[$k-1] + $this->stat_data[$k])/2;
417  }
418  }
419  else
420  {
421  $k = ceil($nprod);
422  return $this->stat_data[$k-1];
423  }
424  }
425 
434  function validate()
435  {
436  $result = true;
437  foreach ($this->stat_data as $key => $value)
438  {
439  if (!is_numeric($value)) {
440  switch ($this->nan_handling) {
441  case NAN_HANDLING_REMOVE:
442  unset($this->stat_data[$key]);
443  break;
444  case NAN_HANDLING_ZERO:
445  $this->stat_data[$key] = 0;
446  break;
447  default:
448  $result = false;
449  }
450  }
451  }
452  sort($this->stat_data);
453  return $result;
454  }
455 }
456 
457 ?>