如果需要统计一组数据的波动性,那么方差比较基础
方差计算公式
$arr1 = array(99, 100, 100, 91, 85);
$arr2 = array(73, 70, 75, 72, 70);
function variance($arr) {
$length = count($arr);
if ($length == 0) {
return array(0,0);
}
$average = array_sum($arr)/$length;
$count = 0;
foreach ($arr as $v) {
$count += pow($average-$v, 2);
}
$variance = $count/$length;
return array('variance' => $variance, 'square' => sqrt($variance), 'average' => $average);
}
print_r(variance($arr1));
print_r(variance($arr2));
输出
Array
(
[variance] => 36.4
[square] => 6.0332412515993
[average] => 95
)
Array
(
[variance] => 3.6
[square] => 1.897366596101
[average] => 72
)
由此得出,数组1的方差大于数组2,所以数组1波动性比数组2大