java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

计算一组数据的波动性-方差算法

如果需要统计一组数据的波动性,那么方差比较基础
方差计算公式

$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大


This entry was posted in PHP, 数学与算法 and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/php-variance/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply