Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Lorenzkurve | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| getGiniCoefficient | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Business\Finance |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Business\Finance; |
| 16 | |
| 17 | /** |
| 18 | * Finance class. |
| 19 | * |
| 20 | * @package phpOMS\Business\Finance |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class Lorenzkurve |
| 26 | { |
| 27 | /** |
| 28 | * Constructor |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | * @codeCoverageIgnore |
| 32 | */ |
| 33 | private function __construct() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Calculate Gini coefficient |
| 39 | * |
| 40 | * @param array<int|float> $data Datapoints (can be unsorted) |
| 41 | * |
| 42 | * @return float Returns the gini coefficient |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | public static function getGiniCoefficient(array $data) : float |
| 47 | { |
| 48 | $sum1 = 0; |
| 49 | $sum2 = 0; |
| 50 | $i = 1; |
| 51 | $n = \count($data); |
| 52 | |
| 53 | \sort($data); |
| 54 | |
| 55 | foreach ($data as $key => $value) { |
| 56 | $sum1 += $i * $value; |
| 57 | $sum2 += $value; |
| 58 | ++$i; |
| 59 | } |
| 60 | |
| 61 | return 2 * $sum1 / ($n * $sum2) - ($n + 1) / $n; |
| 62 | } |
| 63 | } |