Skip to content

Instantly share code, notes, and snippets.

@greydnls
Last active May 7, 2021 07:48

Revisions

  1. greydnls revised this gist Dec 19, 2014. 1 changed file with 68 additions and 59 deletions.
    127 changes: 68 additions & 59 deletions LSM.php
    Original file line number Diff line number Diff line change
    @@ -3,84 +3,93 @@
    * Created by PhpStorm.
    * User: kayladnls
    * Date: 12/19/14
    * Time: 4:20 PM
    * Time: 4:52 PM
    */

    $x = [1, 2, 3, 4, 5];
    $y = [2, 4, 5, 4, 5];
    $barx = [];
    $bary = [];
    class LeastSquares
    {
    private $y_int;
    private $slope;

    $mean_x = calculateMean($x);
    $mean_y = calculateMean($y);
    $mean_intercept = [$mean_x, $mean_y];
    /*
    * 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);

    //calculate x - barx
    foreach ($x as $idx => $point)
    {
    $barx[$idx] = $point - $mean_x;
    }
    $mean_x = $this->mean($x);
    $mean_y = $this->mean($y);
    $mean_intercept = [$mean_x, $mean_y];

    //calculate y - bary
    foreach ($y as $idx => $point)
    {
    $bary[$idx] = $point - $mean_y;
    }
    $barx = array_map(function($x) use($mean_x) {
    return $x - $mean_x;
    }, $x);

    $slope = findSlope($barx, $bary);
    $bary = array_map(function($y) use($mean_y) {
    return $y - $mean_y;
    }, $y);

    $yint = calcYInt($slope, $mean_intercept);
    $this->slope = $this->calcSlope($barx, $bary);

    function calcYInt($slope, $mean_intercept)
    {
    return $mean_intercept[1] - ($slope * $mean_intercept[0]);
    }
    $this->y_int = $this->calcYInt($this->slope, $mean_intercept);
    }

    function yhat($x, $yint, $slope)
    {
    return $yint + ($slope * $x);
    }

    function findSlope($barx, $bary)
    {
    $barxsq = squareArr($barx);
    public function estimate($x)
    {
    return $this->y_int + ($this->slope * $x);
    }

    $barx_times_bary = multArrs($barx, $bary);
    private function calcYInt($slope, $mean_intercept)
    {
    return $mean_intercept[1] - ($slope * $mean_intercept[0]);
    }

    return calcSum($barx_times_bary) / calcSum($barxsq);
    }
    private function splitInput(array $data)
    {
    $x = [];
    $y = [];
    foreach ($data as $datum)
    {
    $x[] = $datum[0];
    $y[] = $datum[1];
    }

    function multArrs($array1, $arry2)
    {
    $multiplied = [];
    foreach ($array1 as $idx => $first)
    return [$x, $y];
    }

    private function calcSlope($barx, $bary)
    {
    $multiplied[] = $first * $arry2[$idx];
    $barxsq = $this->squareArr($barx);

    $barx_times_bary = $this->multArrs($barx, $bary);

    return array_sum($barx_times_bary) / array_sum($barxsq);
    }
    return $multiplied;
    }

    function squareArr(array $vals)
    {
    $return = [];
    foreach ($vals as $val)
    private function multArrs($array1, $arry2)
    {
    $return[] = $val*$val;
    return array_map(function($x, $y) {
    return $x*$y;
    }, $array1, $arry2);
    }
    return $return;
    }

    function calculateMean(array $vals)
    {
    return calcSum($vals) / count($vals);
    }
    private function squareArr(array $vals)
    {
    return array_map(
    function ($val)
    {
    return $val*$val;
    },
    $vals
    );
    }

    function calcSum(array $vals)
    {
    $sum = 0;
    foreach ($vals as $val)
    private function mean(array $vals)
    {
    $sum += $val;
    return array_sum($vals) / count($vals);
    }
    return $sum;
    }
    }
  2. greydnls created this gist Dec 19, 2014.
    86 changes: 86 additions & 0 deletions LSM.php
    Original 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;
    }