2016-04-27 21:51:14 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-04-27 21:51:14 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Regression;
|
2016-04-27 21:51:14 +00:00
|
|
|
|
2017-02-02 08:03:09 +00:00
|
|
|
use Phpml\ModelManager;
|
2017-11-06 07:56:37 +00:00
|
|
|
use Phpml\Regression\LeastSquares;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-27 21:51:14 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class LeastSquaresTest extends TestCase
|
2016-04-27 21:51:14 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPredictSingleFeatureSamples(): void
|
2016-04-27 21:51:14 +00:00
|
|
|
{
|
|
|
|
$delta = 0.01;
|
|
|
|
|
|
|
|
//https://www.easycalculation.com/analytical/learn-least-square-regression.php
|
2016-04-30 11:54:01 +00:00
|
|
|
$samples = [[60], [61], [62], [63], [65]];
|
|
|
|
$targets = [3.1, 3.6, 3.8, 4, 4.1];
|
2016-04-27 21:51:14 +00:00
|
|
|
|
|
|
|
$regression = new LeastSquares();
|
2016-04-29 21:03:08 +00:00
|
|
|
$regression->train($samples, $targets);
|
2016-04-27 21:51:14 +00:00
|
|
|
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(4.06, $regression->predict([64]), $delta);
|
2016-04-27 21:51:14 +00:00
|
|
|
|
|
|
|
//http://www.stat.wmich.edu/s216/book/node127.html
|
2016-04-30 11:54:01 +00:00
|
|
|
$samples = [[9300], [10565], [15000], [15000], [17764], [57000], [65940], [73676], [77006], [93739], [146088], [153260]];
|
|
|
|
$targets = [7100, 15500, 4400, 4400, 5900, 4600, 8800, 2000, 2750, 2550, 960, 1025];
|
2016-04-27 21:51:14 +00:00
|
|
|
|
|
|
|
$regression = new LeastSquares();
|
2016-04-29 21:03:08 +00:00
|
|
|
$regression->train($samples, $targets);
|
2016-04-27 21:51:14 +00:00
|
|
|
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(7659.35, $regression->predict([9300]), $delta);
|
|
|
|
self::assertEqualsWithDelta(5213.81, $regression->predict([57000]), $delta);
|
|
|
|
self::assertEqualsWithDelta(4188.13, $regression->predict([77006]), $delta);
|
|
|
|
self::assertEqualsWithDelta(7659.35, $regression->predict([9300]), $delta);
|
|
|
|
self::assertEqualsWithDelta(278.66, $regression->predict([153260]), $delta);
|
2016-04-27 21:51:14 +00:00
|
|
|
}
|
2016-04-29 21:03:08 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPredictSingleFeatureSamplesWithMatrixTargets(): void
|
2016-04-30 11:54:58 +00:00
|
|
|
{
|
|
|
|
$delta = 0.01;
|
|
|
|
|
|
|
|
//https://www.easycalculation.com/analytical/learn-least-square-regression.php
|
|
|
|
$samples = [[60], [61], [62], [63], [65]];
|
|
|
|
$targets = [[3.1], [3.6], [3.8], [4], [4.1]];
|
|
|
|
|
|
|
|
$regression = new LeastSquares();
|
|
|
|
$regression->train($samples, $targets);
|
|
|
|
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(4.06, $regression->predict([64]), $delta);
|
2016-04-30 11:54:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPredictMultiFeaturesSamples(): void
|
2016-04-29 21:03:08 +00:00
|
|
|
{
|
2016-04-30 11:32:40 +00:00
|
|
|
$delta = 0.01;
|
2016-04-29 21:03:08 +00:00
|
|
|
|
|
|
|
//http://www.stat.wmich.edu/s216/book/node129.html
|
2016-04-30 11:54:01 +00:00
|
|
|
$samples = [[73676, 1996], [77006, 1998], [10565, 2000], [146088, 1995], [15000, 2001], [65940, 2000], [9300, 2000], [93739, 1996], [153260, 1994], [17764, 2002], [57000, 1998], [15000, 2000]];
|
|
|
|
$targets = [2000, 2750, 15500, 960, 4400, 8800, 7100, 2550, 1025, 5900, 4600, 4400];
|
2016-04-29 21:03:08 +00:00
|
|
|
|
|
|
|
$regression = new LeastSquares();
|
|
|
|
$regression->train($samples, $targets);
|
|
|
|
|
2019-04-10 18:42:59 +00:00
|
|
|
self::assertEqualsWithDelta(-800614.957, $regression->getIntercept(), $delta);
|
|
|
|
self::assertEqualsWithDelta([-0.0327, 404.14], $regression->getCoefficients(), $delta);
|
|
|
|
self::assertEqualsWithDelta(4094.82, $regression->predict([60000, 1996]), $delta);
|
|
|
|
self::assertEqualsWithDelta(5711.40, $regression->predict([60000, 2000]), $delta);
|
2016-04-29 21:03:08 +00:00
|
|
|
}
|
2017-02-02 08:03:09 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSaveAndRestore(): void
|
2017-02-02 08:03:09 +00:00
|
|
|
{
|
|
|
|
//https://www.easycalculation.com/analytical/learn-least-square-regression.php
|
|
|
|
$samples = [[60], [61], [62], [63], [65]];
|
|
|
|
$targets = [[3.1], [3.6], [3.8], [4], [4.1]];
|
|
|
|
|
|
|
|
$regression = new LeastSquares();
|
|
|
|
$regression->train($samples, $targets);
|
|
|
|
|
|
|
|
//http://www.stat.wmich.edu/s216/book/node127.html
|
|
|
|
$testSamples = [[9300], [10565], [15000]];
|
|
|
|
$predicted = $regression->predict($testSamples);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
$filename = 'least-squares-test-'.random_int(100, 999).'-'.uniqid('', false);
|
|
|
|
$filepath = (string) tempnam(sys_get_temp_dir(), $filename);
|
2017-02-02 08:03:09 +00:00
|
|
|
$modelManager = new ModelManager();
|
|
|
|
$modelManager->saveToFile($regression, $filepath);
|
|
|
|
|
|
|
|
$restoredRegression = $modelManager->restoreFromFile($filepath);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($regression, $restoredRegression);
|
|
|
|
self::assertEquals($predicted, $restoredRegression->predict($testSamples));
|
2017-02-02 08:03:09 +00:00
|
|
|
}
|
2016-04-27 21:51:14 +00:00
|
|
|
}
|