For the average: return array_sum($data)/count($data); The above formula computes the total sum of values contained in the array variable, and then divides by the total amount of data in the array. The standard deviation function is rare in PHP, and very tricky: function stdev($data){ $average = average($data); foreach ($data as $value) { $variance[] = pow($value-$average,2); } $standarddeviation = sqrt((array_sum($variance))/((count($data))-1)); return $standarddeviation; } Statistical formula:
There is no built-in PHP function for standard deviation currently supported by a lot of PHP programmers, so a user-defined function is more suitable for doing the computation. First it gets the average of the data contained in the array, and then it will loop the square of the difference between each piece of data and the array average (this is called statistical variance). Finally, all of the variances are added, and then divided by total number of data minus 1. The easier approach could be to use average instead of array_sum($variance)/((count($data))-1) However, it is not accurate because it is NOT actually the "sample" standard deviation. In statistical literature, there are two types of standard deviation, population and sample. If we use the population standard deviation, we can directly use the average instead of the above parameter; however, most scientific experiments are done with sampling. If we are able to conduct an analysis, the scripts shown earlier in this article will produce a result like the one below:
It will show the summary statistics along with the data analyzed. To see this in action, you can go to: http://www.php-developer.org/descriptivestats.php If you would like to download/copy the script, you can go to: http://www.php-developer.org/wp-content/uploads/scripts/descriptivestats.txt
blog comments powered by Disqus |
|
|
|
|
|
|
|