2016-04-27 21:28:01 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-04-27 21:28:01 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Math\Statistic;
|
2016-04-27 21:28:01 +00:00
|
|
|
|
2017-11-28 07:00:13 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2016-04-27 21:28:01 +00:00
|
|
|
use Phpml\Math\Statistic\Correlation;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-27 21:28:01 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class CorrelationTest extends TestCase
|
2016-04-27 21:28:01 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPearsonCorrelation(): void
|
2016-04-27 21:28:01 +00:00
|
|
|
{
|
|
|
|
//http://www.stat.wmich.edu/s216/book/node126.html
|
|
|
|
$delta = 0.001;
|
|
|
|
$x = [9300, 10565, 15000, 15000, 17764, 57000, 65940, 73676, 77006, 93739, 146088, 153260];
|
|
|
|
$y = [7100, 15500, 4400, 4400, 5900, 4600, 8800, 2000, 2750, 2550, 960, 1025];
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(-0.641, Correlation::pearson($x, $y), $delta);
|
2016-04-27 21:28:01 +00:00
|
|
|
|
|
|
|
//http://www.statisticshowto.com/how-to-compute-pearsons-correlation-coefficients/
|
|
|
|
$delta = 0.001;
|
|
|
|
$x = [43, 21, 25, 42, 57, 59];
|
|
|
|
$y = [99, 65, 79, 75, 87, 82];
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(0.549, Correlation::pearson($x, $y), $delta);
|
2016-04-29 21:03:08 +00:00
|
|
|
|
|
|
|
$delta = 0.001;
|
|
|
|
$x = [60, 61, 62, 63, 65];
|
|
|
|
$y = [3.1, 3.6, 3.8, 4, 4.1];
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(0.911, Correlation::pearson($x, $y), $delta);
|
2016-04-27 21:28:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidArgumentsForPearsonCorrelation(): void
|
2016-04-27 21:28:01 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2016-04-27 21:28:01 +00:00
|
|
|
Correlation::pearson([1, 2, 4], [3, 5]);
|
|
|
|
}
|
|
|
|
}
|