Zend - The PHP Company




Math

Add Code


Standard Deviation  

Type: application
Added by: vnath
Entered: 13/04/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 7856
A simple function designed to calculate the standard deviation of an array of values.


<?php
function standard_deviation($array) {
    
    
//Get sum of array values
    
while(list($key,$val) = each($array)) {
        
$total += $val;
    }
    
    
reset($array);
    
$mean $total/count($array);
    
    while(list(
$key,$val) = each($array)) {
        
$sum += pow(($val-$mean),2);
    }
    
$var sqrt($sum/(count($array)-1));
    
    return 
$var;
}
?>


Usage Example


<?php

$array 
= array(5,6,7,10);
$var standard_deviation($array);
echo 
$var//Returns ~2.2 in this example

?>


Rate This Script





Search



This Category All Categories