note 109234 added to function.stats-standard-deviation
| From: | tripollite at gmail dot com | Date: | Thu, 28 Jun 2012 09:58:14 +0000 |
| Subject: | note 109234 added to function.stats-standard-deviation | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188954@lists.php.net to get a copy of this message | ||
// 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)) );
}
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 :
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) ;
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 78.125.26.52
----
Manual Page -- http://www.php.net/manual/en/function.stats-standard-deviation.php
Edit -- https://master.php.net/note/edit/109234
Del: integrated -- https://master.php.net/note/delete/109234/integrated
Del: useless -- https://master.php.net/note/delete/109234/useless
Del: bad code -- https://master.php.net/note/delete/109234/bad+code
Del: spam -- https://master.php.net/note/delete/109234/spam
Del: non-english -- https://master.php.net/note/delete/109234/non-english
Del: in docs -- https://master.php.net/note/delete/109234/in+docs
Del: other reasons-- https://master.php.net/note/delete/109234
Reject -- https://master.php.net/note/reject/109234
Search -- https://master.php.net/manage/user-notes.php