Usage:
sub average{
my($data) = @_;
if (not @$data) {
die("Empty array\n");
}
my $total = 0;
foreach (@$data) {
$total += $_;
}
my $average = $total / @$data;
return $average;
}
sub stdev{
my($data) = @_;
if(@$data == 1){
return 0;
}
my $average = &average($data);
my $sqtotal = 0;
foreach(@$data) {
$sqtotal += ($average-$_) ** 2;
}
my $std = ($sqtotal / (@$data-1)) ** 0.5;
return $std;
}
$ave = &average(\@array);
#$ave = Math::NumberCruncher::Average(\@array); //OLD WAY
$std = &stdev(\@array);
#$std = Math::NumberCruncher::StandardDeviation(\@array); //OLD WAY
Comments (1)
-
|146.244.206.xxx |2011-03-01 16:35:45 RobYou might want to distinguish between "population standard deviation" and "sample standard deviation". If the elements in the array represent the complete population, you should use n instead of (n-1). If the array contains random elements from a larger population, then you would use the (n-1) in the denominator before taking the square root (sample standard deviation).
Only registered users can write comments!
Powered by !JoomlaComment 4.0 beta1


