public static function harmonicMean(array $numbers)
{
if (empty($numbers)) {
return null;
}
// Can't be computed for negative values.
if (!empty(array_filter($numbers, function ($x) {
return $x < 0;
}))) {
throw new Exception\BadDataException('Harmonic mean cannot be computed for negative values.');
}
$n = count($numbers);
return $n / array_sum(array_map(function ($x) {
return 1 / $x;
}, $numbers));
}