mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-10 00:37:55 +00:00
implement standard deviation of population function
This commit is contained in:
parent
af3b57692f
commit
66dcfcf2b7
@ -23,4 +23,22 @@ class InvalidArgumentException extends \Exception
|
||||
{
|
||||
return new self(sprintf('%s must be between 0.0 and 1.0', $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InvalidArgumentException
|
||||
*/
|
||||
public static function arrayCantBeEmpty()
|
||||
{
|
||||
return new self('The array has zero elements');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $minimumSize
|
||||
*
|
||||
* @return InvalidArgumentException
|
||||
*/
|
||||
public static function arraySizeToSmall($minimumSize = 2)
|
||||
{
|
||||
return new self(sprintf('The array must have at least %s elements', $minimumSize));
|
||||
}
|
||||
}
|
||||
|
45
src/Phpml/Math/Statistic/StandardDeviation.php
Normal file
45
src/Phpml/Math/Statistic/StandardDeviation.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Phpml\Math\Statistic;
|
||||
|
||||
use Phpml\Exception\InvalidArgumentException;
|
||||
|
||||
class StandardDeviation
|
||||
{
|
||||
|
||||
/**
|
||||
* @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 = array_sum($a) / $n;
|
||||
$carry = 0.0;
|
||||
foreach ($a as $val) {
|
||||
$d = $val - $mean;
|
||||
$carry += $d * $d;
|
||||
};
|
||||
|
||||
if($sample) {
|
||||
--$n;
|
||||
}
|
||||
|
||||
return sqrt($carry / $n);
|
||||
}
|
||||
|
||||
}
|
@ -16,6 +16,16 @@ class LeastSquares implements Regression
|
||||
*/
|
||||
private $targets;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $slope;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
private $intercept;
|
||||
|
||||
/**
|
||||
* @param array $features
|
||||
* @param array $targets
|
||||
|
43
tests/Phpml/Math/Statistic/StandardDeviationTest.php
Normal file
43
tests/Phpml/Math/Statistic/StandardDeviationTest.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace test\Phpml\Math\StandardDeviation;
|
||||
|
||||
use Phpml\Math\Statistic\StandardDeviation;
|
||||
|
||||
class StandardDeviationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testStandardDeviationOfPopulationSample()
|
||||
{
|
||||
//https://pl.wikipedia.org/wiki/Odchylenie_standardowe
|
||||
$delta = 0.001;
|
||||
$population = [5, 6, 8, 9];
|
||||
$this->assertEquals(1.825, StandardDeviation::population($population), '', $delta);
|
||||
|
||||
//http://www.stat.wmich.edu/s216/book/node126.html
|
||||
$delta = 0.5;
|
||||
$population = [7100, 15500, 4400, 4400, 5900, 4600, 8800, 2000, 2750, 2550, 960, 1025];
|
||||
$this->assertEquals(4079, StandardDeviation::population($population), '', $delta);
|
||||
|
||||
$population = [9300, 10565, 15000, 15000, 17764, 57000, 65940, 73676, 77006, 93739, 146088, 153260];
|
||||
$this->assertEquals(50989, StandardDeviation::population($population), '', $delta);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testThrowExceptionOnEmptyArrayIfNotSample()
|
||||
{
|
||||
StandardDeviation::population([], false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testThrowExceptionOnToSmallArray()
|
||||
{
|
||||
StandardDeviation::population([1]);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user