Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
There's more than one way to do things
 
PerlMonks  

Still confused

by yabba (Initiate)
on Apr 08, 2001 at 04:41 UTC ( [id://70787]=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, this is what I have so far and I need to figure out the standard deviation and what I got doesn't really work, help me!!
use strict; my $number=0; my $line; my $score; my $std; my $avg; my $name; my $sum; my $totd; open(IN, "pa5c.dat") || die ("Can't open file $!\n"); while($line = <IN>){ chomp($line); ++$number; ($name, $score) = split(/:/, $line); $sum = $sum + $score; $totd += abs($sum - $avg); $std = $totd / $number; } $avg = $sum / $number; printf "The class average is: %3.1f\n", $avg; printf "The standard deviation is: %3.1f\n", $std; __END__#pa5.pl
$totd is the total deviation. I'm supposed to get 5 for standard deviation but I get 261.3

Replies are listed 'Best First'.
(jcwren) Re: Still confused
by jcwren (Prior) on Apr 08, 2001 at 04:46 UTC

    How about posting part of your dataset? We really have no idea what to expect without knowing what the input is.

    --Chris

    e-mail jcwren
      The pa5c.DAT contains the following:
      Smith:70 Jones:74 Rider:80 High:82 Billie:68 Smithsonville:76
(jcwren) Re: Still confused
by jcwren (Prior) on Apr 08, 2001 at 05:01 UTC

    No offense, but I think a basic lesson in debugging is applicable here:

    • Is $sum actually 0 when you start (we're pretty sure that my $sum; would set it to 0, but I never trust that kind of thing.)

    • Have you printed the values out as you go? For each loop iteration, printing out the value of all variables involved would prove to be useful.

    • Is the algorithm correct? You're recalculating the $std value everytime through the loop. I suck at math, but is this correct? Using the code you have there, I get the exact same result.

    I'll give you a hint: You're dividing by $avg, but I don't see it being set anywhere. Do you?

    --Chris

    e-mail jcwren
      OK, I think I have it figured out, can someone check this:
      open(IN, "pa5c.dat") || die ("Can't open file $!\n"); while($line = <IN>){ chomp($line); ++$number; ($name, $score) = split(/:/, $line); $sum = $sum + $score; $avg = $sum / $number; $totd += ($score - $avg)**2; $std = sqrt ($totd/ $number); }
        I think your problem is in the algorithm, not just the Perl code.

        You're calculating deviations from the average via ($score - $avg) as you go along. But this won't work because the "average" isn't the true average until you're done counting the data! I think that what you want to do is:
        1. Read in all of the data, calculate $number and $sum as you go
        2. Calculate $avg from the final $number and $sum
        3. Loop through the data again and calculate $totd as you go

        Or, you can use the module Statistics::Descriptive to do it for you. Unless this is a homework assignment...

        buckaduck

      Actually, the value of $sum after my $sum is undefined.

      The following program demonstrates that unless a scalar is set to a value it is initially undefined:

      use strict; my $sum; unless (defined($sum)) { print '$sum is undefined', "\n"; } else { print 'The value of $sum is ', "$sum\n"; }
      This program returns the message that $sum is undefined.
      At the risk of sounding redundant, wouldn't perl -w catch the use of $avg when undefined, thereby making the debugging easier? All the debug tips are great, but the best one is letting perl debug for you with use strict; and -w.

      On a stylistic note, you might want to scope your variables like this:
      my ($a, $b, $c, $d);
      Instead of this:
      my $a; my $b; my $c; my $d;
      This should help keep your code from getting diluted with relatively less important text. (The computation is more important than the declarations, so it should also take up more space on screen to help focus your mind.)

      -Ted

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70787]
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.