|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Phpml\Metric; |
|
6
|
|
|
|
|
7
|
|
|
use Phpml\Exception\InvalidArgumentException; |
|
8
|
|
|
use Phpml\Math\Statistic\Correlation; |
|
9
|
|
|
use Phpml\Math\Statistic\Mean; |
|
10
|
|
|
|
|
11
|
|
|
final class Regression |
|
12
|
|
|
{ |
|
13
|
|
|
public static function meanSquaredError(array $targets, array $predictions): float |
|
14
|
|
|
{ |
|
15
|
|
|
self::assertCountEquals($targets, $predictions); |
|
16
|
|
|
|
|
17
|
|
|
$errors = []; |
|
18
|
|
|
foreach ($targets as $index => $target) { |
|
19
|
|
|
$errors[] = (($target - $predictions[$index]) ** 2); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return Mean::arithmetic($errors); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function meanSquaredLogarithmicError(array $targets, array $predictions): float |
|
26
|
|
|
{ |
|
27
|
|
|
self::assertCountEquals($targets, $predictions); |
|
28
|
|
|
|
|
29
|
|
|
$errors = []; |
|
30
|
|
|
foreach ($targets as $index => $target) { |
|
31
|
|
|
$errors[] = log((1 + $target) / (1 + $predictions[$index])) ** 2; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return Mean::arithmetic($errors); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function meanAbsoluteError(array $targets, array $predictions): float |
|
38
|
|
|
{ |
|
39
|
|
|
self::assertCountEquals($targets, $predictions); |
|
40
|
|
|
|
|
41
|
|
|
$errors = []; |
|
42
|
|
|
foreach ($targets as $index => $target) { |
|
43
|
|
|
$errors[] = abs($target - $predictions[$index]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return Mean::arithmetic($errors); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function medianAbsoluteError(array $targets, array $predictions): float |
|
50
|
|
|
{ |
|
51
|
|
|
self::assertCountEquals($targets, $predictions); |
|
52
|
|
|
|
|
53
|
|
|
$errors = []; |
|
54
|
|
|
foreach ($targets as $index => $target) { |
|
55
|
|
|
$errors[] = abs($target - $predictions[$index]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return (float) Mean::median($errors); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function r2Score(array $targets, array $predictions): float |
|
62
|
|
|
{ |
|
63
|
|
|
self::assertCountEquals($targets, $predictions); |
|
64
|
|
|
|
|
65
|
|
|
return Correlation::pearson($targets, $predictions) ** 2; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function maxError(array $targets, array $predictions): float |
|
69
|
|
|
{ |
|
70
|
|
|
self::assertCountEquals($targets, $predictions); |
|
71
|
|
|
|
|
72
|
|
|
$errors = []; |
|
73
|
|
|
foreach ($targets as $index => $target) { |
|
74
|
|
|
$errors[] = abs($target - $predictions[$index]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return (float) max($errors); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private static function assertCountEquals(array &$targets, array &$predictions): void |
|
81
|
|
|
{ |
|
82
|
|
|
if (count($targets) !== count($predictions)) { |
|
83
|
|
|
throw new InvalidArgumentException('Targets count must be equal with predictions count'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|