Conditions | 6 |
Paths | 6 |
Total Lines | 22 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
14 | public static function population(array $numbers, bool $sample = true): float |
||
15 | { |
||
16 | $n = count($numbers); |
||
17 | if ($n === 0) { |
||
18 | throw new InvalidArgumentException('The array has zero elements'); |
||
19 | } |
||
20 | |||
21 | if ($sample && $n === 1) { |
||
22 | throw new InvalidArgumentException('The array must have at least 2 elements'); |
||
23 | } |
||
24 | |||
25 | $mean = Mean::arithmetic($numbers); |
||
26 | $carry = 0.0; |
||
27 | foreach ($numbers as $val) { |
||
28 | $carry += ($val - $mean) ** 2; |
||
29 | } |
||
30 | |||
31 | if ($sample) { |
||
32 | --$n; |
||
33 | } |
||
34 | |||
35 | return ($carry / $n) ** .5; |
||
36 | } |
||
60 |