PowerRegression::calculate()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 8
Ratio 22.22 %

Importance

Changes 0
Metric Value
dl 8
loc 36
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 8
nop 0
1
<?php
2
3
namespace Regression;
4
5
/**
6
 * Class PowerRegression
7
 *
8
 * @package Regression
9
 * @author  [email protected]
10
 */
11
class PowerRegression extends AbstractRegression implements InterfaceRegression
12
{
13
    /**
14
     * ExponentialRegression constructor.
15
     */
16
    public function __construct()
17
    {
18
        $this->sumIndex = [0, 0, 0, 0];
19
        $this->dimension = \count($this->sumIndex);
20
    }
21
22
    /**
23
     * @throws RegressionException
24
     */
25
    public function calculate()
26
    {
27
        if ($this->sourceSequence === null) {
28
            throw new RegressionException('The input sequence is not set');
29
        }
30
31
        if (\count($this->sourceSequence) < $this->dimension) {
32
            throw new RegressionException('The dimension of the sequence of at least ' . $this->dimension);
33
        }
34
35
        $k = 0;
36
37 View Code Duplication
        foreach ($this->sourceSequence as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            if ($v[1] !== null) {
39
                $this->sumIndex[0] += \log($v[0]);
40
                $this->sumIndex[1] += \log($v[0]) * log($v[1]);
41
                $this->sumIndex[2] += \log($v[1]);
42
                $this->sumIndex[3] += \pow(\log($v[0]), 2);
43
            }
44
        }
45
46
        ++$k;
47
48
        $B = ($k * $this->sumIndex[1] - $this->sumIndex[2] * $this->sumIndex[0])
49
                / ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[0]);
50
        $A = \exp(($this->sumIndex[2] - $B * $this->sumIndex[0]) / $k);
51
52
        foreach ($this->sourceSequence as $i => $val) {
53
            $coordinate = [$val[0], $A * \pow($val[0], $B)];
54
            $this->resultSequence[] = $coordinate;
55
        }
56
57
        $this->equation = 'y = ' . \round($A, 2) . ' * x^' . \round($B, 2);
58
59
        $this->push();
60
    }
61
}
62