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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.