@@ -18,7 +18,7 @@ |
||
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | - * @return \float[] |
|
| 21 | + * @return double[] |
|
| 22 | 22 | */ |
| 23 | 23 | public function getValue() |
| 24 | 24 | { |
@@ -32,8 +32,8 @@ |
||
| 32 | 32 | { |
| 33 | 33 | $result = 0; |
| 34 | 34 | $columns = count($this->value); |
| 35 | - for ($i=0; $i<$columns; $i++) { |
|
| 36 | - $result += $this->value[$i]*$value->getValue()[$i]; |
|
| 35 | + for ($i = 0; $i < $columns; $i++) { |
|
| 36 | + $result += $this->value[$i] * $value->getValue()[$i]; |
|
| 37 | 37 | } |
| 38 | 38 | return $result; |
| 39 | 39 | } |
@@ -30,7 +30,7 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | public function scalar(ValueInterface $value) |
| 32 | 32 | { |
| 33 | - return $value->getValue()*$this->getValue(); |
|
| 33 | + return $value->getValue() * $this->getValue(); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | public function calculate(ValueInterface $coefficient, ValueInterface $variable) |
| 17 | 17 | { |
| 18 | - if (! $variable instanceof VectorValue) { |
|
| 18 | + if (!$variable instanceof VectorValue) { |
|
| 19 | 19 | throw new WrongVariableForHypothesisException(); |
| 20 | 20 | } |
| 21 | 21 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | */ |
| 31 | 31 | public function derivative(ValueInterface $coefficient, ValueInterface $variable, $partialVariable) |
| 32 | 32 | { |
| 33 | - if (! $variable instanceof VectorValue) { |
|
| 33 | + if (!$variable instanceof VectorValue) { |
|
| 34 | 34 | throw new WrongVariableForHypothesisException(); |
| 35 | 35 | } |
| 36 | 36 | |
@@ -24,10 +24,10 @@ discard block |
||
| 24 | 24 | $rawValue = $value->getValue(); |
| 25 | 25 | |
| 26 | 26 | $numberColumns = count($rawValue); |
| 27 | - for ($i=0; $i<$numberColumns; $i++) { |
|
| 27 | + for ($i = 0; $i < $numberColumns; $i++) { |
|
| 28 | 28 | $rawValue[$i] = |
| 29 | 29 | ( |
| 30 | - $rawValue[$i] - $rawCoefficient[static::COEFFICIENT_AVERAGE][$i] |
|
| 30 | + $rawValue[$i]-$rawCoefficient[static::COEFFICIENT_AVERAGE][$i] |
|
| 31 | 31 | ) |
| 32 | 32 | / $rawCoefficient[static::COEFFICIENT_RANGE][$i] |
| 33 | 33 | ; |
@@ -62,8 +62,8 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | foreach ($featuresSum as $i => $featureSum) { |
| 64 | 64 | $featuresAverage[$i] = $featureSum / $numberRows; |
| 65 | - $featuresRange[$i] = ($featuresMaximumValue[$i] - $featuresMinimumValue[$i]) > 0 ? |
|
| 66 | - ($featuresMaximumValue[$i] - $featuresMinimumValue[$i]) |
|
| 65 | + $featuresRange[$i] = ($featuresMaximumValue[$i]-$featuresMinimumValue[$i]) > 0 ? |
|
| 66 | + ($featuresMaximumValue[$i]-$featuresMinimumValue[$i]) |
|
| 67 | 67 | : 1; |
| 68 | 68 | } |
| 69 | 69 | |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | if (!isset($featuresMinimumValue[$i]) || $featuresMinimumValue[$i] > $features[$i]) { |
| 93 | 93 | $featuresMinimumValue[$i] = $features[$i]; |
| 94 | 94 | } |
| 95 | - $featuresSum[$i] = $featuresSum[$i] + $features[$i]; |
|
| 95 | + $featuresSum[$i] = $featuresSum[$i]+$features[$i]; |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | return array($featuresMaximumValue, $featuresMinimumValue, $featuresSum); |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | $coefficientVector[0] = 0; |
| 73 | 73 | $incrementVector = array_fill(0, $features+1, 0); |
| 74 | 74 | |
| 75 | - while(!$convergence && !$divergence) { |
|
| 75 | + while (!$convergence && !$divergence) { |
|
| 76 | 76 | list($convergence, $divergence, $coefficientVector) = $this->doStep( |
| 77 | 77 | $dataset, |
| 78 | 78 | $coefficientVector, |
@@ -100,20 +100,20 @@ discard block |
||
| 100 | 100 | protected function doStep(Dataset $dataset, $coefficientVector, $features, $total, $incrementVector) |
| 101 | 101 | { |
| 102 | 102 | $coefficient = new VectorValue($coefficientVector); |
| 103 | - $costVector = array_fill(0, $features + 1, 0); |
|
| 103 | + $costVector = array_fill(0, $features+1, 0); |
|
| 104 | 104 | |
| 105 | 105 | foreach ($dataset as $result) { |
| 106 | 106 | $costVector = $this->calculateStepCost($features, $coefficient, $result, $costVector); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - for ($j = 0; $j < $features + 1; $j++) { |
|
| 109 | + for ($j = 0; $j < $features+1; $j++) { |
|
| 110 | 110 | $incrementVector[$j] = $this->learningRate * -(1 / ($total)) * $costVector[$j]; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - $convergence = (bool)(abs(array_sum($incrementVector)) < $this->convergenceCriteria); |
|
| 114 | - $divergence = (bool)(abs(array_sum($incrementVector)) > $this->divergenceCriteria); |
|
| 113 | + $convergence = (bool) (abs(array_sum($incrementVector)) < $this->convergenceCriteria); |
|
| 114 | + $divergence = (bool) (abs(array_sum($incrementVector)) > $this->divergenceCriteria); |
|
| 115 | 115 | |
| 116 | - for ($j = 0; $j < $features + 1; $j++) { |
|
| 116 | + for ($j = 0; $j < $features+1; $j++) { |
|
| 117 | 117 | $coefficientVector[$j] += $incrementVector[$j]; |
| 118 | 118 | } |
| 119 | 119 | return array($convergence, $divergence, $coefficientVector); |
@@ -128,20 +128,20 @@ discard block |
||
| 128 | 128 | protected function calculateStepCost($features, ValueInterface $coefficient, Result $result, array $costVector) |
| 129 | 129 | { |
| 130 | 130 | $firstOrderIncrement = ( |
| 131 | - (float)$this->hypothesis->calculate( |
|
| 131 | + (float) $this->hypothesis->calculate( |
|
| 132 | 132 | $coefficient, |
| 133 | 133 | $result->getIndependentVariable() |
| 134 | 134 | )->getValue() |
| 135 | - - (float)$result->getDependentVariable()->getValue() |
|
| 135 | + - (float) $result->getDependentVariable()->getValue() |
|
| 136 | 136 | ); |
| 137 | 137 | $costVector[0] += $firstOrderIncrement; |
| 138 | 138 | |
| 139 | - for ($j = 1; $j < $features + 1; $j++) { |
|
| 139 | + for ($j = 1; $j < $features+1; $j++) { |
|
| 140 | 140 | $costVector[$j] += $firstOrderIncrement |
| 141 | - * (float)$this->hypothesis->derivative( |
|
| 141 | + * (float) $this->hypothesis->derivative( |
|
| 142 | 142 | $coefficient, |
| 143 | 143 | $result->getIndependentVariable(), |
| 144 | - $j - 1 |
|
| 144 | + $j-1 |
|
| 145 | 145 | )->getValue(); |
| 146 | 146 | } |
| 147 | 147 | return $costVector; |