lib/Math/Statistics.php
public static function kdeMode(array $population, $space = 512, $bandwidth = null): float
    {
        if (count($population) === 1) {
            return current($population);
        }

        if (count($population) === 0) {
            return 0.0;
        }

        if (min($population) == max($population)) {
            return min($population);
        }

        $kde = new Kde($population, $bandwidth);
        $space = self::linspace(min($population), max($population), $space, true);
        $dist = $kde->evaluate($space);

        $maxKeys = array_keys($dist, max($dist));
        $modes = [];

        foreach ($maxKeys as $maxKey) {
            $modes[] = $space[$maxKey];
        }

        $mode = array_sum($modes) / count($modes);

        return $mode;
    }
public static function kdeMode(array $population, $space = 512, $bandwidth = null): float
    {
        if (count($population) === 1) {
            return current($population);
        }

        if (count($population) === 0) {
            return 0.0;
        }

        if (min($population) == max($population)) {
            return min($population);
        }

        $kde = new Kde($population, $bandwidth);
        $space = self::linspace(min($population), max($population), $space, true);
        $dist = $kde->evaluate($space);

        $maxKeys = array_keys($dist, max($dist));
        $modes = [];

        foreach ($maxKeys as $maxKey) {
            $modes[] = $space[$maxKey];
        }

        $mode = array_sum($modes) / count($modes);

        return $mode;
    }
public static function kdeMode ( array $ population , $ space = 512 , $ bandwidth = null ) : float { if ( count ( $ population ) === 1 ) { return current ( $ population ) ; } if ( count ( $ population ) === 0 ) { return 0.0 ; } if ( min ( $ population ) == max ( $ population ) ) { return min ( $ population ) ; } $ kde = new Kde ( $ population , $ bandwidth ) ; $ space = self :: linspace ( min ( $ population ) , max ( $ population ) , $ space , true ) ; $ dist = $ kde -> evaluate ( $ space ) ; $ maxKeys = array_keys ( $ dist , max ( $ dist ) ) ; $ modes = [ ] ; foreach ( $ maxKeys as $ maxKey ) { $ modes [ ] = $ space [ $ maxKey ] ; } $ mode = array_sum ( $ modes ) / count ( $ modes ) ; return $ mode ; }
Return the mode using the kernel density estimator using the normal
distribution.

The mode is the point in the kernel density estimate with the highest
frequency, i.e. the time which correlates with the highest peak.

If there are two or more modes (i.e. bimodal, trimodal, etc) then we
could take the average of these modes.

NOTE: If the kde estimate of the population is multi-modal (it has two
points with exactly the same value) then the mean mode is returned. This
is potentially misleading, but When benchmarking this should be a very
rare occurance.

@param array $population
@param int $space
@param string $bandwidth
Return the mode using the kernel density estimator using the normal distribution .