ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Statistics.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  private array $stat_data = [];
27 
28  public function __construct(\ilTestEvaluationData $eval_data)
29  {
30  $median_array = array_map(
31  fn(\ilTestEvaluationUserData $v): float => $v->getReached(),
32  $eval_data->getParticipants()
33  );
34 
35  $this->stat_data = array_values($median_array);
36  sort($this->stat_data);
37 
38  if (($median_user_id = array_search($this->median(), $median_array)) !== false) {
39  $this->median_user = $eval_data->getParticipant($median_user_id);
40  }
41  }
42 
44  {
45  return $this->median_user;
46  }
47 
48  public function min(): ?float
49  {
50  if ($this->stat_data === []) {
51  return null;
52  }
53 
54  return min($this->stat_data);
55  }
56 
60  public function max(): ?float
61  {
62  if ($this->stat_data === []) {
63  return null;
64  }
65 
66  return max($this->stat_data);
67  }
68 
72  public function count(): int
73  {
74  return count($this->stat_data);
75  }
76 
80  public function sum_n(int $n): ?float
81  {
82  if ($this->stat_data === []) {
83  return null;
84  }
85 
86  $sum_n = 0;
87  foreach ($this->stat_data as $value) {
88  $sum_n += pow((float) $value, (float) $n);
89  }
90  return $sum_n;
91  }
92 
96  public function sum(): ?float
97  {
98  return $this->sum_n(1);
99  }
100 
101 
105  public function sum2(): float
106  {
107  return $this->sum_n(2);
108  }
109 
113  public function product_n(int $n): ?float
114  {
115  if ($this->stat_data === []) {
116  return null;
117  }
118 
119  if ($this->min() === 0) {
120  return 0.0;
121  }
122 
123  $prod_n = 1.0;
124  foreach ($this->stat_data as $value) {
125  $prod_n *= pow((float) $value, (float) $n);
126  }
127  return $prod_n;
128  }
129 
133  public function product(int $n): ?float
134  {
135  return $this->product_n(1);
136  }
137 
142  public function arithmeticMean(): ?float
143  {
144  $sum = $this->sum();
145  if ($sum === null) {
146  return null;
147  }
148 
149  $count = $this->count();
150  if ($count === 0) {
151  return null;
152  }
153  return (float) ($sum / $count);
154  }
155 
164  public function geometricMean(): ?float
165  {
166  $prod = $this->product(1);
167  if (($prod === null) || ($prod === 0)) {
168  return null;
169  }
170  $count = $this->count();
171  if ($count === 0) {
172  return null;
173  }
174  return pow((float) $prod, (float) (1 / $count));
175  }
176 
181  public function harmonicMean(): ?float
182  {
183  $min = $this->min();
184  if (($min === null) or ($min === 0)) {
185  return null;
186  }
187  $count = $this->count();
188  if ($count === 0) {
189  return null;
190  }
191  $sum = 0.0;
192  foreach ($this->stat_data as $value) {
193  $sum += 1 / $value;
194  }
195  return $count / $sum;
196  }
197 
201  public function median(): ?float
202  {
203  if ($this->stat_data === []) {
204  return null;
205  }
206 
207  $count = $this->count();
208  if ((count($this->stat_data) % 2) === 0) {
209  return ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
210  }
211 
212  return $this->stat_data[(($count + 1) / 2) - 1];
213  }
214 
218  public function rank(float $value): ?int
219  {
220  $rank = array_search($value, $this->stat_data);
221  if ($rank === false) {
222  return null;
223  }
224 
225  return $this->count() - $rank;
226  }
227 
235  public function rankMedian(): ?int
236  {
237  $count = $this->count();
238  if ($count === 0) {
239  return null;
240  }
241 
242  if (($count % 2) === 0) {
243  return $count / 2;
244  }
245 
246  return ($count + 1) / 2;
247  }
248 
252  public function quantile(float $n): ?float
253  {
254  $count = $this->count();
255  if ($count === 0) {
256  return null;
257  }
258 
259  $nprod = ($n / 100) * $count;
260  if (intval($nprod) != $nprod) {
261  return $this->stat_data[ceil($nprod) - 1];
262  }
263 
264  if ($nprod === 0.0) {
265  return $this->stat_data[0];
266  }
267 
268  if ($nprod === (float) $count) {
269  return $this->stat_data[(int) $nprod - 1];
270  }
271 
272  return ($this->stat_data[(int) $nprod - 1] + $this->stat_data[(int) $nprod]) / 2;
273  }
274 }
rank(float $value)
Returns the rank of a given value.
Definition: Statistics.php:218
product_n(int $n)
Calculates the product of x_1^n * x_2^n * ...
Definition: Statistics.php:113
ilTestEvaluationUserData $median_user
Definition: Statistics.php:26
quantile(float $n)
n-Quantile of the data values
Definition: Statistics.php:252
count()
Calculates number of data values.
Definition: Statistics.php:72
sum()
Calculates the sum of x_1 + x_2 + ...
Definition: Statistics.php:96
sum_n(int $n)
Calculates the sum of x_1^n + x_2^n + ...
Definition: Statistics.php:80
arithmeticMean()
Arithmetic mean of the data values xbar = (1/n)*∑x_i.
Definition: Statistics.php:142
sort()
description: > Example for rendering a Sort Glyph.
Definition: sort.php:41
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
rankMedian()
Returns the rank of the median.
Definition: Statistics.php:235
harmonicMean()
Harmonic mean of the data values harmonic_mean = n/(1/x_1 + 1/x_2 + ...
Definition: Statistics.php:181
geometricMean()
Geometric mean of the data values geometric_mean = (x_1 * x_2 * ...
Definition: Statistics.php:164
max()
Calculates the maximum value.
Definition: Statistics.php:60
sum2()
Calculates the sum of x_1^2 + x_2^2 + ...
Definition: Statistics.php:105
__construct(\ilTestEvaluationData $eval_data)
Definition: Statistics.php:28
median()
Median of the data values.
Definition: Statistics.php:201
product(int $n)
Calculates the product of x_1 * x_2 * ...
Definition: Statistics.php:133