RegressionTest.php 2.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

declare(strict_types=1);

namespace Phpml\Tests\Metric;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Metric\Regression;
use PHPUnit\Framework\TestCase;

final class RegressionTest extends TestCase
{
    public function testMeanSquaredError(): void
    {
        self::assertEquals(6.08, Regression::meanSquaredError(
            [41, 45, 49, 47, 44],
            [43.6, 44.4, 45.2, 46, 46.8]
        ));

        self::assertEquals(0.375, Regression::meanSquaredError(
            [3, -0.5, 2, 7],
            [2.5, 0.0, 2, 8]
        ));
    }

    public function testR2Score(): void
    {
        self::assertEqualsWithDelta(0.1739, Regression::r2Score(
            [41, 45, 49, 47, 44],
            [43.6, 44.4, 45.2, 46, 46.8]
        ), 0.0001);
    }

    public function testMaxError(): void
    {
        self::assertEquals(1, Regression::maxError([3, 2, 7, 1], [4, 2, 7, 1]));

        // test absolute value
        self::assertEquals(5, Regression::maxError([-10, 2, 7, 1], [-5, 2, 7, 1]));
    }

    public function testMeanAbsoluteError(): void
    {
        self::assertEquals(0.5, Regression::meanAbsoluteError([3, -0.5, 2, 7], [2.5, 0.0, 2, 8]));
    }

    public function testMeanSquaredLogarithmicError(): void
    {
        self::assertEqualsWithDelta(0.039, Regression::meanSquaredLogarithmicError(
            [3, 5, 2.5, 7],
            [2.5, 5, 4, 8]
        ), 0.001);
    }

    public function testMedianAbsoluteError(): void
    {
        self::assertEquals(0.5, Regression::medianAbsoluteError(
            [3, -0.5, 2, 7],
            [2.5, 0.0, 2, 8]
        ));
    }

    public function testMeanSquaredErrorInvalidCount(): void
    {
        self::expectException(InvalidArgumentException::class);

        Regression::meanSquaredError([1], [1, 2]);
    }

    public function testR2ScoreInvalidCount(): void
    {
        self::expectException(InvalidArgumentException::class);

        Regression::r2Score([1], [1, 2]);
    }

    public function testMaxErrorInvalidCount(): void
    {
        self::expectException(InvalidArgumentException::class);

        Regression::r2Score([1], [1, 2]);
    }

    public function tesMeanAbsoluteErrorInvalidCount(): void
    {
        self::expectException(InvalidArgumentException::class);

        Regression::meanAbsoluteError([1], [1, 2]);
    }

    public function tesMediaAbsoluteErrorInvalidCount(): void
    {
        self::expectException(InvalidArgumentException::class);

        Regression::medianAbsoluteError([1], [1, 2]);
    }
}