Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
Syntactic Confectionery Delight
 
PerlMonks  

Is there a simpler way??

by yabba (Initiate)
on Apr 08, 2001 at 03:07 UTC ( [id://70774]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Ok, I have 6 test grades: 70 74 80 82 68 76 and the average comes out to 75. Now, I need to figure out the standard deviation, which is square root of (sum of squares of these differences)/(class size). I did it this way:
$std = sqrt((70-75)**2+(74-75)**2+(80-75)**2+ (82-75)**2+(68-75)**2+(76-75)**2)/6))
Now, if I had a DAT file already containing the scores, $score, and I already have the variable $avg, is there a simpler way??

Replies are listed 'Best First'.
Re: Is there a simpler way??
by repson (Chaplain) on Apr 08, 2001 at 03:28 UTC
    Here is one way I put together where I assume @values to already contain the data from the DAT file mentioned. Code untested.
    # compute average $total += $_ for @values; $average = $total / @values; # compute stddev according to your method $total_deviation += ($_ - $average)**2 for @values; $standard_deviation = sqrt($total_deviation) / @values; # compute stddev as I recall it, probably wrong $total_deviation += abs($_ - $average) for @values; $standard_deviation = $total_deviation / @values;
      Hey, thanks for your help, but I have a question. The @values, what are you referring to?
(jeffa) Re: Is there a simpler way??
by jeffa (Bishop) on Apr 08, 2001 at 03:35 UTC
    You bet. It's called CPAN. Here is a solution with Statistics::Descriptive
    use strict; use Statistics::Descriptive; my $stat = Statistics::Descriptive::Full->new(); $stat->add_data(70,74,80,82,68,76); print "mean: ", $stat->mean(), "\n", "sum: ", $stat->sum(), "\n", "var: ", $stat->variance(), "\n", "dev: ", $stat->standard_deviation(), "\n";
    Of course, you won't get much credit for this solution on the test. ;)

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70774]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.