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 | [reply] [d/l] [select] |
Actually, you should compute $number, $sum, and $sqsum (which is the sum of the squares of all of the numbers) as you go and then you can compute standard deviation without having to loop a second time.
I'll omit the details of the calculation since you can probably just look them up in Statistics::Descriptive, though I find deriving the formula is a good way to see if you remember any of your math classes. (:
This standard trick allows you to add numbers to the set (or even remove some) and recalculate the average and stddev without having to even keep the list of numbers around, much less loop through them.
-
tye
(but my friends call me "Tye")
| [reply] |
Oh holy smokes, that works nicely. I looked it up in
Statistics::Descriptive and could have kicked
myself. My way was pretty trivial and I think easy
for anyone to
understand. But it's not all that hard to do it your way,
and it has significant advantages. Well worth a look.
Thanks, tye.
buckaduck
| [reply] |