The 7th Annual China PHP Conference

stats_standard_deviation

(PECL stats >= 1.0.0)

stats_standard_deviation標準偏差を返す

説明

stats_standard_deviation ( array $a [, bool $sample = FALSE ] ) : float

a に含まれる値の標準偏差を返します。

パラメータ

a

標準偏差を取得したいデータの配列。配列のすべての値が float にキャストされることに注意しましょう。

sample

a が、母集団の抽出標本であることを表します。デフォルトは FALSE です。

返り値

成功した場合に標準偏差、失敗した場合に FALSE を返します。

エラー / 例外

a の要素数が 2 より少ない場合に E_WARNING が発生します。

add a note add a note

User Contributed Notes 5 notes

up
9
levim at php dot net
5 years ago
Here's an implementation that adheres to the PECL source quite strictly. This is useful for people who want to use it but don't have the extension, as well as for people who want to understand what is going on.

<?php

if (!function_exists('stats_standard_deviation')) {
   
/**
     * This user-land implementation follows the implementation quite strictly;
     * it does not attempt to improve the code or algorithm in any way. It will
     * raise a warning if you have fewer than 2 values in your array, just like
     * the extension does (although as an E_USER_WARNING, not E_WARNING).
     *
     * @param array $a
     * @param bool $sample [optional] Defaults to false
     * @return float|bool The standard deviation or false on error.
     */
   
function stats_standard_deviation(array $a, $sample = false) {
       
$n = count($a);
        if (
$n === 0) {
           
trigger_error("The array has zero elements", E_USER_WARNING);
            return
false;
        }
        if (
$sample && $n === 1) {
           
trigger_error("The array has only 1 element", E_USER_WARNING);
            return
false;
        }
       
$mean = array_sum($a) / $n;
       
$carry = 0.0;
        foreach (
$a as $val) {
           
$d = ((double) $val) - $mean;
           
$carry += $d * $d;
        };
        if (
$sample) {
           --
$n;
        }
        return
sqrt($carry / $n);
    }
}

?>
up
3
Pascal Woerde
8 years ago
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);
}
up
3
Sharon
9 years ago
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) );
}

?>
up
1
tripollite at gmail dot com
6 years ago
Function to calculate square of value - mean

<?php
function sd_square($x, $mean) { return pow($x - $mean,2); }
?>

Function to calculate standard deviation (uses sd_square)   
<?php
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)) );
}
?>

works better if you don't add +1 to the count

standard deviation of a constant must be 0 ( http://en.wikipedia.org/wiki/Standard_deviation )

different way :
<?php
class Tmath{
    public static function
variance($data, $round = 2)
    {
        if(
count($data) == 0 )
            return
NULL;
       
$total_ecart = 0;
       
$moyenne = self::moyenne($data, 10*$round);
        if(
$moyenne == 0)
            return
0;
        foreach(
$data as $element)
        {
           
$total_ecart += pow($element, 2);
        }
        return
round($total_ecart/count($data) - pow($moyenne,2),$round);
    }
    public static function
ecartType($data, $round = 2)
    {
        return
round(sqrt(self::variance($data,10*$round)),2);
    }
    public static function
moyenne($data, $round = 2)
    {
        if(
count($data) == 0 )
            return
NULL;
        return
round(array_sum($data)/count($data),$round) ;
    }
}
?>
up
-3
pmcdougl at gac dot edu
7 years ago
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
To Top