|
@@ 13-23 (lines=11) @@
|
| 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 |
|
{ |
|
@@ 37-47 (lines=11) @@
|
| 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 |
|
{ |
|
@@ 49-59 (lines=11) @@
|
| 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 |
|
{ |
|
@@ 68-78 (lines=11) @@
|
| 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 |
|
{ |