Completed
Push — master ( 9e6fd9...92ef0c )
by Nikita
03:34
created

src/Regression/ExponentialRegression.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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