php-ml/src/Math/Statistic/StandardDeviation.php
Arkadiusz Kondas 3ba35918a3
Implement VarianceThreshold - simple baseline approach to feature selection. (#228)
* Add sum of squares deviations

* Calculate population variance

* Add VarianceThreshold - feature selection transformer

* Add docs about VarianceThreshold

* Add missing code for pipeline usage
2018-02-10 18:07:09 +01:00

61 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Phpml\Math\Statistic;
use Phpml\Exception\InvalidArgumentException;
class StandardDeviation
{
/**
* @param array|float[]|int[] $numbers
*/
public static function population(array $numbers, bool $sample = true): float
{
if (empty($numbers)) {
throw InvalidArgumentException::arrayCantBeEmpty();
}
$n = count($numbers);
if ($sample && $n === 1) {
throw InvalidArgumentException::arraySizeToSmall(2);
}
$mean = Mean::arithmetic($numbers);
$carry = 0.0;
foreach ($numbers as $val) {
$carry += ($val - $mean) ** 2;
}
if ($sample) {
--$n;
}
return sqrt((float) ($carry / $n));
}
/**
* Sum of squares deviations
* ∑⟮xᵢ - μ⟯²
*
* @param array|float[]|int[] $numbers
*/
public static function sumOfSquares(array $numbers): float
{
if (empty($numbers)) {
throw InvalidArgumentException::arrayCantBeEmpty();
}
$mean = Mean::arithmetic($numbers);
return array_sum(array_map(
function ($val) use ($mean) {
return ($val - $mean) ** 2;
},
$numbers
));
}
}