MathPHP\Statistics\Average::truncatedMean PHP Метод

truncatedMean() публичный статический Метод

This number of points to be discarded is given as a percentage of the total number of points. https://en.wikipedia.org/wiki/Truncated_mean Trim count = floor( (trim percent / 100) * sample size ) For example: [8, 3, 7, 1, 3, 9] with a trim of 20% First sort the list: [1, 3, 3, 7, 8, 9] Sample size = 6 Then determine trim count: floot(20/100 * 6 ) = 1 Trim the list by removing 1 from each end: [3, 3, 7, 8] Finally, find the mean: 5.2
public static truncatedMean ( array $numbers, integer $trim_percent ) : number
$numbers array
$trim_percent integer Percent between 0-99
Результат number
    public static function truncatedMean(array $numbers, int $trim_percent)
    {
        if ($trim_percent < 0 || $trim_percent > 99) {
            throw new Exception\OutOfBoundsException('Trim percent must be between 0 and 99.');
        }
        $n = count($numbers);
        $trim_count = floor($n * ($trim_percent / 100));
        sort($numbers);
        for ($i = 1; $i <= $trim_count; $i++) {
            array_shift($numbers);
            array_pop($numbers);
        }
        return self::mean($numbers);
    }