Last active
May 7, 2021 07:48
Revisions
-
greydnls revised this gist
Dec 19, 2014 . 1 changed file with 68 additions and 59 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,84 +3,93 @@ * Created by PhpStorm. * User: kayladnls * Date: 12/19/14 * Time: 4:52 PM */ class LeastSquares { private $y_int; private $slope; /* * A method to train the function. Pass in an array of points * representing the data that you will be estimating against. * ex: $data = [ 0 => [1, 2], 1 => [2, 4]] */ public function train(array $data) { list($x, $y) = $this->splitInput($data); $mean_x = $this->mean($x); $mean_y = $this->mean($y); $mean_intercept = [$mean_x, $mean_y]; $barx = array_map(function($x) use($mean_x) { return $x - $mean_x; }, $x); $bary = array_map(function($y) use($mean_y) { return $y - $mean_y; }, $y); $this->slope = $this->calcSlope($barx, $bary); $this->y_int = $this->calcYInt($this->slope, $mean_intercept); } public function estimate($x) { return $this->y_int + ($this->slope * $x); } private function calcYInt($slope, $mean_intercept) { return $mean_intercept[1] - ($slope * $mean_intercept[0]); } private function splitInput(array $data) { $x = []; $y = []; foreach ($data as $datum) { $x[] = $datum[0]; $y[] = $datum[1]; } return [$x, $y]; } private function calcSlope($barx, $bary) { $barxsq = $this->squareArr($barx); $barx_times_bary = $this->multArrs($barx, $bary); return array_sum($barx_times_bary) / array_sum($barxsq); } private function multArrs($array1, $arry2) { return array_map(function($x, $y) { return $x*$y; }, $array1, $arry2); } private function squareArr(array $vals) { return array_map( function ($val) { return $val*$val; }, $vals ); } private function mean(array $vals) { return array_sum($vals) / count($vals); } } -
greydnls created this gist
Dec 19, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,86 @@ <?php /** * Created by PhpStorm. * User: kayladnls * Date: 12/19/14 * Time: 4:20 PM */ $x = [1, 2, 3, 4, 5]; $y = [2, 4, 5, 4, 5]; $barx = []; $bary = []; $mean_x = calculateMean($x); $mean_y = calculateMean($y); $mean_intercept = [$mean_x, $mean_y]; //calculate x - barx foreach ($x as $idx => $point) { $barx[$idx] = $point - $mean_x; } //calculate y - bary foreach ($y as $idx => $point) { $bary[$idx] = $point - $mean_y; } $slope = findSlope($barx, $bary); $yint = calcYInt($slope, $mean_intercept); function calcYInt($slope, $mean_intercept) { return $mean_intercept[1] - ($slope * $mean_intercept[0]); } function yhat($x, $yint, $slope) { return $yint + ($slope * $x); } function findSlope($barx, $bary) { $barxsq = squareArr($barx); $barx_times_bary = multArrs($barx, $bary); return calcSum($barx_times_bary) / calcSum($barxsq); } function multArrs($array1, $arry2) { $multiplied = []; foreach ($array1 as $idx => $first) { $multiplied[] = $first * $arry2[$idx]; } return $multiplied; } function squareArr(array $vals) { $return = []; foreach ($vals as $val) { $return[] = $val*$val; } return $return; } function calculateMean(array $vals) { return calcSum($vals) / count($vals); } function calcSum(array $vals) { $sum = 0; foreach ($vals as $val) { $sum += $val; } return $sum; }