Let's find the standard deviation
I will explain how to find the standard deviation using a Perl program.
Find the standard deviation
Consider the following array.
my @array = (10, 50, 60, 65, 70, 90);
Sample program to find the standard deviation of an array
Actually find the standard deviation from the array. The Statistics::Lite module has a stddev function, but I won't use it in this example.
I will not explain how to calculate the standard deviation.
use strict;
use warnings;
sub get_stddev {
return sqrt(get_disp(@_));
}
sub get_disp {
my ($array_ref) = @_;
my $mean = get_avg($array_ref);
my $count = @$array_ref;
my $sum = 0;
for my $num (@$array_ref) {
$sum += (($num - $mean) ** 2);
}
return $sum / $count;
}
sub get_avg {
my ($array_ref) = @_;
my $count = @$array_ref;
my $sum = 0;
for my $num (@$array_ref) {
$sum += $num;
}
return $sum / $count;
}
my @array = (10, 50, 60, 65, 70, 90);
my $stddev = get_stddev(\@array);
print "Standard deviation is " . $stddev . "\n";
Execution result
The standard deviation is 24.452334585202.
You can copy and paste this program and try it immediately at PerlBanjo.
Perl Data Analytics Tutorial