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

Re: variance calculation

by ady (Deacon)
on Jan 12, 2008 at 17:26 UTC ( [id://662080]=note: print w/replies, xml ) Need Help??


in reply to variance calculation

Hi
Following your train of thought, this is probably what you want :
#!/usr/bin/perl -w use strict; use warnings; #open(FH,"$ARGV[0]") or die; #my @temp=<FH>; #close FH; my @temp = <DATA>; my $mean = Mean (\@temp); my $variance = variance(\@temp); print "$variance\n"; sub sum { my ($arrayref) = @_; my $result; foreach(@$arrayref) { $result+= $_; } return $result; } sub Mean { my ($arrayref) = @_; my $result; foreach (@$arrayref) { $result += $_ } return $result / @$arrayref; } sub variance { my ($arrayref)= @_; my @a = map ( ($_ - $mean)**2, @$arrayref); return sum(\@a) / scalar @$arrayref; } __DATA__ 1 2 3 4 5

Check
map
discrete statistics

Best regards,
Allan Dystrup

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://662080]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found