/** * @param array|float[] $a * @param bool $sample * * @return float * * @throws InvalidArgumentException */ 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)); }
/** * @param array|int[]|float[] $x * @param array|int[]|float[] $y * * @return float * * @throws InvalidArgumentException */ public static function pearson(array $x, array $y) { if (count($x) !== count($y)) { throw InvalidArgumentException::arraySizeNotMatch(); } $count = count($x); $meanX = Mean::arithmetic($x); $meanY = Mean::arithmetic($y); $axb = 0; $a2 = 0; $b2 = 0; for ($i = 0; $i < $count; ++$i) { $a = $x[$i] - $meanX; $b = $y[$i] - $meanY; $axb = $axb + $a * $b; $a2 = $a2 + pow($a, 2); $b2 = $b2 + pow($b, 2); } $corr = $axb / sqrt((double) ($a2 * $b2)); return $corr; }
public function testModeOnArray() { $numbers = [5, 2, 6, 1, 3, 4, 6, 6, 5]; $this->assertEquals(6, Mean::mode($numbers)); }
/** * @param array $currentAxis * * @return float|mixed */ public function replaceValue(array $currentAxis) { return Mean::mode($currentAxis); }
/** * @param array $currentAxis * * @return float */ public function replaceValue(array $currentAxis) { return Mean::arithmetic($currentAxis); }