php-ml/tests/Math/Statistic/MeanTest.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2016-04-27 21:57:23 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-04-27 21:57:23 +00:00
namespace Phpml\Tests\Math\Statistic;
2016-04-27 21:57:23 +00:00
use Phpml\Exception\InvalidArgumentException;
2016-04-27 21:57:23 +00:00
use Phpml\Math\Statistic\Mean;
2017-02-03 11:58:25 +00:00
use PHPUnit\Framework\TestCase;
2016-04-27 21:57:23 +00:00
2017-02-03 11:58:25 +00:00
class MeanTest extends TestCase
2016-04-27 21:57:23 +00:00
{
public function testArithmeticThrowExceptionOnEmptyArray(): void
2016-05-08 17:23:54 +00:00
{
$this->expectException(InvalidArgumentException::class);
2016-05-08 17:23:54 +00:00
Mean::arithmetic([]);
}
public function testArithmeticMean(): void
2016-04-27 21:57:23 +00:00
{
$delta = 0.01;
2018-10-28 06:44:52 +00:00
self::assertEquals(3.5, Mean::arithmetic([2, 5]), '', $delta);
self::assertEquals(41.16, Mean::arithmetic([43, 21, 25, 42, 57, 59]), '', $delta);
self::assertEquals(1.7, Mean::arithmetic([0.5, 0.5, 1.5, 2.5, 3.5]), '', $delta);
2016-04-27 21:57:23 +00:00
}
2016-05-08 17:12:39 +00:00
public function testMedianThrowExceptionOnEmptyArray(): void
2016-05-08 17:12:39 +00:00
{
$this->expectException(InvalidArgumentException::class);
2016-05-08 17:12:39 +00:00
Mean::median([]);
}
public function testMedianOnOddLengthArray(): void
2016-05-08 17:12:39 +00:00
{
$numbers = [5, 2, 6, 1, 3];
2018-10-28 06:44:52 +00:00
self::assertEquals(3, Mean::median($numbers));
2016-05-08 17:12:39 +00:00
}
public function testMedianOnEvenLengthArray(): void
2016-05-08 17:12:39 +00:00
{
$numbers = [5, 2, 6, 1, 3, 4];
2018-10-28 06:44:52 +00:00
self::assertEquals(3.5, Mean::median($numbers));
2016-05-08 17:12:39 +00:00
}
public function testModeThrowExceptionOnEmptyArray(): void
2016-05-08 17:23:54 +00:00
{
$this->expectException(InvalidArgumentException::class);
2016-05-08 17:23:54 +00:00
Mean::mode([]);
}
public function testModeOnArray(): void
2016-05-08 17:23:54 +00:00
{
$numbers = [5, 2, 6, 1, 3, 4, 6, 6, 5];
2018-10-28 06:44:52 +00:00
self::assertEquals(6, Mean::mode($numbers));
2016-05-08 17:23:54 +00:00
}
2016-04-27 21:57:23 +00:00
}