Test Setup Failed
Push — master ( f6aa1a...8544cf )
by Arkadiusz
02:28
created

Regression   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 57.89 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 44
loc 76
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A meanSquaredError() 11 11 2
A meanSquaredLogarithmicError() 0 11 2
A meanAbsoluteError() 11 11 2
A medianAbsoluteError() 11 11 2
A r2Score() 0 6 1
A maxError() 11 11 2
A assertCountEquals() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public static function meanSquaredError(array $targets, array $predictions): float
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) - log(1 + $predictions[$index])) ** 2;
32
        }
33
34
        return Mean::arithmetic($errors);
35
    }
36
37 View Code Duplication
    public static function meanAbsoluteError(array $targets, array $predictions): float
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public static function medianAbsoluteError(array $targets, array $predictions): float
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public static function maxError(array $targets, array $predictions): float
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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