If you want one function for the population and sample, you can use this function:
<?php
function standard_deviation($aValues, $bSample = false)
{
$fMean = array_sum($aValues) / count($aValues);
$fVariance = 0.0;
foreach ($aValues as $i)
{
$fVariance += pow($i - $fMean, 2);
}
$fVariance /= ( $bSample ? count($aValues) - 1 : count($aValues) );
return (float) sqrt($fVariance);
}
stats_standard_deviation
(PECL stats >= 1.0.0)
stats_standard_deviation — Returns the standard deviation
Beschreibung
float stats_standard_deviation
( array $a
[, bool $sample = false
] )
Warnung
Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.
Parameter-Liste
- a
-
- sample
-
Rückgabewerte
Pascal Woerde
06-Sep-2010 06:04
Sharon
15-Apr-2010 08:40
If you don't have the stat package you can use these functions:
<?php
// Function to calculate square of value - mean
function sd_square($x, $mean) { return pow($x - $mean,2); }
// Function to calculate standard deviation (uses sd_square)
function sd($array) {
// square root of sum of squares devided by N-1
return sqrt(array_sum(array_map("sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)-1) );
}
?>
