Jump to content

Standard Deviation


php_joe

Recommended Posts

Hello,

 

I am trying to write a function that will calculate the standard deviation of values in an array. I did search past topics and used them to come up with this script:

 

function standard_deviation($sample){
if(is_array($sample)){
	$mean = array_sum($sample) / count($sample);
	foreach($sample as $key => $num) $devs[$key] = pow($num - $mean, 2);
	return sqrt(array_sum($devs) / count($devs));
}
}

 

It seems to work, but I get different values when I've checked it by using STDEV in OpenOffice, so I think I've made an error somewhere.

Link to comment
https://forums.phpfreaks.com/topic/253594-standard-deviation/
Share on other sites

:rtfm:

http://php.net/manual/en/function.stats-standard-deviation.php

function standard_deviation($aValues, $bSample = false)
{
    $fMean = array_sum($aValues) / count($aValues);
    $fVariance = 0.0;
    foreach ($aValues as $i)
    {
        $fVariance += pow($i - $fMean, 2);
    }
    $fVariance /= ( $bSample ? count($aValues) - 1 : count($aValues) );
    return (float) sqrt($fVariance);
}

Link to comment
https://forums.phpfreaks.com/topic/253594-standard-deviation/#findComment-1300209
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.