Home > Archive > PERL Beginners > October 2007 > average and standard deviation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
average and standard deviation
|
|
| Mahurshi Akilla 2007-10-03, 7:01 pm |
| is there an easy way to get the average and standard deviation in perl
(given an array of numbers). i don't want to reinvent the wheel and
write my own functions.
i saw something for the average (see below). it works fine. is there
a similar quickie for std deviation (or perhaps, the median)
use List::Util qw(sum);
@array = (1, 1.5, 2);
$average = sum(@array)/@array;
print "$average
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2007-10-03, 7:01 pm |
|
If you have any problems or questions, please let me know.
Thanks.
Wags ;)
David R Wagner
Senior Programmer Analyst
FedEx Freight
1.408.323.4225x2224 TEL
1.408.323.4449 FAX
http://fedex.com/us=20
=20
> -----Original Message-----
> From: Mahurshi Akilla [mailto:mahurshi@gmail.com]=20
> Sent: Wednesday, October 03, 2007 11:18
> To: beginners@perl.org
> Subject: average and standard deviation
>=20
> is there an easy way to get the average and standard deviation in perl
> (given an array of numbers). i don't want to reinvent the wheel and
> write my own functions.
>=20
> i saw something for the average (see below). it works fine. is there
> a similar quickie for std deviation (or perhaps, the median)
>=20
> use List::Util qw(sum);
> @array =3D (1, 1.5, 2);
> $average =3D sum(@array)/@array;
> print "$average
>=20
>=20
You did not state which OS you are running and assuiming you can load
modules, but from ActivePerl for Windows (also on CPAN )
Statistics::Descriptive ( Loaded and took the example from the doc and
ran):
#!perl
use strict;
use warnings;
use Statistics::Descriptive;
my $stat =3D Statistics::Descriptive::Full->new();
$stat->add_data(1, 1.5, 2);=20
my $mean =3D $stat->mean();
my $var =3D $stat->variance();
my $tm =3D $stat->trimmed_mean(.25);
my $std =3D $stat->standard_deviation();
$Statistics::Descriptive::Tolerance =3D 1e-10;
printf "Mean: %10.4f Var: %10.4f Std Dev: %10.4f Trimmed mean:
%10.4f\n",
$mean,
$var ,
$std ,
$tm ;
Output:
Mean: 1.5000 Var: 0.2500 Std Dev: 0.5000 Trimmed mean:
1.5000
Wags ;)
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20
>=20
****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************
|
|
|
|
|