@@ -10,54 +10,54 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class LinearRegression extends AbstractRegression implements InterfaceRegression |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * LinearRegression constructor. |
|
| 15 | - */ |
|
| 16 | - public function __construct() |
|
| 17 | - { |
|
| 18 | - $this->sumIndex = [0, 0, 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 | - 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]; |
|
| 42 | - $this->sumIndex[3] += $v[0] * $v[1]; |
|
| 43 | - $this->sumIndex[4] += $v[1] * $v[1]; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $k += 1; |
|
| 48 | - |
|
| 49 | - $gradient = ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[1]) / |
|
| 50 | - ($k * $this->sumIndex[2] - $this->sumIndex[0] * $this->sumIndex[0]); |
|
| 13 | + /** |
|
| 14 | + * LinearRegression constructor. |
|
| 15 | + */ |
|
| 16 | + public function __construct() |
|
| 17 | + { |
|
| 18 | + $this->sumIndex = [0, 0, 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 | + 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]; |
|
| 42 | + $this->sumIndex[3] += $v[0] * $v[1]; |
|
| 43 | + $this->sumIndex[4] += $v[1] * $v[1]; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $k += 1; |
|
| 48 | + |
|
| 49 | + $gradient = ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[1]) / |
|
| 50 | + ($k * $this->sumIndex[2] - $this->sumIndex[0] * $this->sumIndex[0]); |
|
| 51 | 51 | |
| 52 | - $intercept = $this->sumIndex[1] / $k - $gradient * $this->sumIndex[0] / $k; |
|
| 52 | + $intercept = $this->sumIndex[1] / $k - $gradient * $this->sumIndex[0] / $k; |
|
| 53 | 53 | |
| 54 | - foreach ($this->sourceSequence as $i => $val) { |
|
| 55 | - $coordinate = [$val[0], $val[0] * $gradient + $intercept]; |
|
| 56 | - $this->resultSequence[] = $coordinate; |
|
| 57 | - } |
|
| 54 | + foreach ($this->sourceSequence as $i => $val) { |
|
| 55 | + $coordinate = [$val[0], $val[0] * $gradient + $intercept]; |
|
| 56 | + $this->resultSequence[] = $coordinate; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - $this->equation = 'y = ' . round($gradient, 1) . 'x + ' . round($intercept, 1); |
|
| 59 | + $this->equation = 'y = ' . round($gradient, 1) . 'x + ' . round($intercept, 1); |
|
| 60 | 60 | |
| 61 | - $this->push(); |
|
| 62 | - } |
|
| 61 | + $this->push(); |
|
| 62 | + } |
|
| 63 | 63 | } |