I have been given the following PHP function that returns a trend line using linear regression...
function linear_regression($x, $y) {
$n = count($x);
$x_sum = array_sum($x);
$y_sum = array_sum($y);
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $n; $i++) {
$xy_sum+=($x[$i]*$y[$i]);
$xx_sum+=($x[$i]*$x[$i]);
}
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
$b = ($y_sum - ($m * $x_sum)) / $n;
return array("m"=>$m, "b"=>$b);
}
However, given inputs of...
$x = array(1,2,3,3,4,5,6,7);
$y = array(1,2,3,4,5,6,7,8);
The line plotted in under the points. From what I can tell its because this function minmized the standard deviation rather than the variance. I'm thinking if it did that it would plot the line through the middle. Does anyone have any idea about how to tweak it so it will minimize the variance?

