function arithmetic_mean($a) {
return array_sum($a)/count($a);
}
function geometric_mean($a) {
foreach($a as $i=>$n) $mul = $i == 0 ? $n : $mul*$n;
return pow($mul,1/count($a));
}
function harmonic_mean($a) {
$sum = 0;
foreach($a as $n) $sum += 1 / $n;
return (1/$sum)*count($a);
}
function median($a) {
sort($a,SORT_NUMERIC);
return (count($a) % 2) ?
$a[floor(count($a)/2)] :
($a[floor(count($a)/2)] + $a[floor(count($a)/2) - 1]) / 2;
}
function modal_score($a) {
$quant = array();
foreach($a as $n) $quant["$n"]++;
$max = 0;
$mode = 0;
foreach($quant as $key=>$n) {
if($n>$max) {
$max = $n;
$mode = $key;
}
}
return $mode;
}
Showing posts with label statistics. Show all posts
Showing posts with label statistics. Show all posts
May 1, 2009
PHP: average functions: Mean (Arithmetic, Geometric, Harmonic) · Median · Mode
See Wiki for detail on mean, median or mode.
Please write your own optimizations / implementations in the comments.
Subscribe to:
Posts (Atom)