Return the standard deviation of a given population.

@param array $values
@param bool $sample

@return float
428881
public static function stdev(array $values, $sample = false)
    {
        $variance = self::variance($values, $sample);

        return \sqrt($variance);
    }
704303
private static function sampleRow(array $sample): string
    {
        $row = [];
        foreach ($sample as $index => $feature) {
            $row[] = sprintf('%s:%s', $index + 1, $feature);
        }

        return implode(' ', $row);
    }
758927
protected function aggregateValues(array $values)
    {
        $min = $values[0];

        foreach ($values as $value) {
            if ($value < $min) {
                $min = $value;
            }
        }

        return $min;
    }
1092656
public function prune(array $results)
    {
        $mean = array_sum($results) / count($results);
        $deviation = $this->deviations * $this->standardDeviation($results);
        $lower = $mean - $deviation;
        $upper = $mean + $deviation;

        return array_values(array_filter($results, function ($val) use ($lower, $upper) {
                return $val >= $lower && $val <= $upper;
            }));
    }
12713
private function frequent(array $samples): array
    {
        return array_values(array_filter($samples, function ($entry) {
            return $this->support($entry) >= $this->support;
        }));
    }
679731
protected function resample(): array
    {
        $weights = $this->weights;
        $std = StandardDeviation::population($weights);
        $mean = Mean::arithmetic($weights);
        $min = min($weights);
        $minZ = (int) round(($min - $mean) / $std);

        $samples = [];
        $targets = [];
        foreach ($weights as $index => $weight) {
            $z = (int) round(($weight - $mean) / $std) - $minZ + 1;
            for ($i = 0; $i < $z; ++$i) {
                if (random_int(0, 1) == 0) {
                    continue;
                }

                $samples[] = $this->samples[$index];
                $targets[] = $this->targets[$index];
            }
        }

        return [$samples, $targets];
    }
386141
public static function skewness(array $values, ?float $mean = null, ?int $n = null) : float
    {
        $n = $n ?? count($values);

        if ($n === 0) {
            throw new InvalidArgumentException('Skewness is undefined for'
                . ' empty set.');
        }

        $mean = $mean ?? self::mean($values, $n);

        $numerator = self::centralMoment($values, 3, $mean);
        $denominator = self::centralMoment($values, 2, $mean) ** 1.5;

        return $numerator / ($denominator ?: EPSILON);
    }
1134496
public static function percentile(array $values, float $p) : float
    {
        if (empty($values)) {
            throw new InvalidArgumentException('Percentile is not defined for'
                . ' an empty set.');
        }

        if ($p < 0. or $p > 100.) {
            throw new InvalidArgumentException('P must be between 0 and 1,'
                . " $p given.");
        }

        $n = count($values);

        sort($values);

        $x = ($p / 100) * ($n - 1) + 1;

        $xHat = (int) $x;

        $remainder = $x - $xHat;

        $a = $values[$xHat - 1];
        $b = $values[$xHat];

        return $a + $remainder * ($b - $a);
    }
970051
public static function range(array $values) : float
    {
        if (empty($values)) {
            throw new InvalidArgumentException('Range is undefined for empty'
                . ' set.');
        }

        return (float) (max($values) - min($values));
    }
107019
public static double stddev(int[] values) {
        double mean = mean(values);
        double sum = 0d;
        for (int i : values) {
            double d = i - mean;
            sum += d*d;
        }
        return Math.sqrt(sum / values.length);
    }
118263
protected function inertia(array $samples) : float
    {
        $inertia = 0.;

        foreach ($samples as $sample) {
            foreach ($this->centroids as $centroid) {
                $inertia += $this->kernel->compute($sample, $centroid);
            }
        }

        return $inertia;
    }
600483
private boolean rule7(double sample) {
        if (!hasMean()) {
            return false;
        }

        if (sample == mean.getResult()) {
            rule7Count = 0;
            return false;
        }

        if (Math.abs(sample - mean.getResult()) <= oneDeviation) {
            ++rule7Count;
        } else {
            rule7Count = 0;
        }

        return rule7Count >= 15;
    }
1287200
public function evaluate($values)
    {
        $returnValue = (bool) false;

        if (is_string($values)){
        	$values = trim($values);
        }

        $isScalar = is_bool($values) || is_int($values) || is_float($values);

        if( $isScalar === true ){
            $returnValue = true;
        } elseif( is_array($values) ) {
            $returnValue = (count($values) >= 1);
        } else {
            $returnValue = !empty($values);
        }

        return (bool) $returnValue;
    }
677826
protected function calculate_sample($sampleid, $samplesorigin, $starttime = false, $endtime = false) {
        $user = $this->retrieve('user', $sampleid);
        return ($user->trackforums) ? self::get_max_value() : self::get_min_value();
    }
922555
protected function jointLogLikelihood(array $sample) : array
    {
        $likelihoods = [];

        foreach ($this->means as $class => $means) {
            $variances = $this->variances[$class];

            $likelihood = $this->priors[$class] ?? LOG_EPSILON;

            foreach ($sample as $column => $feature) {
                $mean = $means[$column];
                $variance = $variances[$column];

                $pdf = -0.5 * log(TWO_PI * $variance);
                $pdf -= 0.5 * (($feature - $mean) ** 2) / $variance;

                $likelihood += $pdf;
            }

            $likelihoods[$class] = $likelihood;
        }

        return $likelihoods;
    }
940934
private function frequency(array $sample): int
    {
        return count(array_filter($this->samples, function ($entry) use ($sample) {
            return $this->subset($entry, $sample);
        }));
    }
317000
protected function isolationScore(array $sample) : float
    {
        $depth = 0.;

        foreach ($this->forest as $tree) {
            $node = $tree->search($sample);

            $depth += $node ? $node->depth() : EPSILON;
        }

        $depth /= $this->estimators;

        return 2. ** -($depth / $this->pHat);
    }