ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Averages.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class Averages extends AggregateBase
8 {
22  public static function averageDeviations(...$args)
23  {
24  $aArgs = Functions::flattenArrayIndexed($args);
25 
26  // Return value
27  $returnValue = 0;
28 
29  $aMean = self::average(...$args);
30  if ($aMean === Functions::DIV0()) {
31  return Functions::NAN();
32  } elseif ($aMean === Functions::VALUE()) {
33  return Functions::VALUE();
34  }
35 
36  $aCount = 0;
37  foreach ($aArgs as $k => $arg) {
38  $arg = self::testAcceptedBoolean($arg, $k);
39  // Is it a numeric value?
40  // Strings containing numeric values are only counted if they are string literals (not cell values)
41  // and then only in MS Excel and in Open Office, not in Gnumeric
42  if ((is_string($arg)) && (!is_numeric($arg)) && (!Functions::isCellValue($k))) {
43  return Functions::VALUE();
44  }
45  if (self::isAcceptedCountable($arg, $k)) {
46  $returnValue += abs($arg - $aMean);
47  ++$aCount;
48  }
49  }
50 
51  // Return
52  if ($aCount === 0) {
53  return Functions::DIV0();
54  }
55 
56  return $returnValue / $aCount;
57  }
58 
71  public static function average(...$args)
72  {
73  $returnValue = $aCount = 0;
74 
75  // Loop through arguments
76  foreach (Functions::flattenArrayIndexed($args) as $k => $arg) {
77  $arg = self::testAcceptedBoolean($arg, $k);
78  // Is it a numeric value?
79  // Strings containing numeric values are only counted if they are string literals (not cell values)
80  // and then only in MS Excel and in Open Office, not in Gnumeric
81  if ((is_string($arg)) && (!is_numeric($arg)) && (!Functions::isCellValue($k))) {
82  return Functions::VALUE();
83  }
84  if (self::isAcceptedCountable($arg, $k)) {
85  $returnValue += $arg;
86  ++$aCount;
87  }
88  }
89 
90  // Return
91  if ($aCount > 0) {
92  return $returnValue / $aCount;
93  }
94 
95  return Functions::DIV0();
96  }
97 
110  public static function averageA(...$args)
111  {
112  $returnValue = null;
113 
114  $aCount = 0;
115  // Loop through arguments
116  foreach (Functions::flattenArrayIndexed($args) as $k => $arg) {
117  if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
118  } else {
119  if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
120  if (is_bool($arg)) {
121  $arg = (int) $arg;
122  } elseif (is_string($arg)) {
123  $arg = 0;
124  }
125  $returnValue += $arg;
126  ++$aCount;
127  }
128  }
129  }
130 
131  if ($aCount > 0) {
132  return $returnValue / $aCount;
133  }
134 
135  return Functions::DIV0();
136  }
137 
150  public static function median(...$args)
151  {
152  $aArgs = Functions::flattenArray($args);
153 
154  $returnValue = Functions::NAN();
155 
156  $aArgs = self::filterArguments($aArgs);
157  $valueCount = count($aArgs);
158  if ($valueCount > 0) {
159  sort($aArgs, SORT_NUMERIC);
160  $valueCount = $valueCount / 2;
161  if ($valueCount == floor($valueCount)) {
162  $returnValue = ($aArgs[$valueCount--] + $aArgs[$valueCount]) / 2;
163  } else {
164  $valueCount = floor($valueCount);
165  $returnValue = $aArgs[$valueCount];
166  }
167  }
168 
169  return $returnValue;
170  }
171 
184  public static function mode(...$args)
185  {
186  $returnValue = Functions::NA();
187 
188  // Loop through arguments
189  $aArgs = Functions::flattenArray($args);
190  $aArgs = self::filterArguments($aArgs);
191 
192  if (!empty($aArgs)) {
193  return self::modeCalc($aArgs);
194  }
195 
196  return $returnValue;
197  }
198 
199  protected static function filterArguments($args)
200  {
201  return array_filter(
202  $args,
203  function ($value) {
204  // Is it a numeric value?
205  return (is_numeric($value)) && (!is_string($value));
206  }
207  );
208  }
209 
210  //
211  // Special variant of array_count_values that isn't limited to strings and integers,
212  // but can work with floating point numbers as values
213  //
214  private static function modeCalc($data)
215  {
216  $frequencyArray = [];
217  $index = 0;
218  $maxfreq = 0;
219  $maxfreqkey = '';
220  $maxfreqdatum = '';
221  foreach ($data as $datum) {
222  $found = false;
223  ++$index;
224  foreach ($frequencyArray as $key => $value) {
225  if ((string) $value['value'] == (string) $datum) {
226  ++$frequencyArray[$key]['frequency'];
227  $freq = $frequencyArray[$key]['frequency'];
228  if ($freq > $maxfreq) {
229  $maxfreq = $freq;
230  $maxfreqkey = $key;
231  $maxfreqdatum = $datum;
232  } elseif ($freq == $maxfreq) {
233  if ($frequencyArray[$key]['index'] < $frequencyArray[$maxfreqkey]['index']) {
234  $maxfreqkey = $key;
235  $maxfreqdatum = $datum;
236  }
237  }
238  $found = true;
239 
240  break;
241  }
242  }
243 
244  if ($found === false) {
245  $frequencyArray[] = [
246  'value' => $datum,
247  'frequency' => 1,
248  'index' => $index,
249  ];
250  }
251  }
252 
253  if ($maxfreq <= 1) {
254  return Functions::NA();
255  }
256 
257  return $maxfreqdatum;
258  }
259 }
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
$index
Definition: metadata.php:60
static flattenArrayIndexed($array)
Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing...
Definition: Functions.php:616
$key
Definition: croninfo.php:18
$data
Definition: bench.php:6