public static function quartilesInclusive(array $numbers) : array
{
if (empty($numbers)) {
return array();
}
sort($numbers);
$length = count($numbers);
if ($length % 2 == 0) {
$lower_half = array_slice($numbers, 0, $length / 2);
$upper_half = array_slice($numbers, $length / 2);
} else {
$lower_half = array_slice($numbers, 0, intdiv($length, 2));
$upper_half = array_slice($numbers, intdiv($length, 2) + 1);
// Add median to both halves
$median = Average::median($numbers);
array_push($lower_half, $median);
array_unshift($upper_half, $median);
}
$lower_quartile = Average::median($lower_half);
$upper_quartile = Average::median($upper_half);
return ['0%' => min($numbers), 'Q1' => $lower_quartile, 'Q2' => Average::median($numbers), 'Q3' => $upper_quartile, '100%' => max($numbers), 'IQR' => $upper_quartile - $lower_quartile];
}