src/Statistics.php
public static function variance($values, $sample = true)
    {
        $numberOfValues = count($values);
        $mean = self::mean($values);

        $squaredDifferences = [];
        foreach ($values as $value) {
            $squaredDifferences[] = self::squaredDifference($value, $mean);
        }
        $sumOfSquaredDifferences = self::sum($squaredDifferences);

        if ($sample) {
            $variance = $sumOfSquaredDifferences / ($numberOfValues - 1);
        } else {
            $variance = $sumOfSquaredDifferences / $numberOfValues;
        }

        return $variance;
    }
public static function variance($values, $sample = true)
    {
        $numberOfValues = count($values);
        $mean = self::mean($values);

        $squaredDifferences = [];
        foreach ($values as $value) {
            $squaredDifferences[] = self::squaredDifference($value, $mean);
        }
        $sumOfSquaredDifferences = self::sum($squaredDifferences);

        if ($sample) {
            $variance = $sumOfSquaredDifferences / ($numberOfValues - 1);
        } else {
            $variance = $sumOfSquaredDifferences / $numberOfValues;
        }

        return $variance;
    }
public static function variance ( $ values , $ sample = true ) { $ numberOfValues = count ( $ values ) ; $ mean = self :: mean ( $ values ) ; $ squaredDifferences = [ ] ; foreach ( $ values as $ value ) { $ squaredDifferences [ ] = self :: squaredDifference ( $ value , $ mean ) ; } $ sumOfSquaredDifferences = self :: sum ( $ squaredDifferences ) ; if ( $ sample ) { $ variance = $ sumOfSquaredDifferences / ( $ numberOfValues - 1 ) ; } else { $ variance = $ sumOfSquaredDifferences / $ numberOfValues ; } return $ variance ; }
Calculates the variance for a given set of values.

@param array $values The input values
@param bool $sample Whether or not to compensate for small samples (n - 1), defaults to true
@return float|int The variance of values as an integer or float
Calculates the variance for a given set of values .