src/Regression/LinearRegression.php
private function compute(Dataset $data): array
    {
        $dimension = $data->dimension()->rows();
        $elements = $dimension->value();
        $x = $data->abscissas()->toArray();
        $y = $data->ordinates()->toArray();

        $xSum = $data->abscissas()->sum();
        $ySum = $data->ordinates()->sum();
        $xxSum = new Integer(0);
        $xySum = new Integer(0);

        for ($i = 0; $i < $elements; $i++) {
            $xySum = add($xySum, multiply($x[$i], $y[$i]));
            $xxSum = add($xxSum, multiply($x[$i], $x[$i]));
        }

        $slope = divide(
            subtract(
                $dimension->multiplyBy($xySum),
                $xSum->multiplyBy($ySum)
            ),
            subtract(
                $dimension->multiplyBy($xxSum),
                $xSum->power(new Integer(2))
            )
        );
        $intercept = divide(
            subtract(
                $ySum,
                $slope->multiplyBy($xSum)
            ),
            $dimension
        );

        return [$slope, $intercept];
    }
private function compute(Dataset $data): array
    {
        $dimension = $data->dimension()->rows();
        $elements = $dimension->value();
        $x = $data->abscissas()->toArray();
        $y = $data->ordinates()->toArray();

        $xSum = $data->abscissas()->sum();
        $ySum = $data->ordinates()->sum();
        $xxSum = new Integer(0);
        $xySum = new Integer(0);

        for ($i = 0; $i < $elements; $i++) {
            $xySum = add($xySum, multiply($x[$i], $y[$i]));
            $xxSum = add($xxSum, multiply($x[$i], $x[$i]));
        }

        $slope = divide(
            subtract(
                $dimension->multiplyBy($xySum),
                $xSum->multiplyBy($ySum)
            ),
            subtract(
                $dimension->multiplyBy($xxSum),
                $xSum->power(new Integer(2))
            )
        );
        $intercept = divide(
            subtract(
                $ySum,
                $slope->multiplyBy($xSum)
            ),
            $dimension
        );

        return [$slope, $intercept];
    }
private function compute ( Dataset $ data ) : array { $ dimension = $ data -> dimension ( ) -> rows ( ) ; $ elements = $ dimension -> value ( ) ; $ x = $ data -> abscissas ( ) -> toArray ( ) ; $ y = $ data -> ordinates ( ) -> toArray ( ) ; $ xSum = $ data -> abscissas ( ) -> sum ( ) ; $ ySum = $ data -> ordinates ( ) -> sum ( ) ; $ xxSum = new Integer ( 0 ) ; $ xySum = new Integer ( 0 ) ; for ( $ i = 0 ; $ i < $ elements ; $ i ++ ) { $ xySum = add ( $ xySum , multiply ( $ x [ $ i ] , $ y [ $ i ] ) ) ; $ xxSum = add ( $ xxSum , multiply ( $ x [ $ i ] , $ x [ $ i ] ) ) ; } $ slope = divide ( subtract ( $ dimension -> multiplyBy ( $ xySum ) , $ xSum -> multiplyBy ( $ ySum ) ) , subtract ( $ dimension -> multiplyBy ( $ xxSum ) , $ xSum -> power ( new Integer ( 2 ) ) ) ) ; $ intercept = divide ( subtract ( $ ySum , $ slope -> multiplyBy ( $ xSum ) ) , $ dimension ) ; return [ $ slope , $ intercept ] ; }
Determine the slope and intercept for the given dataset

@see https://richardathome.wordpress.com/2006/01/25/a-php-linear-regression-function/

@param Dataset $data

@return array
Determine the slope and intercept for the given dataset