PiyaPerl has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple array with few number and all I want is a quick one line easy approach to calculate the standard deviation. I see the Statistics::Basic lib available but how to implement the same using arrays and not list.

my @act_ret=(1,2,3,4) my @std_dev=???

Replies are listed 'Best First'.
Re: PERL Standard Deviation lib
by hippo (Archbishop) on Sep 07, 2016 at 09:37 UTC
    I see the Statistics::Basic lib available but how to implement the same using arrays and not list.

    You can think of an array as a named list. What did you try?

    my $std_dev = Statistics::Basic::stddev (@act_ret);

    looks like it should work, according to the documentation. Does it not?

Re: PERL Standard Deviation lib
by BrowserUk (Patriarch) on Sep 07, 2016 at 09:10 UTC

    Take a look at Statistics::Basic; but be aware that you pick up a lot of stuff you don't need to get the simple bits you want.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: PERL Standard Deviation lib
by Corion (Patriarch) on Sep 07, 2016 at 08:23 UTC

    Have you looked at the formula for calculating the mean and standard deviation? They are not that hard to implement yourself.

      Like creating a subroutine and doing the calculation for the same...???? wont the lib be a better way of doing it.
Re: PERL Standard Deviation lib
by karlgoethebier (Abbot) on Sep 08, 2016 at 09:03 UTC

    I use Statistics::Descriptive:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dump; use Statistics::Descriptive; my $stat = Statistics::Descriptive::Full->new(); my @act_ret = ( 1, 2, 3, 4 ); $stat->add_data(@act_ret); dd $stat->standard_deviation(); __END__ karls-mac-mini:monks karl$ ./deviation.pl 1.29099444873581

    Seems to work ;-)

    See also

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»