phpOMS/Business/Finance/Lorenzkurve.php
Dennis Eichhorn 0a8a5c63c5
Some checks failed
Compress images / calibreapp/image-actions (push) Has been cancelled
CI / general_module_workflow_php (push) Has been cancelled
fix permissions
2025-04-02 14:15:07 +00:00

64 lines
1.1 KiB
PHP

<?php
/**
* Jingga
*
* PHP Version 8.2
*
* @package phpOMS\Business\Finance
* @copyright Dennis Eichhorn
* @license OMS License 2.2
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\Business\Finance;
/**
* Finance class.
*
* @package phpOMS\Business\Finance
* @license OMS License 2.2
* @link https://jingga.app
* @since 1.0.0
*/
final class Lorenzkurve
{
/**
* Constructor
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Calculate Gini coefficient
*
* @param array<int|float> $data Data points (can be unsorted)
*
* @return float Returns the Gini coefficient
*
* @since 1.0.0
*/
public static function getGiniCoefficient(array $data) : float
{
$sum1 = 0;
$sum2 = 0;
$i = 1;
$n = \count($data);
\sort($data);
foreach ($data as $key => $value) {
$sum1 += $i * $value;
$sum2 += $value;
++$i;
}
return 2 * $sum1 / ($n * $sum2) - ($n + 1) / $n;
}
}