mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-05-31 23:50:50 +00:00
3ba35918a3
* Add sum of squares deviations * Calculate population variance * Add VarianceThreshold - feature selection transformer * Add docs about VarianceThreshold * Add missing code for pipeline usage
61 lines
1.3 KiB
PHP
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
|
|
));
|
|
}
|
|
}
|