my custom PHP stdev function VS mysql's STDDEV_POP -
so have code:
$arr = array( 1,2,3,4,5,6 ); $num = count($arr); $sum = array_sum($arr); $average = $sum/$num; foreach($arr $val) { $sum += pow(($val - $average), 2); } $stdev = sqrt($sum / ($num - 1));
versus
select stddev_pop(something) table;
whereby table
something '1' '2' '3' '4' '5' '6'
and yet $stdev returns
2.7748873851023
whereas select returns 1.707825127659933
what's wrong stdev code?
this gave me 1.70
$arr = array( 1,2,3,4,5,6 ); $num = count($arr); $sum2 = 0; $sum = array_sum($arr); $average = $sum/$num; foreach($arr $val) { $sum2 += pow(($val - $average), 2); } $stdev = sqrt($sum2 / ($num)); echo $stdev;exit;
Comments
Post a Comment