Phpml\Math\Statistic\StandardDeviation::population PHP Method

population() public static method

public static population ( array $a, boolean $sample = true ) : float
$a array
$sample boolean
return float
    public static function population(array $a, $sample = true)
    {
        if (empty($a)) {
            throw InvalidArgumentException::arrayCantBeEmpty();
        }
        $n = count($a);
        if ($sample && $n === 1) {
            throw InvalidArgumentException::arraySizeToSmall(2);
        }
        $mean = Mean::arithmetic($a);
        $carry = 0.0;
        foreach ($a as $val) {
            $d = $val - $mean;
            $carry += $d * $d;
        }
        if ($sample) {
            --$n;
        }
        return sqrt((double) ($carry / $n));
    }

Usage Example

Example #1
0
 /**
  * @expectedException \Phpml\Exception\InvalidArgumentException
  */
 public function testThrowExceptionOnToSmallArray()
 {
     StandardDeviation::population([1]);
 }
StandardDeviation