Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

covariance calculation

by vis1982 (Acolyte)
on Dec 28, 2009 at 18:22 UTC ( [id://814642]=perlquestion: print w/replies, xml ) Need Help??

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

Want to solve the covariance
!/usr/bin/perl -w use strict; use warnings; my @hits = (2378, 4024, 9696, 7314, 7710); my @sales = (310.94, 315.88, 514.15, 500.18, 610.37); my $covariance = covariance (@hits,\@sales); print "$covariance\n"; sub covariance { my ($arraylref, $array2ref) = @_; my ($i, $result); for ($i = 0; $i < @$arraylref; $i++) { $result += $arraylref->[$i] * $array2ref->[$i]; } $result /= @$arraylref; $result -= mean($arraylref) * mean($array2ref); }

while running display --> Can't use string ("2378") as an ARRAY ref while "strict refs" in use at covariance.pl line 14.

Replies are listed 'Best First'.
Re: covariance calculation
by MidLifeXis (Monsignor) on Dec 28, 2009 at 18:28 UTC
    my $covariance = covariance (@hits,\@sales);
    my $covariance = covariance (\@hits,\@sales);

    Update: Changed to block code instead of inline.

    --MidLifeXis

Re: covariance calculation
by zwon (Abbot) on Dec 28, 2009 at 19:14 UTC

    Note, that there's a lot of modules for statistic computations on CPAN. Particularly you can use Statistics::Basic:

    use strict; use warnings; use 5.010; use Statistics::Basic qw(covariance); my @hits = (2378, 4024, 9696, 7314, 7710); my @sales = (310.94, 315.88, 514.15, 500.18, 610.37); my $covariance = covariance(\@hits, \@sales); say $covariance;
Re: covariance calculation
by kennethk (Abbot) on Dec 28, 2009 at 18:30 UTC
    The error message should be a hint that your are not passing what you think. Line 14 accesses $arraylref, so clearly that contains the string "2378" instead of an array reference. $arraylref gets its value from @_ on line 12. @_ is set on the method call on line 7, where you are passing the array and not an array reference. The line should read:

    my $covariance = covariance (\@hits,\@sales);

    And, incidentally, you don't include the mean method you invoke on line 18 - when you post, please include all methods in your posted code.

Re: covariance calculation
by Khen1950fx (Canon) on Dec 28, 2009 at 20:21 UTC
    My math is a little rusty, but I tried your code with Statistics::Basic::Covariance:

    #!/usr/bin/perl use strict; use warnings; use Statistics::Basic qw(:all); use constant HITS => qw(2378 4024 9696 7314 7710); use constant SALES => qw(310.94 315.88 514.15 500.18 610.37); my $cov = (HITS, SALES); print "The covariance is : $cov\n";

    Update: Fixed---here's the corrected code:

    #!/usr/bin/perl use strict; use warnings; use Statistics::Basic qw(:all); $Statistics::Basic::DEBUG = 1; use constant HITS => qw(2378 4024 9696 7314 7710); use constant SALES => qw(310.94 315.88 514.15 500.18 610.37); my $cov = Statistics::Basic::Covariance->new; $cov = covariance( [HITS], [SALES] ); print "The covariance is : $cov\n";
Re: covariance calculation
by amir_e_a (Hermit) on Dec 28, 2009 at 18:54 UTC

    What the other monks answered is correct, and here's a little addition:

    The names of the variables $arraylref and $array2ref are hints that both of them are supposed to be references to arrays. \@sales is an array reference: the @ stands for "array" and \ stands for "reference". @hits, on the other hand, is just an array, not a reference.

    Of course, you can't always count on variable names to hint at what they are supposed to be, but in this case it is correct.

    Another hint that both of these variables really need to be references is the usage of the arrow operator -> in the line

    $arraylref->[$i] * $array2ref->[$i];

    The arrow operator is only used with references.

Re: covariance calculation
by ahmad (Hermit) on Dec 29, 2009 at 02:27 UTC

    To be in the safe side, Next time when you write something like that consider adding some extra checking in your sub for example

    sub covariance { my ($arraylref, $array2ref) = @_; if ( ref($arraylref) ne 'ARRAY' or ref($array2ref) ne 'ARRAY') { return "expecting array reference to be passed"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://814642]
Approved by MidLifeXis
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found