2

I can normally catch these easy, but......

function linear_regression($x, $y) {

// calculate number points
$n = count($x);

// ensure both arrays of points are the same size
if ($n != count($y)) {

trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);

}

// calculate sums
$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]);

  }



  // calculate slope
  //$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

  $divisor = (($n * $xx_sum) – ($x_sum * $x_sum));
if ($divisor == 0){
 $m = 0;
} else {
$m = (($n * $xy_sum) – ($x_sum * $y_sum)) / $divisor;
}


  // calculate intercept
  $b = ($y_sum - ($m * $x_sum)) / $n;

  // return result
  return array("m"=>$m, "b"=>$b);

}

var_dump( linear_regression(array(1, 2, 3, 4, 4), array(1.5, 1.6, 2.1, 3.0, 6)) );

The error is happening here $divisor = (($n * $xx_sum) – ($x_sum * $x_sum));

Any ideas why?

8
  • You don't need brackets in this expression (and others): (($n * $xx_sum) – ($x_sum * $x_sum));. PHP's operator precedence follows arithmetic conventions. Commented Nov 10, 2013 at 3:07
  • The error persists even if you take off the outer set of parenthesis. Commented Nov 10, 2013 at 3:08
  • okay so I copied this over and ran it and got the error. I was shocked. And then I slowly started to add/remove variables and I don't know why that happened but it works now? Same whitespace and brackets and everything. I just replaced the variables with 1 and added/removed things. I have no idea why it happened or what I fixed. Commented Nov 10, 2013 at 3:11
  • 1
    Is the minus sign actually a minus sign. I've had situations where I was copy/pasting and the minus symbol was an html entity an not the actual symbol. Commented Nov 10, 2013 at 3:11
  • 3
    This question appears to be off-topic because it is about a syntax error. Commented Nov 10, 2013 at 3:14

4 Answers 4

2

The minus sign is a fancy unicode dash (I think the em dash) instead of the regular ascii - character.

Sign up to request clarification or add additional context in comments.

2 Comments

The way I found it btw was just copy/pasting the code from firefox into an rxvt to run it on the php command line. rxvt pasted it as \uxxxx instead of -, triggering a warning and error from php and immediately looking different too.
@JakeGould: Actually it was "Jeff" in the comments. Beat Adam by a nose.
1

Don't ask me how I discovered that, but your "-" is not a real "-", is another character, but have the same apparence

Ok you didn't asked me, but I have discovered at http://writecodeonline.com/php/ , it doesn't recognize yours "-" character

Now working code with real "-", you can copy an paste, and see:

function linear_regression($x, $y) {

// calculate number points
$n = count($x);

// ensure both arrays of points are the same size
if ($n != count($y)) {

trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);

}

// calculate sums
$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]);

  }



  // calculate slope
  //$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

  $divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
if ($divisor == 0){
 $m = 0;
} else {
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
}


  // calculate intercept
  $b = ($y_sum - ($m * $x_sum)) / $n;

  // return result
  return array("m"=>$m, "b"=>$b);

}

var_dump( linear_regression(array(1, 2, 3, 4, 4), array(1.5, 1.6, 2.1, 3.0, 6)) );

2 Comments

how did that happen?? i was shocked when i copied and pasted the code and the error was real... lol... how do you think he got a - that was fake?
I don't have php at my pc so I used writecodeonline.com/php and it recognized that "-" was actually another character per encoding. Was pretty easy.
0

I retyped the offending line like this:

$divisor = (($n*$xx_sum)-($x_sum * $x_sum));

The error disappeared. After a bit of poking the error seems to be related to the minus sign in the middle. deleting it and retyping it seems to fix the problem.

See this fiddle:

Comments

0

Adam is right, but also, you didn’t close the for loop. Here try this.

Also, the minus sign was the wrong sign here:

$divisor = (($n * $xx_sum) - ($x_sum * $x_sum));

As well as here:

$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;

My cleaned up version of your function with the closed for loop in place as well here.

function linear_regression($x, $y) {

  // calculate number points
  $n = count($x);

  // ensure both arrays of points are the same size
  if ($n != count($y)) {
    trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
  }

  // calculate sums
  $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]);

    // calculate slope
    //$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

    $divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
    if ($divisor == 0) {
      $m = 0;
    }
    else {
      $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
    }

    // calculate intercept
    $b = ($y_sum - ($m * $x_sum)) / $n;

  } 

  // return result
  return array("m"=>$m, "b"=>$b);

} // linear_regression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.