Statistic 函数
在线手册:中文 英文
PHP手册

stats_standard_deviation

(PECL stats >= 1.0.0)

stats_standard_deviationReturns the standard deviation

说明

float stats_standard_deviation ( array $a [, bool $sample = false ] )
Warning

本函数还未编写文档,仅有参数列表。

参数

a

sample

返回值


Statistic 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Returns the standard deviation

用户评论:

pmcdougl at gac dot edu (07-Mar-2012 06:40)

It should be known that this function also casts all of the values of the array to floats. If you pass an array of integers, they will come out as an array of floats.

$data = array(2,3,3,3,3,2,5);
var_dump($data);
$standardDeviation = stats_standard_deviation($data);
var_dump($data);

Prints:
array
  0 => int 2
  1 => int 3
  2 => int 3
  3 => int 3
  4 => int 3
  5 => int 2
  6 => int 5
array
  0 => float 2
  1 => float 3
  2 => float 3
  3 => float 3
  4 => float 3
  5 => float 5
  6 => float 2

Pascal Woerde (07-Sep-2010 08:04)

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);
}

Sharon (16-Apr-2010 10: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) );
}

 
?>