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