php variance

时间:2021-08-16 23:11:24
function variance ($a)
{
/** variable and initializations */
$the_variance = 0.0;
$the_mean = 0.0;
$the_array_sum = array_sum($a);
$number_elements = count($a); /** calculate the mean */
$the_mean = $the_array_sum / $number_elements; /** calculate the variance */
for ($i = 0; $i < $number_elements; $i++)
{
/** sum the array */
$the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
} $the_variance = $the_variance / ($number_elements - 1.0); /** return the variance */
return $the_variance;
}