2016-04-27 21:04:59 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-04-27 21:04:59 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Math\Statistic;
|
2016-04-27 21:04:59 +00:00
|
|
|
|
2017-11-28 07:00:13 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2016-04-27 21:04:59 +00:00
|
|
|
use Phpml\Math\Statistic\StandardDeviation;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-27 21:04:59 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class StandardDeviationTest extends TestCase
|
2016-04-27 21:04:59 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testStandardDeviationOfPopulationSample(): void
|
2016-04-27 21:04:59 +00:00
|
|
|
{
|
|
|
|
//https://pl.wikipedia.org/wiki/Odchylenie_standardowe
|
|
|
|
$delta = 0.001;
|
|
|
|
$population = [5, 6, 8, 9];
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(1.825, StandardDeviation::population($population), $delta);
|
2016-04-27 21:04:59 +00:00
|
|
|
|
|
|
|
//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];
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(4079, StandardDeviation::population($population), $delta);
|
2016-04-27 21:04:59 +00:00
|
|
|
|
|
|
|
$population = [9300, 10565, 15000, 15000, 17764, 57000, 65940, 73676, 77006, 93739, 146088, 153260];
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(50989, StandardDeviation::population($population), $delta);
|
2016-04-27 21:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnEmptyArrayIfNotSample(): void
|
2016-04-27 21:04:59 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2016-04-27 21:04:59 +00:00
|
|
|
StandardDeviation::population([], false);
|
|
|
|
}
|
|
|
|
|
2018-02-15 17:14:06 +00:00
|
|
|
public function testThrowExceptionOnTooSmallArray(): void
|
2016-04-27 21:04:59 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2016-04-27 21:04:59 +00:00
|
|
|
StandardDeviation::population([1]);
|
|
|
|
}
|
2018-02-10 17:07:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderForSumOfSquaresDeviations
|
|
|
|
*/
|
|
|
|
public function testSumOfSquares(array $numbers, float $sum): void
|
|
|
|
{
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta($sum, StandardDeviation::sumOfSquares($numbers), 0.0001);
|
2018-02-10 17:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataProviderForSumOfSquaresDeviations(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[[3, 6, 7, 11, 12, 13, 17], 136.8571],
|
|
|
|
[[6, 11, 12, 14, 15, 20, 21], 162.8571],
|
|
|
|
[[1, 2, 3, 6, 7, 11, 12], 112],
|
|
|
|
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 0], 82.5],
|
|
|
|
[[34, 253, 754, 2342, 75, 23, 876, 4, 1, -34, -345, 754, -377, 3, 0], 6453975.7333],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowExceptionOnEmptyArraySumOfSquares(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
StandardDeviation::sumOfSquares([]);
|
|
|
|
}
|
2016-04-27 21:04:59 +00:00
|
|
|
}
|