ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilStatistics.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
34 {
35  public array $stat_data = [];
36 
37  public function setData(array $stat_data): void
38  {
39  $this->stat_data = array_values($stat_data);
40  $this->removeNonNumericValues();
41  }
42 
43  public function getData(): array
44  {
45  return $this->stat_data;
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 arithmetic_mean(): ?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 geometric_mean(): ?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 harmonic_mean(): ?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 === null) {
204  return null;
205  }
206 
207  $median = 0.0;
208  $count = $this->count();
209  if ((count($this->stat_data) % 2) === 0) {
210  return ($this->stat_data[($count / 2) - 1] + $this->stat_data[($count / 2)]) / 2;
211  }
212 
213  return $this->stat_data[(($count + 1) / 2) - 1];
214  }
215 
219  public function rank(float $value): ?int
220  {
221  $rank = array_search($value, $this->stat_data);
222  if ($rank === false) {
223  return null;
224  }
225 
226  return $this->count() - $rank;
227  }
228 
236  public function rank_median(): ?float
237  {
238  $count = $this->count();
239  if ($count === 0) {
240  return null;
241  }
242 
243  if (($count % 2) === 0) {
244  return $rank_median = ($count + 1) / 2;
245  }
246 
247  return $rank_median = ($count + 1) / 2;
248  }
249 
253  public function quantile(float $n): ?float
254  {
255  $count = $this->count();
256  if ($count === 0) {
257  return null;
258  }
259 
260  $nprod = ($n / 100) * $count;
261  if (intval($nprod) != $nprod) {
262  return $this->stat_data[ceil($nprod) - 1];
263  }
264 
265  if ($nprod === 0.0) {
266  return $this->stat_data[0];
267  }
268 
269  if ($nprod === (float) $count) {
270  return $this->stat_data[(int) $nprod - 1];
271  }
272 
273  return ($this->stat_data[(int) $nprod - 1] + $this->stat_data[(int) $nprod]) / 2;
274  }
275 
276  private function removeNonNumericValues(): void
277  {
278 
279  foreach ($this->stat_data as $key => $value) {
280  if (!is_numeric($value)) {
281  unset($this->stat_data[$key]);
282  break;
283  }
284  }
285  sort($this->stat_data);
286  }
287 }
product(int $n)
Calculates the product of x_1 * x_2 * ...
sum()
Calculates the sum of x_1 + x_2 + ...
arithmetic_mean()
Arithmetic mean of the data values xbar = (1/n)*∑x_i.
rank(float $value)
Returns the rank of a given value.
max()
Calculates the maximum value.
median()
Median of the data values.
string $key
Consumer key/client ID value.
Definition: System.php:193
harmonic_mean()
Harmonic mean of the data values harmonic_mean = n/(1/x_1 + 1/x_2 + ...
geometric_mean()
Geometric mean of the data values geometric_mean = (x_1 * x_2 * ...
sum2()
Calculates the sum of x_1^2 + x_2^2 + ...
count()
Calculates number of data values.
rank_median()
Returns the rank of the median.
Constants for the handling of elements which are not a number.
sum_n(int $n)
Calculates the sum of x_1^n + x_2^n + ...
product_n(int $n)
Calculates the product of x_1^n * x_2^n * ...
quantile(float $n)
n-Quantile of the data values
setData(array $stat_data)