@@ -3,8 +3,6 @@ |
||
3 | 3 | |
4 | 4 | namespace Regression; |
5 | 5 | |
6 | -use Carbon\Carbon; |
|
7 | - |
|
8 | 6 | /** |
9 | 7 | * Class ExponentialRegression |
10 | 8 | * @package Regression |
@@ -16,50 +16,50 @@ |
||
16 | 16 | /** |
17 | 17 | * ExponentialRegression constructor. |
18 | 18 | */ |
19 | - public function __construct() |
|
20 | - { |
|
21 | - $this->sumIndex = [0, 0, 0, 0, 0, 0]; |
|
22 | - $this->dimension = count($this->sumIndex); |
|
23 | - } |
|
19 | + public function __construct() |
|
20 | + { |
|
21 | + $this->sumIndex = [0, 0, 0, 0, 0, 0]; |
|
22 | + $this->dimension = count($this->sumIndex); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @throws RegressionException |
|
27 | - */ |
|
28 | - public function calculate() |
|
29 | - { |
|
30 | - if ($this->sourceSequence === null) { |
|
31 | - throw new RegressionException('The input sequence is not set'); |
|
32 | - } |
|
25 | + /** |
|
26 | + * @throws RegressionException |
|
27 | + */ |
|
28 | + public function calculate() |
|
29 | + { |
|
30 | + if ($this->sourceSequence === null) { |
|
31 | + throw new RegressionException('The input sequence is not set'); |
|
32 | + } |
|
33 | 33 | |
34 | - if (count($this->sourceSequence) < $this->dimension) { |
|
35 | - throw new RegressionException('The dimension of the sequence of at least ' . $this->dimension); |
|
36 | - } |
|
34 | + if (count($this->sourceSequence) < $this->dimension) { |
|
35 | + throw new RegressionException('The dimension of the sequence of at least ' . $this->dimension); |
|
36 | + } |
|
37 | 37 | |
38 | - $k = 0; |
|
38 | + $k = 0; |
|
39 | 39 | |
40 | - foreach ($this->sourceSequence as $k => $v) { |
|
41 | - if ($v[1] !== null) { |
|
42 | - $this->sumIndex[0] += $v[0]; |
|
43 | - $this->sumIndex[1] += $v[1]; |
|
44 | - $this->sumIndex[2] += $v[0] * $v[0] * $v[1]; |
|
45 | - $this->sumIndex[3] += $v[1] * log($v[1]); |
|
46 | - $this->sumIndex[4] += $v[0] * $v[1] * log($v[1]); |
|
47 | - $this->sumIndex[5] += $v[0] * $v[1]; |
|
48 | - } |
|
49 | - } |
|
40 | + foreach ($this->sourceSequence as $k => $v) { |
|
41 | + if ($v[1] !== null) { |
|
42 | + $this->sumIndex[0] += $v[0]; |
|
43 | + $this->sumIndex[1] += $v[1]; |
|
44 | + $this->sumIndex[2] += $v[0] * $v[0] * $v[1]; |
|
45 | + $this->sumIndex[3] += $v[1] * log($v[1]); |
|
46 | + $this->sumIndex[4] += $v[0] * $v[1] * log($v[1]); |
|
47 | + $this->sumIndex[5] += $v[0] * $v[1]; |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - $denominator = $this->sumIndex[1] * $this->sumIndex[2] - $this->sumIndex[5] * $this->sumIndex[5]; |
|
51 | + $denominator = $this->sumIndex[1] * $this->sumIndex[2] - $this->sumIndex[5] * $this->sumIndex[5]; |
|
52 | 52 | |
53 | - $A = exp(($this->sumIndex[2] * $this->sumIndex[3] - $this->sumIndex[5] * $this->sumIndex[4]) / $denominator); |
|
54 | - $B = ($this->sumIndex[1] * $this->sumIndex[4] - $this->sumIndex[5] * $this->sumIndex[3]) / $denominator; |
|
53 | + $A = exp(($this->sumIndex[2] * $this->sumIndex[3] - $this->sumIndex[5] * $this->sumIndex[4]) / $denominator); |
|
54 | + $B = ($this->sumIndex[1] * $this->sumIndex[4] - $this->sumIndex[5] * $this->sumIndex[3]) / $denominator; |
|
55 | 55 | |
56 | - foreach ($this->sourceSequence as $i => $val) { |
|
57 | - $coordinate = [$val[0], $A * exp($B * $val[0])]; |
|
58 | - $this->resultSequence[] = $coordinate; |
|
59 | - } |
|
56 | + foreach ($this->sourceSequence as $i => $val) { |
|
57 | + $coordinate = [$val[0], $A * exp($B * $val[0])]; |
|
58 | + $this->resultSequence[] = $coordinate; |
|
59 | + } |
|
60 | 60 | |
61 | - $this->equation = 'y = ' . round($A, 2) . '+ e^(' . round($B, 2) . 'x)'; |
|
61 | + $this->equation = 'y = ' . round($A, 2) . '+ e^(' . round($B, 2) . 'x)'; |
|
62 | 62 | |
63 | - $this->push(); |
|
64 | - } |
|
63 | + $this->push(); |
|
64 | + } |
|
65 | 65 | } |
@@ -58,7 +58,7 @@ |
||
58 | 58 | $this->resultSequence[] = $coordinate; |
59 | 59 | } |
60 | 60 | |
61 | - $this->equation = 'y = ' . round($A, 2) . '+ e^(' . round($B, 2) . 'x)'; |
|
61 | + $this->equation = 'y = ' . round($A, 2) . '+ e^(' . round($B, 2) . 'x)'; |
|
62 | 62 | |
63 | 63 | $this->push(); |
64 | 64 | } |
@@ -3,10 +3,6 @@ |
||
3 | 3 | |
4 | 4 | namespace Regression; |
5 | 5 | |
6 | -use Carbon\Carbon; |
|
7 | -use Exception; |
|
8 | -use IllegalArgumentException; |
|
9 | - |
|
10 | 6 | /** |
11 | 7 | * Class LinearRegression |
12 | 8 | * @package Regression |
@@ -14,54 +14,54 @@ |
||
14 | 14 | */ |
15 | 15 | class LinearRegression extends AbstractRegression implements InterfaceRegression |
16 | 16 | { |
17 | - /** |
|
18 | - * LinearRegression constructor. |
|
19 | - */ |
|
20 | - public function __construct() |
|
21 | - { |
|
22 | - $this->sumIndex = [0, 0, 0, 0, 0, 0]; |
|
23 | - $this->dimension = count($this->sumIndex); |
|
24 | - } |
|
17 | + /** |
|
18 | + * LinearRegression constructor. |
|
19 | + */ |
|
20 | + public function __construct() |
|
21 | + { |
|
22 | + $this->sumIndex = [0, 0, 0, 0, 0, 0]; |
|
23 | + $this->dimension = count($this->sumIndex); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @throws RegressionException |
|
28 | - */ |
|
29 | - public function calculate() |
|
30 | - { |
|
31 | - if ($this->sourceSequence === null) { |
|
32 | - throw new RegressionException('The input sequence is not set'); |
|
33 | - } |
|
26 | + /** |
|
27 | + * @throws RegressionException |
|
28 | + */ |
|
29 | + public function calculate() |
|
30 | + { |
|
31 | + if ($this->sourceSequence === null) { |
|
32 | + throw new RegressionException('The input sequence is not set'); |
|
33 | + } |
|
34 | 34 | |
35 | - if (count($this->sourceSequence) < $this->dimension) { |
|
36 | - throw new RegressionException('The dimension of the sequence of at least ' . $this->dimension); |
|
37 | - } |
|
35 | + if (count($this->sourceSequence) < $this->dimension) { |
|
36 | + throw new RegressionException('The dimension of the sequence of at least ' . $this->dimension); |
|
37 | + } |
|
38 | 38 | |
39 | - $k = 0; |
|
39 | + $k = 0; |
|
40 | 40 | |
41 | - foreach ($this->sourceSequence as $k => $v) { |
|
42 | - if ($v[1] !== null) { |
|
43 | - $this->sumIndex[0] += $v[0]; |
|
44 | - $this->sumIndex[1] += $v[1]; |
|
45 | - $this->sumIndex[2] += $v[0] * $v[0]; |
|
46 | - $this->sumIndex[3] += $v[0] * $v[1]; |
|
47 | - $this->sumIndex[4] += $v[1] * $v[1]; |
|
48 | - } |
|
49 | - } |
|
41 | + foreach ($this->sourceSequence as $k => $v) { |
|
42 | + if ($v[1] !== null) { |
|
43 | + $this->sumIndex[0] += $v[0]; |
|
44 | + $this->sumIndex[1] += $v[1]; |
|
45 | + $this->sumIndex[2] += $v[0] * $v[0]; |
|
46 | + $this->sumIndex[3] += $v[0] * $v[1]; |
|
47 | + $this->sumIndex[4] += $v[1] * $v[1]; |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - $k += 1; |
|
51 | + $k += 1; |
|
52 | 52 | |
53 | - $gradient = ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[1]) / |
|
54 | - ($k * $this->sumIndex[2] - $this->sumIndex[0] * $this->sumIndex[0]); |
|
53 | + $gradient = ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[1]) / |
|
54 | + ($k * $this->sumIndex[2] - $this->sumIndex[0] * $this->sumIndex[0]); |
|
55 | 55 | |
56 | - $intercept = $this->sumIndex[1] / $k - $gradient * $this->sumIndex[0] / $k; |
|
56 | + $intercept = $this->sumIndex[1] / $k - $gradient * $this->sumIndex[0] / $k; |
|
57 | 57 | |
58 | - foreach ($this->sourceSequence as $i => $val) { |
|
59 | - $coordinate = [$val[0], $val[0] * $gradient + $intercept]; |
|
60 | - $this->resultSequence[] = $coordinate; |
|
61 | - } |
|
58 | + foreach ($this->sourceSequence as $i => $val) { |
|
59 | + $coordinate = [$val[0], $val[0] * $gradient + $intercept]; |
|
60 | + $this->resultSequence[] = $coordinate; |
|
61 | + } |
|
62 | 62 | |
63 | - $this->equation = 'y = ' . round($gradient, 1) . 'x + ' . round($intercept, 1); |
|
63 | + $this->equation = 'y = ' . round($gradient, 1) . 'x + ' . round($intercept, 1); |
|
64 | 64 | |
65 | - $this->push(); |
|
66 | - } |
|
65 | + $this->push(); |
|
66 | + } |
|
67 | 67 | } |
@@ -60,7 +60,7 @@ |
||
60 | 60 | $this->resultSequence[] = $coordinate; |
61 | 61 | } |
62 | 62 | |
63 | - $this->equation = 'y = ' . round($gradient, 1) . 'x + ' . round($intercept, 1); |
|
63 | + $this->equation = 'y = ' . round($gradient, 1) . 'x + ' . round($intercept, 1); |
|
64 | 64 | |
65 | 65 | $this->push(); |
66 | 66 | } |
@@ -59,7 +59,7 @@ |
||
59 | 59 | * |
60 | 60 | * @param array $rightData |
61 | 61 | * @param array $hypothesisData |
62 | - * @return int |
|
62 | + * @return double |
|
63 | 63 | * @throws IllegalArgumentException |
64 | 64 | */ |
65 | 65 | public static function r2(array $rightData, array $hypothesisData) |
@@ -10,18 +10,18 @@ |
||
10 | 10 | interface InterfaceRegression |
11 | 11 | { |
12 | 12 | |
13 | - public function calculate(); |
|
13 | + public function calculate(); |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param array $data |
|
17 | - * |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - public function setSourceSequence(array $data); |
|
15 | + /** |
|
16 | + * @param array $data |
|
17 | + * |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + public function setSourceSequence(array $data); |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return RegressionModel |
|
24 | - */ |
|
25 | - public function getRegressionModel(): RegressionModel; |
|
22 | + /** |
|
23 | + * @return RegressionModel |
|
24 | + */ |
|
25 | + public function getRegressionModel(): RegressionModel; |
|
26 | 26 | |
27 | 27 | } |
28 | 28 | \ No newline at end of file |
@@ -47,14 +47,14 @@ |
||
47 | 47 | $B = ($k * $this->sumIndex[1] - $this->sumIndex[2] * $this->sumIndex[0]) |
48 | 48 | / ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[0]); |
49 | 49 | |
50 | - $A = exp(($this->sumIndex[2] - $B * $this->sumIndex[0])/ $k); |
|
50 | + $A = exp(($this->sumIndex[2] - $B * $this->sumIndex[0]) / $k); |
|
51 | 51 | |
52 | 52 | foreach ($this->sourceSequence as $i => $val) { |
53 | 53 | $coordinate = [$val[0], $A * exp($B * $val[0])]; |
54 | 54 | $this->resultSequence[] = $coordinate; |
55 | 55 | } |
56 | 56 | |
57 | - $this->equation = 'y = ' . round($A, 2) . '+ x^' . round($B, 2); |
|
57 | + $this->equation = 'y = ' . round($A, 2) . '+ x^' . round($B, 2); |
|
58 | 58 | |
59 | 59 | $this->push(); |
60 | 60 | } |
@@ -12,64 +12,64 @@ |
||
12 | 12 | */ |
13 | 13 | class RegressionFactory |
14 | 14 | { |
15 | - /** |
|
16 | - * @param array $data |
|
17 | - * @return RegressionModel |
|
18 | - * @throws TypeError |
|
19 | - */ |
|
20 | - public static function Linear(array $data): RegressionModel |
|
21 | - { |
|
22 | - return self::createContainer(LinearRegression::class, $data); |
|
23 | - } |
|
15 | + /** |
|
16 | + * @param array $data |
|
17 | + * @return RegressionModel |
|
18 | + * @throws TypeError |
|
19 | + */ |
|
20 | + public static function Linear(array $data): RegressionModel |
|
21 | + { |
|
22 | + return self::createContainer(LinearRegression::class, $data); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @param array $data |
|
27 | - * @return RegressionModel |
|
28 | - * @throws TypeError |
|
29 | - */ |
|
30 | - public static function Exponential(array $data): RegressionModel |
|
31 | - { |
|
32 | - return self::createContainer(ExponentialRegression::class, $data); |
|
33 | - } |
|
25 | + /** |
|
26 | + * @param array $data |
|
27 | + * @return RegressionModel |
|
28 | + * @throws TypeError |
|
29 | + */ |
|
30 | + public static function Exponential(array $data): RegressionModel |
|
31 | + { |
|
32 | + return self::createContainer(ExponentialRegression::class, $data); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param array $data |
|
37 | - * @return RegressionModel |
|
38 | - * @throws TypeError |
|
39 | - */ |
|
40 | - public static function Logarithmic(array $data): RegressionModel |
|
41 | - { |
|
42 | - return self::createContainer(LogarithmicRegression::class, $data); |
|
43 | - } |
|
35 | + /** |
|
36 | + * @param array $data |
|
37 | + * @return RegressionModel |
|
38 | + * @throws TypeError |
|
39 | + */ |
|
40 | + public static function Logarithmic(array $data): RegressionModel |
|
41 | + { |
|
42 | + return self::createContainer(LogarithmicRegression::class, $data); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param array $data |
|
47 | - * @return RegressionModel |
|
48 | - * @throws TypeError |
|
49 | - */ |
|
50 | - public static function Power(array $data): RegressionModel |
|
51 | - { |
|
52 | - return self::createContainer(PowerRegression::class, $data); |
|
53 | - } |
|
45 | + /** |
|
46 | + * @param array $data |
|
47 | + * @return RegressionModel |
|
48 | + * @throws TypeError |
|
49 | + */ |
|
50 | + public static function Power(array $data): RegressionModel |
|
51 | + { |
|
52 | + return self::createContainer(PowerRegression::class, $data); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $className |
|
57 | - * @param array $data |
|
58 | - * @return InterfaceRegression |
|
59 | - * @throws TypeError |
|
60 | - */ |
|
61 | - protected static function createContainer(string $className, array $data): RegressionModel |
|
62 | - { |
|
63 | - /** @var InterfaceRegression $regressionObj */ |
|
64 | - $regressionObj = new $className(); |
|
55 | + /** |
|
56 | + * @param string $className |
|
57 | + * @param array $data |
|
58 | + * @return InterfaceRegression |
|
59 | + * @throws TypeError |
|
60 | + */ |
|
61 | + protected static function createContainer(string $className, array $data): RegressionModel |
|
62 | + { |
|
63 | + /** @var InterfaceRegression $regressionObj */ |
|
64 | + $regressionObj = new $className(); |
|
65 | 65 | |
66 | - if (!$regressionObj instanceof InterfaceRegression) { |
|
67 | - throw new TypeError('the object' . $className . |
|
68 | - ' is not compatible with the interface ' . InterfaceRegression::class); |
|
69 | - } |
|
66 | + if (!$regressionObj instanceof InterfaceRegression) { |
|
67 | + throw new TypeError('the object' . $className . |
|
68 | + ' is not compatible with the interface ' . InterfaceRegression::class); |
|
69 | + } |
|
70 | 70 | |
71 | - $regressionObj->setSourceSequence($data); |
|
72 | - $regressionObj->calculate(); |
|
73 | - return $regressionObj->getRegressionModel(); |
|
74 | - } |
|
71 | + $regressionObj->setSourceSequence($data); |
|
72 | + $regressionObj->calculate(); |
|
73 | + return $regressionObj->getRegressionModel(); |
|
74 | + } |
|
75 | 75 | } |
76 | 76 | \ No newline at end of file |
@@ -1,21 +1,21 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | spl_autoload_register( |
4 | - function ($className) { |
|
5 | - $className = ltrim($className, '\\'); |
|
6 | - $fileName = ''; |
|
7 | - if ($lastNsPos = strripos($className, '\\')) { |
|
8 | - $namespace = substr($className, 0, $lastNsPos); |
|
9 | - $className = substr($className, $lastNsPos + 1); |
|
10 | - $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; |
|
11 | - } |
|
12 | - $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php'; |
|
13 | - if (file_exists($fileName)) { |
|
14 | - include $fileName; |
|
15 | - return true; |
|
16 | - } |
|
17 | - return false; |
|
18 | - } |
|
4 | + function ($className) { |
|
5 | + $className = ltrim($className, '\\'); |
|
6 | + $fileName = ''; |
|
7 | + if ($lastNsPos = strripos($className, '\\')) { |
|
8 | + $namespace = substr($className, 0, $lastNsPos); |
|
9 | + $className = substr($className, $lastNsPos + 1); |
|
10 | + $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; |
|
11 | + } |
|
12 | + $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php'; |
|
13 | + if (file_exists($fileName)) { |
|
14 | + include $fileName; |
|
15 | + return true; |
|
16 | + } |
|
17 | + return false; |
|
18 | + } |
|
19 | 19 | ); |
20 | 20 | |
21 | 21 | ?> |
22 | 22 | \ No newline at end of file |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | spl_autoload_register( |
4 | - function ($className) { |
|
4 | + function($className) { |
|
5 | 5 | $className = ltrim($className, '\\'); |
6 | 6 | $fileName = ''; |
7 | 7 | if ($lastNsPos = strripos($className, '\\')) { |